:root {
  --primary: #4757d1;
  --secondary: #7b61d9;
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-soft: #fbfdff;
  --line: #e6eef8;
  --line-strong: #d7def0;
  --text: #0f172a;
  --muted: #64748b;
  --danger: #dc2626;
  --success: #059669;
  --radius: 8px;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
  font-family: Inter, "Noto Sans KR", "Malgun Gothic", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

a { color: inherit; text-decoration: none; }

.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 700;
}

.brand img { height: 38px; width: auto; }

.nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.nav a,
.link-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.nav a:hover,
.link-button:hover { background: var(--bg); color: var(--text); }

.main {
  width: min(1180px, calc(100vw - 32px));
  margin: 24px auto 60px;
}

.messages { margin-bottom: 16px; display: grid; gap: 8px; }
.message { padding: 10px 12px; border-radius: 6px; background: #eef2ff; color: #1e293b; }
.message.success { background: #dcfce7; color: #14532d; }
.message.error { background: #fee2e2; color: #7f1d1d; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  margin-bottom: 18px;
}

.page-header h1 { margin: 0 0 4px; font-size: 26px; }
.page-header p { margin: 0; color: var(--muted); }
.page-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.panel,
.document-sheet,
.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel { padding: 18px; margin-bottom: 16px; }
.panel h2 { margin: 0 0 14px; font-size: 18px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 9px 13px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  min-height: 38px;
}

.btn.primary {
  background: linear-gradient(90deg, var(--primary), var(--secondary));
  color: #fff;
}

.btn.secondary {
  background: var(--surface);
  border-color: var(--line-strong);
  color: #334155;
}

.btn.danger { background: var(--danger); color: #fff; }
.btn.danger-outline {
  background: #fff;
  border-color: #fecaca;
  color: var(--danger);
}
.btn.small { min-height: 30px; padding: 5px 9px; font-size: 12px; }
.btn.full { width: 100%; }

input,
textarea,
select,
.form-control {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 9px 10px;
  font: inherit;
  color: var(--text);
  background: #fff;
}

textarea { resize: vertical; }
label { display: grid; gap: 6px; font-weight: 700; color: #334155; }
label input, label textarea, label select { font-weight: 400; }
.help { color: var(--muted); font-size: 12px; line-height: 1.5; }
.form-error, .errorlist { color: var(--danger); font-size: 13px; }
.errorlist { margin: 4px 0 0; padding-left: 18px; }

.search-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  margin-bottom: 14px;
}

.status-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.status-tabs a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 7px 11px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  color: #334155;
  font-weight: 700;
}

.status-tabs a.active {
  border-color: var(--primary);
  color: var(--primary);
  background: #eef2ff;
}

.status-tabs span {
  color: var(--muted);
  font-weight: 600;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
}

.badge.success { background: #dcfce7; color: #166534; }
.badge.muted { background: #e2e8f0; color: #334155; }
.badge.danger { background: #fee2e2; color: #991b1b; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: 8px;
}

.data-table th {
  background: linear-gradient(90deg, var(--primary), var(--secondary));
  color: #fff;
  text-align: left;
  padding: 12px;
}

.data-table td {
  border-bottom: 1px solid var(--line);
  padding: 11px 12px;
  vertical-align: middle;
}

.data-table tbody tr:nth-child(even) { background: var(--surface-soft); }
.text-right { text-align: right; }
.empty { text-align: center; color: var(--muted); padding: 30px !important; }
.actions-cell { display: flex; gap: 6px; flex-wrap: wrap; }
.actions-cell form,
.page-actions form {
  margin: 0;
}

.login-shell {
  min-height: calc(100vh - 80px);
  display: grid;
  place-items: center;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  padding: 28px;
}

.login-card h1 { margin: 0; text-align: center; }
.login-logo { width: 180px; margin: 0 auto 4px; }

.settings-grid {
  display: grid;
  gap: 16px;
}

.form-grid {
  display: grid;
  gap: 14px;
  margin-bottom: 14px;
}

.form-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.ai-panel {
  display: grid;
  grid-template-columns: minmax(220px, 0.5fr) minmax(0, 1fr) auto;
  align-items: end;
  gap: 14px;
}

.ai-panel h2 { margin-bottom: 4px; }
.ai-panel p { margin: 0; color: var(--muted); }
#ai-status { grid-column: 2 / 4; }

.document-sheet {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 26px;
}

.sheet-header {
  display: flex;
  align-items: center;
  gap: 20px;
  border-bottom: 4px solid #eef2ff;
  padding-bottom: 14px;
  margin-bottom: 18px;
}

.sheet-logo { width: 160px; max-height: 72px; object-fit: contain; }
.sheet-title { flex: 1; text-align: center; }
.sheet-title h2 { margin: 0 0 4px; font-size: 28px; letter-spacing: 6px; }
.sheet-title p { margin: 0; color: var(--muted); font-size: 12px; }
.sheet-meta { width: 220px; display: grid; gap: 7px; font-size: 13px; }
.sheet-meta p { margin: 0; }
.checkbox-line { display: flex; align-items: center; gap: 8px; }
.checkbox-line input { width: auto; }

.sheet-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto;
  gap: 0 20px;
}

.info-box,
.project-box,
.description-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 14px;
}

.info-box h3 {
  margin: 0 0 10px;
  font-size: 15px;
  border-left: 4px solid var(--primary);
  padding-left: 10px;
}

.info-box { display: grid; gap: 9px; }
.company-box { grid-column: 2; grid-row: 1 / span 2; align-content: start; }
.company-box p { margin: 2px 0; line-height: 1.45; color: #374151; }
.project-box { grid-column: 1; grid-row: 2; background: #fff; }
.description-box { margin-top: 18px; background: #fff; }

.items-box { margin-top: 18px; }
.items-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.items-table th {
  background: linear-gradient(90deg, var(--primary), var(--secondary));
  color: white;
  padding: 11px;
  text-align: left;
}

.items-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  vertical-align: top;
}

.items-table input { padding: 7px 8px; }
.items-table .no-col { width: 48px; }
.items-table .qty-col { width: 90px; }
.items-table .price-col { width: 130px; }
.items-table .amount-col { width: 140px; }
.items-table .remove-col { width: 56px; text-align: center; }
.row-number { text-align: center; color: var(--muted); padding-top: 15px !important; }
.line-total { text-align: right; font-weight: 700; padding-top: 15px !important; }
.delete-field { display: none; }
.item-row.is-deleted { display: none; }

.icon-button {
  width: 30px;
  height: 30px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  color: var(--danger);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

.table-actions { display: flex; justify-content: flex-end; margin-top: 10px; }

.totals-box {
  margin-top: 16px;
  padding: 16px 18px;
  background: #f8fafc;
  border-top: 2px solid var(--primary);
  display: grid;
  gap: 8px;
}

.totals-box div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
}

.totals-box .grand {
  font-size: 20px;
  color: var(--primary);
}

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, 0.6fr);
  gap: 16px;
}

.detail-list {
  display: grid;
  grid-template-columns: 130px minmax(0, 1fr);
  gap: 8px 12px;
  margin: 0;
}

.detail-list dt { color: var(--muted); font-weight: 700; }
.detail-list dd { margin: 0; }
.detail-list .strong { font-weight: 800; color: var(--primary); }

@media (max-width: 860px) {
  .topbar,
  .page-header,
  .sheet-header {
    align-items: stretch;
    flex-direction: column;
  }
  .nav { flex-wrap: wrap; }
  .ai-panel,
  .sheet-grid,
  .detail-grid,
  .form-grid.two,
  .form-grid.three {
    grid-template-columns: 1fr;
  }
  #ai-status { grid-column: auto; }
  .sheet-meta { width: 100%; }
  .company-box,
  .project-box {
    grid-column: auto;
    grid-row: auto;
  }
}
