:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #0f1b2d;
  --muted: #5b6b80;
  --line: #dfe5ee;
  --primary: #1f3a5f;
  --primary-ink: #ffffff;
  --accent: #0f766e;
  --focus: #2563eb;
  --flag: #fff4c2;
  --flag-hard: #ffe58a;
  --flag-edge: #d4a017;
  --danger: #b42318;
  --soft: #eef3f9;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1521; --card: #151f2e; --ink: #e8eef7; --muted: #93a4ba; --line: #26364b;
    --primary: #6ea0e0; --primary-ink: #0b1220; --accent: #34d3c0; --focus: #7db0ff;
    --flag: #4a3f12; --flag-hard: #5c4c0c; --flag-edge: #e0b73c; --danger: #ff8b7e; --soft: #1b2a3e;
  }
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 76px; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.wrap { max-width: 1120px; margin: 0 auto; padding: 0 16px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }

/* header + steps */
.top { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--card) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.top-inner { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 60px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--primary); letter-spacing: -0.01em; }
.steps { display: flex; gap: 6px; list-style: none; margin: 0; padding: 0; }
.steps a { display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 999px; color: var(--muted); text-decoration: none; font-size: 14px; font-weight: 600; }
.steps b { display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--line); color: var(--muted); font-size: 12px; }
.steps li.active a { color: var(--ink); background: var(--soft); }
.steps li.active b { background: var(--primary); color: var(--primary-ink); }
.steps li.done b { background: var(--accent); color: #fff; }
.steps li.done a { color: var(--ink); }

/* privacy */
.privacy { display: flex; gap: 12px; align-items: flex-start; margin: 20px 0 16px; padding: 12px 16px; border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line)); background: color-mix(in srgb, var(--accent) 8%, var(--card)); border-radius: var(--radius); font-size: 14px; color: var(--ink); }
.privacy svg { flex: none; margin-top: 2px; color: var(--accent); }
.privacy span { min-width: 0; }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; box-shadow: 0 1px 2px rgba(15,27,45,.04); }
.card h2 { display: flex; align-items: center; gap: 10px; margin: 0 0 4px; font-size: 20px; letter-spacing: -0.01em; }
.num { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--primary); color: var(--primary-ink); font-size: 14px; }
.hint { margin: 0 0 16px; color: var(--muted); }
mark.legend { background: var(--flag); color: var(--ink); padding: 0 6px; border-radius: 4px; border: 1px solid var(--flag-edge); }

/* form bits */
.row { display: flex; gap: 16px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 12px; }
.tools { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; margin-left: auto; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 600; min-width: 0; }
.field.grow { flex: 1 1 260px; }
.field em { font-weight: 400; font-style: normal; }
select, input[type=text], input[type=password], input[type=date] {
  font: inherit; font-size: 15px; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; min-height: 40px; max-width: 100%;
}
select:focus, input:focus, textarea:focus, button:focus-visible, .btn:focus-within { outline: 2px solid var(--focus); outline-offset: 1px; }
textarea {
  width: 100%; min-height: 280px; resize: vertical; font: 13.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px;
}
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; min-height: 40px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; font: inherit; font-weight: 600; font-size: 15px; min-height: 40px; padding: 8px 16px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--ink); cursor: pointer; }
.btn:hover:not(:disabled) { background: var(--soft); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { background: var(--primary); filter: brightness(1.12); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 4px 12px; font-size: 14px; }
.btn.big { min-height: 48px; padding: 10px 26px; font-size: 17px; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; font-size: 0; }
.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 12px 0; }
.error { margin: 8px 0; padding: 10px 14px; border-radius: 8px; color: var(--danger); background: color-mix(in srgb, var(--danger) 9%, var(--card)); border: 1px solid color-mix(in srgb, var(--danger) 35%, var(--line)); font-size: 14px; }
.error[hidden] { display: none; }
.fname { background: var(--soft); padding: 6px 10px; border-radius: 6px; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--soft); border-radius: 10px; padding: 10px 14px; min-width: 0; }
.stat small { display: block; color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat strong { font-size: 19px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat.warn { background: var(--flag); outline: 1px solid var(--flag-edge); }

/* filters */
.filters { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; padding: 12px 0; }
.spacer { flex: 1; }

/* table */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; min-width: 1340px; table-layout: fixed; font-size: 14px; }
thead th { position: sticky; top: 0; background: var(--soft); text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 9px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th.num-h { text-align: right; }
tbody td { padding: 3px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
td input[type=text] { width: 100%; border-color: transparent; background: transparent; padding: 7px 6px; min-height: 36px; border-radius: 6px; font-size: 14px; }
td input[type=text]:hover { border-color: var(--line); }
td input[type=text]:focus { border-color: var(--focus); background: var(--card); }
td input.money { text-align: right; font-variant-numeric: tabular-nums; }
td input.bad { border-color: var(--danger); }
.c-sel { width: 36px; text-align: center; }
.c-sel input, td.c-sel input { width: 17px; height: 17px; accent-color: var(--primary); margin-top: 10px; }
.c-act { width: 74px; }
td.c-act { white-space: nowrap; }
thead th:nth-child(1) { width: 40px; }
thead th:nth-child(2) { width: 118px; }
thead th:nth-child(3) { width: 200px; }
thead th:nth-child(4) { width: 280px; }
thead th:nth-child(5), thead th:nth-child(6), thead th:nth-child(7) { width: 138px; }
thead th:nth-child(8) { width: 140px; }
thead th:nth-child(10) { width: 78px; }
td.orig {  font-size: 12px; color: var(--muted); padding: 8px 8px; }
td.orig .why { color: var(--ink); font-weight: 600; margin-bottom: 2px; }
td.orig .txt { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; word-break: break-word; }
td.orig .txt:hover { -webkit-line-clamp: unset; }
tr.flagged td { background: var(--flag); }
tr.flagged.hard td { background: var(--flag-hard); }
tr.flagged td:first-child { box-shadow: inset 4px 0 0 var(--flag-edge); }
tr.ai-row td.orig .why::after { content: " (AI)"; font-weight: 400; color: var(--muted); }
.icon { width: 32px; height: 32px; border-radius: 6px; border: 1px solid transparent; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; }
.icon:hover { background: var(--soft); border-color: var(--line); color: var(--ink); }
.icon.ok { color: var(--accent); font-weight: 700; }
.icon.del:hover { color: var(--danger); }
.empty { padding: 20px; text-align: center; color: var(--muted); margin: 0; }

/* AI panel */
.ai { border: 1px solid var(--flag-edge); background: color-mix(in srgb, var(--flag) 55%, var(--card)); border-radius: 10px; margin-bottom: 14px; }
.ai > summary { padding: 12px 16px; cursor: pointer; font-weight: 600; }
.ai-body { padding: 0 16px 14px; }
.ai-body p { margin: 8px 0; }
.lines pre { max-height: 180px; overflow: auto; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 10px; white-space: pre-wrap; word-break: break-word; margin: 8px 0; }
.lines > summary { cursor: pointer; font-size: 14px; color: var(--muted); }
.ai .field { max-width: 420px; margin: 10px 0 4px; }
.ai input[type=password] { width: 100%; }

/* expense / rules / skipped */
.expense { margin-top: 16px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.expense h3 { margin: 0; padding: 10px 14px; background: var(--soft); font-size: 15px; }
.expense table { min-width: 0; }
.expense td, .expense th { padding: 8px 14px; border-bottom: 1px solid var(--line); text-align: left; }
.expense td.r, .expense th.r { text-align: right; font-variant-numeric: tabular-nums; }
.expense tfoot td { font-weight: 700; background: var(--soft); }
.rules, .skipped { margin-top: 16px; border-top: 1px solid var(--line); padding-top: 12px; }
.rules > summary, .skipped > summary { cursor: pointer; font-weight: 600; }
.rule-add { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.rule-add input { flex: 1 1 200px; }
.rule-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rule-list li { display: flex; align-items: center; gap: 10px; padding: 6px 10px; background: var(--soft); border-radius: 8px; }
.rule-list li span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.skipped pre { max-height: 200px; overflow: auto; background: var(--soft); padding: 10px; border-radius: 8px; white-space: pre-wrap; word-break: break-word; }

.foot { padding-bottom: 40px; color: var(--muted); font-size: 13px; }

@media (max-width: 640px) {
  .card { padding: 16px; }
  .tools { margin-left: 0; width: 100%; }
  .tools .field { flex: 1 1 100%; }
  .field select { width: 100%; }
  .row { align-items: stretch; }
  .tools .btn { flex: 1 1 auto; }
  .steps a { padding: 6px 8px; }
  .steps a b { display: none; }
  .filters .spacer { display: none; }
  textarea { min-height: 220px; }
  .btn.big { width: 100%; }
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
