:root {
  color-scheme: light;
  --page: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #f3f4f8;
  --surface-3: #e9ebf2;
  --ink: #12131a;
  --ink-2: #4a4c58;
  --muted: #6e7080;
  --grid: #e6e7ee;
  --axis: #c9cbd6;
  --border: rgba(18, 19, 26, 0.08);
  --brand: #4338ca;
  --brand-2: #6d28d9;
  --brand-ink: #ffffff;
  --brand-soft: #eceefe;
  --brand-text: #3730a3;
  /* Chart colours: the validated categorical order (identity), first four slots only. */
  --series-1: #2a78d6;
  --cat-1: #2a78d6;
  --cat-2: #eb6834;
  --cat-3: #1baf7a;
  --cat-4: #eda100;
  --track: #eceef4;
  --good-text: #00702b;
  --bad-text: #b42828;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --info-bg: #e7f0fb;
  --info-text: #1c5cab;
  --good-bg: #e3f5e8;
  --bad-bg: #fdecec;
  --warn-bg: #fdf3dc;
  --warn-text: #7a5200;
  --neutral-bg: #eef0f5;
  --tone-blue-bg: #e6f0fc; --tone-blue: #1c5cab;
  --tone-green-bg: #e3f5ec; --tone-green: #0f7a50;
  --tone-orange-bg: #fdeee6; --tone-orange: #b5461a;
  --tone-violet-bg: #efeafd; --tone-violet: #5b3fc4;
  --shadow: 0 1px 2px rgba(18, 19, 26, 0.04), 0 4px 16px -6px rgba(18, 19, 26, 0.08);
  --shadow-lg: 0 2px 4px rgba(18, 19, 26, 0.04), 0 16px 40px -12px rgba(18, 19, 26, 0.18);
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0c0d12;
    --surface: #17181f;
    --surface-2: #1f2029;
    --surface-3: #282a35;
    --ink: #f0f1f5;
    --ink-2: #c4c6d2;
    --muted: #9799a8;
    --grid: #2a2c36;
    --axis: #3a3c48;
    --border: rgba(255, 255, 255, 0.08);
    --brand: #8b8ff7;
    --brand-2: #b18cf5;
    --brand-ink: #12131a;
    --brand-soft: #23244a;
    --brand-text: #b4b7fb;
    --series-1: #3987e5;
    --cat-1: #3987e5;
    --cat-2: #d95926;
    --cat-3: #199e70;
    --cat-4: #c98500;
    --track: #262833;
    --good-text: #4cc970;
    --bad-text: #f07a7a;
    --info-bg: #182a40;
    --info-text: #8fbaf0;
    --good-bg: #15301f;
    --bad-bg: #3a1c1c;
    --warn-bg: #3a2f14;
    --warn-text: #f3c867;
    --neutral-bg: #262833;
    --tone-blue-bg: #182a40; --tone-blue: #8fbaf0;
    --tone-green-bg: #15301f; --tone-green: #5fd197;
    --tone-orange-bg: #3a2318; --tone-orange: #f3a07a;
    --tone-violet-bg: #2a2348; --tone-violet: #bba8f8;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 16px 40px -12px rgba(0, 0, 0, 0.6);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0c0d12;
  --surface: #17181f;
  --surface-2: #1f2029;
  --surface-3: #282a35;
  --ink: #f0f1f5;
  --ink-2: #c4c6d2;
  --muted: #9799a8;
  --grid: #2a2c36;
  --axis: #3a3c48;
  --border: rgba(255, 255, 255, 0.08);
  --brand: #8b8ff7;
  --brand-2: #b18cf5;
  --brand-ink: #12131a;
  --brand-soft: #23244a;
  --brand-text: #b4b7fb;
  --series-1: #3987e5;
  --cat-1: #3987e5;
  --cat-2: #d95926;
  --cat-3: #199e70;
  --cat-4: #c98500;
  --track: #262833;
  --good-text: #4cc970;
  --bad-text: #f07a7a;
  --info-bg: #182a40;
  --info-text: #8fbaf0;
  --good-bg: #15301f;
  --bad-bg: #3a1c1c;
  --warn-bg: #3a2f14;
  --warn-text: #f3c867;
  --neutral-bg: #262833;
  --tone-blue-bg: #182a40; --tone-blue: #8fbaf0;
  --tone-green-bg: #15301f; --tone-green: #5fd197;
  --tone-orange-bg: #3a2318; --tone-orange: #f3a07a;
  --tone-violet-bg: #2a2348; --tone-violet: #bba8f8;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 16px 40px -12px rgba(0, 0, 0, 0.6);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background:
    radial-gradient(1200px 380px at 10% -120px, color-mix(in srgb, var(--brand) 12%, transparent), transparent 70%),
    radial-gradient(900px 320px at 95% -140px, color-mix(in srgb, var(--brand-2) 10%, transparent), transparent 70%),
    var(--page);
  background-repeat: no-repeat;
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

a { color: inherit; }
svg { flex: none; }

.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-top: 12px; padding-bottom: 10px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo {
  width: 40px; height: 40px; border-radius: 12px; flex: none;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff;
  font-weight: 750; font-size: 14px; letter-spacing: 0.04em;
  box-shadow: 0 6px 16px -6px color-mix(in srgb, var(--brand) 70%, transparent), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
.brand-name { font-weight: 700; font-size: 16.5px; line-height: 1.2; letter-spacing: -0.01em; }
.brand-sub { color: var(--muted); font-size: 12.5px; }
.top-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sync { color: var(--muted); font-size: 12.5px; margin-right: 4px; display: inline-flex; align-items: center; gap: 6px; }
.sync::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 20%, transparent); }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  font: inherit; font-size: 13.5px; font-weight: 500; cursor: pointer; text-decoration: none;
  box-shadow: 0 1px 2px rgba(18, 19, 26, 0.05);
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.btn:hover { background: var(--surface-2); border-color: color-mix(in srgb, var(--ink) 14%, transparent); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .chip:focus-visible, .tabs a:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.btn.icon-only { padding: 0; width: 36px; justify-content: center; }
.btn[disabled] { opacity: 0.6; cursor: progress; }
.btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; padding-bottom: 10px; }
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 14px; border-radius: 10px; text-decoration: none; color: var(--ink-2); font-size: 14px; font-weight: 550;
  white-space: nowrap; transition: background 0.15s, color 0.15s;
}
.tabs a svg { opacity: 0.75; }
.tabs a:hover { color: var(--ink); background: var(--surface-2); }
.tabs a[aria-current="page"] { color: var(--brand-text); background: var(--brand-soft); }
.tabs a[aria-current="page"] svg { opacity: 1; }
.count { color: var(--muted); font-weight: 500; font-variant-numeric: tabular-nums; font-size: 12px; background: var(--surface-3); border-radius: 999px; padding: 0 7px; }
.count:empty { display: none; }
.tabs a[aria-current="page"] .count { background: color-mix(in srgb, var(--brand) 16%, transparent); color: var(--brand-text); }

/* Layout */
main { padding-top: 24px; padding-bottom: 24px; }
.view > * + * { margin-top: 18px; }
.view { animation: rise 0.25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } }
.grid { display: grid; gap: 16px; }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.hero-grid { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); }
.grid.hero-grid, .grid.cols-2 { align-items: start; }
@media (max-width: 900px) {
  .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.cols-3, .grid.cols-2, .grid.hero-grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 420px) {
  .grid.cols-4 { grid-template-columns: minmax(0, 1fr); }
  .sync { flex-basis: 100%; order: 9; }
}

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); min-width: 0;
}
.card h2 { font-size: 15.5px; font-weight: 650; margin: 0 0 4px; letter-spacing: -0.005em; }
.card .sub { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.card-head .sub { margin-bottom: 10px; }
.card-head > div { flex: 1 1 220px; min-width: 0; }
.card h2.sub-h { margin-top: 24px; }
.stat .detail a { color: var(--brand-text); font-weight: 600; text-decoration: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head h1 { font-size: 24px; margin: 0; font-weight: 750; letter-spacing: -0.02em; }
.page-head p { margin: 4px 0 0; color: var(--muted); font-size: 13.5px; }
.page-head p b { color: var(--ink); margin-left: 4px; }
.page-head p a { color: var(--brand-text); font-weight: 600; text-decoration: none; }

/* Hero */
.hero { position: relative; overflow: hidden; display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 32px; align-items: center; padding: 24px; }
.hero::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(600px 240px at 0% 0%, color-mix(in srgb, var(--tint, var(--brand)) 10%, transparent), transparent 70%);
}
.hero.is-loss { --tint: var(--critical); }
.hero.is-profit { --tint: var(--good); }
.hero > * { position: relative; }
.hero .label { color: var(--ink-2); font-size: 13.5px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.hero .value { font-size: 44px; font-weight: 800; line-height: 1.05; margin: 8px 0 4px; letter-spacing: -0.03em; }
.hero .value.neg { color: var(--bad-text); }
.hero .value.pos { color: var(--good-text); }
.hero .explain { color: var(--ink-2); font-size: 13.5px; margin: 8px 0 0; }
.hero-balance { padding-left: 32px; border-left: 1px solid var(--grid); }
@media (max-width: 900px) {
  .hero { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .hero-balance { padding-left: 0; border-left: 0; padding-top: 16px; border-top: 1px solid var(--grid); }
  .hero .value { font-size: 36px; }
}
.card .table-wrap { border-radius: 12px; box-shadow: none; }
.calc { margin-top: 14px; display: grid; gap: 7px; font-size: 13.5px; max-width: 460px; }
.calc-row { display: grid; grid-template-columns: 14px minmax(0, 1fr) auto; gap: 8px; align-items: baseline; }
.calc-sign { color: var(--muted); font-weight: 600; text-align: center; }
.calc-row b { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.calc-note { display: block; color: var(--muted); font-size: 12px; }
.calc-total { border-top: 1px dashed var(--axis); padding-top: 8px; font-weight: 650; }
.calc-total b { font-size: 15.5px; }
.equation { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: baseline; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.equation b { color: var(--ink); font-weight: 600; }
.small-note { font-size: 12.5px; margin: 14px 0 0; line-height: 1.55; }
.banner a { color: inherit; font-weight: 650; }

/* Icon chips (decoration only: tiles, headers) */
.ico {
  width: 38px; height: 38px; border-radius: 11px; display: inline-grid; place-items: center; flex: none;
  background: var(--brand-soft); color: var(--brand-text);
}
.ico.sm { width: 30px; height: 30px; border-radius: 9px; }
.ico.blue { background: var(--tone-blue-bg); color: var(--tone-blue); }
.ico.green { background: var(--tone-green-bg); color: var(--tone-green); }
.ico.orange { background: var(--tone-orange-bg); color: var(--tone-orange); }
.ico.violet { background: var(--tone-violet-bg); color: var(--tone-violet); }
.ico.red { background: var(--bad-bg); color: var(--bad-text); }

/* Stat tiles */
.stat { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 4px 14px; align-items: start; transition: transform 0.15s, box-shadow 0.15s; }
.stat:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.stat .ico { grid-row: span 3; }
.stat.no-ico { grid-template-columns: minmax(0, 1fr); }
.stat .label { color: var(--muted); font-size: 12.5px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.stat .value { font-size: 26px; font-weight: 750; margin: 0; line-height: 1.2; letter-spacing: -0.02em; }
.stat .detail { color: var(--muted); font-size: 12.5px; line-height: 1.45; }

/* Bar list chart (horizontal bars, one series) */
.bars { display: grid; gap: 12px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 160px) minmax(0, 1fr) auto; align-items: center; gap: 12px; font-size: 13.5px; }
.bar-row .name { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.bar-row .amt { color: var(--ink); font-variant-numeric: tabular-nums; text-align: right; min-width: 64px; font-weight: 600; }
.bar-row .amt small { color: var(--muted); margin-left: 4px; font-weight: 400; }
.bar-track { position: relative; height: 24px; border-radius: 6px; cursor: default; background: var(--track); }
.bar-fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--series-1); border-radius: 6px; min-width: 4px; transition: filter 0.15s; }
.bar-track:hover .bar-fill, .bar-track:focus .bar-fill { filter: brightness(1.12); }

/* Balance: two bars on one scale, coloured by part (categorical order) */
.balance-row + .balance-row { margin-top: 18px; }
.balance-head { display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; margin-bottom: 8px; font-weight: 600; }
.balance-head b { font-variant-numeric: tabular-nums; }
.balance-track { display: flex; gap: 2px; height: 28px; border-radius: 8px; background: var(--track); overflow: hidden; }
.balance-track .seg { height: 100%; background: var(--cat-1); cursor: default; min-width: 3px; transition: filter 0.15s; }
.balance-track .seg:first-child { border-radius: 8px 0 0 8px; }
.balance-track .seg:last-child { border-radius: 0 8px 8px 0; }
.balance-track .seg:only-child { border-radius: 8px; }
.seg.c1, .legend i.c1 { background: var(--cat-1); }
.seg.c2, .legend i.c2 { background: var(--cat-2); }
.seg.c3, .legend i.c3 { background: var(--cat-3); }
.seg.c4, .legend i.c4 { background: var(--cat-4); }
.balance-track .seg:hover, .balance-track .seg:focus { filter: brightness(1.12); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 8px; font-size: 12.5px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.legend i { width: 10px; height: 10px; border-radius: 3px; background: var(--cat-1); display: inline-block; }

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
.chip {
  height: 30px; padding: 0 12px; border-radius: 8px; border: 0;
  background: transparent; color: var(--ink-2); font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; transition: background 0.15s, color 0.15s;
}
.chip:hover { color: var(--ink); background: var(--surface-2); }
.chip[aria-pressed="true"] { background: var(--brand); color: var(--brand-ink); }
.chip .n { font-variant-numeric: tabular-nums; opacity: 0.7; font-size: 12px; }
.search, .select {
  height: 40px; border-radius: 12px; border: 1px solid var(--border); background-color: var(--surface); color: var(--ink);
  font: inherit; font-size: 13.5px; padding: 0 12px; min-width: 0; box-shadow: var(--shadow);
}
.search {
  flex: 1 1 220px; max-width: 360px; padding-left: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236e7080' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M20 20l-4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: 12px center;
}
.select { flex: 0 1 260px; }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { font-size: 11.5px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th .arrow { font-size: 10px; margin-left: 4px; color: var(--brand); }
tbody tr { transition: background 0.1s; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: color-mix(in srgb, var(--brand) 4%, var(--surface)); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td .small { display: block; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; margin-top: 2px; }
td.prod { min-width: 200px; font-weight: 550; }
.card td.prod { min-width: 140px; }
tfoot td { font-weight: 650; border-top: 1px solid var(--axis); border-bottom: 0; background: var(--surface-2); }
.pos { color: var(--good-text); }
.neg { color: var(--bad-text); }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.no-rows { padding: 32px; text-align: center; color: var(--muted); }

.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; white-space: nowrap;
}
.badge.good { background: var(--good-bg); color: var(--good-text); }
.badge.info { background: var(--info-bg); color: var(--info-text); }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); }
.badge.bad { background: var(--bad-bg); color: var(--bad-text); }
.badge.neutral { background: var(--neutral-bg); color: var(--ink-2); }
.badge.brand { background: var(--brand-soft); color: var(--brand-text); }

.meter { display: inline-block; width: 60px; height: 6px; border-radius: 3px; background: var(--track); vertical-align: middle; margin-left: 8px; overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--series-1); border-radius: 3px; }

td .small.reason { max-width: 220px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.copy { border: 0; background: none; color: var(--brand-text); cursor: pointer; padding: 0 2px; font: inherit; font-size: 12px; font-weight: 600; }
.copy:hover { text-decoration: underline; }

/* Banner, loading, tooltip */
.banner { margin-top: 18px; padding: 12px 16px; border-radius: 12px; font-size: 13.5px; background: var(--warn-bg); color: var(--warn-text); border: 1px solid color-mix(in srgb, var(--warn-text) 18%, transparent); }
.banner.error { background: var(--bad-bg); color: var(--bad-text); border-color: color-mix(in srgb, var(--bad-text) 18%, transparent); }
.loading { display: grid; gap: 16px; }
.skeleton { height: 96px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.4s ease-in-out infinite; }
.skeleton.tall { height: 200px; }
.skeleton-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  .skeleton, .btn.spinning svg, .view { animation: none; }
  .stat:hover { transform: none; }
}

.tooltip {
  position: fixed; z-index: 50; pointer-events: none; max-width: 260px;
  background: var(--ink); color: var(--surface); padding: 8px 11px; border-radius: 8px;
  font-size: 12.5px; line-height: 1.45; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}
.tooltip b { font-variant-numeric: tabular-nums; }

/* Buttons used by forms */
.btn.primary {
  background: linear-gradient(135deg, var(--brand), color-mix(in srgb, var(--brand) 70%, var(--brand-2)));
  border-color: transparent; color: var(--brand-ink); font-weight: 650;
  box-shadow: 0 4px 12px -4px color-mix(in srgb, var(--brand) 60%, transparent), inset 0 1px 0 rgba(255, 255, 255, 0.18);
}
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad-text); }
.btn.danger:hover { background: var(--bad-bg); }
.link-btn { border: 0; background: none; padding: 4px 8px; border-radius: 6px; font: inherit; font-size: 13px; color: var(--brand-text); cursor: pointer; font-weight: 600; }
.link-btn:hover { background: var(--brand-soft); }
td.actions { width: 1%; text-align: right; white-space: nowrap; }
.icon-btn { border: 0; background: none; color: var(--muted); font-size: 22px; line-height: 1; width: 34px; height: 34px; border-radius: 8px; cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

/* Pop-up form */
.modal {
  border: 1px solid var(--border); border-radius: 20px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink); box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.45);
  max-height: calc(100vh - 32px); overflow: auto;
}
.modal[open] { animation: pop 0.18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
.modal::backdrop { background: rgba(12, 13, 18, 0.5); backdrop-filter: blur(3px); }
.modal-form { padding: 22px 24px 24px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.modal-head h2 { margin: 0; font-size: 19px; font-weight: 750; letter-spacing: -0.015em; }
.modal-intro, .modal-body { color: var(--ink-2); font-size: 13.5px; margin: 0 0 16px; }
.modal-body ol { padding-left: 20px; }
.modal-body code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.field { display: grid; gap: 5px; min-width: 0; align-content: start; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field .req { color: var(--bad-text); }
.field input, .field select {
  height: 42px; border-radius: 10px; border: 1px solid var(--axis); background: var(--surface); color: var(--ink);
  font: inherit; font-size: 14px; padding: 0 12px; width: 100%; min-width: 0; transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:hover, .field select:hover { border-color: color-mix(in srgb, var(--brand) 40%, var(--axis)); }
.field input:focus-visible, .field select:focus-visible { outline: none; border-color: var(--brand); box-shadow: 0 0 0 4px color-mix(in srgb, var(--brand) 18%, transparent); }
.field input:user-invalid, .field select:user-invalid { border-color: var(--critical); }
.field .help { font-size: 12px; color: var(--muted); }
.check { display: flex; gap: 8px; align-items: flex-start; margin-top: 12px; font-size: 13.5px; color: var(--ink-2); cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--brand); }
.modal-form.narrow .field + .field { margin-top: 10px; }
.form-hint { margin-top: 16px; padding: 12px 14px; border-radius: 10px; background: var(--brand-soft); font-size: 13px; color: var(--ink-2); }
.form-error { margin-top: 12px; padding: 12px 14px; border-radius: 10px; background: var(--bad-bg); color: var(--bad-text); font-size: 13px; }
.modal-foot { display: flex; align-items: center; gap: 8px; margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--grid); }
.modal-foot .spacer { flex: 1; }
.modal-form.narrow .field { margin-bottom: 4px; }
@media (max-width: 520px) {
  .form-grid { grid-template-columns: 1fr; }
}

/* Download menu */
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 8px); z-index: 20; min-width: 250px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg);
  display: grid;
}
.menu-list a { padding: 9px 12px; border-radius: 8px; text-decoration: none; font-size: 13.5px; color: var(--ink); }
.menu-list a:hover, .menu-list a:focus-visible { background: var(--brand-soft); color: var(--brand-text); }
.user-name:not(:empty)::before { content: "· "; }
@media (max-width: 520px) { .user-name { display: none; } }

.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn.tiny { height: 28px; padding: 0 10px; font-size: 12.5px; font-weight: 650; color: var(--brand-text); border-color: color-mix(in srgb, var(--brand) 40%, transparent); background: var(--brand-soft); margin-right: 6px; box-shadow: none; }
.btn.tiny:hover { background: color-mix(in srgb, var(--brand) 16%, var(--surface)); }
.note { padding: 12px 16px; border-radius: 12px; font-size: 13.5px; background: var(--warn-bg); color: var(--warn-text); border: 1px solid color-mix(in srgb, var(--warn-text) 18%, transparent); }

/* Import preview */
.modal.wide { width: min(760px, calc(100vw - 32px)); }
.modal-body h3 { font-size: 14px; margin: 18px 0 8px; color: var(--ink); }
.import-summary { font-size: 14px; color: var(--ink); margin: 0 0 4px; }
.map-list { display: grid; gap: 8px; }
.map-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 260px); gap: 10px; align-items: center; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--border); }
.map-row .small { display: block; color: var(--muted); font-size: 12px; }
.map-row select { height: 38px; border-radius: 10px; border: 1px solid var(--axis); background: var(--surface); color: var(--ink); font: inherit; font-size: 13.5px; padding: 0 8px; min-width: 0; }
.change-list { margin: 0; padding-left: 18px; font-size: 13px; color: var(--ink-2); display: grid; gap: 4px; max-height: 240px; overflow: auto; }
.change-list .small { display: block; color: var(--muted); font-size: 12px; }
.modal-body details { margin-top: 12px; font-size: 13px; }
.modal-body summary { cursor: pointer; color: var(--ink-2); }
@media (max-width: 560px) { .map-row { grid-template-columns: minmax(0, 1fr); } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); z-index: 60;
  background: var(--ink); color: var(--surface); padding: 11px 18px; border-radius: 12px; font-size: 14px; font-weight: 550;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; box-shadow: var(--shadow-lg);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.foot { color: var(--muted); font-size: 12.5px; padding-top: 8px; padding-bottom: 32px; text-align: center; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Partners */
.avatar {
  width: 44px; height: 44px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  color: #fff; font-weight: 750; font-size: 16px; background: var(--cat-1);
  box-shadow: 0 0 0 3px var(--surface), 0 0 0 4px var(--border);
}
.avatar.c2 { background: var(--cat-2); }
.avatar.c3 { background: var(--cat-3); }
.avatar.c4 { background: var(--cat-4); }
.avatar.lg { width: 56px; height: 56px; font-size: 20px; }
.avatar.xs { width: 22px; height: 22px; font-size: 10px; box-shadow: none; }
.who-chip { display: inline-flex; align-items: center; gap: 7px; font-weight: 550; white-space: nowrap; }
.partner-amt .detail b { color: var(--ink); font-weight: 650; }
.partner-amt .detail + .detail { margin-top: 2px; }
.partner-card { display: grid; gap: 14px; transition: transform 0.15s, box-shadow 0.15s; }
.partner-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.partner-top { display: flex; align-items: center; gap: 12px; }
.partner-top .who { min-width: 0; flex: 1; }
.partner-top .who b { display: block; font-size: 16px; }
.partner-top .who span { color: var(--muted); font-size: 12.5px; }
.partner-amt .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.partner-amt .value { font-size: 28px; font-weight: 800; letter-spacing: -0.02em; line-height: 1.15; }
.partner-amt .detail { color: var(--muted); font-size: 12.5px; }
.partner-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding-top: 12px; border-top: 1px solid var(--grid); font-size: 13px; color: var(--ink-2); }
.income-card { background: linear-gradient(135deg, color-mix(in srgb, var(--brand) 8%, var(--surface)), var(--surface) 60%); }

.split-track { position: relative; display: flex; gap: 2px; height: 40px; border-radius: 12px; overflow: hidden; background: var(--track); }
.split-track .seg { display: flex; align-items: center; padding: 0 12px; color: #fff; font-weight: 700; font-size: 13px; white-space: nowrap; overflow: hidden; min-width: 4px; cursor: default; }
.split-track .seg:hover, .split-track .seg:focus { filter: brightness(1.1); }
.split-wrap { position: relative; padding-top: 22px; }
.split-mark { position: absolute; top: 0; bottom: -4px; width: 0; border-left: 2px dashed var(--ink); opacity: 0.55; pointer-events: none; }
.split-mark span { position: absolute; top: 0; transform: translateX(-50%); font-size: 11.5px; font-weight: 650; color: var(--ink-2); white-space: nowrap; background: var(--surface); padding: 0 4px; }

.settle-card { display: grid; gap: 18px; }
.transfer { display: flex; align-items: center; justify-content: center; gap: 16px; padding: 22px 16px; border-radius: 14px; background: var(--surface-2); flex-wrap: wrap; }
.transfer .party { display: grid; justify-items: center; gap: 6px; font-weight: 650; font-size: 14px; min-width: 80px; }
.transfer .party small { color: var(--muted); font-weight: 500; font-size: 12px; }
.transfer .flow { display: grid; justify-items: center; gap: 2px; flex: 1 1 160px; max-width: 280px; }
.transfer .flow .amt { font-size: 30px; font-weight: 800; letter-spacing: -0.02em; color: var(--ink); }
.transfer .flow .line { width: 100%; height: 2px; background: linear-gradient(90deg, var(--axis), var(--brand)); position: relative; border-radius: 2px; }
.transfer .flow .line::after { content: ""; position: absolute; right: -2px; top: -5px; border: 6px solid transparent; border-left: 9px solid var(--brand); border-right: 0; }
.transfer .flow small { color: var(--muted); font-size: 12px; }
.transfer.square { color: var(--good-text); font-weight: 700; font-size: 16px; gap: 10px; }
/* ---------- Phones ---------- */
@media (max-width: 640px) {
  body { font-size: 14.5px; padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
  main { padding-top: 16px; }
  .view > * + * { margin-top: 14px; }

  /* Compact one-row header: icon buttons only. Backdrop blur would trap the fixed tab bar inside the header. */
  .topbar { backdrop-filter: none; -webkit-backdrop-filter: none; background: var(--surface); }
  .topbar-inner { flex-wrap: nowrap; padding-top: 10px; padding-bottom: 10px; }
  .logo { width: 36px; height: 36px; border-radius: 10px; font-size: 13px; }
  .brand { gap: 10px; }
  .brand-name { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .brand-sub { display: none; }
  .top-actions { flex-wrap: nowrap; gap: 6px; }
  .top-actions .btn { width: 36px; padding: 0; justify-content: center; }
  .top-actions .btn > span, .top-actions summary > span { display: none; }
  .sync { display: none; }
  .bar-row { grid-template-columns: minmax(0, 1fr) auto; gap: 4px 10px; }
  .bar-row .bar-track { grid-column: 1 / -1; grid-row: 2; height: 14px; }

  /* Tabs become a bottom bar, like a phone app. */
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; margin: 0; max-width: none;
    display: grid; grid-template-columns: repeat(5, 1fr); gap: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--surface) 96%, transparent); border-top: 1px solid var(--border);
    box-shadow: 0 -6px 20px -10px rgba(0, 0, 0, 0.2);
  }
  .tabs a { flex-direction: column; justify-content: center; gap: 3px; padding: 6px 2px; font-size: 11px; font-weight: 600; border-radius: 10px; min-width: 0; }
  .tabs a svg { width: 20px; height: 20px; }
  .tabs a .count { display: none; }

  .page-head h1 { font-size: 21px; }
  .page-head .btn { flex: 1 1 auto; justify-content: center; }
  .head-actions { width: 100%; }
  .card { padding: 16px; border-radius: 14px; }
  .hero { padding: 18px; }
  .hero .value { font-size: 34px; }

  /* Two tiles per row, icon on top. */
  .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { grid-template-columns: minmax(0, 1fr); gap: 2px; padding: 14px; }
  .stat .ico { grid-row: auto; width: 32px; height: 32px; border-radius: 9px; margin-bottom: 6px; }
  .stat .ico svg { width: 17px; height: 17px; }
  .stat .label { font-size: 11px; }
  .stat .value { font-size: 20px; }
  .stat .detail { font-size: 11.5px; }
  .stat:hover, .partner-card:hover { transform: none; box-shadow: var(--shadow); }

  /* Filters: chips scroll sideways in one row; search and select take the full width. */
  .toolbar { gap: 8px; }
  .chips { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: none; }
  .search, .select { flex: 1 1 100%; max-width: none; }

  /* Tables: each row becomes a card with "label … value" lines. */
  .table-wrap { overflow: visible; border: 0; background: none; box-shadow: none; border-radius: 0; }
  table, tbody, tfoot, tr, td { display: block; width: 100%; }
  thead { display: none; }
  tbody tr {
    display: flex; flex-direction: column;
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    padding: 4px 14px; margin-bottom: 10px; box-shadow: var(--shadow);
  }
  tbody td.prod { order: -1; }
  tfoot td:first-child { text-align: left; }
  tfoot td:first-child::before { display: none; }
  tbody tr:hover td { background: none; }
  td { border: 0; padding: 7px 0; text-align: right; }
  td::before {
    content: attr(data-label); float: left; margin-right: 12px; color: var(--muted);
    font-size: 11.5px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.04em; line-height: 1.9;
  }
  td::after { content: ""; display: table; clear: both; }
  td:empty, td[data-label=""]::before { display: none; }
  td .small { display: block; }
  td .small.reason { max-width: none; white-space: normal; }
  td.prod, .card td.prod { text-align: left; font-size: 15px; min-width: 0; padding: 10px 0; border-bottom: 1px solid var(--grid); margin-bottom: 2px; }
  td.prod::before { display: none; }
  td.actions { width: 100%; text-align: right; border-top: 1px solid var(--grid); margin-top: 4px; padding-top: 10px; }
  td.num { white-space: normal; }
  .meter { width: 48px; }
  tfoot tr { background: var(--surface-2); border-radius: 14px; padding: 4px 14px; border: 1px solid var(--border); }
  tfoot td { background: none; border: 0; }
  .no-rows { text-align: center; }
  .no-rows::before { display: none; }
  /* Tables inside cards: flatter rows. */
  .card tbody tr { background: var(--surface-2); box-shadow: none; border: 0; }

  /* Partners */
  .transfer { flex-wrap: nowrap; gap: 8px; padding: 16px 10px; }
  .transfer .party { min-width: 0; font-size: 13px; }
  .transfer .avatar.lg { width: 44px; height: 44px; font-size: 16px; }
  .transfer .flow { flex: 1 1 auto; min-width: 0; }
  .transfer .flow .amt { font-size: 22px; }
  .partner-amt .value { font-size: 24px; }
  .split-track .seg { font-size: 12px; padding: 0 8px; }

  /* Pop-ups open as bottom sheets. */
  .modal { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 20px 20px 0 0; max-height: 92vh; }
  .modal-form { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
  .modal-foot .btn { flex: 1 1 auto; justify-content: center; }
  .modal-foot .spacer { display: none; }
  .toast { bottom: calc(90px + env(safe-area-inset-bottom)); }
  .menu-list { position: fixed; left: 12px; right: 12px; top: 60px; min-width: 0; }
  .foot { padding-bottom: 16px; }
}

.empty { display: grid; justify-items: center; gap: 8px; padding: 28px 16px; text-align: center; color: var(--muted); font-size: 13.5px; border: 1px dashed var(--axis); border-radius: 14px; }
