:root {
  --bg: #f2f3f5; --panel: #ffffff; --fg: #15161a; --muted: #6b6f7a; --line: #d9dbe1;
  --accent: #3f8cff; --ok: #3fb950; --bad: #e5484d; --tab: #e7e9ee;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #26272d; --panel: #2f3037; --fg: #ffffff; --muted: #9a9ca6; --line: #3d3e47; --tab: #1f2025;
    color-scheme: dark;
  }
}
:root[data-theme=dark] {
  --bg: #26272d; --panel: #2f3037; --fg: #ffffff; --muted: #9a9ca6; --line: #3d3e47; --tab: #1f2025;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--fg); font: 16px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h2, h3 { margin: 0; }
svg { width: 24px; height: 24px; fill: currentColor; }
.muted { color: var(--muted); }

#app { height: 100%; height: 100dvh; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
#status { display: flex; align-items: center; background: var(--tab); border-bottom: 1px solid var(--line); min-height: 40px; }
.lang { margin-right: 6px; gap: 4px; font-size: 13px; }
.lang svg { width: 20px; height: 20px; }
#chips { display: flex; flex: 1; justify-content: space-evenly; flex-wrap: wrap; gap: 4px 12px; padding: 6px 8px; }
.chip { background: none; border: 0; display: flex; align-items: center; gap: 8px; letter-spacing: .08em; font-size: 13px; text-transform: uppercase; }
.chip i { width: 14px; height: 14px; border-radius: 50%; background: var(--bad); display: inline-block; }
.chip.on i { background: var(--ok); }
.chip.wait i { background: #e9c23c; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }
.icon-btn { background: none; border: 0; padding: 8px; display: grid; place-items: center; border-radius: 8px; }
.icon-btn:hover { background: var(--line); }

main { flex: 1; min-height: 0; overflow: hidden; position: relative; }
.view { display: none; height: 100%; overflow: auto; padding: 12px 16px; }
.view.active { display: block; }
#view-home.active { display: grid; padding: 0; overflow-x: hidden; overflow-y: auto;
  grid-template-rows: auto minmax(70px, 1fr) minmax(80px, .8fr) auto auto; }

/* metrics */
.metrics { display: grid; grid-template-columns: 1fr 1fr 1.7fr 1fr; gap: 0;
  grid-template-areas: "plap pow itime hr" "slope tgt etime cad"; padding: 8px 4px; }
.m { text-align: center; padding: 4px 2px; min-width: 0; position: relative; cursor: help; }
#stats div[data-help] { cursor: help; }
.m label { display: block; font-size: clamp(12px, 2.2vw, 22px); text-transform: uppercase; }
.m b { display: block; font-size: clamp(30px, 7vw, 92px); font-weight: 700; line-height: 1.05; font-variant-numeric: tabular-nums; white-space: nowrap; }
.m small { font-size: .4em; margin-left: .3em; font-weight: 500; }
.m .zone { display: block; margin: 0; min-height: 1.3em; font-size: clamp(10px, 1.8vw, 18px); font-weight: 600; letter-spacing: .04em; text-transform: uppercase; white-space: nowrap; }
.prog { height: 6px; margin: 4px 12% 0; background: var(--line); border-radius: 3px; overflow: hidden; }
.prog i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 1s linear; }
.prog.warn i { background: #e9c23c; }
.m[style*=pow] b { font-size: clamp(40px, 10vw, 128px); transition: color .3s; }
.m[style*=pow] b[data-dev=ok] { text-shadow: 0 0 .4em color-mix(in srgb, var(--ok) 50%, transparent); }
.m[style*=pow] b[data-dev=high]::after { content: ' ▲'; font-size: .35em; }
.m[style*=pow] b[data-dev=low]::after { content: ' ▼'; font-size: .35em; }
.m .next { display: block; margin: 0; font-size: clamp(10px, 1.6vw, 14px); color: var(--muted); font-weight: 400; white-space: normal; line-height: 1.2; }
.m[style*=itime] b { font-size: clamp(32px, 6.5vw, 100px); }
.m[style*=etime] b { font-size: clamp(28px, 5.2vw, 84px); }

#pane { border-top: 1px solid var(--line); position: relative; min-height: 0; display: flex; flex-direction: column; }
#pages { flex: 1; min-height: 0; display: flex; overflow-x: auto; scroll-snap-type: x mandatory; scrollbar-width: none; }
#pages::-webkit-scrollbar { display: none; }
.page { flex: 0 0 100%; scroll-snap-align: start; position: relative; min-height: 0; display: flex; flex-direction: column; padding: 4px 8px; }
.page h3, #profile h3 { text-align: center; font-size: 14px; font-weight: 600; }
.page canvas { flex: 1; min-height: 0; width: 100%; }
#dots { position: absolute; top: -10px; left: 0; right: 0; display: flex; justify-content: center; gap: 36px; pointer-events: none; }
.dot { pointer-events: auto; width: 14px; height: 14px; border-radius: 50%; border: 1.5px solid var(--fg); background: var(--bg); padding: 0; }
.dot.on { background: var(--fg); }
#stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; align-content: center; flex: 1; text-align: center; }
#stats div b { display: block; font-size: 22px; }
#stats div span { font-size: 12px; color: var(--muted); text-transform: uppercase; }

#profile { border-top: 1px solid var(--line); position: relative; display: flex; flex-direction: column; padding: 4px 0 0; min-height: 0; }
#profile canvas { flex: 1; min-height: 0; width: 100%; }
#toast { position: absolute; left: 50%; top: 28px; z-index: 2; pointer-events: none; transform: translateX(-50%); background: var(--accent); color: #fff; padding: 6px 14px; border-radius: 20px; font-weight: 600; max-width: 90%; text-align: center; }

#controls { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 10px 8px; }
#modes { display: flex; align-items: center; gap: 4px; }
#modes > button:not(.icon-btn) { background: none; border: 0; padding: 6px 22px; font-size: 12px; letter-spacing: .02em; color: var(--muted); }
#modes > button.on { color: var(--fg); font-weight: 700; }
#adjust { display: flex; border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
#adjust button { width: 96px; height: 64px; touch-action: manipulation; user-select: none; background: var(--panel); border: 0; font-size: 22px; }
#adjust output { width: 104px; display: grid; place-items: center; background: #fff; color: #000; font-size: 22px; }
:root[data-theme=light] #adjust output { background: var(--panel); border-inline: 1px solid var(--line); color: var(--fg); }

#transport { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-top: 1px solid var(--line); }
#transport strong { letter-spacing: .02em; text-transform: uppercase; }
#tstate { flex: 1; color: var(--muted); font-size: 13px; }
.icon-btn.big svg { width: 32px; height: 32px; }
#btn-play.go { color: var(--ok); }

#tabs { display: flex; background: var(--tab); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
#tabs button { flex: 1; background: none; border: 0; padding: 8px 0 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 13px; letter-spacing: .04em; color: var(--muted); }
#tabs button.active { color: var(--fg); }

/* lists */
.toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar h2 { flex: 1; }
.btn { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; display: inline-block; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: .5; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; margin: 10px 0 24px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px; text-align: left; display: block; width: 100%; }
.cat { margin: 18px 0 0; font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.cat + .cards { margin-top: 8px; margin-bottom: 8px; }
.card h4 { margin: 0 0 2px; }
.card p { margin: 0; color: var(--muted); font-size: 13px; }
.card canvas { width: 100%; height: 44px; margin-top: 6px; display: block; }
.card .acts { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.card .acts button { padding: 4px 10px; font-size: 13px; }

/* settings */
form fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 0 0 14px; padding: 10px 14px; display: grid; gap: 10px; background: var(--panel); max-width: 560px; }
legend { padding: 0 6px; color: var(--muted); }
form label { display: grid; gap: 4px; font-size: 14px; }
form label.row, .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
input[type=number], input[type=text], input[type=password], input[type=url], select { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 8px; width: 100%; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg label { flex: 1; margin: 0; position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 14px 8px; font-size: 16px; cursor: pointer; background: var(--panel); }
.seg label + label span { border-left: 1px solid var(--line); }
.seg input:checked + span { background: var(--accent); color: #fff; font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
code { word-break: break-all; font-size: 12px; }

dialog { background: var(--panel); color: var(--fg); border: 1px solid var(--line); border-radius: 12px; max-width: min(560px, 94vw); width: 100%; }
dialog::backdrop { background: rgba(0,0,0,.5); }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.dlg-actions.wrap { flex-wrap: wrap; }
.srow { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.srow small { color: var(--muted); display: block; }
#c-preview { width: 100%; height: 100px; }
#c-builder { width: 100%; height: 90px; margin-top: 8px; }
.wb-row { border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin: 6px 0; }
.wb-row header { display: flex; align-items: center; gap: 6px; font-weight: 600; margin-bottom: 6px; }
.wb-row header span { flex: 1; }
.wb-row header button { padding: 2px 8px; }
.wb-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 6px; }
.wb-fields label { font-size: 12px; color: var(--muted); display: block; }
.wb-fields input { margin-top: 2px; }
.wb-add { display: flex; gap: 8px; margin-top: 8px; }
.wb-add select { width: auto; flex: 1; }
#wb-err { color: #e5484d; min-height: 1.2em; margin: 6px 0 0; }
#finish-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
#finish-stats b { display: block; font-size: 22px; }
#finish-stats span { font-size: 12px; color: var(--muted); text-transform: uppercase; }

@media (max-width: 560px) {
  .chip { font-size: 11px; gap: 5px; letter-spacing: .04em; }
  .chip i { width: 10px; height: 10px; }
  .metrics { grid-template-columns: repeat(6, minmax(0, 1fr)); padding: 4px 0;
    grid-template-areas: "pow pow pow hr hr hr" "itime itime itime cad cad cad" "tgt tgt tgt slope slope slope" "plap plap plap etime etime etime"; }
  .m { overflow: hidden; }
  .m label { font-size: clamp(9px, min(3vw, 1.7vh), 14px); }
  .m { padding: 2px 2px; }
  .m b, .m[style*=itime] b, .m[style*=etime] b { font-size: clamp(20px, min(9vw, 5vh), 52px); }
  .m::after { display: none; }
  #view-home.active { grid-template-rows: auto minmax(0, 1fr) minmax(0, .7fr) auto auto; overflow: hidden; }
  .page h3 { display: none; }
  #controls { gap: 6px; padding: 4px 8px; }
  #adjust button { height: 44px; }
  #transport { padding: 2px 12px; }
  #tabs button { padding: 5px 0 4px; }
  #modes > button:not(.icon-btn) { padding: 6px 12px; }
  #adjust button { width: 64px; }
}
@media (max-height: 430px) and (min-width: 561px) { .m b { font-size: clamp(22px, 5vw, 60px); } }


#view-settings > *, #view-workouts > * { max-width: 640px; margin-left: auto; margin-right: auto; }
#view-settings h2 { text-align: center; }
[hidden] { display: none !important; }

.sico { width: 26px; height: 26px; flex: none; fill: currentColor; color: var(--muted); }
.sname { flex: 1; min-width: 0; }

#dlg-wizard label { display: block; margin: 10px 0; }
#dlg-wizard input { width: 100%; box-sizing: border-box; }
.wiz-dots { display: flex; gap: 6px; justify-content: center; margin-bottom: 8px; }
.wiz-dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.wiz-dots i.on { background: var(--fg); }
.wiz-list { padding-left: 20px; line-height: 1.5; }
.wiz-workouts { display: grid; gap: 6px; max-height: 50vh; overflow: auto; }
.wiz-workouts button { text-align: left; }
.wiz-workouts button.on { outline: 2px solid var(--ok); }
.wiz-workouts small { display: block; color: var(--muted); }

#p-laps { overflow: auto; }
#laps table { width: 100%; border-collapse: collapse; text-align: center; font-variant-numeric: tabular-nums; }
#laps th { font-size: 11px; color: var(--muted); text-transform: uppercase; font-weight: 500; padding: 2px 4px; position: sticky; top: 0; background: var(--bg); }
#laps td { padding: 5px 4px; border-top: 1px solid var(--line); }
#laps tr.cur td { font-weight: 600; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* chips with live values */
.chip .cv { color: var(--muted); letter-spacing: 0; text-transform: none; font-variant-numeric: tabular-nums; }
.chip.on .cv { color: var(--fg); }

/* next-interval banner and tooltip dot */
#next-banner { position: absolute; left: 8px; right: 8px; top: 6px; z-index: 1; padding: 6px 12px; border-radius: 10px; text-align: center; font-weight: 700;
  font-size: clamp(18px, 3.4vw, 34px); background: var(--accent); color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.35); font-variant-numeric: tabular-nums; pointer-events: none; }
#next-banner[data-dir=up] { background: #d9622b; }
#next-banner[data-dir=down] { background: #2f9e44; }
.tdot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; border: 1.5px solid #fff; vertical-align: baseline; }
#c-profile.jumpable { cursor: pointer; }

/* pre-ride screen */
#preride { position: absolute; inset: 0; z-index: 6; background: var(--bg); overflow: auto; display: grid; place-items: center; padding: 16px; }
#view-home:not(.active) ~ #preride { display: none; }
.pre-card { width: min(560px, 100%); display: grid; gap: 12px; text-align: center; }
.pre-card p { margin: 0; }
#pre-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
#pre-stats b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
#pre-stats span { font-size: 12px; color: var(--muted); text-transform: uppercase; }
#c-pre { width: 100%; height: 110px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.big-start { font-size: 20px; font-weight: 700; padding: 16px; }

/* start countdown */
#countdown { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.78); color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: pointer; }
#countdown b { font-size: min(45vw, 45vh, 320px); line-height: 1; font-variant-numeric: tabular-nums; animation: cdpop 1s ease-out; }
#countdown small { font-size: 16px; opacity: .7; margin-top: 12px; }
@keyframes cdpop { from { transform: scale(1.4); opacity: .2; } to { transform: scale(1); opacity: 1; } }

/* empty states */
.empty { grid-column: 1 / -1; border: 1px dashed var(--line); border-radius: 10px; padding: 18px; text-align: center; color: var(--muted); display: grid; gap: 4px; }
.empty b { color: var(--fg); font-weight: 600; }

/* ride summary */
#c-finish { width: 100%; height: 90px; display: block; margin-top: 10px; background: var(--bg); border-radius: 8px; }
#finish-stats { gap: 10px 8px; }
#finish-zones { display: grid; gap: 4px; margin-top: 10px; }
.zrow { display: grid; grid-template-columns: 28px 1fr 92px; gap: 8px; align-items: center; font-size: 12px; }
.zn { font-weight: 700; }
.zbar { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.zbar i { display: block; height: 100%; }
.zt { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.field-label { font-size: 14px; }

/* motion */
.view.active { animation: viewin .18s ease-out; }
@keyframes viewin { from { opacity: 0; transform: translateY(4px); } }
.btn, .icon-btn, #adjust button, #tabs button { transition: transform .08s, background-color .15s, color .15s; }
.btn:active, .icon-btn:active { transform: scale(.96); }
#adjust button:active { background: var(--line); }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* landscape tablets / desktop: metrics left, charts right */
@media (orientation: landscape) and (min-width: 900px) and (min-height: 520px) {
  #view-home.active { grid-template-columns: minmax(340px, 40%) 1fr; grid-template-rows: minmax(0, 1fr) minmax(0, .55fr) auto auto; overflow: hidden;
    grid-template-areas: "metrics pane" "metrics profile" "controls controls" "transport transport"; }
  #metrics { grid-area: metrics; grid-template-columns: 1fr 1fr; align-content: space-evenly; border-right: 1px solid var(--line); padding: 8px;
    grid-template-areas: "pow pow" "itime itime" "tgt hr" "slope cad" "plap etime"; }
  #pane { grid-area: pane; border-top: 0; }
  #dots { top: 8px; }
  .page { padding-top: 30px; }
  #profile { grid-area: profile; }
  #controls { grid-area: controls; flex-direction: row; justify-content: center; flex-wrap: wrap; gap: 24px; }
  #transport { grid-area: transport; }
  #metrics .m b { font-size: clamp(28px, 3.6vw, 64px); }
  #metrics .m[style*=pow] b { font-size: clamp(56px, 8vw, 140px); }
  #metrics .m[style*=itime] b { font-size: clamp(40px, 5.6vw, 100px); }
  #metrics .m[style*=etime] b { font-size: clamp(28px, 3.6vw, 64px); }
}
