:root {
  --ink: #f4f7fa;
  --muted: #a9b8c6;
  --panel: rgba(10, 22, 34, 0.72);
  --panel-solid: #0e1c2a;
  --line: rgba(255, 255, 255, 0.16);
  --accent: #ffc21a;
  --accent-ink: #1b1400;
  --danger: #ff6a5a;
  --bg: #0b1622;
  --hud-h: 46px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink); font-family: var(--sans); }
body { user-select: none; -webkit-user-select: none; }
[hidden] { display: none !important; }

#view { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; transition: filter 0.4s; }
#view.dim { filter: saturate(0.9) brightness(0.8); }

.key {
  display: inline-block; min-width: 1.6em; padding: 0 0.35em; border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 5px; font: 600 0.8em/1.5 var(--mono); text-align: center; background: rgba(255, 255, 255, 0.08);
  color: var(--ink); margin: 0 0.1em;
}

#status {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); padding: 14px 20px;
  background: var(--panel); border-radius: 10px; font-size: 15px;
}

#lock-hint {
  position: fixed; right: 12px; bottom: calc(var(--hud-h) + 10px); font-size: 12px; color: var(--muted);
  background: rgba(10, 22, 34, 0.55); padding: 4px 10px; border-radius: 999px; pointer-events: none;
  opacity: 0; transition: opacity 0.3s;
}
body[data-draw="2"]:not(.locked) #lock-hint { opacity: 1; }

/* ---- HUD ---- */
#hud { position: fixed; inset: 0; pointer-events: none; font-variant-numeric: tabular-nums; }
.hud-bar {
  position: absolute; left: 0; right: 0; bottom: 0; height: var(--hud-h); display: flex; align-items: stretch;
  background: linear-gradient(to top, rgba(6, 14, 22, 0.92), rgba(6, 14, 22, 0.78)); border-top: 1px solid var(--line);
  font: 600 17px/1 var(--mono);
}
.hud-bar .cell { display: flex; align-items: center; gap: 6px; padding: 0 14px; border-right: 1px solid var(--line); white-space: nowrap; }
.hud-bar small { font: 500 11px var(--sans); color: var(--muted); letter-spacing: 0.04em; }
.hud-bar .mode { background: var(--accent); color: var(--accent-ink); min-width: 92px; justify-content: center; letter-spacing: 0.08em; }
.hud-bar .ico { width: 14px; height: 14px; flex: none; }
.hud-bar .speed, .hud-bar .hdg, .hud-bar .heel, .hud-bar .tws, .hud-bar .ap-target { display: inline-block; text-align: right; }
.hud-bar .speed { min-width: 3ch; }
.hud-bar .hdg, .hud-bar .tws, .hud-bar .ap-target { min-width: 3ch; }
.hud-bar .heel { min-width: 4ch; }   /* "+14°": the cells keep one width as the digits change */
.hud-bar .sail { display: flex; align-items: center; gap: 12px; padding: 0 14px; font-size: 15px; }
.sail-bar { width: 150px; height: 10px; border: 1px solid var(--ink); border-radius: 3px; padding: 1px; }
.sail-bar .fill { height: 100%; background: var(--ink); border-radius: 2px; width: 0; }
.hud-bar .spacer { flex: 1; }
.hud-bar .icon { font-size: 20px; border-left: 1px solid var(--line); border-right: 0; }
.hud-bar .ap .needle { display: inline-block; transform: rotate(-90deg); font-size: 15px; }
.hud-bar .dial-cell { border-left: 1px solid var(--line); border-right: 0; }
.dial { width: 34px; height: 34px; }
.dial .nogo { fill: var(--ink); opacity: 0.3; stroke: none; }
.dial .ring { fill: none; stroke: var(--ink); stroke-width: 1.4; }
.dial .bow { stroke: var(--accent); stroke-width: 2; }
.dial .wind { stroke: #7fd0ff; stroke-width: 2.4; stroke-linecap: round; }

.hud-banners {
  position: absolute; left: 50%; bottom: calc(var(--hud-h) + 14px); transform: translateX(-50%); white-space: pre-line;
  text-align: center; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 14px;
  font-size: 14px; line-height: 1.5;
}
.hud-praise {
  position: absolute; left: 50%; top: 14%; transform: translateX(-50%); font: 800 clamp(22px, 4vw, 40px)/1.1 var(--sans);
  letter-spacing: 0.02em; text-shadow: 0 2px 10px rgba(0, 0, 0, 0.55); animation: pop 0.35s ease-out;
}
@keyframes pop { from { transform: translateX(-50%) scale(0.8); opacity: 0; } }
.hud-top { position: absolute; left: 0; right: 0; top: 10px; display: flex; justify-content: center; gap: 10px; }
.hud-clock, .hud-ghost {
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 5px 14px; font: 600 15px var(--mono);
}
.hud-clock { position: absolute; left: 12px; }
.hud-warning {
  position: absolute; left: 12px; right: 12px; top: 50px; padding: 6px 10px; background: rgba(120, 30, 20, 0.8);
  border-radius: 6px; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.hud-box {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%); min-width: 300px; text-align: center;
  background: var(--panel); backdrop-filter: blur(6px); border: 1px solid var(--line); border-radius: 14px; padding: 20px 28px;
}
.hud-box .head { font: 800 26px var(--sans); letter-spacing: 0.1em; }
.hud-box .line { margin-top: 8px; font: 600 18px var(--mono); }
.hud-box .line:empty { display: none; }
.hud-box .act { margin-top: 14px; color: var(--muted); font-size: 14px; }

.hud-marker, .hud-ghost-arrow { position: absolute; left: 0; top: 0; will-change: transform; }
.hud-marker .tri, .hud-ghost-arrow .tri {
  position: absolute; left: -9px; top: -9px; width: 0; height: 0; border-left: 9px solid transparent;
  border-right: 9px solid transparent; border-top: 15px solid var(--accent); filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));
}
.hud-marker.on .tri { top: -17px; }
.hud-marker:not(.on) .tri, .hud-ghost-arrow .tri {
  /* an arrow pointing right, rotated toward the target */
  left: -8px; top: -9px; border-top: 9px solid transparent; border-bottom: 9px solid transparent;
  border-left: 15px solid var(--accent); border-right: 0;
}
.hud-ghost-arrow .tri { border-left-color: #cfe6f6; }
.hud-marker .lab, .hud-ghost-arrow .lab {
  position: absolute; left: 14px; top: -10px; font: 600 13px var(--mono); white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.hud-marker.on .lab { left: 0; top: -40px; transform: translateX(-50%); }

/* ---- main menu ---- */
#menu { position: fixed; inset: 0; display: flex; flex-direction: column; }
.menu-card {
  flex: 1; display: grid; grid-template-columns: minmax(220px, 1fr) minmax(280px, 1.25fr); align-items: center; gap: 4vw;
  width: min(980px, 100%); margin: 0 auto; padding: 6vh 32px 24px;
}
.menu-title .eyebrow { color: var(--muted); font-size: 14px; letter-spacing: 0.06em; min-height: 1.2em; }
.menu-title h1 {
  margin: 6px 0 12px; font: 800 clamp(34px, 6vw, 64px)/1 var(--sans); letter-spacing: -0.01em; text-transform: capitalize;
  text-shadow: 0 3px 18px rgba(0, 0, 0, 0.35);
}
.menu-title .rule { height: 5px; border-top: 1px solid var(--ink); border-bottom: 1px solid var(--ink); width: 70%; }
.menu-title { padding: 22px 26px; border-radius: 16px; background: rgba(8, 18, 28, 0.38); backdrop-filter: blur(3px); }
.menu-title .sub { margin-top: 12px; font-size: 17px; color: #d6e2ec; }
.menu-title .extra { margin-top: 26px; font: 600 15px/1.6 var(--mono); }
.menu-list { display: flex; flex-direction: column; gap: 6px; }
.menu-list .item {
  position: relative; padding: 12px 18px 12px 44px; border-radius: 10px; font: 650 clamp(18px, 2.4vw, 24px) var(--sans);
  background: rgba(10, 22, 34, 0.45); border: 1px solid transparent; text-transform: none;
}
.menu-list .item.sel { background: var(--ink); color: #0b1622; box-shadow: 0 6px 22px rgba(0, 0, 0, 0.35); }
.menu-list .item.sel::before {
  content: ""; position: absolute; left: 18px; top: 50%; transform: translateY(-50%); border-left: 10px solid #0b1622;
  border-top: 7px solid transparent; border-bottom: 7px solid transparent;
}
.menu-list .item.off { opacity: 0.38; }
.menu-list .item:not(.off) { cursor: pointer; }
.menu-list .item:not(.sel):not(.off):hover { border-color: var(--line); }
.menu-list .more, .help-body .more { cursor: pointer; }
.menu-list .more { text-align: center; color: var(--muted); font-size: 12px; }
.menu-hints {
  display: flex; align-items: center; gap: 22px; padding: 14px 28px; background: rgba(6, 14, 22, 0.85);
  border-top: 1px solid var(--line); font: 600 13px var(--sans); letter-spacing: 0.08em;
}
.menu-hints .grow { flex: 1; }
.menu-hints .hint { display: inline-flex; align-items: center; gap: 6px; }
.menu-hints button.hint {
  font: inherit; letter-spacing: inherit; color: inherit; background: none; border: 1px solid transparent; border-radius: 8px;
  padding: 4px 8px; margin: -5px -8px; cursor: pointer;
}
.menu-hints button.hint:hover, .menu-hints button.hint:focus-visible { background: rgba(255, 255, 255, 0.1); border-color: var(--line); }
.menu-hints button.about { margin-left: 0; color: var(--accent); text-transform: uppercase; }
.menu-hints .key.a { background: var(--ink); color: #0b1622; }
.menu-preview { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 10px; height: 100%; min-height: 0; }
.pv-head { display: flex; align-items: baseline; justify-content: space-between; }
.pv-head h2 { margin: 0; font: 800 clamp(24px, 4vw, 38px) var(--sans); }
.pv-best { font: 600 16px var(--mono); color: var(--muted); }
.menu-preview canvas {
  flex: 1; cursor: pointer; width: 100%; min-height: 240px; background: rgba(8, 30, 48, 0.72); border: 1px solid var(--line); border-radius: 14px;
}
.pv-info { color: var(--muted); font-size: 15px; }
.menu-help {
  grid-column: 1 / -1; align-self: start; background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 18px 24px; max-height: 100%; overflow: hidden;
}
.menu-help h2 { margin: 0 0 10px; font-size: 22px; letter-spacing: 0.08em; text-transform: uppercase; }
.help-keys { margin-bottom: 12px; padding: 8px 12px; background: rgba(255, 194, 26, 0.12); border-radius: 8px; font-size: 14px; line-height: 1.8; }
.help-body { font: 15px/1.55 var(--mono); }
.help-body .h-head { margin-top: 6px; padding: 2px 8px; background: var(--ink); color: #0b1622; font-weight: 700; border-radius: 4px; }
.help-body .h-bullet { padding-left: 22px; position: relative; }
.help-body .h-bullet.first::before { content: "–"; position: absolute; left: 6px; }
.help-body .more { text-align: center; color: var(--muted); }

/* ---- system menu ---- */
#sysmenu { position: fixed; inset: 0; z-index: 5; background: rgba(3, 8, 14, 0.45); display: flex; justify-content: flex-end; }
.sys-panel {
  width: min(380px, 100%); height: 100%; overflow: auto; background: var(--panel-solid); border-left: 1px solid var(--line);
  padding: 24px; display: flex; flex-direction: column; gap: 14px; user-select: auto;
}
.sys-panel h2 { margin: 0; font-size: 22px; letter-spacing: 0.1em; text-transform: uppercase; }
.sys-panel h3 { margin: 10px 0 0; font-size: 13px; color: var(--muted); letter-spacing: 0.08em; text-transform: uppercase; }
.sys-rows { font: 15px/1.7 var(--mono); padding: 10px 12px; background: rgba(255, 255, 255, 0.05); border-radius: 8px; }
.sys-picks { display: flex; flex-direction: column; gap: 10px; }
.sys-panel label { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 15px; }
.sys-panel select, .sys-panel button {
  font: inherit; color: var(--ink); background: rgba(255, 255, 255, 0.08); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 12px;
}
.sys-panel select { min-width: 160px; text-transform: capitalize; }
.sys-panel button { cursor: pointer; text-align: left; }
.sys-panel button:hover, .sys-panel select:hover { background: rgba(255, 255, 255, 0.14); }
.sys-panel .sys-back { color: var(--danger); }
.sys-panel .sys-resume { margin-top: auto; background: var(--ink); color: #0b1622; font-weight: 700; display: flex; justify-content: space-between; }
.sys-panel .range input { flex: 1; }
.sys-panel .range span { font: 13px var(--mono); color: var(--muted); min-width: 64px; text-align: right; }
.sys-panel .check { justify-content: flex-start; }

/* ---- about ---- */
#about { position: fixed; inset: 0; z-index: 10; background: rgba(3, 8, 14, 0.6); display: flex; align-items: center; justify-content: center; padding: 16px; }
.about-panel {
  position: relative; width: min(720px, 100%); max-height: 100%; overflow: auto; background: var(--panel-solid);
  border: 1px solid var(--line); border-radius: 14px; padding: 28px 32px; user-select: text; -webkit-user-select: text;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.5);
}
.about-close {
  position: sticky; top: 0; float: right; margin: -12px -16px 0 12px; width: 36px; height: 36px; border-radius: 999px;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); color: var(--ink); font-size: 16px; cursor: pointer;
}
.about-close:hover { background: rgba(255, 255, 255, 0.16); }
.about-body { font-size: 16px; line-height: 1.6; color: #d6e2ec; }
.about-body h1 { margin: 0 0 14px; font: 800 clamp(28px, 4vw, 38px)/1.1 var(--sans); color: var(--ink); }
.about-body h2 { margin: 24px 0 8px; font-size: 15px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); }
.about-body h3 { margin: 18px 0 6px; font-size: 16px; color: var(--ink); }
.about-body p { margin: 0 0 12px; }
.about-body ul, .about-body ol { margin: 0 0 12px; padding-left: 22px; }
.about-body li { margin: 4px 0; }
.about-body strong { color: var(--ink); }
.about-body a { color: #7fd0ff; }
.about-body code { font: 0.9em var(--mono); padding: 1px 6px; border-radius: 5px; background: rgba(255, 255, 255, 0.08); }
.about-body pre { padding: 12px 14px; border-radius: 8px; background: rgba(255, 255, 255, 0.06); overflow: auto; }
.about-body pre code { padding: 0; background: none; }
.about-body blockquote { margin: 0 0 12px; padding-left: 14px; border-left: 3px solid var(--line); color: var(--muted); }
.about-body hr { border: 0; border-top: 1px solid var(--line); margin: 22px 0; }

@media (max-width: 720px) {
  .about-panel { padding: 22px 18px; }
  :root { --hud-h: 40px; }
  .hud-bar { font-size: 13px; }
  .hud-bar .cell { padding: 0 5px; gap: 3px; }
  .hud-bar .mode { min-width: 48px; padding: 0 4px; letter-spacing: 0.02em; }
  .hud-bar small { display: none; }   /* the "kn" units: the icons and numbers carry the meaning */
  .hud-bar .sail { padding: 0 6px; gap: 6px; font-size: 12px; min-width: 0; overflow: hidden; }
  .sail-bar { width: 36px; flex: none; }
  .sail-name { overflow: hidden; text-overflow: ellipsis; }
  .hud-bar .icon { font-size: 16px; }
  .dial { width: 26px; height: 26px; }
  .menu-card { grid-template-columns: 1fr; gap: 18px; padding: 24px 16px 12px; }
  .menu-hints { padding: 10px 16px; gap: 12px; }
}

@media (max-width: 480px) {   /* portrait phone: the sail name gives way to the bar so every instrument still fits */
  .hud-bar .mode { min-width: 44px; }
  .hud-bar .cell { padding: 0 4px; }
  .sail-name { display: none; }
  .dial { width: 22px; height: 22px; }
}

/* ---- touch ---- */
.touch-only { display: none; }
body.touch .touch-only { display: inline; }
body.touch .mouse-only { display: none; }
body.touch #lock-hint { left: 50%; right: auto; transform: translateX(-50%); bottom: auto; top: 54px; font-size: 13px; }
body.touch.dragged #lock-hint { opacity: 0; }
body.touch #view { touch-action: none; }

#touch { position: fixed; inset: 0; pointer-events: none; z-index: 2; --pad: min(152px, 38vh); --btn: min(66px, 17vh); }
#touch > * { pointer-events: auto; touch-action: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
#touch:not([data-play="1"]) .tc-pad, #touch:not([data-play="1"]) .tc-buttons { display: none; }
.tc-pad {
  position: absolute; left: calc(18px + env(safe-area-inset-left)); bottom: calc(var(--hud-h) + 14px);
  width: var(--pad); height: var(--pad); border-radius: 50%; background: rgba(10, 22, 34, 0.38);
  border: 1px solid var(--line); box-shadow: inset 0 0 0 calc(var(--pad) * 0.11) rgba(255, 255, 255, 0.04);
}
.tc-arrow {
  position: absolute; width: 34%; height: 34%; display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--pad) * 0.15); color: rgba(244, 247, 250, 0.8); border-radius: 12px; transition: background 0.08s;
}
.tc-arrow.up { left: 33%; top: 2%; }
.tc-arrow.down { left: 33%; bottom: 2%; }
.tc-arrow.left { left: 2%; top: 33%; }
.tc-arrow.right { right: 2%; top: 33%; }
.tc-pad[data-dir="4"] .up, .tc-pad[data-dir="8"] .down, .tc-pad[data-dir="1"] .left, .tc-pad[data-dir="2"] .right {
  background: var(--accent); color: var(--accent-ink);
}
.tc-buttons {
  position: absolute; right: calc(18px + env(safe-area-inset-right)); bottom: calc(var(--hud-h) + 14px);
  display: flex; align-items: flex-end; gap: 14px;
}
.tc-btn {
  width: var(--btn); height: var(--btn); border-radius: 50%; border: 1px solid var(--line); padding: 0;
  background: rgba(10, 22, 34, 0.5); color: var(--ink); font: 800 calc(var(--btn) * 0.36) var(--sans);
  backdrop-filter: blur(3px); user-select: none; -webkit-user-select: none;
}
.tc-btn.a { margin-bottom: calc(var(--btn) * 0.5); }   /* B lower left of A, as on the Playdate */
.tc-btn.down { background: var(--ink); color: #0b1622; }
.tc-menu {
  position: absolute; right: calc(12px + env(safe-area-inset-right)); top: calc(10px + env(safe-area-inset-top));
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  color: var(--ink); font-size: 22px; padding: 0;
}
body[data-draw="1"] #touch .tc-menu { display: none; }   /* the main menu has its own buttons */
#rotate { display: none; }
@media (orientation: portrait) {
  body.touch #rotate {
    position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 14px; background: var(--bg); font-size: 18px; color: var(--ink); text-align: center; padding: 24px;
  }
  #rotate div { font-size: 56px; color: var(--accent); }
}
/* the sail, a game's worth of buttons and the d-pad share one short phone screen */
body.touch .hud-clock { left: calc(12px + env(safe-area-inset-left)); }

/* ---- full screen ---- */
#fullscreen {
  position: fixed; z-index: 4; left: calc(12px + env(safe-area-inset-left)); top: calc(10px + env(safe-area-inset-top));
  width: 40px; height: 40px; padding: 8px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  color: var(--ink); cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
#fullscreen:hover, #fullscreen:focus-visible { background: rgba(255, 255, 255, 0.14); }
#fullscreen svg { display: block; width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
#fullscreen .fs-leave, #fullscreen.on .fs-enter { display: none; }
#fullscreen.on .fs-leave { display: inline; }
/* the race clock sits beside it, the warning line below it */
body.can-fs .hud-clock { left: calc(62px + env(safe-area-inset-left)); }
body.can-fs .hud-warning { top: calc(58px + env(safe-area-inset-top)); }
body.touch .hud-bar { padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left); }

@media (max-height: 500px) {
  :root { --hud-h: 38px; }
  .hud-bar { font-size: 14px; }
  .hud-bar .cell { padding: 0 8px; }
  .hud-bar .mode { min-width: 64px; }
  .hud-bar .sail { padding: 0 8px; gap: 8px; font-size: 13px; }
  .sail-bar { width: 80px; }
  .hud-box { top: 38%; padding: 12px 18px; min-width: 240px; }
  .hud-box .head { font-size: 20px; }
  .hud-praise { top: 10%; }
  /* one row that fits the screen, so the hint bar below always shows; the controls page scrolls inside it */
  .menu-card { grid-template-rows: minmax(0, 1fr); min-height: 0; grid-template-columns: minmax(180px, 1fr) minmax(240px, 1.25fr); gap: 3vw; padding: 12px calc(16px + env(safe-area-inset-right)) 8px calc(16px + env(safe-area-inset-left)); }
  .menu-title { padding: 12px 16px; }
  .menu-title h1 { font-size: clamp(26px, 8vh, 44px); margin: 4px 0 8px; }
  .menu-title .sub { font-size: 14px; margin-top: 8px; }
  .menu-title .extra { margin-top: 12px; font-size: 13px; }
  .menu-list { gap: 4px; }
  .menu-list .item { padding: 8px 14px 8px 38px; font-size: 17px; }
  .menu-list .item.sel::before { left: 14px; }
  .menu-hints { padding: 8px calc(16px + env(safe-area-inset-right)) 8px calc(16px + env(safe-area-inset-left)); gap: 14px; }
  .menu-preview canvas { min-height: 0; }
  .pv-head h2 { font-size: 22px; }
  .menu-help { padding: 12px 16px; max-height: 100%; overflow: auto; overscroll-behavior: contain; }
  .menu-help h2 { font-size: 17px; margin-bottom: 6px; }
  .help-body { font-size: 13px; }
  .help-keys { font-size: 12px; line-height: 1.6; }
  .sys-panel { padding: 16px; gap: 10px; }
}
