/* Dragonboat Lab v2 — mobile-first, Swim Finder–ish portrait layout */
:root {
  --bg: #0b1220;
  --panel: #121a2b;
  --ink: #e8eefc;
  --muted: #8b9bb8;
  --accent: #3ecf8e;
  --accent-dim: #2a9b6a;
  --warn: #f0b429;
  --boat: #c47a3a;
  --water: #1a4a6e;
  --border: #243049;
  --danger: #ef6b6b;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
}

#app {
  width: 100%;
  height: 100%;
  max-width: min(960px, 100%);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  position: relative;
  background: linear-gradient(180deg, #0b1220 0%, #0e1a2e 100%);
}

header.top {
  flex: 0 0 auto;
  padding: 12px 16px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  z-index: 2;
}

header.top .title-block {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

header.top a.back {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-decoration: none;
}

header.top a.back:hover,
header.top a.back:focus-visible {
  color: var(--accent);
}

header.top h1 {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

header.top .badge {
  font-size: 0.7rem;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 4px 8px;
  border-radius: 999px;
}

#stage-wrap {
  flex: 1 1 auto;
  position: relative;
  min-height: 220px;
  /* Keep the race view visible even when the edit dock is tall */
  flex-basis: 42vh;
  display: block;
  padding: 0 8px 8px;
  overflow: hidden;
}

canvas#scene {
  /* Fill stage; JS also sets explicit px size (Safari blank-canvas fix) */
  position: absolute;
  left: 8px;
  top: 8px;
  right: 8px;
  bottom: 8px;
  width: calc(100% - 16px);
  height: calc(100% - 16px);
  max-width: none;
  max-height: none;
  background: #071018;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: block;
  z-index: 1;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

/* Live graph lives in #race-dock (not over the canvas) */
#graph-wrap {
  display: none;
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: rgba(7, 16, 28, 0.9);
}

#graph-wrap.active { display: block; }

canvas#graph {
  display: block;
  width: 100%;
  height: 72px;
  background: transparent;
}

/* Overlay panels */
.panel {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: stretch;
  padding: 20px 18px calc(20px + var(--safe-bottom));
  background: rgba(7, 12, 22, 0.82);
  backdrop-filter: blur(6px);
  z-index: 5;
  overflow-y: auto;
}

.panel.active { display: flex; }

.panel h2 {
  font-size: 1.6rem;
  margin-bottom: 8px;
  text-align: center;
}

.panel p.lead {
  color: var(--muted);
  text-align: center;
  font-size: 0.92rem;
  line-height: 1.45;
  margin-bottom: 20px;
}

.panel .card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 10px;
  margin-bottom: 12px;
}

.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  border-radius: 12px;
  padding: 14px 18px;
  background: var(--accent);
  color: #062016;
  transition: transform 0.08s ease, filter 0.12s ease;
  width: 100%;
  margin-top: 8px;
}

.btn:active { transform: scale(0.98); }
.btn.secondary {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--border);
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.btn.tiny {
  width: auto;
  padding: 8px 14px;
  margin: 0;
  font-size: 0.82rem;
  pointer-events: auto;
}

/* Pause / Restart — in race dock, not floating on canvas */
.run-controls {
  display: none;
  position: relative;
  gap: 10px;
  align-items: stretch;
  justify-content: stretch;
  pointer-events: auto;
}

.run-controls.active {
  display: flex;
}
.run-controls .btn {
  flex: 1;
  width: auto;
  margin: 0;
  padding: 12px 14px;
  font-size: 0.9rem;
  font-weight: 700;
  border-radius: 12px;
}
.run-controls .btn.tiny {
  padding: 12px 14px;
  font-size: 0.9rem;
}
.run-controls .btn.secondary {
  background: rgba(18, 26, 43, 0.95);
  color: var(--ink);
  border: 1px solid var(--border);
}

/* Edit dock under canvas */
#edit-dock {
  flex: 0 1 auto;
  display: none;
  flex-direction: column;
  gap: 4px;
  padding: 8px 14px calc(12px + var(--safe-bottom));
  background: var(--panel);
  border-top: 1px solid var(--border);
  max-height: min(42vh, 360px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 3;
}

#edit-dock.active { display: flex; }

.slider-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 1px 10px;
  align-items: center;
  padding: 3px 0;
}

.slider-row label {
  font-size: 0.74rem;
  color: var(--muted);
  grid-column: 1 / -1;
}

.slider-row .name {
  color: var(--ink);
  font-weight: 600;
  font-size: 0.8rem;
}

.slider-row .val {
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 700;
  text-align: right;
}

.slider-row input[type="range"] {
  grid-column: 1 / -1;
  width: 100%;
  accent-color: var(--accent);
  height: 26px;
}

#edit-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

#edit-actions .btn { margin-top: 0; flex: 1; }

/* Race dock: stacked under canvas (HUD → graph → buttons) */
#race-dock {
  display: none;
  flex: 0 0 auto;
  flex-direction: column;
  gap: 8px;
  padding: 8px 12px calc(12px + var(--safe-bottom));
  background: var(--panel);
  border-top: 1px solid var(--border);
  z-index: 3;
}

#race-dock.active { display: flex; }

/* Compact header while racing to free vertical space */
#app.racing header.top {
  padding: 8px 12px 4px;
}
#app.racing header.top .badge { display: none; }

#hud {
  display: block;
  position: relative;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

#hud .row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  background: rgba(7, 12, 22, 0.55);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
  max-width: 100%;
}

#hud .row span.k { color: var(--muted); }
#hud .row span.v { font-weight: 700; color: var(--accent); }

/* Result stats */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  text-align: center;
}

.stat .label { font-size: 0.72rem; color: var(--muted); margin-bottom: 4px; }
.stat .num {
  font-size: 1.35rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}
.stat .unit { font-size: 0.75rem; color: var(--muted); margin-left: 2px; }

.note {
  background: rgba(62, 207, 142, 0.08);
  border: 1px solid rgba(62, 207, 142, 0.25);
  border-radius: 12px;
  padding: 12px;
  font-size: 0.88rem;
  line-height: 1.45;
  color: var(--ink);
  margin-bottom: 8px;
}

.note strong { color: var(--warn); }

.hint {
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  margin-top: 6px;
}

@media (max-height: 700px) {
  #edit-dock { max-height: min(38vh, 300px); }
  #stage-wrap { min-height: 200px; flex-basis: 48vh; }
  canvas#graph { height: 56px; }
}

@media (max-height: 560px) {
  #edit-dock { max-height: min(34vh, 240px); }
  #stage-wrap { min-height: 180px; flex-basis: 50vh; }
  header.top { padding: 8px 12px 4px; }
  header.top h1 { font-size: 0.95rem; }
}

.impact-panel {
  margin-top: 6px;
  padding: 8px 10px;
  background: rgba(7, 12, 22, 0.55);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.72rem;
  line-height: 1.35;
  color: var(--muted);
}
.impact-panel .impact-title {
  color: var(--ink);
  font-weight: 700;
  font-size: 0.78rem;
  margin-bottom: 6px;
}
.impact-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 8px;
  align-items: center;
  padding: 3px 0;
}
.impact-row .bar {
  grid-column: 1 / -1;
  height: 5px;
  border-radius: 999px;
  background: #1a2438;
  overflow: hidden;
}
.impact-row .bar > i {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--accent-dim), var(--accent));
  border-radius: 999px;
}
.impact-row .delta { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.impact-row .hint-dir { color: var(--warn); }
#edit-actions { flex-wrap: wrap; }
#edit-actions .btn { min-width: 30%; }

.crew-panel {
  margin: 6px 0 4px;
  padding: 8px 10px;
  background: rgba(7, 12, 22, 0.45);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.crew-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 6px;
}
.crew-head .name {
  font-weight: 700;
  font-size: 0.82rem;
  color: var(--ink);
}
.crew-sub {
  font-size: 0.7rem;
  color: var(--muted);
}
.crew-seats {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.seat-row {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 8px;
  align-items: center;
}
.seat-row .seat-label {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
}
.seat-row .seat-label strong {
  color: var(--ink);
  display: block;
  font-size: 0.78rem;
}
.seat-row select {
  width: 100%;
  appearance: none;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
}
.seat-row select.skill-beginner { border-color: #ef6b6b; }
.seat-row select.skill-amateur { border-color: #f0b429; }
.seat-row select.skill-advanced { border-color: #3ecf8e; }
