:root {
  --ink: #1f2430;
  --panel: rgba(38, 30, 24, 0.72);
  --red: #e5383b;
  --dark-red: #b3261e;
  --blue: #2f6fe4;
  --green: #5fd068;
  --wood: #b07a45;
  font-family: 'Baloo 2', 'Comic Sans MS', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #9fd8f5; color: #fff; }
button { font: inherit; touch-action: manipulation; }
[hidden] { display: none !important; }
kbd {
  font: 700 0.85em/1 ui-monospace, monospace;
  padding: 0.15em 0.4em;
  border-radius: 0.3em;
  background: #f4ede4;
  border: 1px solid #d9c8b4;
  color: var(--ink);
}

/* ---------- Scene ---------- */
#stage { position: fixed; inset: 0; overflow: hidden; touch-action: none; }
#scene3d { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 5; }

#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.pop-label, .track-label {
  position: absolute; left: 0; top: 0;
  transform: translate(-50%, -50%);
  white-space: nowrap;
}
.pop-label {
  font-weight: 800; line-height: 1;
  animation: label-in 0.3s cubic-bezier(.2, .9, .3, 1.6) both;
  -webkit-text-stroke: 0.35vh var(--ink);
  paint-order: stroke;
}
.pop-label.say { font-size: 5vh; color: #fff; }
.pop-label.bonk { font-size: 7vh; color: #ffd23f; }
.pop-label.splash { font-size: 7vh; color: #8fd8ff; }
@keyframes label-in { from { transform: translate(-50%, -50%) scale(0.2); opacity: 0; } to { transform: translate(-50%, -50%) scale(1); opacity: 1; } }

/* Over each piece: its height, and what a loop or pond needs */
.track-label.tag {
  transform: translate(-50%, -100%);
  text-align: center;
  background: rgba(255, 255, 255, 0.92);
  color: var(--ink);
  border-radius: 1vh;
  padding: 0.2vh 1vh;
  line-height: 1.1;
  box-shadow: 0 0.4vh 0 rgba(0, 0, 0, 0.2);
}
.track-label.tag b { display: block; font-size: 2.6vh; }
.track-label.tag span { display: block; font-size: 1.9vh; color: #8a3b12; font-weight: 700; }
.track-label.tag:empty { display: none; }
.track-label.tag.on { background: #ffe066; outline: 0.4vh solid #fff; }
[data-phase='lift'] .track-label.tag, [data-phase='ride'] .track-label.tag { opacity: 0.6; }

/* Wooden signs between pieces: height left to spend */
.track-label.sign {
  transform: translate(-50%, 0);
  text-align: center;
  background: var(--wood);
  border: 0.3vh solid #7a4f26;
  color: #fff;
  border-radius: 0.8vh;
  padding: 0.1vh 0.9vh 0.3vh;
  line-height: 1;
  box-shadow: 0 0.4vh 0 rgba(0, 0, 0, 0.25);
  transition: background 0.2s, transform 0.2s;
}
.track-label.sign b { display: block; font-size: 2.6vh; }
.track-label.sign span { display: block; font-size: 1.4vh; opacity: 0.9; }
.track-label.sign.lit { background: #ffd23f; color: var(--ink); transform: translate(-50%, 0) scale(1.2); }

/* ---------- Top: toolbar, message, info ---------- */
#hud-top {
  position: fixed; left: 0; right: 0; top: 0;
  display: flex; align-items: flex-start; gap: 2vw;
  padding: 2vh 2vw;
  z-index: 10;
  pointer-events: none;
}
#hud-top > * { pointer-events: auto; }
#toolbar { display: flex; gap: 0.5vw; flex: 0 0 auto; width: 22vh; }
#toolbar button {
  width: 5.4vh; height: 5.4vh;
  border-radius: 1.4vh;
  border: none;
  background: var(--panel);
  font-size: 2.8vh;
  cursor: pointer;
  color: #fff;
  display: grid; place-items: center;
}
#toolbar button:hover { background: rgba(38, 30, 24, 0.9); }

#board { flex: 1; display: flex; justify-content: center; }
#task {
  text-align: center;
  font-weight: 800;
  line-height: 1.2;
  border-radius: 3vh;
  font-size: clamp(18px, 3.6vh, 52px);
  padding: 0.6vh 2.4vw;
  max-width: 64vw;
  background: var(--panel);
  text-shadow: 0 0.25vh 0 rgba(0, 0, 0, 0.3);
}
#task:empty { visibility: hidden; }
#task.pop { animation: pop 0.45s cubic-bezier(.2, .9, .3, 1.4); }
@keyframes pop { from { transform: scale(0.85); opacity: 0.3; } to { transform: scale(1); opacity: 1; } }

#info {
  flex: 0 0 auto; width: 22vh;
  text-align: center;
  background: var(--panel);
  border-radius: 2vh;
  padding: 0.6vh 1vh 0.8vh;
  pointer-events: none;
  line-height: 1;
}
#info span { display: block; font-size: 2.1vh; font-weight: 700; opacity: 0.9; }
#info b { display: block; font-size: 6vh; line-height: 1.05; }
#info b.over { color: #ff8a80; }

/* ---------- Bottom: the piece tray and buttons ---------- */
#controls {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 0 1.5vw 2vh;
  z-index: 10;
  pointer-events: none;
}
#controls > * { pointer-events: auto; }
#build-bar, #ride-bar, #end-bar { display: none; }
[data-phase='build'] #build-bar { display: block; }
[data-phase='lift'] #ride-bar, [data-phase='ride'] #ride-bar { display: flex; justify-content: flex-end; }
[data-phase='end'] #end-bar { display: flex; justify-content: center; gap: 1.5vw; }

#editor {
  display: flex; justify-content: center; align-items: stretch; gap: 1.2vw;
  margin-bottom: 1.2vh;
}
#kinds, #size {
  display: flex; gap: 0.6vh;
  background: var(--panel);
  border-radius: 2vh;
  padding: 0.8vh;
}
#kinds button {
  border: none;
  border-radius: 1.4vh;
  background: rgba(255, 255, 255, 0.92);
  color: var(--ink);
  width: 11vh;
  padding: 0.4vh 0 0.6vh;
  cursor: pointer;
  display: grid; justify-items: center;
  font-size: 1.9vh; font-weight: 700; line-height: 1.1;
}
#kinds button[aria-pressed='true'] { background: #ffe066; box-shadow: inset 0 0 0 0.4vh var(--dark-red); }
#kinds svg { width: 5vh; height: 5vh; }
#size { align-items: center; gap: 1.2vh; }
#size button {
  width: 8vh; height: 8vh;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: var(--ink);
  font-size: 6vh; font-weight: 800; line-height: 1;
  cursor: pointer;
  box-shadow: 0 0.5vh 0 #b9a68f;
}
#size button:active { transform: translateY(0.3vh); box-shadow: none; }
#size div { min-width: 13vh; text-align: center; line-height: 1; }
#size span { display: block; font-size: 2vh; font-weight: 700; opacity: 0.9; }
#size b { display: block; font-size: 5.4vh; }
#undo {
  border: none;
  border-radius: 2vh;
  background: var(--panel);
  color: #fff;
  font-size: 2.6vh; font-weight: 800;
  padding: 0 2vh;
  cursor: pointer;
}
#undo:disabled { opacity: 0.4; cursor: default; }

#tray-row { display: flex; align-items: stretch; gap: 1.2vw; }
#tray {
  flex: 1;
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 0.8vh;
  background: var(--panel);
  border-radius: 2vh;
  padding: 0.8vh;
}
.card {
  border: none;
  border-radius: 1.4vh;
  background: #fff8ef;
  color: var(--ink);
  padding: 0.4vh 0.4vh 0.6vh;
  cursor: pointer;
  display: grid; justify-items: center;
  line-height: 1.05;
  min-width: 0;
}
.card svg { width: 6vh; height: 6vh; }
.card b { font-size: 2.2vh; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.card span { font-size: 2.2vh; font-weight: 700; color: #8a3b12; }
.card[aria-pressed='true'] { background: #ffe066; box-shadow: inset 0 0 0 0.45vh var(--dark-red); }
.card.flag { cursor: default; background: rgba(255, 255, 255, 0.55); }
svg path { fill: none; stroke: var(--dark-red); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
svg path.water { stroke: var(--blue); }
svg path.fill { fill: #e7a07a; stroke: var(--dark-red); }
svg path.chk { fill: #222; stroke: #222; stroke-width: 2; }

.go, #end-bar button, #stop {
  border: none;
  border-radius: 2vh;
  font-weight: 800; line-height: 1;
  cursor: pointer;
}
.go {
  background: var(--green); color: #0e3a1a;
  box-shadow: 0 0.7vh 0 #2f8f3a;
  font-size: 5.4vh;
  padding: 0 4vh;
  min-width: 24vh;
}
.go:active { transform: translateY(0.4vh); box-shadow: none; }
.go:disabled { background: #b8c4b9; box-shadow: 0 0.7vh 0 #8a958b; color: #59615a; cursor: default; }
#end-bar button { height: 9vh; padding: 0 3.5vh; font-size: 3.6vh; }
#end-bar button:not(.go) { background: #fff; color: var(--ink); box-shadow: 0 0.6vh 0 #b9a68f; }
#end-bar .go { font-size: 4.4vh; }
#stop { background: var(--panel); color: #fff; font-size: 3vh; padding: 1.4vh 3vh; }

/* ---------- Ride graph ---------- */
#graph {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  z-index: 15;
  background: rgba(20, 14, 10, 0.55);
}
#graph .panel { position: relative; width: min(1000px, 92vw); padding: 16px 28px 14px; }
#graph h2 { margin: 0; font-size: 30px; color: var(--dark-red); }
#graph svg { display: block; width: 100%; height: auto; max-height: 30vh; }
#graph .grid { stroke: #e6ddd2; stroke-width: 1.5; }
#graph .ax { fill: #6b5a48; font-size: 18px; font-weight: 700; }
#graph .title { fill: var(--ink); font-size: 24px; font-weight: 800; }
#graph .line { fill: none; stroke-width: 5; stroke-linejoin: round; }
#graph .dot { fill: #ffd23f; stroke: var(--ink); stroke-width: 3; }
#graph-note { margin: 4px 0 0; font-size: 19px; font-weight: 700; color: #3c3228; }
#graph-close {
  position: absolute; right: 14px; top: 12px;
  width: 44px; height: 44px;
  border: none; border-radius: 50%;
  background: #f1e7da; color: var(--ink);
  font-size: 22px; cursor: pointer;
}

/* ---------- Setup menu ---------- */
#setup {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  z-index: 20;
  background: radial-gradient(ellipse at center, rgba(40, 24, 12, 0.45), rgba(24, 14, 6, 0.85));
  overflow: auto;
  padding: 3vh 2vw;
}
.panel {
  width: min(900px, 94vw);
  background: rgba(255, 252, 247, 0.97);
  color: var(--ink);
  border-radius: 28px;
  padding: 26px 34px 22px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
}
.logo { margin: 0; font-size: 50px; line-height: 1; font-weight: 800; color: var(--dark-red); letter-spacing: -0.5px; }
.tagline { margin: 8px 0 18px; font-size: 19px; line-height: 1.35; color: #4f4234; }
.panel h2 { margin: 0 0 8px; font-size: 20px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; margin-bottom: 18px; }
.cards button {
  border: 3px solid #eadccb;
  border-radius: 16px;
  background: #fff;
  color: var(--ink);
  padding: 8px 12px;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}
.cards button:hover { transform: translateY(-2px); }
.cards button[aria-pressed='true'] { border-color: var(--dark-red); background: #fff1e8; }
.cards b { display: block; font-size: 22px; line-height: 1.1; }
.cards span { display: block; font-size: 15px; color: #6b5a48; line-height: 1.3; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.check input { width: 20px; height: 20px; accent-color: var(--dark-red); flex: 0 0 auto; }
.buttons { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-top: 20px; }
.buttons button.primary {
  border: none;
  background: var(--dark-red); color: #fff;
  border-radius: 999px;
  font-size: 22px; font-weight: 800;
  padding: 12px 34px;
  box-shadow: 0 6px 0 #7a1712;
  cursor: pointer;
}
.buttons button.primary:active { transform: translateY(3px); box-shadow: 0 3px 0 #7a1712; }
.buttons button.secondary {
  border: 2px solid #d9c8b4; background: #fff; color: var(--ink);
  border-radius: 999px; font-size: 16px; font-weight: 700; padding: 8px 18px; cursor: pointer;
}
#offline-status { margin: 10px 0 0; font-size: 15px; font-weight: 700; color: #2e7d4a; }
.help { margin-top: 16px; font-size: 15px; color: #4f4234; }
.help summary { cursor: pointer; font-weight: 700; }
.help ul { padding-left: 20px; line-height: 1.5; }
