:root {
  --bg: #f4f6fa; --card: #ffffff; --ink: #18202c; --muted: #5b6676; --line: #dde2ea; --soft: #eef1f6;
  --brand: #2457d6; --brand-ink: #ffffff; --danger: #b42318; --warn-bg: #fff4e0; --warn-ink: #7a4a00; --ok: #17794a;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 4px 14px rgba(20, 30, 50, .06);
  --radius: 12px; color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #11151c; --card: #1a2029; --ink: #e8ecf2; --muted: #9aa5b5; --line: #2c3544; --soft: #232b37; --brand: #6c93f5; --brand-ink: #0b1220; --danger: #ff8a7a; --warn-bg: #3a2c10; --warn-ink: #ffd89a; --ok: #5fd09a; --shadow: none; color-scheme: dark; }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; overflow-x: hidden; }
h1 { font-size: 1.5rem; margin: 0 0 14px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
p { margin: 0 0 10px; }
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }

/* shell */
.top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 14px; background: var(--card); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; white-space: nowrap; }
.brand img { border-radius: 6px; }
nav { display: flex; gap: 4px; }
.navbtn { white-space: nowrap; border: 0; background: transparent; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); }
.navbtn.on { background: var(--soft); color: var(--ink); }
#main { max-width: 1320px; margin: 0 auto; padding: 14px 14px 110px; }
.banner { margin: 0; padding: 10px 14px; background: var(--warn-bg); color: var(--warn-ink); font-weight: 600; }
.foot { max-width: 1320px; margin: 0 auto; padding: 0 14px 90px; color: var(--muted); font-size: .85rem; display: flex; flex-wrap: wrap; gap: 4px 8px; }
.toast { position: fixed; left: 50%; bottom: 82px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; max-width: min(92vw, 520px); padding: 10px 16px; border-radius: 10px; background: #1b2430; color: #fff; font-weight: 600; transition: opacity .2s, transform .2s; z-index: 50; text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.err { background: #8a1c12; }

.nudge { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: var(--soft); font-size: .92rem; }

@media (max-width: 480px) { .top { padding: 6px 8px; } .brand { font-size: .95rem; gap: 6px; } .navbtn { padding: 8px 7px; font-size: .9rem; } }

/* controls */
.btn { border: 1px solid var(--line); background: var(--card); padding: 9px 14px; border-radius: 9px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { background: var(--soft); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.quiet { background: transparent; }
.mini { border: 1px solid var(--line); background: var(--card); padding: 5px 9px; border-radius: 7px; cursor: pointer; font-size: .85rem; font-weight: 600; }
.mini:disabled { opacity: .4; cursor: default; }
.mini.danger { color: var(--danger); }
.linkbtn { border: 0; background: none; color: var(--brand); text-decoration: underline; cursor: pointer; padding: 0; font-weight: 600; }
input, select, textarea { width: 100%; min-width: 0; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
textarea { resize: vertical; min-height: 44px; }
input[type="color"] { width: 44px; height: 38px; padding: 2px; flex: none; }
input[type="checkbox"] { width: 18px; height: 18px; padding: 0; }
input.invalid { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, var(--card)); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field:has(.span2) { grid-column: 1 / -1; }
.lbl { font-size: .78rem; font-weight: 700; color: var(--muted); }
.hint, .sub { font-size: .82rem; color: var(--muted); }
.warn { margin: 8px 0 0; padding: 8px 10px; border-radius: 8px; background: var(--warn-bg); color: var(--warn-ink); font-size: .88rem; font-weight: 600; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.spacer { flex: 1; }
.check { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: .9rem; }

/* cards */
.page { max-width: 760px; margin: 0 auto; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; box-shadow: var(--shadow); }
.grid2 { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 520px) { .grid2 { grid-template-columns: 1fr 1fr; } }
.fold > summary { cursor: pointer; font-weight: 700; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.fold[open] > summary { margin-bottom: 10px; }

/* editor */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; margin-bottom: 12px; }
.seggroup { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg { border: 0; background: var(--card); padding: 9px 14px; font-weight: 700; cursor: pointer; }
.seg.on { background: var(--brand); color: var(--brand-ink); }
label.inline { display: inline-flex; align-items: center; gap: 6px; }
label.inline select { width: auto; }
.dirty { font-size: .85rem; color: var(--muted); font-weight: 600; }
.dirty.on { color: var(--warn-ink); background: var(--warn-bg); padding: 3px 8px; border-radius: 99px; }
.chip { font-size: .85rem; background: var(--soft); padding: 3px 10px; border-radius: 99px; font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.split { display: grid; grid-template-columns: 1fr; gap: 14px; align-items: start; }
.tabs { display: none; }
.items { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.item { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.f-desc, .f-foot { grid-column: 1 / -1; }
.f-amt { display: flex; flex-direction: column; gap: 4px; justify-content: flex-start; }
.amount { font-weight: 700; padding: 9px 0; text-align: right; overflow-wrap: anywhere; }
.f-foot { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.f-qty, .f-disc { grid-column: span 1; }
@media (max-width: 480px) {
  .item { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .f-amt { grid-column: 1 / -1; flex-direction: row; justify-content: space-between; align-items: center; }
}
.summary { display: grid; grid-template-columns: 1fr auto; gap: 4px 16px; margin: 12px 0 0; padding-top: 10px; border-top: 1px dashed var(--line); }
.summary dt, .summary dd { margin: 0; }
.summary dd { text-align: right; font-variant-numeric: tabular-nums; }
.summary .strong { font-weight: 800; font-size: 1.05rem; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.sw { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--card); box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.sw.on { box-shadow: 0 0 0 3px var(--ink); }
.sw[data-c="#2457d6"] { background: #2457d6; } .sw[data-c="#0f766e"] { background: #0f766e; } .sw[data-c="#15803d"] { background: #15803d; } .sw[data-c="#b45309"] { background: #b45309; }
.sw[data-c="#c2410c"] { background: #c2410c; } .sw[data-c="#be123c"] { background: #be123c; } .sw[data-c="#7e22ce"] { background: #7e22ce; } .sw[data-c="#1f2937"] { background: #1f2937; }

/* preview */
.previewcol { min-width: 0; }
.prevhead { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.pages { display: flex; flex-direction: column; gap: 14px; }
.sheetwrap { background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .18), 0 8px 24px rgba(0, 0, 0, .12); border-radius: 3px; overflow: hidden; line-height: 0; }
.sheet { display: block; width: 100%; height: auto; }

@media (min-width: 1000px) {
  .split { grid-template-columns: minmax(0, 1fr) minmax(0, 560px); }
  .previewcol { position: sticky; top: 64px; max-height: calc(100vh - 80px); overflow: auto; padding-right: 2px; }
  #main { padding-bottom: 40px; } .foot { padding-bottom: 24px; }
  .toast { bottom: 24px; }
}
@media (max-width: 999px) {
  .tabs { display: flex; position: sticky; top: 49px; z-index: 15; background: var(--bg); margin: -14px -14px 10px; padding: 8px 14px; gap: 8px; border-bottom: 1px solid var(--line); }
  .tab { flex: 1; border: 1px solid var(--line); background: var(--card); padding: 9px; border-radius: 9px; font-weight: 700; cursor: pointer; }
  .tab.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
  .show-edit .previewcol { display: none; }
  .show-preview .formcol { display: none; }
  .actions { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: var(--card); border-top: 1px solid var(--line); flex-wrap: nowrap; gap: 6px; }
  .actions .btn { flex: 1; padding: 11px 4px; min-width: 0; text-overflow: ellipsis; overflow: hidden; }
  .long { display: none; }
}

/* saved list */
.filters { margin-bottom: 10px; }
.filters input { flex: 1 1 220px; width: auto; }
.filters select { width: auto; }
.outstanding { font-weight: 700; margin: 0 0 10px; }
.list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.rowcard { display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.rowcard.slim { align-items: center; }
.rc-main { min-width: 0; }
.rc-title { display: flex; flex-wrap: wrap; gap: 6px 8px; align-items: center; }
.rc-sub { color: var(--muted); font-size: .88rem; overflow-wrap: anywhere; }
.rc-amt { font-weight: 800; text-align: right; display: flex; flex-direction: column; }
.rc-amt small { font-weight: 600; color: var(--muted); }
.rc-acts { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.rc-acts select { width: auto; padding: 5px 8px; }
.tag { font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--soft); color: var(--muted); }
.tag.late { background: var(--warn-bg); color: var(--danger); }
.empty { color: var(--muted); padding: 24px 8px; text-align: center; }
.logobox { width: 150px; height: 84px; border: 2px dashed var(--line); border-radius: 10px; display: flex; align-items: center; justify-content: center; overflow: hidden; background: #fff; }
.logoimg { max-width: 100%; max-height: 100%; }
.importpanel { margin-top: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--soft); }
.madeby { font-weight: 700; }

/* print: only the pages, at the real paper size (@page and the sheet size are set from code) */
@media print {
  html, body { background: #fff; }
  .top, .banner, .foot, .toast, .toolbar, .tabs, .formcol, .prevhead, .page, .warn { display: none !important; }
  #main, .editor-wrap, .editor, .split { display: block !important; padding: 0; margin: 0; max-width: none; }
  .previewcol { display: block !important; position: static; max-height: none; overflow: visible; padding: 0; }
  .pages { display: block; }
  .sheetwrap { box-shadow: none; border-radius: 0; break-after: page; page-break-after: always; margin: 0; }
  .sheetwrap:last-child { break-after: auto; page-break-after: auto; }
  .sheet { display: block; }
}
