/*
 * 📊 Точность — Telegram Mini App.
 *
 * The visual language is carried over from the printed report the user approved: the same
 * ink-on-paper density, uppercase micro-labels, hairline table rules and pill states. Two
 * things had to change for a phone, and only two:
 *   1. the 11-column match table becomes a card list below 720px (same markup, CSS only);
 *   2. a dark scheme exists, chosen by Telegram's colorScheme rather than by inheriting the
 *      user's theme colours — see the palette note below.
 *
 * ⚠️ PALETTE. These values were checked with the dataviz validator against THESE surfaces
 * (#ffffff / #17212b), not chosen by eye:
 *   - categorical model↔line: #2a78d6/#eb6834 light, #3987e5/#d95926 dark — pass CVD
 *     separation, the normal-vision floor and 3:1 contrast in both modes.
 *   - hit/miss are STATUS colours. A red/green pair fails deuteranopia by construction, so it
 *     is never the only carrier: every pill prints ✓/✗ and a word, and every bar is
 *     direct-labelled. Do not "simplify" a pill down to a colour swatch.
 * Contrast on text ink: 17.8 light / 13.8 dark; muted 4.8 / 6.3; hit 6.2 / 9.4; miss 7.3 / 5.9.
 *
 * ⚠️ We deliberately do NOT paint from --tg-theme-* custom properties. Those are arbitrary
 * per-user values (custom Telegram themes exist) and would silently invalidate every contrast
 * result above. app.js reads only the light/dark CHOICE and stamps data-theme.
 */

:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --ink: #16181d;
  --ink-2: #3d434c;
  --muted: #6b7280;
  --faint: #9aa1ab;
  --rule: #eef0f3;
  --rule-2: #dfe3e8;
  --zebra: #fafbfc;

  --hit: #12703f;
  --hit-bg: #dff3e6;
  --miss: #a4262c;
  --miss-bg: #fbe4e5;
  --none-bg: #eef0f3;

  --model: #2a78d6;
  --line: #eb6834;
  --band: rgba(42, 120, 214, .13);
  --ref: #c9ced6;

  --radius: 10px;
  --tap: 44px; /* the minimum comfortable touch target */
}

:root[data-theme="dark"] {
  --bg: #17212b;
  --panel: #1d2a38;
  --ink: #e8edf2;
  --ink-2: #c2ccd6;
  --muted: #93a3b5;
  --faint: #6d8098;
  --rule: #24344a;
  --rule-2: #2f4258;
  --zebra: #1b2836;

  --hit: #4ade80;
  --hit-bg: rgba(74, 222, 128, .14);
  --miss: #f87171;
  --miss-bg: rgba(248, 113, 113, .15);
  --none-bg: rgba(147, 163, 181, .14);

  --model: #3987e5;
  --line: #d95926;
  --band: rgba(57, 135, 229, .18);
  --ref: #3d5169;
}

* { box-sizing: border-box; }

/*
 * ⚠️ The page must never scroll sideways; wide content scrolls inside its OWN box (.scroll).
 * `overflow-x: clip` on the root is the backstop, but the causes are fixed individually below
 * (flex items get min-width:0, long strings get overflow-wrap) — a clip alone would hide a
 * cut-off column rather than reveal it. Measured symptom before this existed: the league
 * <select>'s widest option ("The Boris Invitational: Streamer Battles 2") is ~430px, a flex
 * item defaults to min-width:auto and so refuses to shrink below it, which stretched the whole
 * body and clipped every section at the viewport edge.
 */
html, body { max-width: 100%; overflow-x: clip; }

body {
  margin: 0;
  padding: 0 14px calc(28px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Roboto, Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-text-size-adjust: 100%;
}

/* --- header / footer ---------------------------------------------------- */

.hd { padding: 14px 0 10px; border-bottom: 2px solid var(--ink); }
.hd h1 { margin: 0; font-size: 19px; font-weight: 650; letter-spacing: -.02em; }
.hd-sub { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.ft {
  margin-top: 26px; padding-top: 10px; border-top: 1px solid var(--rule-2);
  color: var(--faint); font-size: 11px;
}

/* --- filters ------------------------------------------------------------ */

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 12px 0 8px; }
.filters > * { min-width: 0; } /* see the overflow note at the top — without this the select wins */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; min-height: 32px; padding: 0 12px;
  border: 1px solid var(--rule-2); border-radius: 999px;
  color: var(--ink-2); text-decoration: none; font-size: 13px; font-weight: 500;
  background: var(--panel);
}
.chip.on { background: var(--ink); border-color: var(--ink); color: var(--bg); font-weight: 620; }
.sel {
  min-height: 32px; max-width: 100%; flex: 1 1 12rem; text-overflow: ellipsis; padding: 0 8px; font: inherit; font-size: 13px;
  color: var(--ink); background: var(--panel);
  border: 1px solid var(--rule-2); border-radius: 8px;
}

.meta { margin: 6px 0 14px; color: var(--muted); font-size: 11px; line-height: 1.45; overflow-wrap: anywhere; }
.cover { margin: -6px 0 14px; color: var(--muted); font-size: 12px; }

/* --- sections ----------------------------------------------------------- */

section { margin: 22px 0; }
h2 {
  margin: 0 0 8px; padding-bottom: 5px; font-size: 13px; font-weight: 650;
  letter-spacing: .01em; border-bottom: 1.5px solid var(--ink);
}
h3 { margin: 16px 0 6px; font-size: 12.5px; font-weight: 620; }
.sub { margin: 0 0 10px; color: var(--muted); font-size: 12px; }
.note {
  margin: 10px 0; padding: 2px 0 2px 10px; border-left: 2.5px solid var(--rule-2);
  color: var(--ink-2); font-size: 12px;
}
.nodata { color: var(--faint); font-style: italic; font-size: 12px; }

/* --- tiles -------------------------------------------------------------- */

.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 7px; margin: 12px 0; }
.tile { border: 1px solid var(--rule-2); border-radius: var(--radius); padding: 8px 10px; background: var(--panel); }
.tile .k {
  font-size: 9px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
}
.tile .v { margin-top: 2px; font-size: 19px; font-weight: 660; letter-spacing: -.02em; }
.tile .v small { font-size: 10px; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.tile.good .v { color: var(--hit); }
.tile.bad .v { color: var(--miss); }

/* --- tables ------------------------------------------------------------- */

/*
 * A table wider than the phone scrolls inside its own box. The gradients are the AFFORDANCE:
 * without them a hidden last column (the P/L table's z score, measured) simply looks absent —
 * the reader has no reason to swipe. background-attachment: local pins the white "caps" to the
 * content so the shadow only shows on the side that still has content off-screen.
 */
.scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background:
    linear-gradient(to right, var(--bg) 30%, transparent) left center / 24px 100% no-repeat local,
    linear-gradient(to left, var(--bg) 30%, transparent) right center / 24px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, .13), transparent) left center / 12px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, .13), transparent) right center / 12px 100% no-repeat scroll;
}
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
th {
  padding: 0 6px 4px; text-align: left; font-size: 9px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  border-bottom: 1px solid var(--rule-2); white-space: nowrap;
}
td { padding: 5px 6px; border-bottom: 1px solid var(--rule); }
.mc-2, .kv b, .kv span { overflow-wrap: anywhere; }
th.n, td.n { text-align: right; }
tbody tr:nth-child(even) { background: var(--zebra); }
td.st, .st { font-weight: 640; }
.good { color: var(--hit); font-weight: 600; }
.bad { color: var(--miss); font-weight: 600; }
.dim { color: var(--muted); }
.thin { font-weight: 500; }
.vs { color: var(--faint); font-size: .85em; }

.pill {
  display: inline-block; padding: 1px 7px; border-radius: 999px;
  font-size: 10px; font-weight: 640; letter-spacing: .02em; white-space: nowrap;
}
.pill.hit { background: var(--hit-bg); color: var(--hit); }
.pill.miss { background: var(--miss-bg); color: var(--miss); }
.pill.none { background: var(--none-bg); color: var(--muted); }

/* --- match rows: table on wide, cards on narrow ------------------------- */

.only-narrow { display: none; }
@media (max-width: 719px) {
  .only-wide { display: none; }
  .only-narrow { display: block; }
  /* Let headers wrap on a phone: nowrap is what pushed the six-column P/L table past the
     viewport and hid its z column entirely. Two short lines beat an invisible column. */
  th { white-space: normal; }
  table { font-size: 12px; }
  td, th { padding-left: 4px; padding-right: 4px; }
}

.cards { margin: 0; padding: 0; list-style: none; }
.mcard {
  padding: 9px 10px; margin-bottom: 6px; background: var(--panel);
  border: 1px solid var(--rule-2); border-left-width: 3px; border-radius: var(--radius);
  cursor: pointer;
}
.mcard.hit { border-left-color: var(--hit); }
.mcard.miss { border-left-color: var(--miss); }
.mcard.none { border-left-color: var(--rule-2); }
.mc-1 { display: flex; align-items: center; gap: 8px; }
.mc-time { font-weight: 640; }
.mc-map { color: var(--muted); font-size: 11.5px; }
.mc-1 .pill { margin-left: auto; }
.mc-2 { margin-top: 3px; font-size: 13.5px; }
.mc-3, .mc-4 { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 3px; font-size: 11.5px; }

.matches tbody tr { cursor: pointer; }
.matches tbody tr:focus-visible, .mcard:focus-visible {
  outline: 2px solid var(--model); outline-offset: 1px;
}

/* --- key/value grids ---------------------------------------------------- */

.kv { display: grid; gap: 1px; background: var(--rule); border-radius: var(--radius); overflow: hidden; }
.kv > div {
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline;
  padding: 7px 10px; background: var(--panel); font-size: 12.5px;
}
.kv span { color: var(--muted); }
.kv b { font-weight: 620; text-align: right; }
.mk { font-style: normal; }
.mk.hit { color: var(--hit); }
.mk.miss { color: var(--miss); }

.chips.flat { gap: 6px; }
.stat {
  display: inline-flex; gap: 5px; align-items: baseline; padding: 4px 9px;
  background: var(--panel); border: 1px solid var(--rule-2); border-radius: 999px; font-size: 12px;
}
.stat b { font-weight: 650; }

/* --- charts ------------------------------------------------------------- */
/*
 * Colour is applied ONLY here, by role class. chart.go emits geometry and class names and
 * knows nothing about the theme, so light/dark is this block and nothing else.
 */

.chart { margin: 10px 0 12px; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }

.c-axis { stroke: var(--rule-2); stroke-width: 1; }
.c-ref { stroke: var(--ref); stroke-width: 1; stroke-dasharray: 3 3; }
.c-diag { stroke-dasharray: 4 3; }
.c-connector { stroke: var(--rule-2); stroke-width: 2; }
.c-ci { stroke: var(--model); stroke-width: 3; stroke-linecap: round; opacity: .55; }

.c-hit { fill: var(--hit); }
.c-miss { fill: var(--miss); }
.c-band { fill: var(--band); stroke: none; }

/* A path in these roles is a line; a circle is a marker. Split so one class can do both. */
path.c-model, path.c-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
path.c-model { stroke: var(--model); }
path.c-line { stroke: var(--line); }
path.c-expected { stroke: var(--line); stroke-dasharray: 4 3; }
circle.c-model { fill: var(--model); }
circle.c-line { fill: var(--line); }
/* The 2px surface ring that keeps overlapping markers legible. */
circle.c-dot { stroke: var(--bg); stroke-width: 2; }

text { font-size: 9px; fill: var(--muted); }
text.t-val { font-size: 10px; font-weight: 640; fill: var(--ink); }
text.t-model { fill: var(--model); }
text.t-line { fill: var(--line); }
text.t-dim { fill: var(--faint); }

/* --- disclosure & how-to-read ------------------------------------------ */

.more { margin: 22px 0; border: 1px solid var(--rule-2); border-radius: var(--radius); padding: 10px 12px; }
.more > summary {
  cursor: pointer; font-size: 13px; font-weight: 620; min-height: 24px;
  list-style: revert;
}
.howto ol { margin: 8px 0 0; padding-left: 18px; }
.howto li { margin-bottom: 7px; font-size: 12.5px; color: var(--ink-2); }
.howto b { color: var(--ink); }

/* --- drawer ------------------------------------------------------------- */

.drawer { position: fixed; inset: 0; z-index: 20; }
.drawer[hidden] { display: none; }
.drawer-scrim { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); }
.drawer-body {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 88vh; overflow-y: auto;
  padding: 14px 16px calc(20px + env(safe-area-inset-bottom));
  background: var(--bg); border-radius: 16px 16px 0 0;
  box-shadow: 0 -6px 26px rgba(0, 0, 0, .28);
}
.drawer-close {
  position: absolute; top: 8px; right: 8px; width: var(--tap); height: var(--tap);
  border: 0; background: transparent; color: var(--muted); font-size: 17px; cursor: pointer;
}
.dr-title { margin: 0 34px 2px 0; font-size: 16px; font-weight: 650; border: 0; padding: 0; }
.pm { font-size: 13px; color: var(--ink-2); }

/* --- loading & error ---------------------------------------------------- */

.skeleton { padding: 8px 0; }
.sk { border-radius: var(--radius); background: var(--rule); animation: pulse 1.4s ease-in-out infinite; }
.sk-tiles { height: 116px; }
.sk-line { height: 14px; margin: 12px 0; width: 65%; }
.sk-block { height: 200px; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .5; } }

.err { padding: 14px; border: 1px solid var(--rule-2); border-radius: var(--radius); }
.err h2 { border: 0; padding: 0; }
.err button {
  min-height: var(--tap); padding: 0 16px; margin-top: 8px; font: inherit; font-weight: 600;
  color: var(--bg); background: var(--ink); border: 0; border-radius: 8px; cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  .sk { animation: none; }
}
