:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #16181d;
  --muted: #646b78;
  --line: #dfe2e8;
  --accent: #1d4ed8;
  --accent-ink: #ffffff;
  --accent-soft: #eaf0ff;
  --danger: #b42318;
  --paper: #fbfaf5;
  --guide: #9aa3b5;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121418; --card: #1b1e24; --ink: #e8eaee; --muted: #9aa1ad; --line: #2c313a;
    --accent: #7aa2ff; --accent-ink: #0d1120; --accent-soft: #1e2a47; --danger: #ff8a7a; --paper: #f4f2ea; --guide: #8e97aa;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121418; --card: #1b1e24; --ink: #e8eaee; --muted: #9aa1ad; --line: #2c313a;
  --accent: #7aa2ff; --accent-ink: #0d1120; --accent-soft: #1e2a47; --danger: #ff8a7a; --paper: #f4f2ea; --guide: #8e97aa;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", "PingFang HK", "Noto Sans HK", sans-serif; }
.app { max-width: 1280px; margin: 0 auto; padding: 0 16px 48px; }
.top { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 12px 0 8px; display: flex; gap: 16px; align-items: center; flex-wrap: wrap; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: .01em; }
.brand svg { width: 24px; height: 24px; fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; }
.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; max-width: 100%; }
.tabs button { border: 0; background: none; color: var(--muted); padding: 7px 12px; border-radius: 8px; font: inherit; cursor: pointer; white-space: nowrap; }
.tabs button:hover { color: var(--ink); background: var(--card); }
.tabs button[aria-selected="true"] { color: var(--accent); background: var(--accent-soft); font-weight: 600; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; min-width: 0; }
.card h2 { font-size: 16px; margin: 0 0 12px; }
.stack { display: grid; gap: 16px; }
.write-grid { display: grid; grid-template-columns: minmax(300px, 400px) 1fr; gap: 16px; align-items: start; }
.write-grid > .preview { position: sticky; top: 76px; }
@media (max-width: 900px) { .write-grid { grid-template-columns: 1fr; } .write-grid > .preview { position: static; order: -1; } }

.form { display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.inline { display: inline-flex; align-items: center; gap: 6px; color: var(--ink); font-size: 14px; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0; }
input[type="checkbox"] { width: auto; accent-color: var(--accent); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; align-items: end; }
@media (max-width: 640px) { .row2, .row3 { grid-template-columns: 1fr; } }
.with-toggle > span { display: inline-flex; gap: 6px; align-items: center; }
.pair { display: grid; grid-template-columns: 1.4fr 1fr auto; gap: 6px; align-items: center; }
.unit { color: var(--muted); font-size: 12px; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 12px; display: grid; gap: 10px; margin: 0; }
legend { padding: 0 4px; }

.wording { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: grid; gap: 6px; }
.wording-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.wording-head select { width: auto; padding: 4px 8px; }
.words { margin: 0; font-size: 14px; }

button, .button { font: inherit; border: 1px solid var(--line); background: var(--card); color: var(--ink); padding: 8px 14px; border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap; }
.button { font-size: 15px; color: var(--ink); }
button:hover, .button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: none; border-color: transparent; color: var(--muted); }
button.danger, button.armed { color: var(--danger); border-color: var(--danger); }
button.link { border: 0; background: none; padding: 2px 6px; color: var(--accent); }
button.link.danger { color: var(--danger); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions.sticky { position: sticky; bottom: 12px; background: var(--bg); padding: 8px; border-radius: 10px; border: 1px solid var(--line); }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar h2 { margin: 0; }
.toolbar input { width: auto; flex: 1; min-width: 180px; }
.toolbar select { width: auto; }
.spacer { flex: 1; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; margin-bottom: 12px; }
.filters label { min-width: 130px; flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.preview-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.preview-head h2 { margin: 0 0 8px; }
.cheque-svg { width: 100%; height: auto; display: block; }
.cheque-svg .paper { fill: var(--paper); stroke: #c9c5b4; stroke-width: .3; }
.cheque-svg .guide { fill: none; stroke: var(--guide); stroke-width: .18; stroke-dasharray: .8 .6; }
.cheque-svg text { fill: #111; }
.mini-preview { max-width: 520px; }

.note { background: var(--accent-soft); color: var(--ink); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; margin: 0; }
code { font-size: 12px; background: var(--accent-soft); padding: 1px 4px; border-radius: 4px; }

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 8px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.void td { color: var(--muted); text-decoration: line-through; }
tr.void td:last-child, tr.void td:first-child { text-decoration: none; }
.row-actions { white-space: nowrap; text-align: right; }
.tag { font-size: 11px; border: 1px solid var(--danger); color: var(--danger); border-radius: 4px; padding: 0 4px; text-decoration: none; display: inline-block; }

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.stat { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; display: grid; gap: 2px; }
.stat span { font-size: 12px; color: var(--muted); }
.stat b { font-variant-numeric: tabular-nums; font-weight: 600; }
.stat.strong { background: var(--accent-soft); border-color: transparent; }

.plain-list { list-style: none; margin: 0; padding: 0; }
.plain-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
details { margin-top: 12px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px); z-index: 20; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--danger); color: #fff; }

#print-root { display: none; }
@media print {
  html, body { background: #fff !important; }
  .app, .toast { display: none !important; }
  #print-root { display: block; }
  .print-sheet { overflow: hidden; page-break-after: always; break-after: page; }
  .print-sheet:last-child { page-break-after: auto; break-after: auto; }
  .print-page { display: block; }
  .print-page text { fill: #000; }
  .report { font: 11px/1.4 Arial, sans-serif; color: #000; }
  .report h1 { font-size: 16px; margin: 0 0 4px; }
  .report h2 { font-size: 13px; margin: 14px 0 4px; }
  .report table { width: 100%; border-collapse: collapse; }
  .report th, .report td { border-bottom: 1px solid #ccc; padding: 3px 4px; font-size: 11px; }
  .report tr.sum td { font-weight: 700; border-top: 1.5px solid #000; }
  .report tr.void td { text-decoration: line-through; color: #777; }
}

.printer-grid { display: grid; grid-template-columns: 1fr 220px; gap: 20px; align-items: start; }
@media (max-width: 700px) { .printer-grid { grid-template-columns: 1fr; } }
.sheet-fig { margin: 0; }
.sheet-svg { width: 100%; height: auto; max-height: 320px; display: block; }
.sheet-svg .sheet { fill: #fff; stroke: var(--muted); stroke-width: 1; }
.sheet-svg .cheque-area { fill: #9fd9a8; stroke: #2f8f46; stroke-width: 1; }
.sheet-svg .corner { stroke: #b42318; stroke-width: 2; fill: none; }
.sheet-svg .cheque-label { fill: #14532d; }
.sheet-svg .feed-label { fill: var(--muted); }
select:disabled { opacity: .5; }
