:root {
  --bg: #241511;
  --panel: #33201735;
  --panel-solid: #332017;
  --panel-2: #43291c;
  --text: #f7efe2;
  --muted: #c9a98c;
  --border: #57371f;
  --accent: #e8912d;
  --tile-bg: #f2d9a6;
  --tile-edge: #c9a468;
  --tile-ink: #3a2413;
  --bad: #e04b40;
  --good: #7fb069;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  background:
    radial-gradient(1200px 500px at 50% -200px, #3a2013 0%, var(--bg) 60%);
  color: var(--text);
  font-family: "Helvetica Neue", Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
  overflow: hidden;
  position: fixed;
  inset: 0;
}

header {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 12px 6px;
  border-bottom: 1px solid var(--border);
}
header h1 {
  font-size: clamp(19px, 5.5vw, 27px);
  letter-spacing: 2px;
  font-weight: 900;
}
header h1 span { color: var(--accent); }
.icon-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}
.icon-btn:active { background: var(--panel-2); }

.daybar-wrap {
  position: relative;
  width: 100%;
  max-width: 560px;
  min-height: 22px;
}
.daybar {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  padding: 5px 0 0;
  letter-spacing: 1px;
}
.sound-toggle {
  position: absolute;
  top: 1px;
  right: 12px;
  width: 30px;
  height: 25px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}
.sound-toggle[aria-pressed="true"] {
  border-color: var(--good);
  background: #7fb06920;
}

/* ---------------- status bar: timer · message · sap bucket ---------------- */
.statusbar {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 14px 2px;
}
.timer-wrap {
  min-width: 72px;
  text-align: left;
}
.timer {
  font-size: 24px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  line-height: 1;
}
.pace-ghost {
  margin-top: 3px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.pace-ghost.ahead {
  color: #ffd166;
  text-shadow: 0 0 8px #e8912d88;
}
.status-msg {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  padding: 3px;
  text-align: center;
  font: inherit;
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.25;
}
.status-msg[aria-disabled="true"] { cursor: default; }
.status-msg[aria-disabled="false"] { cursor: pointer; }
.status-msg:focus-visible { outline: 2px solid var(--bad); border-radius: 6px; }
.status-msg.warn { color: #f0b45f; }
.status-msg.bad { color: var(--bad); }
.status-msg.good { color: var(--good); font-weight: 800; }
.bucket { width: 34px; height: 41px; flex: none; }

/* Give-up pill: floats in the board's corner once the player has been at
   it a while; never competes with the tiles. */
.stuck-btn {
  position: absolute;
  top: 8px; right: 8px;
  z-index: 5;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 6px 11px;
  border-radius: 999px;
  cursor: pointer;
}
.stuck-btn:active { background: var(--panel); }
.stuck-btn.working { opacity: 0.6; pointer-events: none; }
.keep-trying { display: block; width: 100%; margin-top: 8px; min-height: 38px; }
#sap { transition: y 0.5s ease, height 0.5s ease; }
#drip { animation: drip 0.9s ease-in infinite; }
@keyframes drip {
  0% { opacity: 0; transform: translateY(0); }
  25% { opacity: 1; }
  100% { opacity: 0; transform: translateY(30px); }
}

/* ---------------- board ---------------- */
main {
  flex: 1;
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 4px 8px 0;
}
#boardView {
  flex: 1;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 14px;
  background:
    radial-gradient(600px 300px at 50% 20%, #2d1a10 0%, #271610 70%);
  touch-action: none;
  min-height: 0;
}
#plane {
  position: absolute;
  left: 0; top: 0;
  width: 0; height: 0;
  transform-origin: 0 0;
  transition: transform 0.28s ease;
}
#plane.no-anim { transition: none; }
#gridlines {
  position: absolute;
  background-image:
    linear-gradient(to right, #ffffff0e 1px, transparent 1px),
    linear-gradient(to bottom, #ffffff0e 1px, transparent 1px);
  border: 1px solid #ffffff10;
  border-radius: 6px;
}
#dropCursor {
  position: absolute;
  z-index: 4;
  border-radius: 10px;
  border: 2.5px dashed var(--good);
  background: #7fb06922;
  transition: left 0.06s linear, top 0.06s linear;
}
#dropCursor.blocked { border-color: var(--bad); background: #e04b4022; }
#badRunOutline {
  position: absolute;
  z-index: 3;
  border: 3px solid var(--bad);
  border-radius: 11px;
  background: #e04b4010;
  box-shadow: 0 0 12px #e04b4055;
  pointer-events: none;
}
#badRunOutline.flash { animation: bad-run-flash 0.38s ease-out; }
@keyframes bad-run-flash {
  45% { border-color: #ffb3aa; background: #e04b4040; box-shadow: 0 0 22px #e04b40aa; }
}
#firstHint {
  position: absolute;
  left: 50%; bottom: 14px;
  transform: translateX(-50%);
  width: max-content;
  max-width: 90%;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  animation: bob 1.6s ease-in-out infinite;
  pointer-events: none;
}
@keyframes bob { 50% { transform: translateX(-50%) translateY(-6px); } }

/* ---------------- tiles ---------------- */
.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #f9e6bd 0%, var(--tile-bg) 55%, #e4c489 100%);
  border: 1px solid var(--tile-edge);
  border-bottom-width: 4px;
  border-radius: 9px;
  color: var(--tile-ink);
  font-weight: 900;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  touch-action: none;
}
#plane .tile {
  position: absolute;
  transition: box-shadow 0.2s;
}
.tile.ghosted { opacity: 0.25; }
.tile.pop { animation: pop 0.18s ease; }
@keyframes pop { 50% { transform: scale(1.14); } }
.tile.valid-flash { animation: valid-flash 0.55s ease-out; }
@keyframes valid-flash {
  35% {
    background: #cfe9b8;
    border-color: var(--good);
    box-shadow: 0 0 18px 4px #7fb069aa;
  }
}
.tile.wave { animation: wave 0.55s ease; }
@keyframes wave { 30% { transform: translateY(-14px) rotate(-4deg); } 60% { transform: translateY(2px); } }

.drag-ghost {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  box-shadow: 0 10px 22px #000a;
  transform: scale(1.12);
  cursor: grabbing;
}

/* ---------------- tray ---------------- */
#trayWrap {
  padding: 8px 2px calc(8px + env(safe-area-inset-bottom));
}
#tray {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  min-height: 55px;
  padding: 9px 6px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--border);
}
#tray .tile {
  width: min(48px, calc((100vw - 84px) / 6));
  height: min(48px, calc((100vw - 84px) / 6));
  font-size: min(25px, calc((100vw - 84px) / 12));
}
#tray.empty { background: #7fb06915; border-color: #7fb06955; }

/* ---------------- toast + tapped banner ---------------- */
#toast {
  position: absolute;
  top: 12px; left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-weight: 700;
  font-size: 14px;
  padding: 9px 15px;
  border-radius: 9px;
  z-index: 40;
  width: max-content;
  max-width: 88%;
  text-align: center;
}
#tapped {
  position: fixed;
  top: 34%; left: 50%;
  transform: translate(-50%, -50%) scale(0.3);
  font-size: clamp(46px, 15vw, 90px);
  font-weight: 900;
  letter-spacing: 4px;
  color: var(--accent);
  text-shadow: 0 4px 0 #7c4a12, 0 10px 34px #000c;
  z-index: 70;
  pointer-events: none;
}
#tapped.show { animation: tapped 1.5s cubic-bezier(0.2, 1.4, 0.4, 1) forwards; }
@keyframes tapped {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.3) rotate(-8deg); }
  35% { opacity: 1; transform: translate(-50%, -50%) scale(1.12) rotate(2deg); }
  55% { transform: translate(-50%, -50%) scale(1) rotate(0deg); }
  85% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -55%) scale(1); }
}
#newBestStamp {
  position: fixed;
  top: 45%;
  left: 50%;
  z-index: 71;
  transform: translate(-50%, -50%) rotate(-5deg);
  padding: 8px 16px;
  border: 3px double #ffd166;
  border-radius: 8px;
  color: #ffe6a1;
  background: #5a350de8;
  box-shadow: 0 5px 24px #000b, 0 0 24px #e8912d88;
  font-size: clamp(22px, 7vw, 34px);
  font-weight: 900;
  letter-spacing: 2px;
  pointer-events: none;
}
#newBestStamp.show { animation: best-stamp 1.5s cubic-bezier(0.2, 1.4, 0.4, 1) forwards; }
@keyframes best-stamp {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(1.8) rotate(-8deg); }
  28%, 82% { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(-5deg); }
  100% { opacity: 0; transform: translate(-50%, -56%) scale(0.98) rotate(-4deg); }
}
.leaf {
  position: fixed;
  top: -40px;
  z-index: 65;
  pointer-events: none;
  animation: fall linear forwards;
}
.leaf.gold {
  filter: sepia(1) saturate(3) hue-rotate(350deg) brightness(1.35);
  text-shadow: 0 0 10px #ffd166;
}
@keyframes fall {
  to { transform: translateY(110vh) rotate(720deg); opacity: 0.7; }
}

/* ---------------- overlays / modals (fleet style) ---------------- */
.overlay {
  position: fixed;
  inset: 0;
  background: #000a;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 80;
  padding: 16px;
}
.modal {
  position: relative;
  background: var(--panel-solid);
  border: 1px solid var(--border);
  border-radius: 16px;
  max-width: 430px;
  width: 100%;
  max-height: 86vh;
  overflow-y: auto;
  padding: 22px 20px 18px;
}
.modal h2 { font-size: 19px; margin-bottom: 10px; }
.modal h3 { font-size: 15px; margin: 14px 0 8px; }
.modal p { font-size: 14.5px; line-height: 1.45; color: var(--text); margin-bottom: 10px; }
.modal .fine { font-size: 12.5px; color: var(--muted); }
.modal a { color: var(--accent); }
.small-modal { max-width: 340px; text-align: center; }
.modal-action {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
}
.close {
  position: absolute;
  top: 8px; right: 12px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 26px;
  cursor: pointer;
}
.bad-demo { color: var(--bad); font-weight: 700; }

.example {
  display: grid;
  gap: 4px;
  justify-content: center;
  margin: 12px 0;
}
.example .row { display: flex; gap: 4px; justify-content: flex-start; }
.example .cell, .example .tile {
  width: 34px; height: 34px;
  font-size: 18px;
  cursor: default;
}
.example .cell { border-radius: 8px; }

#resultCard h2 { color: var(--accent); font-size: 24px; }
.result-sub { color: var(--muted); font-size: 14px; margin-bottom: 12px; }

.stat-row { display: flex; gap: 8px; margin: 10px 0 4px; }
.stat {
  flex: 1;
  background: var(--panel-2);
  border-radius: 10px;
  text-align: center;
  padding: 9px 2px;
}
.stat b { font-size: 21px; display: block; }
.stat small { font-size: 10.5px; color: var(--muted); line-height: 1.1; display: block; margin-top: 2px; }

#finishedRow { display: flex; gap: 10px; align-items: stretch; margin-top: 14px; }
#finishedRow.hidden { display: none; }
.next-box {
  flex: 1;
  background: var(--panel-2);
  border-radius: 10px;
  padding: 8px;
  text-align: center;
}
.next-label { font-size: 10.5px; color: var(--muted); letter-spacing: 1px; }
.countdown { font-size: 21px; font-weight: 800; font-variant-numeric: tabular-nums; }
.share-btn {
  flex: 1;
  background: var(--accent);
  color: #2b1706;
  font-weight: 900;
  font-size: 16px;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  letter-spacing: 1px;
}
.share-btn:active { filter: brightness(1.1); }

/* ---------------- leaderboard (fleet style) ---------------- */
.lb { border-top: 1px solid var(--border); margin-top: 16px; }
.lb-tabs { display: flex; gap: 8px; }
.tab {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.tab.sel { background: var(--accent); color: #2b1706; border-color: var(--accent); }
.lb-status { font-size: 12.5px; color: var(--muted); margin: 8px 0 4px; min-height: 16px; }
#lbList { list-style: none; margin: 4px 0; }
#lbList li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 8px;
  font-size: 14.5px;
}
#lbList li.me { background: #e8912d26; }
#lbList .rank { width: 26px; text-align: center; }
#lbList .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lbList .sc { font-weight: 800; font-variant-numeric: tabular-nums; }
.lb-form { display: flex; gap: 8px; margin: 8px 0; }
.lb-form input {
  flex: 1;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 14px;
}
.lb-rename { margin-top: 6px; }

footer {
  width: 100%;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  padding: 4px 0 calc(6px + env(safe-area-inset-bottom));
}
footer a, .fleet-credit a { color: var(--accent); text-decoration: none; }
.fleet-credit {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.5;
  text-align: center;
}

.hidden { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  #plane, #sap { transition: none; }
  #firstHint, #drip, .tile.pop, .tile.valid-flash, .tile.wave,
  #badRunOutline.flash, #tapped.show, #newBestStamp.show {
    animation: none;
  }
  #badRunOutline.flash {
    border-color: #ffb3aa;
    background: #e04b4030;
  }
  .tile.valid-flash {
    background: #cfe9b8;
    border-color: var(--good);
    box-shadow: 0 0 12px 2px #7fb06988;
  }
  #tapped.show {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
  #newBestStamp.show { opacity: 1; }
  .leaf { display: none; }
}

/* ------------------------------------------------------------ duel mode */

.hdr-btns { display: flex; gap: 6px; }

.duelbar {
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--maple, #d97516);
  padding: 2px 8px 0;
}

.duel-label {
  display: block;
  text-align: left;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.7;
  margin: 10px 0 4px;
}
.duel-input {
  width: 100%;
  padding: 10px 12px;
  font: inherit;
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  border: 2px solid var(--line, #d9c9a8);
  border-radius: 10px;
  background: var(--panel, #fff);
  color: inherit;
}
.duel-code { letter-spacing: 0.4em; text-indent: 0.4em; text-transform: uppercase; font-size: 24px; }
.duel-error { color: #b3402a; font-weight: 700; }

#lobbyCode {
  font-size: 42px;
  font-weight: 900;
  letter-spacing: 0.35em;
  text-indent: 0.35em;
  color: var(--maple, #d97516);
  padding: 8px 0 2px;
}

#duelDoneRows { margin: 10px 0 4px; }
.duel-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 10px;
  border-radius: 10px;
  font-weight: 700;
}
.duel-row.win { background: var(--panel-2, #f3e7cd); }
.duel-row .duel-time { font-variant-numeric: tabular-nums; }

/* group duels: seat picker + lobby roster */
.seat-btns { display: flex; gap: 8px; justify-content: center; }
.seat-btn {
  flex: 1;
  max-width: 64px;
  padding: 10px 0;
  font: inherit;
  font-size: 18px;
  font-weight: 800;
  color: inherit;
  background: var(--panel, #fff);
  border: 2px solid var(--line, #d9c9a8);
  border-radius: 10px;
  cursor: pointer;
}
.seat-btn.selected {
  border-color: var(--maple, #d97516);
  background: var(--panel-2, #f3e7cd);
}
#lobbyList { font-weight: 700; font-size: 14px; padding: 2px 0 4px; }
#lobbyList .chair-empty { opacity: 0.45; }
