:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --line: #e5e7ef;
  --line-strong: #d4d7e2;
  --ink: #111827;
  --ink-2: #4b5563;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --accent-soft: #eef0ff;
  --ok: #059669;
  --ok-soft: #e7f7f0;
  --warn: #b45309;
  --warn-soft: #fef5e6;
  --danger: #dc2626;
  --danger-soft: #fdeeee;
  --a: #0891b2; --a-soft: #e3f6fa;
  --b: #7c3aed; --b-soft: #f1ebfe;
  --c: #db2777; --c-soft: #fdebf3;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(17, 24, 39, .04), 0 4px 16px rgba(17, 24, 39, .04);
  --shadow-lg: 0 24px 60px rgba(17, 24, 39, .18);
  --sidebar: 248px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1017; --surface: #151922; --surface-2: #1b202b; --line: #262c39; --line-strong: #333b4c;
    --ink: #eef0f5; --ink-2: #c3c8d4; --muted: #8b93a5;
    --accent: #7c74ff; --accent-soft: #23234a;
    --ok: #34d399; --ok-soft: #10302a; --warn: #fbbf24; --warn-soft: #33270f; --danger: #f87171; --danger-soft: #3a1a1c;
    --a: #22d3ee; --a-soft: #0f2d35; --b: #a78bfa; --b-soft: #251c40; --c: #f472b6; --c-soft: #3a1729;
    --shadow: 0 1px 2px rgba(0,0,0,.3); --shadow-lg: 0 24px 60px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.5 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg); color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 24px; font-weight: 700; }
h2 { font-size: 15px; font-weight: 600; }
a { color: var(--accent); cursor: pointer; }
code { font-family: "JetBrains Mono", monospace; font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; border: 1px solid var(--line); }
.muted { color: var(--muted); }
.sm { font-size: 12px; }
.hidden { display: none !important; }
.num { font-family: "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; font-size: 13px; }

/* ---------- controls ---------- */
input, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 10px;
  padding: 9px 12px; width: 100%; min-width: 0;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
select { appearance: none; padding-right: 32px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 16px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  font: inherit; font-weight: 600; font-size: 13.5px; line-height: 1;
  padding: 10px 14px; border-radius: 10px; border: 1px solid transparent;
  cursor: pointer; white-space: nowrap; transition: background .15s, border-color .15s, transform .05s, opacity .15s;
  color: var(--ink); background: transparent;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn.ghost { background: var(--surface); border-color: var(--line-strong); }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn.ghost.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.btn.danger { color: var(--danger); }
.btn.lg { padding: 14px 18px; font-size: 15px; border-radius: 12px; }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 10px; font-size: 12.5px; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.icon-btn { border: 0; background: transparent; color: var(--muted); padding: 6px; border-radius: 8px; cursor: pointer; display: inline-grid; place-items: center; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.danger:hover { color: var(--danger); background: var(--danger-soft); }

.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { font: inherit; font-weight: 600; font-size: 13px; border: 0; background: transparent; color: var(--muted); padding: 6px 12px; border-radius: 7px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.type-seg button.on[data-v=A] { color: var(--a); } .type-seg button.on[data-v=B] { color: var(--b); } .type-seg button.on[data-v=C] { color: var(--c); }

.badge { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 6px; font-style: normal; font-size: 11.5px; font-weight: 700; }
.badge.A { color: var(--a); background: var(--a-soft); } .badge.B { color: var(--b); background: var(--b-soft); } .badge.C { color: var(--c); background: var(--c-soft); }
button.badge { border: 0; cursor: pointer; font-family: inherit; }

.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 13px; color: var(--ink-2); cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(1200px 600px at 20% -10%, color-mix(in srgb, var(--accent) 16%, transparent), transparent 60%), var(--bg); }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 36px 28px 28px; box-shadow: var(--shadow-lg); text-align: center; display: grid; gap: 12px; }
.login-card h1 { margin-top: 8px; }
.login-card p { margin: 0 0 10px; }
.login-card input { text-align: center; padding: 12px; font-size: 15px; }
.login-err { color: var(--danger); font-size: 13px; min-height: 18px; margin: 0 !important; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; background: var(--accent); display: grid; place-items: center; color: #fff; }
.brand-mark svg { width: 22px; height: 22px; stroke-width: 2.6; }
.brand-mark.lg { width: 56px; height: 56px; border-radius: 16px; margin: 0 auto; }
.brand-mark.lg svg { width: 34px; height: 34px; }

/* ---------- shell ---------- */
.app { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 18px; padding: 20px 14px; background: var(--surface); border-right: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 6px; }
.brand strong { display: block; font-size: 15px; line-height: 1.1; }
.brand span { font-size: 12px; color: var(--muted); }
.eyebrow { display: block; font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin: 0 0 6px 4px; }
.user-switch select { font-weight: 600; background-color: var(--surface-2); }
.nav { display: grid; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 11px; padding: 10px 12px; border-radius: 10px; color: var(--ink-2); text-decoration: none; font-weight: 500; transition: background .12s; }
.nav a:hover { background: var(--surface-2); }
.nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sync { margin-top: auto; display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); padding: 10px 6px 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); }
.sync #syncText { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.sync.ok .dot { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.sync.busy .dot { background: var(--warn); animation: pulse 1s infinite; }
.sync.off .dot { background: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
@keyframes pulse { 50% { opacity: .35; } }

.main { padding: 28px 32px 48px; min-width: 0; }
.page { display: none; max-width: 1280px; margin: 0 auto; animation: fade .2s ease; }
.page.on { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters select { width: auto; min-width: 130px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.stack { display: grid; gap: 14px; align-content: start; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid.one { grid-template-columns: 1fr; }
.form-grid label { display: grid; gap: 5px; font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.form-grid label small { font-weight: 500; color: var(--accent); }

details.company > summary { list-style: none; cursor: pointer; margin: 0; }
details.company > summary::-webkit-details-marker { display: none; }
details.company > summary .muted { flex: 1; text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
details.company .chev { transition: transform .2s; color: var(--muted); }
details.company[open] .chev { transform: rotate(180deg); }
details.company[open] > summary { margin-bottom: 14px; }

.odo-preview { margin-top: 14px; display: flex; gap: 6px; flex-wrap: wrap; font-size: 12.5px; }
.odo-preview .pill { background: var(--surface-2); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; color: var(--ink-2); }
.odo-preview .pill b { color: var(--ink); font-variant-numeric: tabular-nums; }
.odo-preview .pill.bad { background: var(--danger-soft); border-color: transparent; color: var(--danger); }

/* ---------- generate ---------- */
.gen-grid { display: grid; grid-template-columns: 360px 1fr; gap: 22px; align-items: start; }
.gen-grid > .stack { position: sticky; top: 20px; }
.report-col { min-width: 0; display: grid; gap: 14px; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.stats:empty { display: none; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); }
.stat span { display: block; font-size: 12px; color: var(--muted); font-weight: 500; }
.stat b { display: block; font-size: 20px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat.hl { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.stat.hl span { color: color-mix(in srgb, var(--accent-ink) 75%, transparent); }
.edit-banner { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 12px; font-size: 13px; background: var(--warn-soft); color: var(--warn); }
.edit-banner.ok { background: var(--ok-soft); color: var(--ok); }
.edit-banner .btn { margin-left: auto; }

.paper { background: #fff; color: #111827; border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 40px 44px; min-height: 420px; }
.paper .empty { text-align: center; color: #6b7280; padding: 70px 0; }
.paper .empty svg { width: 44px; height: 44px; color: #c7cbe0; stroke-width: 1.5; }
.paper .empty h3 { color: #111827; margin: 10px 0 6px; font-size: 17px; }
.paper .empty p { margin: 0; line-height: 1.7; }
.r-title { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid #111827; padding-bottom: 14px; margin-bottom: 18px; gap: 20px; }
.r-title h2 { font-size: 22px; letter-spacing: .08em; text-transform: uppercase; font-weight: 700; }
.r-title .co { text-align: right; font-size: 12px; color: #4b5563; line-height: 1.55; }
.r-title .co strong { color: #111827; font-size: 14px; display: block; }
.r-meta { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0; border: 1px solid #e5e7eb; border-radius: 10px; overflow: hidden; margin-bottom: 18px; }
.r-meta div { padding: 9px 12px; border-right: 1px solid #e5e7eb; }
.r-meta div:last-child { border-right: 0; }
.r-meta span { display: block; font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; color: #6b7280; font-weight: 600; }
.r-meta b { font-size: 13.5px; }
table.r { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.r th { text-align: left; font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em; color: #6b7280; font-weight: 600; padding: 8px 10px; border-bottom: 1px solid #d1d5db; background: #f9fafb; }
table.r td { padding: 7px 10px; border-bottom: 1px solid #eef0f3; vertical-align: top; }
table.r tr:nth-child(even) td { background: #fbfbfd; }
table.r .n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.r tfoot td { font-weight: 700; border-top: 2px solid #111827; border-bottom: 0; background: #fff !important; padding-top: 10px; }
table.r td.cust { line-height: 1.55; }
table.r input, table.r textarea { padding: 4px 6px; font-size: 12.5px; border-radius: 6px; background: #fff; color: #111827; border-color: #d1d5db; }
table.r input { width: 70px; text-align: right; }
table.r textarea { min-height: 52px; }
.r-sign { display: grid; grid-template-columns: 1fr 1fr; gap: 48px; margin-top: 56px; font-size: 12px; color: #4b5563; }
.r-sign div { border-top: 1px solid #9ca3af; padding-top: 6px; }
.legacy-wrap .header-table { width: 100%; border-collapse: collapse; margin-bottom: 16px; font-size: 13px; }
.legacy-wrap .header-table td { padding: 5px 8px; border: 1px solid #e5e7eb; }
.legacy-wrap .header-table .lbl { font-weight: 600; background: #f9fafb; width: 16%; }
.legacy-wrap .summary-item { display: inline-block; margin: 14px 18px 0 0; font-weight: 600; }

/* ---------- lists ---------- */
.add-row { display: flex; gap: 8px; margin-bottom: 14px; }
.add-row input[type=text] { flex: 1; }
.add-row input[type=date] { width: 180px; flex: none; }
.search { position: relative; margin-bottom: 8px; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 16px; height: 16px; }
.search input { padding-left: 34px; background: var(--surface-2); border-color: var(--line); }
.rows { display: grid; }
.row { display: flex; align-items: center; gap: 12px; padding: 10px 6px; border-bottom: 1px solid var(--line); min-height: 50px; }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .grow b { font-weight: 600; }
.row .sub { font-size: 12px; color: var(--muted); }
.row .acts { display: flex; gap: 2px; opacity: .55; transition: opacity .12s; }
.row:hover .acts { opacity: 1; }
.row input.inline { padding: 5px 8px; }
.row-empty { text-align: center; color: var(--muted); padding: 36px 0; }
.legend { display: flex; gap: 18px; flex-wrap: wrap; align-items: center; margin: -6px 0 14px; font-size: 13px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.date-chip { width: 52px; text-align: center; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; flex: none; background: var(--surface); }
.date-chip i { display: block; font-style: normal; font-size: 10px; font-weight: 700; text-transform: uppercase; background: var(--danger); color: #fff; padding: 1px 0; letter-spacing: .05em; }
.date-chip b { display: block; font-size: 17px; padding: 2px 0; }
.group-label { font-size: 12px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); padding: 14px 6px 4px; }
.avatar { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: var(--accent); background: var(--accent-soft); flex: none; }

table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 11.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 12px 16px; background: var(--surface-2); border-bottom: 1px solid var(--line); }
table.list td { padding: 12px 16px; border-bottom: 1px solid var(--line); }
table.list tr:last-child td { border-bottom: 0; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: var(--surface-2); }
table.list .n { text-align: right; font-variant-numeric: tabular-nums; }
table.list .acts { text-align: right; white-space: nowrap; }
.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.tile { display: grid; gap: 10px; align-content: start; }
.tile p { margin: 0; line-height: 1.6; }
.tile .btn { justify-self: start; margin-top: 4px; }

/* ---------- toasts / dialog ---------- */
.toasts { position: fixed; right: 20px; bottom: 20px; display: grid; gap: 8px; z-index: 100; }
.toast { background: var(--ink); color: var(--bg); padding: 11px 16px; border-radius: 12px; font-size: 13.5px; font-weight: 500; box-shadow: var(--shadow-lg); animation: toast .25s ease; max-width: 360px; display: flex; gap: 10px; align-items: center; }
.toast.err { background: var(--danger); color: #fff; }
.toast.ok::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
@keyframes toast { from { opacity: 0; transform: translateY(8px); } }
dialog { border: 0; border-radius: 18px; padding: 0; width: min(420px, calc(100vw - 32px)); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg); }
dialog::backdrop { background: rgba(15, 18, 30, .45); backdrop-filter: blur(2px); }
.dlg-body { padding: 22px; display: grid; gap: 12px; }
.dlg-body h3 { font-size: 17px; }
#dlgMsg { color: var(--ink-2); line-height: 1.55; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .gen-grid { grid-template-columns: 1fr; }
  .gen-grid > .stack { position: static; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 50; top: auto; bottom: 0; left: 0; right: 0; height: auto; flex-direction: row; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--line); gap: 0; }
  .brand, .sync { display: none; }
  .user-switch { display: none; }
  .nav { grid-auto-flow: column; grid-auto-columns: 1fr; width: 100%; }
  .nav a { flex-direction: column; gap: 3px; font-size: 10.5px; padding: 6px 2px; text-align: center; }
  .main { padding: 0 16px 96px; }
  .mobile-top { display: flex !important; }
  .stats { grid-template-columns: 1fr 1fr; }
  .paper { padding: 22px 16px; overflow-x: auto; }
  .r-meta { grid-template-columns: 1fr; }
  .r-meta div { border-right: 0; border-bottom: 1px solid #e5e7eb; }
  .r-meta div:last-child { border-bottom: 0; }
  .r-title { flex-direction: column; }
  .r-title .co { text-align: left; }
  .toolbar-actions .btn span { display: none; }
  .toolbar-actions .btn.primary span { display: inline; }
  .add-row { flex-wrap: wrap; }
  .add-row input[type=date], .add-row input[type=text] { width: 100%; flex: 1 1 100%; }
  .add-row .type-seg { flex: 1; } .add-row .type-seg button { flex: 1; }
  table.list .hide-sm { display: none; }
  .toasts { left: 16px; right: 16px; bottom: 80px; }
}
.mobile-top { display: none; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 40; margin: 0 -16px 16px; padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.mobile-top select { flex: 1; font-weight: 600; }
.mobile-top .sync { display: flex; margin: 0; padding: 7px 8px; }
.mobile-top .sync #syncText2 { display: none; }

/* ---------- print ---------- */
@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; }
  .sidebar, .page-head, .gen-grid > .stack, .toolbar, .stats, .edit-banner, .toasts, .mobile-top { display: none !important; }
  .app, .gen-grid { display: block; }
  .main { padding: 0; }
  .paper { border: 0; box-shadow: none; padding: 0; border-radius: 0; }
  table.r tr { break-inside: avoid; }
  table.r tr:nth-child(even) td { background: #fff; }
}
