:root {
  --bg: #0f1216;
  --panel: #171b21;
  --panel2: #1e232b;
  --line: #2a313b;
  --text: #e8ecf1;
  --muted: #8a96a6;
  --accent: #3d8bfd;
  --good: #2fbf71;
  --warn: #f0a030;
  --bad: #e5484d;
}
* { box-sizing: border-box; }
html, body { overscroll-behavior-y: none; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
body { display: flex; flex-direction: column; overflow: hidden; user-select: none; }

.bar { display: flex; align-items: center; gap: 12px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { font-weight: 700; letter-spacing: .02em; }
.route-name { color: var(--muted); flex: 1; min-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.btn { display: inline-flex; align-items: center; gap: 6px; background: var(--panel2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; font: inherit; font-size: 15px; cursor: pointer; min-height: 40px; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.active { border-color: var(--accent); color: var(--accent); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #555; display: inline-block; }
.dot.connected { background: var(--good); }
.dot.connecting, .dot.reconnecting { background: var(--warn); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }

.layout { flex: 1; min-height: 0; display: grid; gap: 8px; padding: 8px;
  grid-template-columns: 1.1fr 1fr; grid-template-rows: 1.3fr .8fr .7fr;
  grid-template-areas: "map tiles" "next tiles" "profile profile"; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; position: relative; min-height: 0; overflow: hidden; }
.panel canvas { width: 100%; height: 100%; display: block; }
.map-wrap { grid-area: map; }
.next { grid-area: next; display: flex; flex-direction: column; }
.next canvas { flex: 1; min-height: 0; }
.profile { grid-area: profile; }
.panel-title { font-size: 12px; color: var(--muted); padding: 6px 10px 0; text-transform: uppercase; letter-spacing: .06em; }
.overlay-msg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 24px; color: var(--muted); font-size: 16px; }

.tiles { grid-area: tiles; display: grid; gap: 8px; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr; min-height: 0; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px;
  display: flex; flex-direction: column; justify-content: center; min-width: 0; min-height: 0; }
.tile .lbl { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.tile .val { font-size: clamp(22px, 3.6vw, 44px); font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.tile.big .val { font-size: clamp(28px, 5vw, 64px); }
.tile .unit { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.demo-panel { position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); background: var(--panel2);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; width: min(520px, 90vw); z-index: 5; }
.demo-panel label { display: block; }
.demo-panel input { width: 100%; margin-top: 6px; }

.toast { position: fixed; top: 70px; left: 50%; transform: translateX(-50%); background: #2a2f38; border: 1px solid var(--line);
  padding: 10px 16px; border-radius: 10px; z-index: 10; max-width: 90vw; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 0; width: min(440px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.6); }
.settings-form { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.settings-form h2 { margin: 0 0 4px; font-size: 20px; }
.settings-form label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
.settings-form input[type=number], .settings-form input[type=date] { background: var(--panel2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 8px; font: inherit; }
.settings-form small { color: var(--muted); }
.settings-form .row.wrap { flex-wrap: wrap; }
.settings-form .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* Portrait or narrow screens: stack everything */
@media (orientation: portrait), (max-width: 760px) {
  body { overflow: auto; }
  .layout { flex: none; grid-template-columns: 1fr; grid-template-rows: 280px auto 160px 140px;
    grid-template-areas: "map" "tiles" "next" "profile"; }
  .tiles { grid-auto-rows: 84px; }
}

/* ---------- phones ---------- */
.main-actions { display: flex; gap: 6px; align-items: center; }
.menu-btn { display: none; }
.menu-btn .dot { margin-right: 2px; }

/* Compact top bar: dRide, status, Start, and a menu with everything else */
@media (max-width: 760px), (max-height: 500px) {
  .bar { flex-wrap: nowrap; gap: 8px; padding: 6px 10px; padding-top: max(6px, env(safe-area-inset-top)); position: relative; z-index: 1100; }
  .route-name { font-size: 13px; min-width: 0; }
  .menu-btn { display: inline-flex; font-size: 20px; padding: 6px 10px; }
  .actions { display: none; }
  body.menu-open .actions { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px;
    position: absolute; top: 100%; left: 0; right: 0; padding: 10px; background: var(--panel); border-bottom: 1px solid var(--line);
    box-shadow: 0 12px 24px rgba(0,0,0,.5); max-height: calc(100dvh - 60px); overflow: auto; }
  .actions .btn { justify-content: center; min-height: 46px; }
  .btn.primary { min-width: 84px; justify-content: center; }
  /* dialogs: never taller than the screen */
  dialog { max-height: calc(100dvh - 16px); overflow: auto; }
  .wo-body { max-height: none; }
  .map-btns .btn.sm { padding: 4px 8px; font-size: 13px; }
}

/* Phone, portrait: tiles first (what you look at while riding), then the charts, then the map */
@media (max-width: 560px) and (orientation: portrait) {
  .brand { font-size: 17px; }
  .layout { gap: 6px; padding: 6px; grid-template-rows: auto 190px 240px 130px; grid-template-areas: "tiles" "next" "map" "profile"; }
  .tiles { gap: 6px; grid-auto-rows: 84px; }
  .tile { padding: 4px 8px; }
  .tile .lbl, .tile .unit { font-size: 11px; }
  .tile .val { font-size: 30px; }
  .tile.big .val { font-size: 38px; }
  .ghost-info { float: none; display: block; margin-top: 2px; font-size: 13px; }
  .wo-ctl { float: none; display: flex; margin: 4px 0 0; }
}

/* Phone, landscape (on a bike mount): map + chart on the left, 9 key tiles on the right, no full-route profile */
@media (max-height: 500px) and (orientation: landscape) {
  body { overflow: hidden; }
  .brand { font-size: 16px; }
  .layout { flex: 1; gap: 6px; padding: 6px; grid-template-columns: 1.1fr 1fr; grid-template-rows: 1.15fr 1fr;
    grid-template-areas: "map tiles" "next tiles"; }
  .profile { display: none; }
  .tiles { gap: 6px; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr; }
  .tile.minor { display: none; }
  .tile { padding: 2px 8px; }
  .tile .lbl, .tile .unit { font-size: 10px; }
  .tile .val { font-size: min(9vh, 34px); }
  .tile.big .val { font-size: min(12vh, 46px); }
  .panel-title { padding: 4px 8px 0; font-size: 11px; }
  .map-legend { display: none; }
  .demo-panel { bottom: 6px; left: 6px; transform: none; width: 46vw; padding: 6px 12px; }
  body.wo-noroute .map-wrap { display: none; }
  body.wo-noroute .layout { grid-template-areas: "next tiles" "next tiles"; }
}
@media (max-width: 560px) and (orientation: portrait) {
  body.wo-noroute .map-wrap { display: none; }
  body.wo-noroute .layout { grid-template-rows: auto 220px 150px; grid-template-areas: "tiles" "next" "profile"; }
}
.settings-form .hint { color: var(--muted); font-size: 13px; margin: 0; }
a.btn { text-decoration: none; }

/* ---------- workouts ---------- */
.wo-dlg { width: min(680px, 94vw); }
.wo-body { padding: 12px 18px 18px; display: flex; flex-direction: column; gap: 8px; max-height: 78vh; overflow: auto; }
.wo-item { display: grid; grid-template-columns: 96px 1fr auto; align-items: center; gap: 4px 12px; width: 100%; text-align: left;
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; color: var(--text); font: inherit; cursor: pointer; }
.wo-item:hover, .wo-item:focus { border-color: var(--accent); outline: none; }
.wo-item.today { border-color: var(--accent); }
.wo-item.past { opacity: .6; }
.wo-item .d { color: var(--muted); font-size: 13px; }
.wo-item .t { font-size: 15px; }
.wo-item .m { color: var(--muted); font-size: 13px; white-space: nowrap; }
.wo-item svg { grid-column: 1 / -1; width: 100%; height: 22px; display: block; }
.wo-week { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; margin-top: 6px; }
.wo-list { display: flex; flex-direction: column; gap: 6px; }
.wo-ctl { float: right; display: inline-flex; align-items: center; gap: 6px; text-transform: none; letter-spacing: 0; margin-left: 8px; }
.wo-ctl .btn.sm { min-height: 28px; padding: 2px 8px; font-size: 13px; }
.wo-ctl b { color: var(--text); font-size: 13px; min-width: 36px; text-align: center; }
#woBtn.active { border-color: var(--accent); color: var(--accent); }

/* ---------- my workouts ---------- */
.wo-row { display: grid; grid-template-columns: 1fr auto; gap: 6px; align-items: center; }
.wo-x { width: 32px; height: 32px; font-size: 18px; }

/* ---------- plans ---------- */
.plan-bar { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.plan-bar select { flex: 1; min-width: 180px; background: var(--panel2); color: var(--text); border: 1px solid var(--accent);
  border-radius: 8px; padding: 8px 10px; font: inherit; }
.gen-dlg { width: min(640px, 94vw); }
.settings-form select, .settings-form input[type=text] { background: var(--panel2); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 8px; font: inherit; }
.settings-form label.chk { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.settings-form .hint-small { color: var(--muted); font-size: 12px; margin-top: -6px; }
.settings-form label.chk input { width: 18px; height: 18px; accent-color: var(--accent); }

/* ---------- Bluetooth log ---------- */
.bt-dlg { width: min(760px, 96vw); }
#btLogText { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin: 0;
  max-height: 55vh; overflow: auto; white-space: pre-wrap; word-break: break-all; font: 12px/1.45 ui-monospace, Consolas, monospace; color: var(--text); user-select: text; }

/* ---------- help ---------- */
.help-dlg { width: min(860px, 96vw); height: min(92dvh, 1000px); max-height: none; padding: 0; display: none; flex-direction: column; overflow: hidden; }
.help-dlg[open] { display: flex; }
.help-dlg .dlg-head { gap: 12px; }
.help-dlg .dlg-head h2 { flex: 1; }
.help-dlg iframe { flex: 1; width: 100%; border: 0; background: var(--bg); }
#wHelp { color: #9cc3ff; }
@media (max-width: 760px), (max-height: 500px) {
  .help-dlg { width: 100vw; height: 100dvh; max-width: 100vw; margin: 0; border-radius: 0; border: 0; }
  .help-dlg .dlg-head { padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); }
}

/* ---------- welcome ---------- */
.welcome-dlg { width: min(620px, 94vw); }
.w-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.w-grid input[type=range] { width: 100%; }
.w-steps { margin: 0; padding-left: 20px; color: var(--muted); font-size: 14px; line-height: 1.6; }
.w-steps b { color: var(--text); }
@media (max-width: 560px) { .w-grid { grid-template-columns: 1fr; } }

/* ---------- Garmin import ---------- */
.garmin-dlg { width: min(640px, 94vw); }
.g-setup, .g-list { padding: 14px 18px 18px; display: flex; flex-direction: column; gap: 10px; }
.g-setup p, .g-setup ol { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.5; }
.g-setup a { color: #9cc3ff; }
#gKey { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 10px; font: inherit; }
.g-filters { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.g-filters input, .g-filters select { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 10px; font: inherit; font-size: 14px; min-width: 0; }
.g-filters input { grid-column: 1 / -1; }
.g-filters .on { border-color: var(--accent); }
#gList .g-loc { color: var(--muted); font-size: 12px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 560px) { .g-filters { grid-template-columns: 1fr 1fr; } }
.g-status { color: var(--muted); font-size: 14px; min-height: 1.2em; }
#gList { list-style: none; margin: 0; padding: 0; max-height: 55vh; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
#gList:empty { display: none; }
#gList li { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
#gList li:last-child { border-bottom: none; }
#gList li:hover, #gList li:focus { background: var(--panel2); outline: none; }
#gList .g-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#gList .g-date { color: var(--muted); font-size: 13px; width: 92px; flex: none; }
#gList .g-num { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; flex: none; }
.g-list .row { display: flex; justify-content: space-between; gap: 8px; }

/* ---------- ghost ---------- */
.ghost-info { float: right; text-transform: none; letter-spacing: 0; font-size: 14px; color: #c8aaff; font-weight: 600; }
.ghost-info .ahead { color: var(--good); }
.ghost-info .behind { color: var(--bad); }
.ghost-info .dim { color: var(--muted); font-weight: 400; }

/* ---------- map ---------- */
.leaflet-host { position: absolute; inset: 0; background: var(--panel); }
.leaflet-container { font: 12px system-ui, sans-serif; }
.leaflet-control-attribution { font-size: 10px; background: rgba(15,18,22,.7) !important; color: var(--muted); }
.leaflet-control-attribution a { color: #9cc3ff; }
.leaflet-bar a { background: var(--panel2); color: var(--text); border-color: var(--line); width: 34px; height: 34px; line-height: 34px; }
.leaflet-bar a:hover { background: #2a313b; color: var(--text); }
.map-btns { position: absolute; top: 8px; right: 8px; z-index: 1000; display: flex; gap: 6px; }
.btn.sm { min-height: 34px; padding: 4px 10px; font-size: 14px; background: rgba(30,35,43,.92); }
.map-legend { position: absolute; left: 8px; bottom: 8px; z-index: 1000; display: flex; align-items: center; gap: 4px;
  background: rgba(15,18,22,.8); border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; font-size: 11px; color: var(--text); }
.map-legend i { display: inline-block; width: 12px; height: 8px; border-radius: 2px; }
.map-legend i.ramp { width: 80px; background: linear-gradient(90deg, hsl(225,85%,52%), hsl(170,85%,52%), hsl(110,85%,52%), hsl(55,85%,52%), hsl(0,85%,52%)); }
.map-legend span { margin-right: 4px; }

.map-settings { width: min(600px, 94vw); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { margin: 0; font-size: 18px; }
.icon-btn { background: var(--panel2); border: 1px solid var(--line); color: var(--text); width: 34px; height: 34px; border-radius: 50%;
  font-size: 20px; line-height: 1; cursor: pointer; }
.ms-body { display: flex; gap: 20px; padding: 16px 18px 4px; flex-wrap: wrap; }
.ms-col { display: flex; flex-direction: column; gap: 8px; flex: 1; min-width: 200px; padding: 0 18px 0 0; }
.ms-body .ms-col { padding: 0; }
.map-settings > .ms-col { padding: 4px 18px 18px; }
.ms-label { font-weight: 600; font-size: 14px; }
.ms-sep { height: 1px; background: var(--line); margin: 12px 18px; }
#msProvider { background: var(--panel2); color: var(--text); border: 1px solid var(--accent); border-radius: 8px; padding: 9px 10px; font: inherit; }
.ms-types { display: flex; gap: 10px; flex-wrap: wrap; }
.ms-type { background: none; border: none; padding: 0; color: var(--muted); font: inherit; font-size: 13px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px; }
.ms-type .thumb { position: relative; width: 64px; height: 64px; border-radius: 8px; overflow: hidden; border: 2px solid var(--line); background: #2a313b; }
.ms-type .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.ms-key { display: flex; flex-direction: column; gap: 6px; }
.ms-key[hidden] { display: none; }
.link-btn { background: none; border: none; padding: 4px 0; color: #9cc3ff; font: inherit; font-size: 14px; text-align: left; cursor: pointer; text-decoration: underline; }
#msGoogleKey { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; font: inherit; }
.ms-key code { color: var(--text); }
.g-logo { font-weight: 600; color: #fff; }
.ms-type .thumb.plain { background: linear-gradient(135deg, #171b21, #2a313b); }
.ms-type.sel { color: var(--accent); }
.ms-type.sel .thumb { border-color: var(--accent); }
.ms-radios { display: grid; grid-template-columns: repeat(3, auto); gap: 10px 20px; justify-content: start; }
.ms-radios label { display: flex; align-items: center; gap: 8px; font-size: 15px; cursor: pointer; }
.ms-radios input { width: 18px; height: 18px; accent-color: var(--accent); }
