:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fc;
  --good: #006300;
  --bad: #c02f2f;
  --grid: #e8e7e2;
  --series-1: #2a78d6;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --radius: 10px;
  --nav-w: 220px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #2f2f2c;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #3987e5;
    --accent-soft: #1d2b3d;
    --good: #3fbf3f;
    --bad: #f07070;
    --grid: #2a2a27;
    --series-1: #3987e5;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; }
a { color: var(--accent); }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.pos { color: var(--good); }
.neg { color: var(--bad); }
.hidden { display: none !important; }
.show-sm { display: none; }
@media (max-width: 860px) { .show-sm { display: initial; } div.show-sm { display: block; } }

/* ---------- layout */
.shell { display: flex; min-height: 100vh; }
.nav {
  width: var(--nav-w); flex: none; background: var(--surface); border-right: 1px solid var(--border);
  padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 2px;
}
.brand { font-weight: 700; font-size: 16px; padding: 4px 10px 16px; display: flex; align-items: center; gap: 8px; }
.brand img { width: 24px; height: 24px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
  color: var(--text-2); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .spacer { flex: 1; }
.nav svg, .tabbar svg { width: 18px; height: 18px; flex: none; }
.main { flex: 1; min-width: 0; padding: 24px 28px 60px; max-width: 1400px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .grow { flex: 1; }

.tabbar { display: none; }
.fab { display: none; }

@media (max-width: 860px) {
  .nav { display: none; }
  .main { padding: 16px 14px calc(90px + env(safe-area-inset-bottom)); }
  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px;
    color: var(--text-3); text-decoration: none; padding: 4px 0;
  }
  .tabbar a.active { color: var(--accent); }
  .tabbar svg { width: 22px; height: 22px; }
  .fab {
    display: flex; position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 21;
    width: 54px; height: 54px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
    font-size: 28px; align-items: center; justify-content: center; box-shadow: 0 4px 14px rgba(0,0,0,.25);
  }
  .page-head .btn.primary.add-card { display: none; }
  h1 { font-size: 20px; }
}

/* ---------- controls */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.link { border: 0; background: none; color: var(--accent); padding: 4px 6px; }
.btn:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent;
}
textarea { resize: vertical; min-height: 70px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); font-weight: 500; }
label.field.wide { grid-column: 1 / -1; }
.req::after { content: " *"; color: var(--bad); }

.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.filters > * { width: auto; }
.filters input[type=search] { flex: 1 1 220px; }
.filters select { flex: 0 1 170px; }
@media (max-width: 860px) {
  .filters select { flex: 1 1 140px; }
}

/* ---------- panels, tiles */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.panel + .panel, .section { margin-top: 16px; }
.grid-2 > .panel { margin-top: 0; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .label { font-size: 12px; color: var(--text-2); font-weight: 500; }
.tile .value { font-size: 22px; font-weight: 650; margin-top: 4px; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.tile .sub { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1000px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- tables */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--text-2); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
th.num { text-align: right; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--surface-2); }
tfoot td { font-weight: 650; background: var(--surface-2); border-top: 1px solid var(--border); border-bottom: 0; }
td .title { font-weight: 550; }
td .sub { color: var(--text-3); font-size: 12px; }
.pill { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; border: 1px solid var(--border); }
.summary-bar { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; color: var(--text-2); margin: -4px 0 12px; }
.summary-bar b { color: var(--text); font-variant-numeric: tabular-nums; }
.more { text-align: center; padding: 12px; }

/* Phone: tables with class "stack" become cards */
@media (max-width: 860px) {
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack tr { padding: 10px 12px; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; }
  table.stack td { border: 0; padding: 0; text-align: left; }
  table.stack td.primary { grid-row: 1; grid-column: 1; }
  table.stack td.right { grid-row: 1; grid-column: 2; text-align: right; }
  table.stack td.desc { grid-row: 2; grid-column: 1 / -1; }
  table.stack td.meta { grid-row: 3; grid-column: 1; font-size: 12px; color: var(--text-3); }
  table.stack td.action { grid-row: 3; grid-column: 2; text-align: right; }
  table.stack td.hide-sm { display: none; }
  table.stack tfoot tr { display: grid; }
  table.stack tfoot td { background: none; }
}

/* ---------- modal */
.modal-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; align-items: flex-start;
  justify-content: center; padding: 40px 16px; overflow-y: auto;
}
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 720px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
  border: 1px solid var(--border);
}
.modal.narrow { max-width: 440px; }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; flex: 1; font-size: 17px; }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); align-items: center; }
.modal-foot .grow { flex: 1; }
.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.form-grid.two { grid-template-columns: repeat(2, 1fr); }
.form-section + .form-section { margin-top: 18px; padding-top: 16px; border-top: 1px dashed var(--border); }
.calc { margin-top: 12px; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; }
.calc b { font-variant-numeric: tabular-nums; }
.error-msg { color: var(--bad); font-size: 13px; }
@media (max-width: 860px) {
  .modal-back { padding: 0; align-items: stretch; }
  .modal { border-radius: 0; min-height: 100%; max-width: none; border: 0; }
  .modal-foot { position: sticky; bottom: 0; background: var(--surface); padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .form-grid, .form-grid.two { grid-template-columns: 1fr 1fr; }
  input, select, textarea { font-size: 16px; } /* stops iOS from zooming in */
}

/* ---------- inline add row */
.add-row { display: grid; grid-template-columns: 150px 1fr 170px 130px auto; gap: 8px; align-items: end; }
.add-row.partner { grid-template-columns: 170px 150px 130px 130px 1fr auto; }
@media (max-width: 860px) {
  .add-row, .add-row.partner { grid-template-columns: 1fr 1fr; }
  .add-row > .wide-sm { grid-column: 1 / -1; }
}

/* ---------- charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .zero { stroke: var(--text-3); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; }
.chart .bar { fill: var(--series-1); }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover + .bar, .chart .bar.on { filter: brightness(1.12); }
.chart .hover-band { fill: var(--surface-2); }
.tooltip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--surface); padding: 6px 9px;
  border-radius: 6px; font-size: 12px; white-space: nowrap; transform: translate(-50%, -100%); margin-top: -8px; z-index: 5;
}
.tooltip b { font-variant-numeric: tabular-nums; }

/* ---------- show cards */
.show-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.show-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; cursor: pointer; box-shadow: var(--shadow); text-decoration: none; color: inherit; display: block; }
.show-card:hover { border-color: var(--accent); }
.show-card .name { font-weight: 650; font-size: 15px; }
.show-card .dates { color: var(--text-3); font-size: 12px; margin-bottom: 10px; }
.show-card dl { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin: 0; font-size: 13px; }
.show-card dt { color: var(--text-2); }
.show-card dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.show-card dd.big { font-weight: 650; }

/* ---------- partner */
.partner-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }

/* ---------- login */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login { width: 100%; max-width: 360px; }
.login .brand { justify-content: center; font-size: 20px; padding-bottom: 20px; }
.login form { display: flex; flex-direction: column; gap: 12px; }

/* ---------- toast */
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--text);
  color: var(--surface); padding: 10px 16px; border-radius: 8px; opacity: 0; transition: .2s; z-index: 100;
  pointer-events: none; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--bad); color: #fff; }
@media (max-width: 860px) { #toast { bottom: calc(84px + env(safe-area-inset-bottom)); } }

.empty { padding: 30px; text-align: center; color: var(--text-3); }
.note { font-size: 12px; color: var(--text-3); margin-top: 8px; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.settings-grid textarea { min-height: 220px; }
input[type=checkbox], input[type=radio] { width: auto; padding: 0; margin: 0 6px 0 0; vertical-align: -2px; }
label.check { display: inline-flex; align-items: center; font-size: 13px; font-weight: 500; margin-bottom: 10px; cursor: pointer; }
td.desc { font-weight: 500; }
@media (min-width: 861px) { td.desc { min-width: 170px; } th, td { padding: 9px 10px; } }
@media (min-width: 861px) { td.primary .title { white-space: nowrap; } }
.via::before { content: " · "; }
@media (min-width: 861px) { td.meta { white-space: nowrap; } .via { display: block; font-size: 12px; color: var(--text-3); } .via::before { content: none; } }
