:root {
  --brand: #f5c518; --brand-dark: #c99a00; --ink: #1f2328; --muted: #6a737d; --line: #e3e5e8;
  --bg: #f6f7f9; --card: #fff; --danger: #c62828; --ok: #2e7d32; --warn: #b26a00;
  --radius: 10px; font-size: 16px;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg); }
a { color: inherit; }
button, input, select, textarea { font: inherit; }
.loading { padding: 2rem; color: var(--muted); }

/* Layout */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { background: #23262b; color: #fff; padding: 1rem 0; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.side .brand { padding: 0 1rem 1rem; font-weight: 700; font-size: 1.1rem; }
.side .brand span { background: var(--brand); color: #23262b; padding: 0 .35rem; border-radius: 4px; margin-right: .3rem; }
.side a { display: block; padding: .55rem 1rem; text-decoration: none; color: #d6d9de; }
.side a.active, .side a:hover { background: #34383f; color: #fff; border-left: 3px solid var(--brand); padding-left: calc(1rem - 3px); }
.side .group { padding: .8rem 1rem .2rem; font-size: .75rem; text-transform: uppercase; color: #8b929b; letter-spacing: .04em; }
.side .who { padding: 1rem; font-size: .85rem; color: #a9afb7; border-top: 1px solid #34383f; margin-top: 1rem; }
main { padding: 1.25rem; max-width: 1200px; width: 100%; }
.topbar { display: none; }
.bottomnav { display: none; }

h1 { font-size: 1.35rem; margin: 0 0 1rem; display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
h1 .spacer { flex: 1; }
h2 { font-size: 1.05rem; margin: 1.25rem 0 .5rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.stats { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.35rem; font-weight: 700; margin-top: .2rem; }

label.f { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); }
label.f.req > span::after { content: " *"; color: var(--danger); }
input, select, textarea { border: 1px solid #cfd3d8; border-radius: 8px; padding: .55rem .65rem; background: #fff; color: var(--ink); width: 100%; min-height: 42px; }
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); border-color: var(--brand-dark); }
input.num { text-align: right; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .35rem; border: 1px solid #cfd3d8; background: #fff; border-radius: 8px; padding: .55rem 1rem; cursor: pointer; text-decoration: none; min-height: 42px; white-space: nowrap; }
.btn.primary { background: var(--brand); border-color: var(--brand-dark); color: #23262b; font-weight: 600; }
.btn.danger { color: var(--danger); border-color: #e7b4b4; }
.btn.small { padding: .3rem .6rem; min-height: 34px; font-size: .9rem; }
.btn:disabled { opacity: .5; cursor: default; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: 1rem; }

table.t { width: 100%; border-collapse: collapse; background: var(--card); }
table.t th, table.t td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.t th { font-size: .8rem; color: var(--muted); font-weight: 600; background: #fafbfc; }
table.t td.r, table.t th.r { text-align: right; }
table.t tr.link { cursor: pointer; }
table.t tr.link:hover td { background: #fffbe6; }
.tablewrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.badge { display: inline-block; font-size: .75rem; padding: .1rem .45rem; border-radius: 99px; background: #eef0f3; }
.badge.void { background: #fde8e8; color: var(--danger); }
.badge.ok { background: #e6f4ea; color: var(--ok); }
.badge.warn { background: #fff4e0; color: var(--warn); }
.neg { color: var(--danger); font-weight: 600; }
.muted { color: var(--muted); }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .75rem; align-items: end; }
.filters > * { flex: 1 1 160px; }
.filters > .btn { flex: 0 0 auto; }

/* Document entry lines */
.lines .line { border: 1px solid var(--line); border-radius: var(--radius); padding: .75rem; margin-bottom: .6rem; background: #fcfcfd; position: relative; }
.lines .line .row { display: grid; gap: .5rem; grid-template-columns: 2fr 1fr 1fr; }
.lines .line .row.nums { grid-template-columns: repeat(4, 1fr); margin-top: .5rem; }
.lines .line .rm { position: absolute; top: .35rem; right: .35rem; border: none; background: none; color: var(--danger); font-size: 1.3rem; cursor: pointer; }
.lines .line .amt { text-align: right; font-weight: 600; margin-top: .35rem; }
.picker { position: relative; }
.picker .dd { position: absolute; z-index: 20; left: 0; right: 0; top: 100%; background: #fff; border: 1px solid #cfd3d8; border-radius: 8px; max-height: 280px; overflow-y: auto; box-shadow: 0 6px 20px rgba(0,0,0,.12); }
.picker .dd div { padding: .55rem .65rem; cursor: pointer; border-bottom: 1px solid #f0f1f3; }
.picker .dd div:hover { background: #fffbe6; }
.totals { margin-left: auto; max-width: 360px; }
.totals .tr { display: flex; justify-content: space-between; padding: .3rem 0; gap: 1rem; align-items: center; }
.totals .tr.big { font-size: 1.3rem; font-weight: 700; border-top: 2px solid var(--ink); margin-top: .3rem; padding-top: .5rem; }
.totals input { max-width: 150px; }
.custbox { border-left: 4px solid var(--brand); }
.hint { font-size: .85rem; color: var(--muted); margin: .25rem 0 0; }
.toast { position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%); background: #23262b; color: #fff; padding: .7rem 1.1rem; border-radius: 8px; z-index: 100; max-width: 90vw; }
.toast.err { background: var(--danger); }
.locq { display: flex; gap: .35rem; flex-wrap: wrap; }
.locq span { font-size: .8rem; background: #eef0f3; border-radius: 6px; padding: .1rem .4rem; }

/* Login */
.login { max-width: 380px; margin: 10vh auto; }
.login .logo { font-size: 1.6rem; font-weight: 800; margin-bottom: 1rem; text-align: center; }
.login .logo span { background: var(--brand); padding: 0 .4rem; border-radius: 6px; }

/* Printable A4 document */
.paper { background: #fff; max-width: 210mm; margin: 0 auto; padding: 14mm; border: 1px solid var(--line); font-size: 13px; }
.paper .hd { display: flex; justify-content: space-between; gap: 1rem; border-bottom: 2px solid #23262b; padding-bottom: .6rem; margin-bottom: .8rem; }
.paper .co { font-size: 18px; font-weight: 700; }
.paper .dt { text-align: right; }
.paper .dt .ttl { font-size: 18px; font-weight: 700; letter-spacing: .05em; }
.paper .parties { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: .8rem; }
.paper table { width: 100%; border-collapse: collapse; }
.paper th, .paper td { border-bottom: 1px solid #ddd; padding: 5px 6px; text-align: left; }
.paper th { background: #f2f2f2; }
.paper .r { text-align: right; }
.paper .sum { margin-left: auto; width: 260px; margin-top: .6rem; }
.paper .sum div { display: flex; justify-content: space-between; padding: 2px 0; }
.paper .sum .grand { font-weight: 700; font-size: 15px; border-top: 2px solid #000; padding-top: 4px; }
.paper .foot { margin-top: 2rem; display: flex; justify-content: space-between; gap: 2rem; }
.paper .sign { border-top: 1px solid #000; width: 200px; text-align: center; padding-top: 4px; margin-top: 3rem; }
.paper .voidmark { color: var(--danger); font-size: 22px; font-weight: 800; border: 3px solid var(--danger); display: inline-block; padding: 2px 10px; transform: rotate(-6deg); }
.paper a { word-break: break-all; }

/* Phone */
@media (max-width: 820px) {
  .shell { display: block; }
  .side { display: none; position: fixed; inset: 0 30% 0 0; z-index: 50; height: 100%; }
  .side.open { display: block; }
  .topbar { display: flex; align-items: center; gap: .5rem; background: #23262b; color: #fff; padding: .55rem .75rem; position: sticky; top: 0; z-index: 40; }
  .topbar button { background: none; border: none; color: #fff; font-size: 1.5rem; }
  .topbar .ttl { font-weight: 700; }
  .topbar .ttl span { background: var(--brand); color: #23262b; padding: 0 .3rem; border-radius: 4px; }
  main { padding: .75rem .75rem 90px; }
  .bottomnav { display: grid; grid-template-columns: repeat(4, 1fr); position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--line); z-index: 40; padding-bottom: env(safe-area-inset-bottom); }
  .bottomnav a { text-align: center; padding: .55rem 0; text-decoration: none; font-size: .78rem; color: var(--muted); }
  .bottomnav a b { display: block; font-size: 1.25rem; }
  .bottomnav a.active { color: var(--ink); font-weight: 600; }
  .lines .line .row, .lines .line .row.nums { grid-template-columns: 1fr 1fr; }
  .lines .line .row > :first-child { grid-column: 1 / -1; }
  table.t.cards thead { display: none; }
  table.t.cards tr { display: block; border-bottom: 1px solid var(--line); padding: .4rem 0; }
  table.t.cards td { display: flex; justify-content: space-between; border: none; padding: .2rem .6rem; }
  table.t.cards td::before { content: attr(data-l); color: var(--muted); font-size: .8rem; margin-right: 1rem; }
  .paper { padding: 5mm; font-size: 12px; }
  .paper .parties { grid-template-columns: 1fr; }
}

@media print {
  @page { size: A4; margin: 10mm; }
  body { background: #fff; }
  .side, .topbar, .bottomnav, .noprint, h1 { display: none !important; }
  .shell { display: block; }
  main { padding: 0; max-width: none; }
  .paper { border: none; padding: 0; max-width: none; font-size: 12px; }
}
.warnmsg { color: var(--danger); font-size: .85rem; margin: .35rem 0 0; font-weight: 600; }
@media (max-width: 820px) { .lines .line .row:not(.nums) > * { grid-column: 1 / -1; } }
h1 .btn { font-size: 1rem; font-weight: 600; }

.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 1rem; }
.modal { width: 100%; max-width: 380px; margin: 0; }
.modal label { display: block; margin: .75rem 0; }
.modal input { width: 100%; }

/* Phase 2 */
.printonly { display: none; }
@media print {
  .printonly { display: block; margin-bottom: .5rem; }
  .tablewrap { border: none; overflow: visible; }
  table.t th, table.t td { font-size: 11px; padding: 4px; }
  table.t.cards thead { display: table-header-group !important; }
  table.t.cards tr { display: table-row !important; }
  table.t.cards td { display: table-cell !important; }
  table.t.cards td::before { content: none !important; }
}
table.t tfoot th { background: #f2f3f5; color: var(--ink); }
