:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #f8f9fb; --line: #e3e6eb; --line-2: #eef0f3;
  --ink: #141a24; --ink-2: #4a5465; --ink-3: #7c8697;
  --brand: #12355b; --brand-2: #1d4f86; --accent: #0f8a7e; --accent-soft: #e3f4f1;
  --danger: #c2372f; --danger-soft: #fbeceb; --warn: #b26a00; --warn-soft: #fdf3e2; --ok: #1d7a45; --ok-soft: #e6f4ec;
  --a0: #9cc9c2; --a1: #e8c36b; --a2: #e39b4d; --a3: #d9663d; --a4: #b8402f; --a5: #7d2a26;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --side: 232px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1218; --surface: #161b23; --surface-2: #1b212b; --line: #2a313d; --line-2: #222833;
    --ink: #e8ebf0; --ink-2: #b0b8c5; --ink-3: #808a99;
    --brand: #8fb6e8; --brand-2: #a9c7ef; --accent: #4cc3b5; --accent-soft: #143430;
    --danger: #ef7a72; --danger-soft: #3a1c1b; --warn: #e3a64a; --warn-soft: #36290f; --ok: #6ccf95; --ok-soft: #15301f;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1218; --surface: #161b23; --surface-2: #1b212b; --line: #2a313d; --line-2: #222833;
  --ink: #e8ebf0; --ink-2: #b0b8c5; --ink-3: #808a99;
  --brand: #8fb6e8; --brand-2: #a9c7ef; --accent: #4cc3b5; --accent-soft: #143430;
  --danger: #ef7a72; --danger-soft: #3a1c1b; --warn: #e3a64a; --warn-soft: #36290f; --ok: #6ccf95; --ok-soft: #15301f;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.num, td.n, th.n, .money { font-variant-numeric: tabular-nums; }
a { color: var(--brand-2); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: var(--side) 1fr; min-height: 100vh; }
.side { background: var(--brand); color: #fff; padding: 16px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
:root[data-theme="dark"] .side, .side { background: #12355b; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; font-weight: 700; letter-spacing: .01em; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: #fff; color: #12355b; display: grid; place-items: center; font-weight: 800; font-size: 15px; }
.brand small { display: block; font-weight: 500; opacity: .7; font-size: 11px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: rgba(255,255,255,.82); font-weight: 500; }
.nav a:hover { background: rgba(255,255,255,.08); text-decoration: none; color: #fff; }
.nav a.on { background: rgba(255,255,255,.14); color: #fff; }
.nav svg { width: 18px; height: 18px; flex: none; opacity: .9; }
.nav .sep { height: 1px; background: rgba(255,255,255,.12); margin: 8px 6px; }
.side-foot { margin-top: auto; padding: 10px; font-size: 12px; color: rgba(255,255,255,.7); }
.side-foot button { background: none; border: 0; color: #fff; padding: 0; cursor: pointer; text-decoration: underline; }

.main { min-width: 0; display: flex; flex-direction: column; }
.top { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  display: flex; align-items: center; gap: 12px; padding: 12px 24px; border-bottom: 1px solid var(--line); }
.top h1 { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page { padding: 20px 24px 96px; max-width: 1280px; width: 100%; }
.mtitle { display: none; }

.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: 9px; padding: 2px; gap: 2px; flex-wrap: nowrap; overflow-x: auto; max-width: 100%; }
.seg button { border: 0; background: none; padding: 5px 11px; border-radius: 7px; cursor: pointer; color: var(--ink-2); font-weight: 500; white-space: nowrap; }
.seg button.on { background: var(--brand); color: #fff; }
:root[data-theme="dark"] .seg button.on { color: #0e1218; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .seg button.on { color: #0e1218; } }

/* ---------- controls ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--surface);
  padding: 7px 13px; border-radius: 8px; cursor: pointer; font-weight: 600; white-space: nowrap; color: var(--ink); min-height: 36px; }
.btn:hover { border-color: var(--ink-3); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 3px 9px; min-height: 28px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: none; }
.btn:disabled { opacity: .55; cursor: default; }
.btn svg { width: 16px; height: 16px; }
.input, select.input, textarea.input { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 8px 10px; min-height: 38px; }
.input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); border-color: var(--accent); }
input.money { text-align: right; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 600; color: var(--ink-2); }
label.f .hint { font-weight: 400; color: var(--ink-3); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar .search { flex: 1 1 240px; position: relative; }
.toolbar .search input { padding-left: 34px; }
.toolbar .search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; color: var(--ink-3); }

/* ---------- cards / tiles ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card { margin-top: 16px; }
.card.flush { padding: 0; overflow: hidden; }
.card-h { display: flex; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line-2); }
.card-h h2 { margin: 0; flex: 1; }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .k { font-size: 12px; color: var(--ink-3); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.tile .v { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -.02em; }
.tile .s { font-size: 12px; color: var(--ink-3); margin-top: 2px; }
.tile.hl { border-top: 3px solid var(--accent); }
.tile.bad .v { color: var(--danger); }
.cols { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; align-items: start; }
.cols > div { min-width: 0; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; }
table.t th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-3); font-weight: 600;
  padding: 9px 12px; border-bottom: 1px solid var(--line); background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
table.t td { padding: 9px 12px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
table.t tr:last-child td { border-bottom: 0; }
table.t .n { text-align: right; white-space: nowrap; }
table.t tbody tr.click { cursor: pointer; }
table.t tbody tr.click:hover td { background: var(--surface-2); }
table.t tfoot td { font-weight: 700; border-top: 2px solid var(--line); background: var(--surface-2); }
.sub { color: var(--ink-3); font-size: 12px; }
.strong { font-weight: 600; }
.neg { color: var(--ok); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.pill.open { background: var(--warn-soft); color: var(--warn); }
.pill.closed { background: var(--ok-soft); color: var(--ok); }
.pill.over { background: var(--danger-soft); color: var(--danger); }
.pill.muted { background: var(--line-2); color: var(--ink-2); }
.empty { text-align: center; color: var(--ink-3); padding: 32px 16px; }
.more { display: flex; justify-content: center; padding: 12px; }

/* ---------- charts ---------- */
.agebar { display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: var(--line-2); }
.agebar span { display: block; height: 100%; }
.legend { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px 12px; margin-top: 12px; font-size: 12.5px; }
.legend div { display: flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend b { margin-left: auto; font-variant-numeric: tabular-nums; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--line); }
.chart text { fill: var(--ink-3); font-size: 10px; }
.chart-key { display: flex; gap: 14px; font-size: 12px; color: var(--ink-2); margin-bottom: 6px; }
.chart-key i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* ---------- lists (mobile rows) ---------- */
.list { display: flex; flex-direction: column; }
.li { display: flex; gap: 10px; align-items: center; padding: 11px 16px; border-bottom: 1px solid var(--line-2); cursor: pointer; }
.li:last-child { border-bottom: 0; }
.li:hover { background: var(--surface-2); }
.li .main-t { flex: 1; min-width: 0; }
.li .main-t .t1 { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.li .amt { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.li .amt .a1 { font-weight: 700; }

/* ---------- bill detail ---------- */
.kv { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px 16px; }
.kv div span { display: block; font-size: 11.5px; color: var(--ink-3); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.kv div b { font-size: 16px; font-variant-numeric: tabular-nums; }
.progress { height: 8px; background: var(--line-2); border-radius: 4px; overflow: hidden; display: flex; margin: 14px 0 6px; }
.progress span { height: 100%; }
details.more-ded summary { cursor: pointer; color: var(--brand-2); font-weight: 600; margin: 6px 0; list-style: none; }
details.more-ded summary::-webkit-details-marker { display: none; }
.formfoot { display: flex; align-items: center; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.formfoot .calc { flex: 1; font-size: 13px; color: var(--ink-2); }

/* ---------- sheet / modal ---------- */
.scrim { position: fixed; inset: 0; background: rgba(10,15,25,.45); z-index: 50; display: flex; align-items: flex-start; justify-content: center; padding: 60px 16px; overflow-y: auto; }
.sheet { background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.sheet-h { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line-2); }
.sheet-h h2 { margin: 0; flex: 1; }
.sheet-b { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.x { border: 0; background: none; cursor: pointer; font-size: 22px; line-height: 1; color: var(--ink-3); padding: 4px 8px; }
.err { background: var(--danger-soft); color: var(--danger); padding: 9px 12px; border-radius: 8px; font-weight: 500; }
.note { background: var(--accent-soft); color: var(--ink); padding: 9px 12px; border-radius: 8px; font-size: 13px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 80;
  font-weight: 500; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: calc(100% - 32px); }
.loading { padding: 40px; text-align: center; color: var(--ink-3); }

/* combobox */
.combo { position: relative; }
.combo-list { position: absolute; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0,0,0,.15); max-height: 260px; overflow-y: auto; z-index: 60; }
.combo-list div { padding: 8px 10px; cursor: pointer; }
.combo-list div.on, .combo-list div:hover { background: var(--accent-soft); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, #12355b, #0b2340 60%, #0f8a7e); }
.login .card { width: 100%; max-width: 380px; padding: 28px; }
.login .brand { color: var(--ink); padding: 0 0 18px; }
.login .brand-mark { background: #12355b; color: #fff; }

/* bottom bar (mobile) */
.bottom { display: none; }
.fab-menu { display: flex; flex-direction: column; gap: 8px; }
.fab-menu .btn { justify-content: flex-start; min-height: 48px; font-size: 15px; }

@media print {
  .side, .top .actions, .bottom, .toolbar, .noprint { display: none !important; }
  .shell { display: block; }
  .page { padding: 0; max-width: none; }
  .card { box-shadow: none; border: 0; padding: 0; }
  body { background: #fff; color: #000; }
  table.t th { background: #eee !important; }
}

@media (max-width: 1100px) {
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .cols { grid-template-columns: 1fr; }
  .kv { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: none; }
  .top { padding: 10px 16px; flex-wrap: wrap; }
  .top h1 { font-size: 17px; flex: 1 1 auto; }
  #actions { display: contents; }
  #actions .seg { order: 3; flex: 1 1 100%; }
  .page { padding: 14px 16px 110px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .tiles { gap: 8px; }
  .tile { padding: 12px; }
  .tile .v { font-size: 19px; }
  .legend { grid-template-columns: 1fr 1fr; }
  .hide-m { display: none !important; }
  .bottom { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--surface); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
  .bottom a, .bottom button { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 10.5px; color: var(--ink-3); background: none; border: 0; padding: 4px 0; cursor: pointer; font-weight: 600; }
  .bottom a.on { color: var(--accent); }
  .bottom svg { width: 22px; height: 22px; }
  .bottom .plus span { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; margin-top: -18px; box-shadow: 0 6px 16px rgba(15,138,126,.4); }
  .bottom .plus svg { width: 24px; height: 24px; }
  .scrim { padding: 0; align-items: flex-end; }
  .sheet { border-radius: 16px 16px 0 0; max-height: 92vh; overflow-y: auto; }
  .toast { bottom: 84px; }
  .seg button { padding: 6px 10px; }
}
@media (min-width: 761px) { .show-m { display: none !important; } }
h2 svg { width: 18px; height: 18px; vertical-align: -3px; margin-right: 4px; color: var(--accent); }
