:root {
  color-scheme: dark;
  --bg: #10161a;
  --panel: #151d22;
  --field: #0d1317;
  --hover: #202b32;
  --border: #2c3840;
  --text: #edf1f3;
  --muted: #9baab4;
  --accent: #df8b55;
  --numeric: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  --red: #f29a94;
  --chart-secondary: #a6bddb;
  --chart-tertiary: #a4c7b5;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: .55; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; }
h1, h2, h3, p { margin-top: 0; }
h1 { font-size: 28px; line-height: 1.2; letter-spacing: -.6px; }
h2 { font-size: 20px; line-height: 1.35; margin-bottom: 10px; font-weight: 650; }
h3 { font-size: 15px; margin-bottom: 0; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.eyebrow { margin: 0 0 10px; color: var(--muted); font-size: 11px; letter-spacing: .07em; }
.wide { width: 100%; }
.skip-link { position: fixed; top: 12px; left: 16px; z-index: 10; padding: 12px 18px; background: var(--panel); transform: translateY(-180%); }
.skip-link:focus { transform: translateY(0); }

/* Header and page frame */
.topbar {
  min-height: 64px;
  background: #0c1115;
  padding: 12px max(24px, calc((100vw - 1320px) / 2));
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-size: 19px; font-weight: 800; letter-spacing: .12em; }
.brand:hover { text-decoration: none; }
.brand-mark { display: grid; place-items: center; width: 30px; height: 30px; background: var(--accent); color: #10161a; font-size: 19px; font-style: italic; }
.brand-caption { margin-left: 12px; color: var(--muted); font-size: 10px; font-weight: 500; letter-spacing: .08em; }
#account { display: flex; align-items: center; gap: 12px; min-width: 0; }
#username { max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
main { width: min(100%, 1368px); margin: auto; padding: 28px 24px 56px; min-height: calc(100vh - 125px); }
footer { display: flex; justify-content: space-between; gap: 20px; border-top: 1px solid var(--border); padding: 18px max(24px, calc((100vw - 1320px) / 2)); color: var(--muted); font-size: 11px; }
.page-heading, .section-heading { display: flex; justify-content: space-between; align-items: center; gap: 20px; }
.page-heading { margin-bottom: 24px; }
.page-heading h1 { margin-bottom: 6px; }
.page-heading p:last-child { margin-bottom: 0; }
.section-heading { margin-bottom: 18px; }
.section-heading h2 { margin-bottom: 0; }
.panel { border: 1px solid var(--border); background: var(--panel); padding: 20px; border-radius: 2px; min-width: 0; }

/* Sign in: a compact introduction alongside the account form */
.auth-layout { display: grid; grid-template-columns: minmax(0, 1fr) 390px; gap: 80px; max-width: 960px; margin: 50px auto; align-items: start; }
.intro { padding-top: 18px; }
.intro h1 { font-size: 36px; margin: 12px 0 18px; }
.lead { color: var(--muted); max-width: 420px; font-size: 17px; }
.intro-guide { margin: 32px 0 0; }
.intro-guide > div { display: grid; grid-template-columns: 100px 1fr; gap: 16px; padding: 16px 0; border-top: 1px solid var(--border); }
.intro-guide dt { font-size: 13px; font-weight: 600; }
.intro-guide dd { margin: 0; font-size: 13px; color: var(--muted); }
.auth-panel { padding: 26px; }
.auth-footnote { color: var(--muted); font-size: 11px; margin: 22px 0 0; }
#auth-form label { margin: 18px 0; }

/* Controls */
.tabs { display: flex; gap: 24px; border-bottom: 1px solid var(--border); margin-bottom: 24px; }
.tabs button { padding: 8px 0 12px; background: none; border: 0; border-bottom: 2px solid transparent; color: var(--muted); min-height: 44px; }
.tabs button:hover { color: var(--text); }
.tabs .active { color: var(--text); border-bottom-color: var(--accent); }
.workspace-tabs { margin: 20px 0 24px; gap: 8px; }
.workspace-tabs button { padding: 11px 20px; font-size: 12px; font-weight: 650; letter-spacing: .05em; }
.workspace-tabs .active { color: var(--accent); background: var(--panel); }
label { display: flex; flex-direction: column; gap: 7px; color: var(--muted); font-size: 12px; font-weight: 600; min-width: 0; }
input, select { min-width: 0; width: 100%; border: 1px solid #43535e; border-radius: 2px; background: var(--field); color: var(--text); padding: 10px 12px; min-height: 44px; }
input::placeholder { color: #929aa5; }
input:focus-visible, select:focus-visible { border-color: var(--accent); }
.primary, .quiet { display: inline-flex; align-items: center; justify-content: center; min-height: 42px; padding: 10px 15px; border: 1px solid transparent; border-radius: 2px; font-size: 12px; font-weight: 600; line-height: 1.4; text-align: center; }
.primary { color: #10161a; background: var(--accent); font-weight: 750; }
.primary:hover { background: #e89c6d; }
.quiet { background: transparent; border-color: var(--border); color: var(--text); }
.quiet:hover { background: var(--hover); text-decoration: none; }
.danger { color: var(--red); }
.error { color: var(--red); font-size: 13px; margin: 12px 0; }
.error:empty { display: none; }
#notice:not(:empty) { padding: 12px 16px; border-left: 3px solid var(--accent); background: var(--panel); margin-bottom: 20px; }

/* Import */
#import-panel { margin-bottom: 26px; }
.inline-form { display: flex; align-items: end; gap: 12px; }
.inline-form label { flex: 1; }
.form-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; align-items: end; }
#import-form { border-top: 1px solid var(--border); margin-top: 24px; padding-top: 22px; }
#import-form .small { margin-top: 14px; }
.date-controls { display: flex; align-items: end; gap: 10px; flex-wrap: wrap; }
.date-controls .active { border-color: var(--accent); color: var(--accent); }
.date-controls label { min-width: 180px; }
.date-controls + p { margin-top: 16px; }
#race-choice-form { margin: 18px 0; }

/* History and results */
.dashboard { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 24px; }
.history { align-self: start; padding: 0; background: var(--panel); border: 1px solid var(--border); }
.history > .section-heading { margin: 0; padding: 15px 16px; border-bottom: 1px solid var(--border); }
.history h2 { font-size: 11px; text-transform: uppercase; letter-spacing: .09em; }
.badge { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.session-list { display: flex; flex-direction: column; }
.session-item { width: 100%; padding: 16px 14px; background: transparent; border: 0; border-bottom: 1px solid var(--border); border-left: 3px solid transparent; text-align: left; color: var(--text); overflow-wrap: anywhere; }
.session-item:hover { background: var(--hover); }
.session-item.selected { background: #253039; border-left-color: var(--accent); }
.session-item.selected strong { color: var(--accent); }
.session-item:last-child { border-bottom: 0; }
.session-list > p { padding: 16px; margin: 0; }
.session-item strong { display: block; margin-bottom: 5px; font-size: 13px; font-weight: 600; }
.session-item span { display: block; color: var(--muted); font-size: 11px; line-height: 1.8; }
.detail-column { min-width: 0; }
.detail-column .panel { margin-bottom: 20px; }
.detail-heading h2 { font-size: 25px; overflow-wrap: anywhere; }
.detail-heading .eyebrow { letter-spacing: .04em; font-family: var(--numeric); font-size: 10px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; flex-shrink: 0; max-width: 100%; }
.empty-state { min-height: 300px; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; padding: 36px; background: transparent; border-style: dashed; }
.empty-state p { max-width: 460px; }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-bottom: 20px; }
.metric { padding: 17px 14px; min-width: 0; border: 1px solid var(--border); background: var(--panel); }
.metric:first-child { border-top-color: var(--accent); }
.metric-label { display: block; color: var(--muted); font-size: 10px; margin-bottom: 10px; text-transform: uppercase; letter-spacing: .045em; }
.metric strong { font-family: var(--numeric); font-size: clamp(22px, 2.2vw, 30px); line-height: 1.15; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -1px; overflow-wrap: anywhere; }
.metric:first-child strong { color: var(--accent); }
.metric small { display: block; color: var(--muted); font-size: 11px; margin-top: 6px; }
.chart-panel { padding-bottom: 14px; }
#lap-chart { min-height: 170px; }
#lap-chart svg, #analytics-content svg { display: block; width: 100%; height: auto; }
.table-scroll { overflow-x: auto; scrollbar-color: #43535e var(--panel); }
table { border-collapse: collapse; width: 100%; white-space: nowrap; text-align: right; font-size: 12px; font-variant-numeric: tabular-nums; }
th { color: var(--muted); font-size: 10px; font-weight: 600; background: #11191e; }
td { font-family: var(--numeric); }
.panel h3 { font-size: 12px; letter-spacing: .04em; }
.chart-panel .section-heading { padding-bottom: 14px; border-bottom: 1px solid var(--border); }
#lap-chart svg text, #analytics-content svg text { font-family: var(--numeric); }
td, th { padding: 10px 10px; border-bottom: 1px solid var(--border); }
td:first-child, th:first-child { text-align: left; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--hover); }
.best-row { color: var(--accent); }
#compare-panel { margin-top: 28px; }
#comparison { margin-top: 20px; }
#comparison .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }

/* Analytics and settings */
.analytics-charts { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
#analytics-content .panel { margin-bottom: 20px; }
.chart-legend, .sector-legend { display: flex; flex-wrap: wrap; gap: 12px 18px; font-size: 11px; margin: 12px 0; }
.sector-all_time_best { color: #c4a0ff; }
.sector-session_best { color: #76df9b; }
.sector-session_worst { color: #f4cf65; }
.sector-all_time_worst { color: #ff858d; }
.sector-marker { font-size: 10px; margin-left: 5px; }
.sector-all_time_best, .sector-session_best, .sector-all_time_worst, .sector-session_worst { font-weight: 600; }
#analytics-track, #analytics-sector-mode { min-width: 180px; }
#settings-dialog { max-height: calc(100dvh - 32px); overflow-y: auto; color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 2px; padding: 26px; width: min(460px, 90vw); }
#settings-dialog::backdrop { background: #000a; }
#settings-dialog p { margin: 18px 0 0; }

@media (max-width: 1100px) {
  .dashboard { grid-template-columns: 230px minmax(0, 1fr); gap: 20px; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }


  .analytics-charts { grid-template-columns: 1fr; }
  .detail-heading { align-items: flex-start; flex-direction: column; gap: 12px; }
}

@media (max-width: 800px) {
  .auth-layout { grid-template-columns: 1fr 1fr; gap: 28px; margin-top: 24px; }
  .intro-guide > div { grid-template-columns: 1fr; gap: 4px; }
  .brand-caption { display: none; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid button { grid-column: 1 / -1; }
  #analytics-view > .section-heading { align-items: stretch; flex-direction: column; }
}

@media (max-width: 640px) {
  .topbar { padding: 12px 16px; gap: 12px; flex-wrap: wrap; }
  main { padding: 24px 16px 40px; }
  #account { gap: 8px; }
  #username { max-width: 110px; font-size: 13px; }
  #account .quiet { padding: 8px; }
  .auth-layout { grid-template-columns: 1fr; gap: 24px; margin: 0; }
  .intro { padding-top: 0; }
  .intro h1 { font-size: 28px; }
  .lead { font-size: 15px; margin-bottom: 0; }
  .intro-guide { display: none; }
  .page-heading { align-items: flex-start; flex-direction: column; gap: 16px; }
  .page-heading h1 { font-size: 27px; }
  .dashboard { grid-template-columns: minmax(0, 1fr); gap: 24px; }
  .history { padding: 0; }
  .session-list { max-height: 240px; overflow-y: auto; }
  .inline-form { align-items: stretch; flex-direction: column; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .panel { padding: 18px; }
  .empty-state { min-height: 240px; }
  .section-heading { gap: 12px; flex-wrap: wrap; }
  .metric { padding: 16px 10px; }
  .metric strong { font-size: 22px; }
  .date-controls label { flex: 1; }
  footer { padding: 18px 16px; flex-wrap: wrap; gap: 6px 20px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation: none !important; transition: none !important; }
}

#race-dialog { color: var(--text); background: var(--panel); border: 1px solid var(--border); border-radius: 2px; border-top: 2px solid var(--accent); padding: clamp(16px, 3vw, 28px); width: min(1120px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); overflow-y: auto; }
#race-dialog::backdrop { background: #000b; }
#race-dialog .section-heading { align-items: flex-start; flex-wrap: wrap; }
#race-meta { margin: 8px 0 0; }
#race-description { max-width: 90ch; line-height: 1.6; }
#race-content { margin-top: 20px; }
.race-own-row { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.race-own-row td:nth-child(2) { color: var(--accent); font-weight: 600; }
.race-lap-section { border-top: 1px solid var(--border); margin-top: 24px; padding-top: 24px; }
#race-entry { min-width: 180px; }

.auth-links { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
#challenge-form .wide { margin-top: 10px; }
.security-settings { border-top: 1px solid var(--border); padding-top: 16px; margin-top: 24px; }
.security-settings form { display: grid; gap: 14px; }
#totp-secret, #totp-uri { font-family: monospace; font-size: 14px; }
#totp-uri { width: 100%; min-height: 90px; resize: vertical; overflow-wrap: anywhere; color: var(--text); background: var(--panel); border: 1px solid var(--border); padding: 10px; }
.security-settings details { overflow-wrap: anywhere; }
.security-settings summary { cursor: pointer; margin-bottom: 12px; }

#totp-dialog { color: var(--text); background: var(--panel); border: 1px solid var(--border); padding: 24px; width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); overflow-y: auto; }
#totp-dialog::backdrop { background: #000b; }
#totp-dialog form { display: grid; gap: 14px; }
#totp-dialog .section-heading { flex-wrap: wrap; gap: 12px; }
.totp-methods { display: flex; flex-wrap: wrap; gap: 8px; margin: 18px 0; }
.totp-methods [aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
#totp-qr { display: block; width: min(100%, 300px); height: auto; margin: 0 auto; background: white; }
#totp-secret { width: 100%; }
#totp-enable-form { margin-top: 20px; }
