:root {
  --bg: #0d0f12;
  --panel: #161a20;
  --border: #262c36;
  --fg: #e8ecf1;
  --dim: #8a93a3;
  --accent: #4fc3f7;
  --thr: #4caf50;
  --brk: #ef5350;
  --str: #ffb74d;
}
* { box-sizing: border-box; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 "Segoe UI", system-ui, sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
header {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
header .right { margin-left: auto; display: flex; gap: 12px; align-items: center; }
header select, header button {
  background: #0a0c0f; color: var(--fg); border: 1px solid var(--border);
  border-radius: 6px; padding: 3px 8px; font: inherit;
}
.ctl { color: var(--dim); font-size: 12px; display: flex; gap: 6px; align-items: center; }
#clock { font-variant-numeric: tabular-nums; }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot.on { background: var(--thr); }
.dot.off { background: var(--brk); }
.grid-stack { flex: 1; background: var(--bg); overflow-y: auto; }
/* gridstack's injected stylesheet sets overflow:hidden on the content box.
   Widgets never scroll vertically — tiles auto-fit content and the grid page
   scrolls instead. clip (not hidden): clip creates NO scroll container, so
   sticky class headers can anchor to the .grid-stack scroller above. */
.grid-stack-item-content.widget { overflow: clip !important; }
/* horizontal-only scroll for wide tables; overflow-y:clip keeps it from
   becoming a vertical scroll container (auto would hijack sticky anchoring) */
.tscroll { overflow-x: auto; overflow-y: clip; overscroll-behavior-x: contain; }
.tscroll { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
.tscroll::-webkit-scrollbar { width: 6px; height: 6px; }
.tscroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.widget {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  /* NOT height:100% — the element is absolutely positioned and gridstack
     insets it by the margins (top/bottom:12); an explicit height overrides
     bottom and the widget overflows its tile, killing the vertical gap */
  height: auto;
  overflow: auto;
}
.widget h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--dim);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.gs-drag { cursor: grab; font-size: 13px; opacity: .6; user-select: none; touch-action: none; }
.gs-drag:active { cursor: grabbing; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
td, th { text-align: left; padding: 3px 6px; white-space: nowrap; }
th { color: var(--dim); font-weight: 600; font-size: 11px; }
tr.me td { background: rgba(255, 183, 77, .16); font-weight: 600; }
tr.me td:first-child { box-shadow: inset 3px 0 0 var(--str); }
tr.class-b td:first-child { border-left: 3px solid var(--str); }
.speed { font-size: clamp(30px, 6vw, 52px); font-weight: 700; font-variant-numeric: tabular-nums; }
.speed small, .big small { font-size: 14px; color: var(--dim); margin-left: 4px; }
.delta { font-size: 18px; margin-left: 14px; font-variant-numeric: tabular-nums; }
.delta.fast { color: var(--thr); }
.delta.slow { color: var(--brk); }
.big { font-size: clamp(24px, 5vw, 36px); font-weight: 700; font-variant-numeric: tabular-nums; }
.big.sm { font-size: clamp(20px, 3vw, 28px); line-height: 1.3; }
.row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.row.small { font-size: 12px; color: var(--dim); justify-content: space-between; }
.row.small.wrap { flex-wrap: wrap; justify-content: flex-start; gap: 14px; }
.row.small b { color: var(--fg); }
#gear { font-size: 28px; font-weight: 700; min-width: 30px; text-align: center; }
.rpm { flex: 1; height: 12px; background: #0a0c0f; border-radius: 6px; overflow: hidden; }
#rpmbar { height: 100%; width: 0; background: linear-gradient(90deg, var(--thr), var(--str), var(--brk)); }
.prog { position: relative; height: 8px; background: #0a0c0f; border-radius: 4px; margin: 8px 0 2px; overflow: hidden; }
.prog .pfill { height: 100%; width: 0; background: var(--accent); transition: width .4s linear; }
.prog .pfill.ok { background: var(--thr); }
.prog .pfill.warn { background: var(--str); }
.prog .pfill.bad { background: var(--brk); }
.prog .tick { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--str); }
.ok { color: var(--thr); }
.warn { color: var(--str); }
.bar { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.bar label { width: 30px; font-size: 11px; color: var(--dim); }
.bar .track { flex: 1; height: 14px; background: #0a0c0f; border-radius: 7px; overflow: hidden; position: relative; }
.bar .fill { height: 100%; width: 0; }
.fill.t { background: var(--thr); }
.fill.b { background: var(--brk); }
.fill.s { background: var(--str); width: 4px; position: absolute; left: 50%; }
.tires { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tire { background: #0a0c0f; border-radius: 8px; padding: 8px; display: flex; gap: 8px; align-items: baseline; }
.tire b { color: var(--dim); font-size: 11px; }
.tire span { font-variant-numeric: tabular-nums; font-size: 12px; }
#map { width: 100%; }
.note { color: var(--dim); font-size: 12px; margin-top: 6px; }

/* update banner in the header */
#upd-banner {
  display: inline-flex; gap: 8px; align-items: center;
  background: #123a52; color: var(--accent);
  border: 1px solid #1d5a7a; border-radius: 6px;
  padding: 2px 8px; font-size: 12px;
}
#upd-banner.hidden { display: none; }
#upd-banner button {
  background: var(--accent); color: #04121c; border: 0; border-radius: 4px;
  padding: 1px 8px; font-size: 11px; font-weight: 700; cursor: pointer;
}
#upd-banner #upd-x { background: none; color: var(--dim); font-weight: 400; padding: 0 2px; }
#upd-x:hover { color: var(--fg); }

/* small screens: denser type, tighter widgets */
@media (max-width: 760px) {
  body { font-size: 12px; }
  header { padding: 6px 10px; gap: 8px; }
  header b { font-size: 13px; }
  header select, header button { padding: 2px 8px; font-size: 12px; white-space: nowrap; }
  #track { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 38vw; }
  #toasts { max-width: 90vw; }
  .widget { padding: 8px 10px; border-radius: 8px; }
  td, th { padding: 2px 4px; }
  .speed { font-size: clamp(26px, 9vw, 40px); }
  .row.small { font-size: 11px; }
  .bar { margin: 6px 0; }
  .bar .track { height: 12px; }
  #gear { font-size: 22px; }
}
@media (max-width: 460px) {
  .ctl { font-size: 0; gap: 0; }          /* hide the "rate" text label */
  .ctl select { font-size: 12px; }
  #track { max-width: 30vw; }
  #reset-layout { display: none; }        /* layout is locked on touch anyway */
}
@container (max-width: 640px) {
  table.standings.big { font-size: 12px; }
  table.standings.big td, table.standings.big th { padding: 2px 4px; }
}

/* flag banner in header */
.flag {
  padding: 2px 10px; border-radius: 6px; font-weight: 700; font-size: 12px;
  letter-spacing: .06em;
}

/* notification toasts */
#toasts {
  position: fixed; top: 52px; right: 14px; z-index: 100;
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
.toast {
  background: var(--panel); border: 1px solid var(--border);
  border-left: 4px solid var(--accent); border-radius: 8px;
  padding: 8px 14px; font-size: 13px; opacity: 0; transform: translateX(20px);
  transition: all .3s ease;
}
.toast.show { opacity: 1; transform: none; }
.toast.warn { border-left-color: var(--str); }
.toast.bad { border-left-color: var(--brk); }

/* gap trend arrows */
.dn { color: var(--thr); }
.up { color: var(--brk); }

/* positions gained/lost since start */
.pgain { color: var(--thr); }
.ploss { color: var(--brk); }
.pgain, .ploss { font-size: .9em; }

canvas { display: block; }

/* widgets menu */
#widgets-menu {
  position: absolute; top: 42px; right: 14px; z-index: 200;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; min-width: 180px;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
#widgets-menu.hidden { display: none; }
#widgets-menu label { display: flex; gap: 8px; align-items: center; padding: 4px 0; font-size: 13px; cursor: pointer; }
#widgets-menu .fit { border-bottom: 1px solid var(--border); padding-bottom: 8px; margin-bottom: 8px; font-weight: 600; }
#widgets-menu button {
  width: 100%; margin-bottom: 8px; background: #0a0c0f; color: var(--fg);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; cursor: pointer;
}
#widget-toggles { max-height: 40vh; overflow: auto; }

/* report problem dialog */
#report-dlg {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
}
#report-dlg.hidden { display: none; }
.dlg-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 16px; width: min(420px, 92vw);
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.dlg-card h3 { font-size: 13px; margin-bottom: 10px; }
.dlg-card textarea {
  width: 100%; resize: vertical; background: #0a0c0f; color: var(--fg);
  border: 1px solid var(--border); border-radius: 6px; padding: 8px; font: inherit;
}
.dlg-row { display: flex; gap: 10px; align-items: center; margin-top: 10px; }
.dlg-row button, .dlg-row a {
  background: #0a0c0f; color: var(--fg); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 12px; font: inherit; cursor: pointer;
  text-decoration: none;
}
.dlg-row button:hover, .dlg-row a:hover { border-color: var(--accent); }
#report-status { margin-top: 10px; min-height: 16px; }

/* --- big standings --- */
table.standings.big td, table.standings.big th { padding: 4px 7px; }
table.standings.big td.n, table.standings.big th.n { text-align: right; }
td.fbest, tr.me td.fbest { color: #ce93d8; font-weight: 700; }
table.standings.big td.nm { max-width: 16em; overflow: hidden; text-overflow: ellipsis; }
tr.clshdr td {
  color: var(--dim); font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .08em;
  padding-top: 7px; border-top: 1px solid var(--border);
}
/* class headers pin to the top of the .grid-stack page scroller while their
   rows scroll past; .cl-l additionally pins left inside .tscroll so the class
   label survives horizontal scroll (the full-width filler can't pin — a cell
   as wide as its row has zero sticky displacement room) */
table.standings.big tr.clshdr td {
  /* above everything while pinned at top — data rows' pinned cells slide
     under it during vertical scroll */
  position: sticky; top: 0; z-index: 5;
  background: var(--panel);
}
table.standings.big td.cl-l { left: 0; z-index: 6; }
/* sticky table cells lose stacking vs later rows — give data cells
   position+z0 so the pinned header actually covers them */
table.standings.big td { position: relative; z-index: 0; }
table.standings.big td.pin { z-index: 2; }
.irpill {
  display: inline-block; min-width: 2.6em; text-align: center;
  border-radius: 4px; padding: 0 4px; color: #fff; font-weight: 700;
  text-shadow: 0 1px 2px rgba(0,0,0,.6);
}
.st {
  display: inline-block; min-width: 2.2em; text-align: center;
  border-radius: 4px; padding: 0 4px; font-size: 10px; font-weight: 700;
}
.bpill {
  display: inline-block; min-width: 2.2em; text-align: center;
  border-radius: 4px; padding: 0 4px; font-size: 10px; font-weight: 700;
  letter-spacing: .4px;
}
.b-unk { background: #232a33; color: var(--dim); }
.st-warn { background: #7a5b00; color: #ffd54f; }
.st-bad { background: #7a1f1f; color: #ef9a9a; }
.st-info { background: #123a52; color: #4fc3f7; }
.st-ok { background: #1b4d2e; color: #81c784; }
.st-dim { color: var(--dim); }
.st-wet { background: #123a52; color: #4fc3f7; }
.st-dry { background: #232a33; color: var(--dim); }
.seg { margin-left: auto; display: inline-flex; gap: 2px; }
.seg button {
  background: #0a0c0f; color: var(--dim); border: 1px solid var(--border);
  padding: 1px 8px; font-size: 10px; cursor: pointer; text-transform: none;
  letter-spacing: 0;
}
.seg button:first-child { border-radius: 5px 0 0 5px; }
.seg button:last-child { border-radius: 0 5px 5px 0; }
.seg button.on { background: var(--accent); color: #04121c; font-weight: 700; }
.fc { font-size: 10px; color: var(--dim); }
.fc.warn { color: var(--str); }
.fc.bad, .bad { color: var(--brk); }
.dim { color: var(--dim); font-weight: 400; text-transform: none; letter-spacing: 0; }
.tire .fc { margin-left: auto; }
#wxhist { width: 100%; }

/* keep the big standings table readable but within its widget */
table.standings.big { font-size: 13px; }
table.standings.big td, table.standings.big th { padding: 3px 5px; }
table.standings.big td.nm { max-width: 11em; }
/* pinned position + driver columns: the table scrolls horizontally inside
   .tscroll; pins anchor at left:0/2.1em (NOT negative — an inset that pulls
   past the scrollport edge makes the column visibly slide before sticking).
   Opaque bg + edge shadow on the driver column (rightmost pin). */
table.standings.big td.pin, table.standings.big th.pin {
  position: sticky; z-index: 2;
  background: var(--panel);
}
table.standings.big td.p0, table.standings.big th.p0 {
  left: 0;
  width: 2.1em; min-width: 2.1em; max-width: 2.1em;
  font-weight: 700;
}
table.standings.big td.nm, table.standings.big th.drv {
  left: 2.1em;
  box-shadow: 7px 0 9px -7px rgba(0, 0, 0, .8);
}
table.standings.big th.pin { z-index: 3; }
/* player row: keep the amber strip readable on the pinned cell (same tint
   over opaque panel) plus the accent bar */
tr.me td.pin {
  background:
    linear-gradient(rgba(255, 183, 77, .16), rgba(255, 183, 77, .16))
    var(--panel);
}
tr.me td.p0 { box-shadow: inset 3px 0 0 var(--str); }
tr.me td.nm { box-shadow: 7px 0 9px -7px rgba(0, 0, 0, .8); }
/* keep the widget title (and its buttons) put during horizontal scroll,
   and stop a table swipe from chaining to the page */
.widget h2 { position: sticky; left: 0; }


/* tile-adaptive columns: shrink a widget and it sheds low-priority columns
   instead of clipping (the simple standings widget; the big one scrolls).
   cq1 = first to go, cq2 = second. Container queries degrade gracefully
   (old OBS CEF just keeps the horizontal scroll). */
.widget { container-type: inline-size; }
@container (max-width: 640px) { .cq1 { display: none; } }
@container (max-width: 460px) { .cq2 { display: none; } }
