:root {
  --bg: #09090b;
  --surface: #111113;
  --surface-2: #18181b;
  --surface-3: #222226;
  --border: #232327;
  --border-2: #3a3a41;
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #8e8e97;
  --accent: #10b981;
  --accent-text: #34d399;
  --bust: #ef4444;
  --bust-text: #f87171;
  --bust-bg: #2b1113;
  --radius: 14px;
  --gutter: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
}

html {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  color: var(--text);
  font: 15px/1.4 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

button { font: inherit; color: inherit; }

#app {
  height: 100vh;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding:
    env(safe-area-inset-top, 0px)
    calc(var(--gutter) + env(safe-area-inset-right, 0px))
    max(12px, env(safe-area-inset-bottom, 0px))
    calc(var(--gutter) + env(safe-area-inset-left, 0px));
}

@media (min-width: 560px) {
  #app { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}

/* ---------- header ---------- */

.top {
  flex: none;
  height: 50px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
}

.brand {
  font-size: 16px;
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.brand .num { margin-left: 3px; color: var(--text-3); font-weight: 500; }

.pips { display: flex; gap: 6px; }

.pips i {
  width: 20px;
  height: 4px;
  border-radius: 2px;
  background: #2a2a2f;
  transition: background-color 0.2s;
}

.pips i.done { background: #6b6b74; }
.pips i.bust { background: #6b2a2c; }
.pips i.current { background: var(--text); }

.top-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.total {
  display: flex;
  align-items: baseline;
  gap: 6px;
  white-space: nowrap;
}

.total-label { font-size: 12px; color: var(--text-3); }

.total b {
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
  display: inline-block;
  transition: transform 0.15s, color 0.3s;
}

.total b.bump { transform: scale(1.18); color: var(--accent-text); }

.icon-btn {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid var(--border-2);
  border-radius: 999px;
  background: none;
  color: var(--text-2);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.icon-btn:active { background: var(--surface-2); }
.icon-btn:focus-visible, .btn:focus-visible, .card:focus-visible { outline: 2px solid var(--text-2); outline-offset: 2px; }

/* ---------- play screen ---------- */

.screen {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stage {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.info {
  flex: none;
  width: 100%;
  height: 34px;
  padding-bottom: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  color: var(--text-2);
}

.busts-label { display: inline-flex; align-items: center; gap: 5px; }
.busts-label b { color: var(--text); font-weight: 600; }
.busts-label svg { width: 15px; height: 15px; color: var(--bust-text); }

.board {
  --cell: 64px;
  --gap: 6px;
  --cols: 5;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  gap: var(--gap);
}

.board.shake { animation: shake 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97); }

@keyframes shake {
  10%, 90% { transform: translate3d(-2px, 0, 0); }
  20%, 80% { transform: translate3d(5px, 0, 0); }
  30%, 50%, 70% { transform: translate3d(-9px, 0, 0); }
  40%, 60% { transform: translate3d(9px, 0, 0); }
}

/* ---------- cards ---------- */

.card {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  display: block;
  width: var(--cell);
  height: var(--cell);
  padding: 0;
  border: 0;
  background: none;
  perspective: 700px;
  cursor: pointer;
  touch-action: none;
  -webkit-touch-callout: none;
}

.flipper {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform 0.27s cubic-bezier(0.3, 0.7, 0.3, 1);
}

.card.up .flipper { transform: rotateY(180deg); }
.card:not(.up).pressing .flipper { transform: scale(0.94); transition-duration: 0.12s; }

.board.instant .flipper,
.board.instant .face { transition: none !important; }

.face {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  border: 1px solid;
  border-radius: calc(var(--cell) * 0.13);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  transition: box-shadow 0.15s, border-color 0.15s, background-color 0.15s, opacity 0.2s;
}

.back {
  background: var(--surface-2);
  border-color: #2e2e34;
  display: flex;
  align-items: center;
  justify-content: center;
}

.val {
  font-size: calc(var(--cell) * 0.3);
  font-weight: 600;
  letter-spacing: -0.02em;
  color: #d4d4d8;
}

.val small {
  font-size: 0.7em;
  font-weight: 500;
  color: var(--text-3);
  margin-right: 1px;
}

.front {
  transform: rotateY(180deg);
  background: var(--surface-3);
  border-color: #3f3f46;
}

/* clue face: number top-left, map bottom-right, like a playing card index */
.cn {
  position: absolute;
  left: 9%;
  top: 4%;
  font-size: calc(var(--cell) * 0.42);
  line-height: 1.05;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
}

.cn.zero { color: var(--text-2); }

.map {
  position: absolute;
  right: 8%;
  bottom: 8%;
  height: calc(var(--cell) * var(--map, 0.44));
  width: auto;
  overflow: visible;
}

.map .on { fill: var(--text); }
.map .off { fill: #3f3f46; }
.map .self { fill: none; stroke: var(--accent-text); stroke-width: 1; }

/* face-down marks */
.mark {
  position: absolute;
  right: 6%;
  top: 6%;
  width: calc(var(--cell) * 0.26);
  height: calc(var(--cell) * 0.26);
  display: none;
}

.flag-b .back .mark, .flag-s .back .mark,
.flag-b .front .mark, .flag-s .front .mark { display: block; }

.flag-b .back { background: #121214; border-style: dashed; border-color: #8a8a93; }
.flag-b .back .val { color: #6b6b74; }
.flag-b .mark { color: #e4e4e7; }
.flag-s .back { border-color: #1f6f53; }
.flag-s .mark { color: var(--accent-text); }

.mark .o { display: none; }
.flag-s .mark .o { display: inline; }
.flag-s .mark .x { display: none; }

/* region highlight while a clue is selected */
.card.in-region .face { box-shadow: inset 0 0 0 2px var(--text); }
.card.sel .front { box-shadow: inset 0 0 0 2px var(--accent-text); border-color: var(--accent); }

/* reveal faces */
.kind-bust .front {
  background: var(--bust-bg);
  border-color: var(--bust);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bust-text);
}

.kind-bust .front .burst { width: 54%; height: 54%; }
.kind-bust.hit .front { background: #4a1316; }

.kind-left .front {
  background: var(--surface);
  border-style: dashed;
  border-color: var(--border-2);
  display: flex;
  align-items: center;
  justify-content: center;
}

.kind-left .front .val { color: var(--text-3); }

.card.tremble { animation: tremble 0.3s ease-in-out; z-index: 2; }

@keyframes tremble {
  0% { transform: scale(1); }
  20% { transform: scale(1.07) rotate(-2.5deg); }
  40% { transform: scale(1.07) rotate(2.5deg); }
  60% { transform: scale(1.08) rotate(-2deg); }
  80% { transform: scale(1.08) rotate(1.5deg); }
  100% { transform: scale(1.04); }
}

.card.nudge { animation: nudge 0.3s ease-in-out; }

@keyframes nudge {
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.card.enter { animation: enter 0.32s cubic-bezier(0.2, 0.8, 0.3, 1) both; }

@keyframes enter {
  from { opacity: 0; transform: translateY(10px) scale(0.92); }
  to { opacity: 1; transform: none; }
}

.marking .card:not(.up) .back { border-style: dashed; }

.float {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent-text);
  transform: translate(-50%, -50%);
  animation: float-up 0.7s ease-out forwards;
}

@keyframes float-up {
  from { opacity: 0; transform: translate(-50%, -30%); }
  20% { opacity: 1; }
  to { opacity: 0; transform: translate(-50%, -160%); }
}

.caption {
  flex: none;
  width: 100%;
  height: 22px;
  margin: 10px 0 0;
  text-align: center;
  font-size: 14px;
  line-height: 22px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.caption.warn { color: var(--text); }

/* ---------- dock ---------- */

.dock {
  flex: none;
  height: 74px;
  padding-top: 8px;
}

.dock-live, .dock-result {
  height: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
}

.btn-flag {
  width: 52px;
  height: 52px;
  border-radius: 12px;
}

.btn-flag svg { width: 22px; height: 22px; }
.btn-flag[aria-pressed="true"] { background: var(--text); border-color: var(--text); color: #09090b; }

.pot {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.pot-n {
  display: inline-block;
  align-self: flex-start;
  font-size: 40px;
  line-height: 1;
  font-weight: 700;
  letter-spacing: -0.035em;
  transform-origin: left center;
  transition: color 0.2s;
}

.pot-n.drain { color: var(--bust-text); }
.pot-n.gone { opacity: 0.25; }

.pot-sub { margin-top: 4px; font-size: 13px; color: var(--text-3); white-space: nowrap; }

.mult {
  display: inline-block;
  margin-left: 4px;
  color: var(--accent-text);
  font-weight: 650;
  transition: transform 0.15s;
}

.mult.bump { transform: scale(1.3); }

.btn {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  height: 54px;
  padding: 0 20px;
  font: 600 16px/1 var(--font);
  letter-spacing: -0.005em;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s, color 0.15s, border-color 0.15s, transform 0.1s, opacity 0.25s;
}

.btn:active:not(:disabled) { transform: scale(0.97); }

.btn-bank {
  flex: none;
  width: 132px;
  height: 56px;
  font-size: 17px;
  background: var(--accent);
  color: #03150e;
}

.btn-bank:disabled {
  background: var(--surface-2);
  border-color: var(--border);
  color: #5b5b63;
  cursor: default;
}

.btn-primary {
  background: var(--text);
  color: #09090b;
}

.dock-result .btn-primary { flex: none; min-width: 132px; }

.result {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.result-title {
  font-size: 26px;
  line-height: 1.05;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.result-title.good { color: var(--accent-text); }
.result-title.bad { color: var(--bust-text); }
.result-sub { font-size: 13px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.pending { opacity: 0; pointer-events: none; }
.armed-later { pointer-events: none; }

/* ---------- end screen ---------- */

.end {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-top: 10px;
}

.end-head { display: flex; align-items: baseline; gap: 14px; }

.end-total {
  font-size: 60px;
  line-height: 1;
  font-weight: 700;
  letter-spacing: -0.04em;
}

.end-tier {
  font-size: 22px;
  font-weight: 650;
  color: #e4e4e7;
}

.end-sub { margin: 8px 0 14px; font-size: 14px; color: var(--text-3); }

.ladder {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-bottom: 20px;
}

.ladder div { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: #5b5b63; }
.ladder i { height: 4px; border-radius: 2px; background: #2a2a2f; }
.ladder .reached i { background: #6b6b74; }
.ladder .here { color: var(--text); font-weight: 600; }
.ladder .here i { background: var(--text); }
.ladder span { white-space: nowrap; }
.ladder small { color: #5b5b63; font-weight: 400; margin-left: 4px; }

.end-rows { display: flex; flex-direction: column; gap: 8px; }

.end-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  min-height: 72px;
  padding: 10px 14px 10px 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.thumb { display: grid; gap: 2px; width: 55px; justify-content: start; }
.thumb i { width: 9px; height: 9px; border-radius: 2px; background: #2a2a2f; }
.thumb i.f { background: #a1a1aa; }
.thumb i.o { background: #5b5b63; }
.thumb i.b { background: var(--bust); }
.thumb i.l { background: transparent; box-shadow: inset 0 0 0 1px #3a3a41; }

.end-row-label { font-size: 14px; color: #d4d4d8; }
.end-row-label span { display: block; font-size: 12px; color: var(--text-3); margin-top: 2px; }
.end-row-score { font-size: 20px; font-weight: 650; letter-spacing: -0.02em; }
.end-row-score.bad { color: var(--bust-text); font-size: 16px; font-weight: 600; }

.end-streak { margin: 18px 0 0; font-size: 14px; color: var(--text-2); }
.end-streak b { color: var(--text); font-weight: 650; }

.end-actions {
  margin-top: auto;
  padding-top: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.end-actions .btn { width: 100%; }

.countdown { margin: 0; text-align: center; font-size: 13px; color: var(--text-3); }
.countdown b { color: var(--text-2); font-weight: 600; }
.countdown a { color: var(--text); }

/* ---------- hint ---------- */

.hint {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) 16px max(16px, env(safe-area-inset-bottom, 0px));
  background: rgba(9, 9, 11, 0.86);
  transition: opacity 0.2s;
}

.hint.out { opacity: 0; }

.hint-card {
  width: 100%;
  max-width: 380px;
  padding: 20px 18px 18px;
  border: 1px solid var(--border-2);
  border-radius: 16px;
  background: var(--surface);
}

.hint h2 { margin: 0 0 14px; font-size: 18px; font-weight: 650; letter-spacing: -0.01em; }

.hint-row { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }
.hint-row p { margin: 0; font-size: 14px; line-height: 1.4; color: #d4d4d8; }

.hint-ill { flex: none; width: 48px; display: flex; justify-content: center; }

.mini-card {
  --cell: 46px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  border: 1px solid #2e2e34;
  border-radius: 7px;
  background: var(--surface-2);
  font-size: 14px;
  font-weight: 600;
  color: #d4d4d8;
}

.mini-card.bust { background: var(--bust-bg); border-color: var(--bust); color: var(--bust-text); }
.mini-card.bust svg { width: 26px; height: 26px; }
.mini-card.face-up { background: var(--surface-3); border-color: #3f3f46; }
.mini-card.marked { border-style: dashed; border-color: #8a8a93; background: #121214; }
.mini-card .mk { width: 20px; height: 20px; color: #e4e4e7; }

.hint-foot { margin: 4px 0 16px; font-size: 13px; color: var(--text-3); }
.hint .btn { width: 100%; }

@media (prefers-reduced-motion: reduce) {
  .card.tremble, .card.enter, .board.shake, .card.nudge { animation: none; }
  .flipper { transition-duration: 0.01s; }
}
