/* Design tokens. The look is "telemetry in team colours": condensed display type, monospace labels and
   thin-lined panels, in navy, white and signal orange. Day and Night are both designed: Night applies
   when the phone asks for dark (unless Day was picked) or when Day/Night picked Night (data-theme).
   Orange (--color-accent) marks the race, the current tab and interaction; green, amber and red are
   kept for recovery and status; the --series-* colours are for chart data only. */
:root {
  color-scheme: light;
  --font-anthropic-sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-anthropic-serif: "Barlow Condensed", "Arial Narrow", system-ui, sans-serif; /* display face */
  --font-display: var(--font-anthropic-serif);
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Consolas, monospace;
  --cds-font-size-title: 1.75rem;
  --cds-font-size-heading: 1.1875rem;
  --cds-font-size-body: 0.9375rem;
  --cds-font-size-caption: 0.8125rem;
  --cds-font-weight-medium: 600;
  --cds-gap-xs: 4px;
  --cds-gap-sm: 8px;
  --cds-gap-md: 16px;
  --cds-gap-lg: 24px;
  --cds-pad-xs: 4px;
  --cds-pad-sm: 8px;
  --cds-pad-lg: 18px;
  --cds-radius: 10px;
  --cds-dur-slow: 300ms;

  --color-bg: #f3f5f9;
  --color-panel: #ffffff;
  --color-chip: #eaeef5;
  --color-fg: #0a1633;
  --color-fg-muted: #4f5b72;
  --color-border-line: #dce2ec;
  --color-accent: #ff5b14;
  --color-accent-text: #c2410c;
  --color-on-accent: #0a1633;
  --color-ok: #0f6634;
  --color-ok-bg: #e2f4ea;
  --color-warn: #8a4b00;
  --color-warn-bg: #fff0d9;
  --color-bad: #b3202a;
  --color-bad-bg: #fde8e8;

  --cds-text-secondary: var(--color-fg-muted);
  --cds-border: var(--color-border-line);
  --cds-surface-popover: var(--color-panel);
  --cds-shadow-popover: 0 10px 30px rgba(10, 22, 51, 0.14);
  --cds-chart-axis: #75819a;
  --cds-chart-grid: #e8ecf3;
  --cds-chart-muted: #b7c1d3;
  --cds-chart-reference: #4f5b72;
  --cds-chart-status-critical: #d03b3b;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --color-bg: #08122a;
    --color-panel: #111d3a;
    --color-chip: #1a2848;
    --color-fg: #f2f5fa;
    --color-fg-muted: #a9b5ca;
    --color-border-line: #24345a;
    --color-accent: #ff5b14;
    --color-accent-text: #ff7a3d;
    --color-on-accent: #08122a;
    --color-ok: #5fe08f;
    --color-ok-bg: #0c2a26;
    --color-warn: #fbbf3c;
    --color-warn-bg: #2d2412;
    --color-bad: #ff8a8a;
    --color-bad-bg: #3a1622;
    --cds-shadow-popover: 0 10px 30px rgba(0, 0, 0, 0.5);
    --cds-chart-axis: #8391ad;
    --cds-chart-grid: #1c2a4c;
    --cds-chart-muted: #3d4e78;
    --cds-chart-reference: #a9b5ca;
    --cds-chart-status-critical: #ff6b6b;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --color-bg: #08122a;
  --color-panel: #111d3a;
  --color-chip: #1a2848;
  --color-fg: #f2f5fa;
  --color-fg-muted: #a9b5ca;
  --color-border-line: #24345a;
  --color-accent: #ff5b14;
  --color-accent-text: #ff7a3d;
  --color-on-accent: #08122a;
  --color-ok: #5fe08f;
  --color-ok-bg: #0c2a26;
  --color-warn: #fbbf3c;
  --color-warn-bg: #2d2412;
  --color-bad: #ff8a8a;
  --color-bad-bg: #3a1622;
  --cds-shadow-popover: 0 10px 30px rgba(0, 0, 0, 0.5);
  --cds-chart-axis: #8391ad;
  --cds-chart-grid: #1c2a4c;
  --cds-chart-muted: #3d4e78;
  --cds-chart-reference: #a9b5ca;
  --cds-chart-status-critical: #ff6b6b;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-fg);
  font-family: var(--font-anthropic-sans);
  font-size: var(--cds-font-size-body);
}
main {
  max-width: 1200px; margin: 0 auto;
  padding: var(--cds-gap-md) max(16px, env(safe-area-inset-right)) 48px max(16px, env(safe-area-inset-left));
}
[hidden] { display: none !important; }

/* ---- Page frame: header, page tabs and the account menu ------------------------------------
   Phone (below 640px): a one-line header (title and sync status) and the page tabs in a bar fixed
   to the bottom of the screen, within thumb reach. Wider: one sticky bar with title, tabs and sync. */
:root { --tt-tabbar-h: 58px; }
.tt-top { display: grid; gap: var(--cds-gap-xs); padding-top: env(safe-area-inset-top); }
.tt-bar { display: flex; align-items: center; justify-content: space-between; gap: var(--cds-gap-md); min-height: 48px; }
.tt-brand {
  margin: 0; display: inline-flex; align-items: center; gap: 9px; white-space: nowrap;
  font-family: var(--font-display); font-weight: 700; font-size: 1.375rem; line-height: 1; text-transform: uppercase; letter-spacing: 0.04em;
}
.tt-brand::before { content: ""; width: 9px; height: 22px; background: var(--color-accent); transform: skewX(-14deg); }
.tt-sync { margin: 0; color: var(--color-fg-muted); font-size: var(--cds-font-size-caption); line-height: 1.4; }
#sync-progress:empty { display: none; }

.tt-tabs { display: flex; gap: var(--cds-gap-xs); }
.tt-tabs button {
  all: unset; box-sizing: border-box; cursor: pointer; position: relative;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 12px; border-radius: 8px;
  font-size: var(--cds-font-size-body); color: var(--color-fg-muted);
  -webkit-tap-highlight-color: transparent;
}
.tt-tabs button:hover { color: var(--color-fg); }
.tt-tabs button span { font-family: var(--font-display); font-weight: 600; font-size: 1.0625rem; text-transform: uppercase; letter-spacing: 0.05em; }
.tt-tabs button[aria-current="page"] { color: var(--color-fg); font-weight: var(--cds-font-weight-medium); }
.tt-tabs button[aria-current="page"]::after {
  content: ""; position: absolute; left: 12px; right: 12px; bottom: 4px; height: 3px; border-radius: 1px; background: var(--color-accent);
}
.tt-tabs button:focus-visible { outline: 2px solid var(--color-accent-text); outline-offset: -2px; }
.tt-ico { width: 22px; height: 22px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.tt-account { position: relative; flex: none; }
.tt-syncchip {
  position: relative; font: 500 0.75rem var(--font-mono); letter-spacing: .04em; color: var(--color-fg-muted); cursor: pointer;
  display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 12px;
  border: 1px solid var(--color-border-line); border-radius: 8px; background: var(--color-panel); white-space: nowrap;
}
.tt-account-row { display: flex; align-items: center; gap: 8px; flex: none; }
.tt-iconbtn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; padding: 0; cursor: pointer;
  border: 1px solid var(--color-border-line); border-radius: 8px; background: var(--color-panel); color: var(--color-fg);
}
.tt-iconbtn:hover { border-color: var(--cds-chart-axis); }
.tt-iconbtn:focus-visible { outline: 2px solid var(--color-accent-text); outline-offset: 2px; }
.tt-iconbtn .tt-ico { width: 20px; height: 20px; }
.tt-theme-auto { margin: 0; }
.tt-syncchip:hover { border-color: var(--cds-chart-axis); color: var(--color-fg); }
.tt-syncchip:focus-visible { outline: 2px solid var(--color-accent-text); outline-offset: 2px; }
.tt-syncchip[aria-expanded="true"] { color: var(--color-fg); border-color: var(--cds-chart-axis); }
.tt-syncdot { width: 8px; height: 8px; border-radius: 50%; background: var(--cds-chart-muted); }
.tt-syncchip-error .tt-syncdot { background: var(--color-bad); }
.tt-account-panel {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; width: min(20rem, calc(100vw - 32px));
  display: grid; gap: var(--cds-gap-md); justify-items: start; padding: 16px;
  background: var(--color-panel); border: 1px solid var(--color-border-line); border-radius: 12px; box-shadow: var(--cds-shadow-popover);
}
.tt-account-panel .tt-button { min-height: 44px; padding: 0 18px; font-size: var(--cds-font-size-body); }
.tt-account-panel .tt-linkbutton { min-height: 44px; display: inline-flex; align-items: center; font-size: var(--cds-font-size-body); }

@media (max-width: 639.98px) {
  main { padding-bottom: calc(var(--tt-tabbar-h) + env(safe-area-inset-bottom) + 32px); }
  .tt-syncchip { padding: 0 10px; }
  /* Phones show the dot and the time; "Synced" stays for screen readers. */
  .tt-synced-word { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .tt-bar { gap: 10px; }
  .tt-tabs {
    position: fixed; z-index: 20; left: 0; right: 0; bottom: 0;
    display: grid; grid-template-columns: repeat(4, 1fr); gap: 0;
    padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background: var(--color-panel); border-top: 1px solid var(--color-border-line);
  }
  .tt-tabs button {
    flex-direction: column; gap: 4px; min-height: var(--tt-tabbar-h); padding: 6px 2px 8px; border-radius: 0;
  }
  .tt-tabs button span { font-family: var(--font-mono); font-weight: 500; font-size: 0.625rem; letter-spacing: 0.08em; }
  .tt-brand { font-size: 1.25rem; }
  .tt-tabs button[aria-current="page"]::after { top: 0; bottom: auto; left: 28%; right: 28%; height: 3px; border-radius: 0 0 2px 2px; }
}
@media (min-width: 640px) {
  .tt-top {
    position: sticky; top: 0; z-index: 20;
    margin: calc(-1 * var(--cds-gap-md)) -16px 0; padding: env(safe-area-inset-top) 16px 0;
    background: var(--color-bg); border-bottom: 1px solid var(--color-border-line);
  }
  .tt-top > .tt-sync { padding-bottom: 8px; }
  .tt-bar { min-height: 60px; }
  .tt-tabs { flex: 1; justify-content: center; }
  .tt-ico { width: 18px; height: 18px; }
  .tt-page { scroll-margin-top: 76px; }
}
@media (min-width: 640px) and (max-width: 899.98px) {
  .tt-tabs .tt-ico { display: none; }
  .tt-tabs button { padding: 0 8px; }
  /* Visually hidden, still the button's name for screen readers. */
  .tt-syncchip > span:last-child { position: absolute; left: 0; top: 0; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
}

/* Loading placeholder for values and charts that are still waiting on data. */
.dash-skeleton { position: relative; color: transparent !important; }
.dash-skeleton::after {
  content: ""; position: absolute; inset: 0; border-radius: 6px;
  background: var(--color-border-line); opacity: 0.6; animation: tt-pulse 1.4s ease-in-out infinite;
}
.tt-plot.dash-skeleton::after { inset: 8px 0; }
@keyframes tt-pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .dash-skeleton::after { animation: none; } }

/* Buttons. */
.tt-button {
  font: inherit; font-size: 0.875rem; font-weight: 600; display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 16px; border-radius: 8px; border: 1px solid var(--color-border-line);
  background: var(--color-panel); color: var(--color-fg); cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.tt-button:hover:not(:disabled) { border-color: var(--cds-chart-axis); }
.tt-button:disabled { opacity: 0.6; cursor: default; }
.tt-button:focus-visible { outline: 2px solid var(--color-accent-text); outline-offset: 2px; }
.tt-sync-error { color: var(--color-bad); }

/* Sign-in screen. */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.gate-card {
  width: min(100%, 26rem); display: grid; gap: var(--cds-gap-md); text-align: center;
  background: var(--color-panel); border: 1px solid var(--color-border-line);
  border-radius: 12px; padding: 32px 24px; border-top: 4px solid var(--color-accent);
}
.gate-card h1 { margin: 0; font-family: var(--font-display); font-weight: 700; font-size: 2rem; line-height: 1; text-transform: uppercase; letter-spacing: 0.03em; }
.gate-card p { margin: 0; color: var(--color-fg-muted); line-height: 1.5; }
.gate-card .tt-button { justify-self: center; font-size: var(--cds-font-size-body); padding: 10px 18px; }
.gate-error { color: var(--color-bad) !important; }

/* Next race: card heading row, the edit dialog and its form. */
.tt-nr-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--cds-gap-sm); flex-wrap: wrap; }
.tt-nr-head h2 { margin: 0; }
.tt-nr-name { font-family: var(--font-display); font-weight: 600; font-size: 1.5rem; line-height: 1.05; text-transform: uppercase; letter-spacing: 0.02em; }
.tt-nr-sub { margin: var(--cds-gap-sm) 0 0; font-family: var(--font-display); font-size: 1.0625rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.tt-dialog {
  width: min(100% - 32px, 34rem); max-height: calc(100dvh - 32px); padding: 0;
  border: 1px solid var(--color-border-line); border-radius: 14px;
  background: var(--color-panel); color: var(--color-fg); box-shadow: var(--cds-shadow-popover);
}
.tt-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.tt-dialog form { display: grid; gap: var(--cds-gap-md); padding: 20px; }
.tt-dialog h2 { margin: 0; font-family: var(--font-display); font-weight: 700; font-size: var(--cds-font-size-title); line-height: 1; text-transform: uppercase; letter-spacing: 0.02em; }
.tt-dialog label { display: grid; gap: 4px; font-size: var(--cds-font-size-caption); color: var(--color-fg-muted); }
.tt-dialog input, .tt-dialog select, .tt-dialog textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS zooming into the field */
  color: var(--color-fg); background: var(--color-bg); border: 1px solid var(--color-border-line);
  border-radius: 8px; padding: 8px 10px; width: 100%;
}
.tt-dialog input:focus-visible, .tt-dialog select:focus-visible, .tt-dialog textarea:focus-visible {
  outline: 2px solid var(--color-accent-text); outline-offset: 1px;
}
.tt-dialog fieldset.tt-goal-time { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0; padding: 0; border: 0; min-width: 0; }
.tt-dialog fieldset.tt-goal-time legend { padding: 0; margin-bottom: 4px; font-size: var(--cds-font-size-caption); color: var(--color-fg-muted); }
.tt-dialog fieldset.tt-goal-time label { display: flex; align-items: center; gap: 6px; }
.tt-dialog fieldset.tt-goal-time input { min-width: 0; flex: 1; }
.tt-form-row { display: grid; gap: var(--cds-gap-md); grid-template-columns: repeat(auto-fit, minmax(min(100%, 12rem), 1fr)); }
.tt-form-status { margin: 0; min-height: 1.2em; font-size: var(--cds-font-size-caption); color: var(--color-fg-muted); }
.tt-form-actions { display: flex; justify-content: flex-end; align-items: center; gap: var(--cds-gap-md); }
.tt-button-primary { background: var(--color-accent); color: var(--color-on-accent); border-color: var(--color-accent); }
.tt-button-primary:hover:not(:disabled) { border-color: var(--color-fg); }

/* Race calendar dialog: the list of races and the add/edit form. */
.tt-rc { display: grid; gap: var(--cds-gap-md); padding: 20px; }
.tt-rc h3 { margin: 0; font-size: var(--cds-font-size-body); font-weight: var(--cds-font-weight-medium); }
.tt-rc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--cds-gap-sm); }
.tt-rc-item {
  display: flex; align-items: center; justify-content: space-between; gap: var(--cds-gap-sm);
  padding: 10px 12px; border: 1px solid var(--color-border-line); border-radius: 10px; background: var(--color-bg);
}
.tt-rc-item > div:first-child { display: grid; gap: 2px; min-width: 0; }
.tt-rc-item b { font-weight: var(--cds-font-weight-medium); overflow-wrap: anywhere; }
.tt-rc-item .tt-chip { justify-self: start; }
.tt-rc-next { border-color: var(--color-accent); }
.tt-rc-past { opacity: 0.6; }
.tt-rc-actions { display: flex; gap: var(--cds-gap-md); flex: none; }
.tt-rc-actions .tt-linkbutton, #rc-close { min-height: 44px; padding: 0 4px; }
.tt-dialog .tt-rc-form { display: grid; gap: var(--cds-gap-md); padding: 14px; border: 1px solid var(--color-border-line); border-radius: 10px; }
#rc-add { justify-self: start; min-height: 44px; }
.tt-dialog .tt-rc label { display: grid; gap: 4px; font-size: var(--cds-font-size-caption); color: var(--color-fg-muted); }

/* Day/Night switch: the button shows the moon in Day (tap for Night) and the sun in Night. */
.tt-ico-sun { display: none; }
:root[data-theme="dark"] .tt-ico-sun { display: block; }
:root[data-theme="dark"] .tt-ico-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tt-ico-sun { display: block; }
  :root:not([data-theme="light"]) .tt-ico-moon { display: none; }
}

/* The race calendar dialog is a full-screen sheet on phones: the title and Close stay at the top,
   Save and replan stays at the bottom, and the list and form scroll between them. */
@media (max-width: 639.98px) {
  .tt-dialog {
    width: 100%; max-width: 100%; height: 100dvh; max-height: 100dvh; margin: 0; inset: 0;
    border: 0; border-radius: 0; box-shadow: none; overflow-y: auto; overscroll-behavior: contain;
  }
  .tt-dialog .tt-rc { min-height: 100%; padding: 0 16px; display: flex; flex-direction: column; gap: var(--cds-gap-md); }
  .tt-dialog .tt-rc > .tt-nr-head {
    position: sticky; top: 0; z-index: 2; margin: 0 -16px; padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
    background: var(--color-panel); border-bottom: 1px solid var(--color-border-line); align-items: center;
  }
  .tt-dialog .tt-rc > .tt-form-actions:last-child {
    position: sticky; bottom: 0; z-index: 2; margin: auto -16px 0; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    background: var(--color-panel); border-top: 1px solid var(--color-border-line);
  }
  .tt-dialog .tt-rc > .tt-form-actions:last-child .tt-button-primary { flex: 1; }
  .tt-rc-item { flex-wrap: wrap; }
}
