:root {
  /* Makes native widgets (buttons, selects, scrollbars) follow the OS theme instead of
     staying light. Without it a UA-styled control renders light while inheriting our
     near-white dark-mode text — light button, white text, unreadable. */
  color-scheme: light dark;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --border: #dce1e7;
  --text: #1a2129;
  --muted: #667079;
  --accent: #0a66cf;
  --accent-soft: #e5f0fd;
  --good: #1a7f37;
  --warn: #b3541e;
  --bad: #b42318;
  --gold-bg: #f6e3a4;   --gold-fg: #705200;
  --silver-bg: #e4e8ed; --silver-fg: #55606b;
  --bronze-bg: #f0d3ba; --bronze-fg: #82471c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #161c23;
    --surface-2: #1e262f;
    --border: #2a333d;
    --text: #e6eaef;
    --muted: #93a1ad;
    --accent: #4d9bff;
    --accent-soft: #16283f;
    --good: #4ade80;
    --warn: #f59e42;
    --bad: #f87171;
    --gold-bg: #453811;   --gold-fg: #f2d478;
    --silver-bg: #313b45; --silver-fg: #c6d0da;
    --bronze-bg: #462d15; --bronze-fg: #e8a76c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}
/* Every clickable thing in this SPA is a <button> and every one of them brings its own
   surface (.card, .hit, .seg, .linkbtn, .mainnav …). Killing the UA chrome globally means a
   button can never fall back to a light ButtonFace box with our near-white dark-mode text. */
button {
  font: inherit; color: inherit; cursor: pointer;
  appearance: none; background: none; border: 0; padding: 0;
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { border: 0; background: none; font-weight: 700; font-size: 16px; padding: 4px 6px; border-radius: 8px; }
.brand:hover { background: var(--surface-2); }
/* Underline tabs, flush with the bar's bottom border. Deliberately not the pill-shaped
   .seg used inside pages — top-level navigation should read differently from an in-page
   toggle. The negative margins cancel .topbar's padding so the tabs span its full height. */
.mainnav { display: flex; gap: 2px; align-self: stretch; margin: -10px 0; }
.mainnav button {
  border: 0; border-bottom: 2px solid transparent; background: none;
  color: var(--muted); font-weight: 600; font-size: 14px;
  padding: 0 12px; margin-bottom: -1px; white-space: nowrap;
  transition: color .12s, border-color .12s;
}
.mainnav button:hover { color: var(--text); }
.mainnav button.active { color: var(--accent); border-bottom-color: var(--accent); }
/* flex + min-width:0 är vad som faktiskt får ellipsen att slå till: en flex-item vägrar
   krympa under sitt innehåll utan det, så långa simmarnamn sköt ut ur vyn i stället. */
.crumbs {
  flex: 1 1 auto; min-width: 0;
  color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.crumbs a { color: inherit; text-decoration: none; }
.crumbs a:hover { color: var(--accent); text-decoration: underline; }

/* Mobil: varumärkestexten får ge plats åt brödsmulan — 🏊 räcker som hemknapp när
   utrymmet är slut, och det är simmarens namn man vill kunna läsa. */
@media (max-width: 640px) {
  .topbar { gap: 8px; padding: 8px 10px; }
  .brand { padding: 4px; font-size: 18px; }
  .brandtext { display: none; }
  .mainnav button { padding: 0 9px; font-size: 13.5px; }
  .crumbs { font-size: 12px; }
  .app { padding: 12px 10px; }
}
/* Installerad som app: håll innehållet ur vägen för hörn och hemindikator. */
@supports (padding: max(0px)) {
  .topbar { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
  .app { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
}

.app { max-width: 940px; margin: 0 auto; padding: 16px; }
.loading, .empty { color: var(--muted); padding: 24px 4px; }
.error { color: var(--bad); padding: 16px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }

/* Segmented toggle */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; padding: 6px 16px; border-radius: 999px; color: var(--muted); font-weight: 600; }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 14px; }
.spacer { flex: 1; }

input[type="search"], select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
input[type="search"] { min-width: 180px; }
input[type="search"]:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.linkbtn { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 6px 4px; }
.linkbtn:hover { text-decoration: underline; }

/* Competition cards */
.cards { display: grid; gap: 10px; }
.card {
  text-align: left; width: 100%;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); display: flex; align-items: baseline; gap: 12px;
}
.card:hover { border-color: var(--accent); }
.card h3 { margin: 0; font-size: 15.5px; }
.card .meta { color: var(--muted); font-size: 13px; margin-top: 3px; }
.card .when { margin-left: auto; color: var(--muted); font-size: 13px; white-space: nowrap; }
.pill { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }

/* Sessions & program */
.session { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; overflow: hidden; box-shadow: var(--shadow); }
.session > summary { list-style: none; }
.session > summary::-webkit-details-marker { display: none; }
.shead {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px; cursor: pointer;
  background: var(--surface-2); border-bottom: 1px solid transparent;
}
details[open] .shead { border-bottom-color: var(--border); }
.shead .chev { transition: transform .15s ease; color: var(--muted); }
details[open] .shead .chev { transform: rotate(90deg); }
.shead h2 { margin: 0; font-size: 14.5px; }
.shead .stime { color: var(--muted); font-size: 13px; margin-left: auto; }

.program { list-style: none; margin: 0; padding: 4px 0; }
.prow {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  border: 0; background: none; padding: 9px 14px; border-top: 1px solid transparent;
}
.prow + .prow { border-top-color: var(--border); }
.prow:hover { background: var(--accent-soft); }
.prow .num { flex: 0 0 34px; color: var(--muted); font-variant-numeric: tabular-nums; font-weight: 600; }
.prow .pname { flex: 1; }
.prow .rname { color: var(--muted); font-size: 12.5px; }
.badge { font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.prow .ptime { color: var(--muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Program/session status */
.stat { font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 999px; white-space: nowrap; }
.stat.ongoing { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }
.stat.ongoing::before { content: "●"; margin-right: 4px; animation: livepulse 1.6s ease-in-out infinite; }
.stat.done { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
@keyframes livepulse { 50% { opacity: .3; } }
.prow.ongoing { background: color-mix(in srgb, var(--good) 7%, transparent); }
.prow.ongoing:hover { background: var(--accent-soft); }
.prow.done .num, .prow.done .pname { color: var(--muted); }
.roundnav { margin-bottom: 12px; flex-wrap: wrap; }
.roundnav button { padding: 5px 12px; font-size: 13px; }
.sep { padding: 8px 14px; color: var(--muted); font-size: 12.5px; font-style: italic; border-top: 1px solid var(--border); }
.sep:first-child { border-top: 0; }
.sep + .prow { border-top-color: var(--border); } /* race row after a paus/prisutdelning needs its divider too */

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.tabs button { border: 0; background: none; padding: 9px 16px; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
.tabs button:disabled { opacity: .4; cursor: default; }

.classtitle { margin: 18px 0 8px; font-size: 13.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

/* Result tables */
.heat { margin-bottom: 16px; }
.heat h4 { margin: 0 0 6px; font-size: 13.5px; }
table.grid { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
table.grid th, table.grid td { padding: 7px 10px; text-align: left; border-top: 1px solid var(--border); font-variant-numeric: tabular-nums; }
table.grid thead th { background: var(--surface-2); color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .02em; border-top: 0; }
table.grid td.n, table.grid th.n { text-align: right; }
table.grid td.rank { font-weight: 700; }
table.grid tr.withdrawn td { color: var(--muted); text-decoration: line-through; }
table.grid td .club { color: var(--muted); font-size: 12.5px; }
.tag { font-size: 11px; font-weight: 700; padding: 1px 5px; border-radius: 5px; }
.tag.dsq { background: color-mix(in srgb, var(--bad) 18%, transparent); color: var(--bad); }
.tag.q { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.tableScroll { overflow-x: auto; }

/* Result vs entry-time diff, seed times, heat medals */
.diff { font-size: 12px; font-weight: 600; color: var(--muted); }
.diff.faster { color: var(--good); }
.diff.slower { color: var(--bad); }
.seed { color: var(--muted); }
.medal {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 22px; height: 22px; padding: 0 4px; border-radius: 999px;
  font-size: 12.5px; font-weight: 700;
}
.medal.m1 { background: var(--gold-bg); color: var(--gold-fg); }
.medal.m2 { background: var(--silver-bg); color: var(--silver-fg); }
.medal.m3 { background: var(--bronze-bg); color: var(--bronze-fg); }

/* Search hits, club lists */
.hits { display: grid; gap: 6px; max-width: 560px; }
.hit {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  text-align: left; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px;
}
.hit:hover { border-color: var(--accent); }
.mut { color: var(--muted); font-size: 13px; }
tr.rowlink { cursor: pointer; }
tr.rowlink:hover td { background: var(--accent-soft); }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; cursor: pointer; user-select: none; }
a.rlink { color: var(--accent); text-decoration: none; font-weight: 600; }
a.rlink:hover { text-decoration: underline; }

/* Swimmer event cards */
.ecard { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; box-shadow: var(--shadow); overflow: hidden; }
.ecard.withdrawn { opacity: .6; }
.ehead { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 14px; }
.ehead a { color: var(--text); font-weight: 600; text-decoration: none; }
.ehead a:hover { color: var(--accent); }
.ecard table.grid { border-left: 0; border-right: 0; border-bottom: 0; border-radius: 0; }
.seedline { padding: 9px 14px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 16px; }
.delta { font-weight: 700; font-size: 12px; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.delta.even { color: var(--muted); }
.footnote { color: var(--muted); font-size: 12.5px; margin: 4px 2px 12px; }
.secttitle { margin: 22px 0 10px; font-size: 16.5px; }

/* Swimmer name links & relay expansion */
a.plink { color: inherit; text-decoration: none; border-bottom: 1px dotted color-mix(in srgb, var(--muted) 55%, transparent); }
a.plink:hover { color: var(--accent); border-bottom-color: var(--accent); }
.xwrap { display: flex; align-items: baseline; gap: 6px; }
.xbtn, .pbx { border: 0; background: none; color: var(--accent); padding: 0 2px; font-size: 12px; line-height: 1.5; flex: 0 0 auto; }
tr.legsrow > td { background: var(--surface-2); padding: 8px 10px 10px 32px; }
.splithead { display: flex; align-items: center; gap: 10px; margin: 0 0 6px; flex-wrap: wrap; }
.seg.mini button { padding: 3px 10px; font-size: 12px; }
table.grid.legs { margin: 0; }
table.grid.legs th, table.grid.legs td { padding: 5px 8px; font-size: 13px; }
.seedline strong { color: var(--text); }
.starbtn { border: 0; background: none; font-size: 18px; color: var(--muted); padding: 0 4px; vertical-align: 1px; }
.starbtn.on { color: #eab308; }
.starbtn:hover { color: #eab308; }

/* Swimmer statistics */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; margin-bottom: 8px; }
.statcard { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.statcard .num { font-size: 26px; font-weight: 800; line-height: 1.15; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.statcard .lbl { color: var(--muted); font-size: 12.5px; margin-top: 2px; font-weight: 600; }
.statcard .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.bars { display: grid; gap: 7px; max-width: 460px; }
.barrow { display: grid; grid-template-columns: 76px 1fr 40px; align-items: center; gap: 10px; }
.blabel { font-size: 13px; color: var(--muted); }
.btrack { background: var(--surface-2); border-radius: 4px; height: 12px; }
.bfill { background: var(--accent); height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.bval { font-size: 13px; font-variant-numeric: tabular-nums; text-align: right; }

/* Previous-races panel on swimmer cards & favorite rows */
.pbpanel { background: var(--surface-2); border-top: 1px solid var(--border); }
tr.pbrow > td { padding: 0; background: var(--surface-2); }
tr.pbrow .pbpanel { border-top: 0; }
.pbrace { padding: 8px 14px 10px; }
.pbrace + .pbrace { border-top: 1px solid var(--border); }
.pbhead { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }

/* Live display — sized to be readable across a pool hall */
.displayWrap {
  position: relative;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: clamp(14px, 3vw, 32px);
}
.displayWrap:fullscreen { background: var(--bg); border: 0; border-radius: 0; padding: 4vh 5vw; overflow: auto; }
.dclock {
  position: absolute; top: clamp(12px, 2.5vw, 28px); right: clamp(14px, 3vw, 32px);
  color: var(--muted); font-weight: 700; font-variant-numeric: tabular-nums;
  font-size: clamp(15px, 2.4vw, 28px);
}
.devent { font-size: clamp(18px, 3vw, 32px); font-weight: 700; line-height: 1.2; }
.devent .dround { color: var(--muted); font-weight: 500; font-size: .8em; }
.dheat { font-size: clamp(34px, 8vw, 84px); font-weight: 800; line-height: 1.1; margin-top: 2px; letter-spacing: -.01em; }
.dheat .of { color: var(--muted); font-weight: 600; font-size: .55em; }
.dtable { width: 100%; border-collapse: collapse; margin-top: 14px; font-size: clamp(15px, 2.4vw, 27px); font-variant-numeric: tabular-nums; }
.dtable td { padding: clamp(5px, 1vh, 12px) 8px; border-top: 1px solid var(--border); vertical-align: baseline; }
.dtable .lane { color: var(--muted); font-weight: 700; width: 2ch; }
.dtable .who { font-weight: 600; }
.dtable .who .club { display: block; font-weight: 400; color: var(--muted); font-size: .62em; }
.dtable .res { text-align: right; font-weight: 700; white-space: nowrap; }
.dtable .res .seed { font-weight: 400; }
.dtable .res .diff { font-size: .6em; }
.dtable .plc { text-align: right; width: 3ch; }
.dtable .plc .medal { min-width: 1.5em; height: 1.5em; font-size: .8em; }
.dtable tr.favlane td { background: color-mix(in srgb, #eab308 12%, transparent); }
.favstar { color: #eab308; font-size: .8em; }
.dtable .dsofar { color: var(--muted); font-weight: 600; font-size: .6em; }
.dpaus { text-align: center; padding: 6vh 0 5vh; }
.dpaus h2 { font-size: clamp(30px, 6vw, 64px); margin: 0 0 8px; }
.dpaus .when { font-size: clamp(16px, 3vw, 30px); color: var(--muted); }
.dnext { margin-top: 14px; color: var(--muted); font-size: clamp(13px, 1.8vw, 20px); }

@media (max-width: 560px) {
  .app { padding: 12px; }
  .card .when { margin-left: 0; width: 100%; }
  .card { flex-wrap: wrap; }
  .prow .rname { display: none; }
}
