:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #14161a;
  --muted: #5f6672;
  --border: #d9dde3;
  --accent: #0a7cff;
  --accent-text: #ffffff;
  --good: #1f9d55;
  --warn: #b7791f;
  --bad: #d64545;
  --raw: #9aa3af;
  --rate: #d9620f;       /* stroke rate line, hard workout steps */
  --seg-easy: #8fa9c7;   /* warm-up / cool-down / recovery steps */
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #20242d;
    --text: #eef1f5;
    --muted: #9099a6;
    --border: #2a2f3a;
    --accent: #3d9bff;
    --accent-text: #06121f;
    --good: #3ccf7a;
    --warn: #f0b429;
    --bad: #ff6b6b;
    --raw: #5b6472;
    --rate: #ff8a3d;
    --seg-easy: #5f7fa6;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0);
}
[hidden] { display: none !important; }

button, select, input { font: inherit; color: inherit; }
button, select, .file-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.55em 1em;
  cursor: pointer;
}
button:hover, .file-btn:hover { border-color: var(--muted); }
button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.big { font-size: 1.25rem; padding: 0.8em 2em; }
button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.muted { color: var(--muted); }
.warn { color: var(--warn); }

/* Header */
.bar {
  display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap;
  padding: 0.6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: env(safe-area-inset-top, 0); z-index: 5;
}
.brand { font-weight: 700; letter-spacing: 0.02em; }
.pill { font-size: 0.8rem; padding: 0.2em 0.7em; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill[data-state="connected"] { background: color-mix(in srgb, var(--good) 20%, transparent); color: var(--good); }
.pill[data-state="reconnecting"], .pill[data-state="connecting"] { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }
.tabs { margin-left: auto; display: flex; gap: 0.25rem; }
.tabs button { border: none; background: none; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--text); background: var(--surface-2); }
.icon-btn { padding: 0.35em 0.6em; font-size: 1.1rem; line-height: 1; }

main { padding: 1rem; max-width: 1200px; margin: 0 auto; }

/* Connect */
.connect { text-align: center; padding-block: 12vh 2rem; }
.connect h1 { margin: 0 0 0.5rem; font-size: 1.8rem; }
.connect .alt { margin-top: 3rem; display: flex; gap: 0.6rem; justify-content: center; align-items: center; flex-wrap: wrap; }

/* Live */
.banner {
  text-align: center; font-weight: 600; font-size: 1.1rem;
  padding: 0.6rem; border-radius: 12px; margin-bottom: 0.75rem;
  background: var(--surface-2);
}
.banner[data-kind="warn"] { background: color-mix(in srgb, var(--warn) 22%, var(--surface)); color: var(--text); }

.hero, .grid { display: grid; gap: 0.75rem; }
.hero { grid-template-columns: 2fr 1fr 1fr; margin-bottom: 0.75rem; }
.grid { grid-template-columns: repeat(4, 1fr); }
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 0.8rem 1rem;
  display: flex; flex-direction: column; justify-content: space-between;
  min-width: 0;
}
.label { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; }
.unit { text-transform: none; letter-spacing: 0; }
.value {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: clamp(2.2rem, 7vw, 4rem);
  line-height: 1.1;
  white-space: nowrap;
}
.tile-pace .value { font-size: clamp(3.4rem, 13vw, 8rem); }
.sub { color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.live[data-state="paused"] #v-time { color: var(--muted); }
/* during a workout the knob follows the workout: no buttons to press */
.tile.knob.auto .round { display: none; }
.tile.knob.auto .knob-row { justify-content: center; }
.knob-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.round { width: 3rem; height: 3rem; border-radius: 50%; font-size: 1.5rem; padding: 0; flex: none; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 0.75rem 1rem; margin-top: 0.75rem;
  display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap;
}
.panel label { display: flex; gap: 0.5rem; align-items: center; }
.actions { display: flex; gap: 0.6rem; justify-content: flex-end; margin-top: 1rem; flex-wrap: wrap; }

@media (max-width: 760px) {
  .hero { grid-template-columns: 1fr 1fr; }
  .tile-pace { grid-column: 1 / -1; }
  .grid { grid-template-columns: 1fr 1fr; }
}

/* Full screen ("immersive"): no header or buttons, everything fits one screen.
   Sized for a Pixel 11 Pro in landscape (~915x412 CSS px; Pro XL ~998x448). Numbers that
   would still be too wide (e.g. "1:05:32") are shrunk to fit by fitValues() in live.js. */
.fs-exit {
  position: fixed; z-index: 40; top: calc(0.4rem + env(safe-area-inset-top, 0px)); right: calc(0.4rem + env(safe-area-inset-right, 0px));
  width: 2.2rem; height: 2.2rem; padding: 0; border-radius: 50%; opacity: 0.55; font-size: 1rem;
}
.fs-exit:hover, .fs-exit:focus-visible { opacity: 1; }
body.immersive { padding: 0; }
body.immersive .bar, body.immersive .actions, body.immersive #sim-controls, body.immersive #replay-controls { display: none !important; }
body.immersive main { max-width: none; padding: calc(0.5rem + env(safe-area-inset-top, 0px)) calc(0.5rem + env(safe-area-inset-right, 0px)) calc(0.5rem + env(safe-area-inset-bottom, 0px)) calc(0.5rem + env(safe-area-inset-left, 0px)); }
body.immersive .live { display: flex; flex-direction: column; gap: 0.5rem; height: calc(100dvh - 1rem - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)); }
body.immersive .dash { flex: 1; min-height: 0; display: grid; gap: 0.5rem; grid-template-columns: 1fr 1fr; grid-auto-rows: 1fr; }
body.immersive .hero, body.immersive .grid { display: contents; }
body.immersive .tile-pace { grid-column: 1 / -1; }
body.immersive .tile { padding: 0.35rem 0.7rem; border-radius: 12px; }
body.immersive .label { font-size: 0.75rem; }
body.immersive .sub { font-size: 0.8rem; }
body.immersive .value { font-size: clamp(1.6rem, min(9vh, 9vw), 4.5rem); }
body.immersive .tile-pace .value { font-size: clamp(3rem, min(18vh, 22vw), 11rem); }
body.immersive .round { width: 2.4rem; height: 2.4rem; font-size: 1.2rem; }
body.immersive .value { overflow: hidden; }
body.immersive .knob-row .value { flex: 1; min-width: 0; text-align: center; }
@media (orientation: landscape) {
  /* pace big on the left, six stats in two rows on the right */
  body.immersive .dash { grid-template-columns: 2fr 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; }
  body.immersive .tile-pace { grid-column: 1; grid-row: 1 / span 2; }
  body.immersive .value { font-size: clamp(1.6rem, 19vh, 6rem); }
  body.immersive .tile-pace .value { font-size: clamp(3rem, min(40vh, 14vw), 13rem); }
}
/* workout panel shrinks to a two-line bar: [time left + targets] [step / next] [skip] [end] */
body.immersive .workout {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto;
  grid-template-areas: "main step skip end" "total next skip end" "bar bar bar bar";
  align-items: center; column-gap: 0.8rem; padding: 0.3rem 2.9rem 0.3rem 0.8rem; margin: 0; /* right: room for the exit button */
}
body.immersive .wk-head { display: contents; }
body.immersive .wk-head .spacer, body.immersive .wk-name { display: none; }
body.immersive .wk-main { grid-area: main; margin: 0; display: flex; align-items: baseline; gap: 0.8rem; flex-wrap: nowrap; }
body.immersive .wk-step, body.immersive .wk-next { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.85rem; }
body.immersive .wk-step { grid-area: step; color: var(--text); }
body.immersive .wk-next { grid-area: next; }
body.immersive #wk-skip { grid-area: skip; }
body.immersive #wk-end { grid-area: end; }
body.immersive .wk-head button { padding: 0.3em 0.7em; }
body.immersive .wk-remaining { font-size: clamp(1.4rem, 8vh, 2.2rem); }
body.immersive .wk-targets { white-space: nowrap; }
body.immersive .wk-total { grid-area: total; font-size: 0.8rem; margin: 0; white-space: nowrap; }
body.immersive .wk-timeline { grid-area: bar; margin: 0.25rem 0 0; height: 9px; }
body.immersive .mini-chart { flex: 1 1 0; min-height: 28px; height: auto; margin: 0.15rem 0; }
body.immersive .gauge { flex: 1 1 0; min-height: 0; max-height: none; }
body.immersive .chart-key { display: none; }
/* prompts float over the stats */
body.immersive .knob-prompt, body.immersive .workout.done {
  position: fixed; z-index: 30; left: 50%; top: 50%; transform: translate(-50%, -50%); margin: 0;
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.5); max-width: calc(100vw - 2rem);
}
body.immersive .knob-prompt { padding: 1rem 2rem; }
body.immersive .kp-knob { font-size: 5rem; }
body.immersive .banner {
  position: fixed; z-index: 25; top: calc(0.4rem + env(safe-area-inset-top, 0px)); left: 50%; transform: translateX(-50%);
  margin: 0; padding: 0.35rem 1rem; box-shadow: 0 6px 24px rgb(0 0 0 / 0.4); max-width: calc(100vw - 6rem);
}

/* Debug */
.debug .panel { display: block; }
.debug canvas { width: 100%; height: 180px; display: block; margin-top: 0.5rem; }
#chart-power { height: 140px; }
.legend { margin-left: 1rem; color: var(--muted); font-size: 0.85rem; }
.sw { display: inline-block; width: 14px; height: 3px; margin: 0 0.3rem 0.2rem 0.6rem; vertical-align: middle; }
.sw.raw { background: var(--raw); }
.sw.smooth { background: var(--accent); }
.row { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.log {
  margin: 0.5rem 0 0; max-height: 40vh; overflow: auto;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-2); border-radius: 10px; padding: 0.6rem;
}

/* Workouts */
.pending { font-weight: 600; color: var(--accent); }
.workout {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 0.75rem 1rem; margin-bottom: 0.75rem;
}
.wk-head { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; }
.wk-name { font-weight: 700; }
.wk-step { color: var(--muted); }
.wk-main { display: flex; align-items: baseline; gap: 1rem; flex-wrap: wrap; margin-top: 0.3rem; }
.wk-remaining { font-size: clamp(2rem, 7vw, 3.2rem); font-weight: 700; font-variant-numeric: tabular-nums; }
.wk-targets { font-size: 1.05rem; }
.wk-total { font-size: 0.9rem; margin-top: 0.1rem; }
/* segmented workout timeline: one block per step */
.wk-timeline { position: relative; height: 12px; margin: 0.5rem 0 0.4rem; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.seg { position: absolute; top: 0; bottom: 0; opacity: 0.4; box-shadow: inset -2px 0 0 var(--surface); }
.seg.done { opacity: 0.85; }
.seg.current { opacity: 1; box-shadow: inset 0 0 0 2px var(--text); border-radius: 3px; }
.seg-hard { background: var(--rate); }
.seg-steady { background: var(--accent); }
.seg-easy { background: var(--seg-easy); }
.seg-rest { background: var(--border); }
.seg-marker { position: absolute; top: 0; bottom: 0; width: 3px; margin-left: -1px; background: var(--text); border-radius: 2px; }
.wo-card .wk-timeline { height: 7px; margin: 0.15rem 0 0.1rem; }
.wo-card .seg { opacity: 0.9; }

/* rolling pace / rate chart inside the pace tile */
.mini-chart { display: block; width: 100%; height: 72px; margin: 0.25rem 0; }
.chart-key { margin-left: 0.5rem; font-size: 0.75rem; white-space: nowrap; }
.chart-key i { display: inline-block; width: 10px; height: 3px; margin: 0 3px 2px 6px; vertical-align: middle; border-radius: 2px; }
.key-pace { background: var(--accent); }
.key-rate { background: var(--rate); }

/* stroke-rate gauge */
.gauge { display: block; width: 100%; max-height: 150px; overflow: visible; }
.g-track { fill: none; stroke: var(--surface-2); stroke-width: 16; stroke-linecap: round; }
.g-target { fill: none; stroke: var(--muted); stroke-width: 16; }
.g-target[data-status="low"] { stroke: var(--text); }
.g-target[data-status="ok"] { stroke: var(--good); }
.g-target[data-status="high"] { stroke: var(--bad); }
.g-needle { stroke: var(--text); stroke-width: 5; stroke-linecap: round; }
.g-value { font-size: 62px; font-weight: 700; fill: var(--text); font-variant-numeric: tabular-nums; font-family: inherit; }
.g-min, .g-max { font-size: 12px; fill: var(--muted); font-family: inherit; }
.workout.done p { margin: 0.4rem 0 0.6rem; }
.knob-prompt {
  display: flex; align-items: center; justify-content: center; gap: 0.8rem; flex-wrap: wrap;
  background: var(--accent); color: var(--accent-text); border-radius: 16px;
  padding: 0.6rem 1rem; margin-bottom: 0.75rem; font-weight: 700;
}
.kp-label { font-size: 1.4rem; }
.kp-knob { font-size: 3rem; line-height: 1; font-variant-numeric: tabular-nums; }
.kp-when { font-size: 1.2rem; opacity: 0.85; }
.tile[data-target="ok"] { border-color: var(--good); box-shadow: inset 0 0 0 1px var(--good); }
.tile[data-target="off"] { border-color: var(--warn); box-shadow: inset 0 0 0 1px var(--warn); }
.wo-group { margin: 1rem 0 0.4rem; color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; }
.wo-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(17rem, 1fr)); gap: 0.6rem; }
.wo-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 0.8rem 1rem; display: flex; flex-direction: column; gap: 0.35rem; }
.wo-card h3 { margin: 0; font-size: 1.05rem; }
.wo-card .meta { color: var(--muted); font-size: 0.85rem; }
.wo-card .actions { margin-top: auto; justify-content: flex-start; }
.editor { display: block; }
.editor h2 { margin: 0; font-size: 1.2rem; }
.editor label.wide { display: flex; flex-direction: column; gap: 0.25rem; margin: 0.6rem 0; color: var(--muted); }
.editor input, .editor select { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 0.4em 0.5em; min-width: 0; }
.seg-head, .seg-row { display: grid; grid-template-columns: 5.5rem 4.5rem 4.5rem 3.5rem 7rem 7rem auto; gap: 0.4rem; align-items: center; }
.seg-head { font-size: 0.8rem; margin-top: 0.6rem; }
.seg-row { margin: 0.3rem 0; }
.seg-row .pair { display: flex; gap: 0.2rem; align-items: center; }
.seg-row .pair input { width: 100%; }
.seg-row .btns { display: flex; gap: 0.2rem; }
.seg-row .btns button { padding: 0.3em 0.55em; }
.seg-row[data-type="rest"] .row-only { visibility: hidden; }
.settings-form label.check { flex-direction: row; align-items: center; gap: 0.5rem; color: var(--text); }
@media (max-width: 760px) {
  .seg-head { display: none; }
  .seg-row { grid-template-columns: 1fr 1fr 1fr 1fr; padding: 0.4rem; background: var(--surface-2); border-radius: 10px; }
  .seg-row .pair, .seg-row .btns { grid-column: span 2; }
}

/* History */
.spacer { flex: 1; }
.history-head { margin-bottom: 0.5rem; }
.history-head h2 { margin: 0; font-size: 1.3rem; }
.session-list { display: flex; flex-direction: column; gap: 0.5rem; }
.session-row {
  display: grid; grid-template-columns: 1.6fr 1fr 0.8fr 1fr 1.3fr; gap: 0.5rem; align-items: baseline;
  text-align: left; background: var(--surface); border-radius: 14px; padding: 0.8rem 1rem;
}
.session-row .when { font-weight: 600; }
.session-row .wo-name { display: block; font-weight: 400; color: var(--muted); font-size: 0.9rem; }
.session-row .stat { font-variant-numeric: tabular-nums; }
.session-row .big { font-weight: 700; font-size: 1.1rem; }
.badge { margin-left: 0.5rem; font-size: 0.75rem; font-weight: 500; padding: 0.1em 0.5em; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.detail-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.6rem; }
.tile.small .value { font-size: clamp(1.4rem, 4vw, 2rem); }
.charts { display: block; }
.charts strong { display: block; margin-top: 0.6rem; font-size: 0.85rem; color: var(--muted); font-weight: 600; }
.charts canvas { width: 100%; height: 150px; display: block; }
.charts canvas.short { height: 90px; }
button.danger { color: var(--bad); }
@media (max-width: 760px) {
  .session-row { grid-template-columns: 1fr 1fr; }
  .session-row .when { grid-column: 1 / -1; }
  .detail-grid { grid-template-columns: 1fr 1fr; }
}

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  max-width: min(36rem, calc(100vw - 2rem));
  background: var(--text); color: var(--bg); padding: 0.7rem 1.1rem; border-radius: 12px;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.3); z-index: 20; font-weight: 500;
}

/* Target results */
.targets { display: block; }
.targets .row { margin-bottom: 0.5rem; }
.target-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr)); gap: 0.6rem; margin-bottom: 0.6rem; }
.table-scroll { overflow-x: auto; }
.target-table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; font-size: 0.9rem; white-space: nowrap; }
.target-table th, .target-table td { padding: 0.35rem 0.6rem; text-align: right; border-bottom: 1px solid var(--border); }
.target-table th:first-child, .target-table td:first-child { text-align: left; }
.target-table th { color: var(--muted); font-weight: 500; font-size: 0.8rem; }
.grade-good { color: var(--good); }
.grade-fair { color: var(--warn); }
.grade-poor { color: var(--bad); }
.tile.grade-good { border-color: var(--good); }
.tile.grade-fair { border-color: var(--warn); }
.tile.grade-poor { border-color: var(--bad); }
.tile.grade-good .value, .tile.grade-fair .value, .tile.grade-poor .value { color: inherit; }
.on-target { color: var(--text); }
.band-key { font-weight: 400; }

/* Settings */
dialog {
  border: 1px solid var(--border); border-radius: 16px;
  background: var(--surface); color: var(--text);
  width: min(26rem, calc(100vw - 2rem));
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
.settings-form h2 { margin-top: 0; }
.about { border-top: 1px solid var(--border); margin-top: 1rem; padding-top: 0.8rem; display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.about p { margin: 0.3rem 0 0; flex-basis: 100%; font-size: 0.85rem; }
#btn-reconnect { margin-top: 0.8rem; }
.settings-form label { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.9rem; color: var(--muted); }
.settings-form input, .settings-form select {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 0.5em 0.7em; color: var(--text);
}
