/* Tumblers — mobile-first. One dark theme; the lock is the bright thing. */

:root {
  --bg: #0e1118;
  --panel: #161b26;
  --panel-2: #1e2533;
  --edge: #2b3446;
  --text: #e9edf5;
  --muted: #8d99ad;
  --brass: #e0a33a;
  --steel: #9fb0c6;
  --good: #5fd68a;
  --cell: clamp(40px, 12.5vw, 56px);
  --gap: clamp(4px, 1.6vw, 8px);
  --radius: 12px;
  --ease-out: cubic-bezier(0.2, 0.8, 0.3, 1);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  background: radial-gradient(120% 60% at 50% 0%, #1a2233 0%, var(--bg) 60%);
  overscroll-behavior-y: contain;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 12px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Header */
.topbar {
  display: grid;
  grid-template-columns: 36px 1fr 36px;
  align-items: center;
}

.topbar-title {
  text-align: center;
}

h1 {
  margin: 0;
  font-size: 26px;
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--brass);
  text-indent: 0.26em;
}

.subtitle {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.icon-btn {
  appearance: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--edge);
  background: var(--panel);
  color: var(--steel);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

/* Tabs */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 4px;
}

.tab {
  appearance: none;
  border: 0;
  border-radius: 8px;
  padding: 9px 4px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.tab[aria-selected="true"] {
  background: var(--panel-2);
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--edge);
}

.goal {
  margin: 0;
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  min-height: 1.5em;
}

.goal b {
  color: var(--text);
}

.practice {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  color: var(--muted);
}

select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--edge);
  border-radius: 8px;
  padding: 6px 8px;
  font: inherit;
  font-size: 14px;
}

/* Per-wheel role strip: what each column can and cannot do. */
.roles {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gap);
  padding-right: 22px;
  margin-bottom: 5px;
}

.role {
  /* Flex, not grid: the mark and the word must share one line. */
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: 18px;
  white-space: nowrap;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border-radius: 5px;
  border: 1px solid transparent;
}

.role-free {
  color: #6b7789;
}

.role-drives {
  color: var(--steel);
  background: rgb(159 176 198 / 0.09);
}

.role-pulled {
  color: #b9c6dc;
  background: rgb(159 176 198 / 0.16);
  border-color: rgb(159 176 198 / 0.28);
}

.role-seized {
  color: var(--brass);
  background: rgb(224 163 58 / 0.14);
  border-color: rgb(224 163 58 / 0.4);
}

/* Board */
.board {
  position: relative;
  background: linear-gradient(180deg, var(--panel) 0%, #121722 100%);
  border: 1px solid var(--edge);
  border-radius: 18px;
  padding: 10px var(--gap) 12px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.35);
  user-select: none;
  -webkit-user-select: none;
}

.chains {
  display: block;
  width: 100%;
  height: 76px;
}

.chain {
  fill: none;
  stroke-width: 2.5;
  stroke-linecap: round;
  opacity: 0.9;
}

/* A chain into a seized wheel is the only way that wheel ever moves. */
.chain-rigid {
  stroke-width: 4;
  opacity: 1;
}

/* While previewing a turn, fade the chains it will not pull. */
.previewing .chain,
.previewing .chain-head,
.previewing .chain-label {
  opacity: 0.18;
  transition: opacity 120ms;
}

.previewing .chain.live {
  opacity: 1;
  stroke-width: 4.5;
}

.chain-label {
  font: 600 12px ui-sans-serif, system-ui, sans-serif;
  text-anchor: middle;
  /* Outline the glyphs in the panel colour so they stay readable over a chain. */
  paint-order: stroke fill;
  stroke: #161b26;
  stroke-width: 3px;
  stroke-linejoin: round;
}

.turnrow {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gap);
  padding-right: 22px;
}

.turn {
  appearance: none;
  border: 1px solid var(--edge);
  background: var(--panel-2);
  color: var(--steel);
  border-radius: 8px;
  padding: 6px 0;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}

.turn:active:not(:disabled) {
  background: #2a3346;
  color: var(--brass);
}

.turn:disabled {
  opacity: 0.5;
  color: #5b667a;
  cursor: not-allowed;
}

.turnrow-up {
  margin-bottom: 6px;
}

.turnrow-down {
  margin-top: 6px;
}

.drums {
  position: relative;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gap);
  padding-right: 22px;
}

.drum {
  position: relative;
  height: calc(var(--cell) * 6);
  border-radius: 10px;
  background: linear-gradient(90deg, #0b0e14 0%, #202939 18%, #2b3546 50%, #202939 82%, #0b0e14 100%);
  overflow: hidden;
  cursor: grab;
  /* The drum owns vertical gestures; the page keeps scrolling everywhere else. */
  touch-action: none;
  transition: opacity 120ms, box-shadow 120ms;
}

.drum:active {
  cursor: grabbing;
}

.drum.seized {
  background: linear-gradient(90deg, #0b0e14 0%, #262323 18%, #3a3128 50%, #262323 82%, #0b0e14 100%);
  box-shadow: inset 0 0 0 1px rgb(224 163 58 / 0.3);
  cursor: not-allowed;
}

/*
 * The strip is eight cells tall, two more than the drum shows, and rests one
 * cell up so a spare letter waits just off both edges. --drag slides it live
 * under a finger; .settling eases it home.
 */
.strip {
  display: grid;
  grid-template-rows: repeat(8, var(--cell));
  transform: translateY(calc(var(--cell) * -1 + var(--drag, 0px)));
  will-change: transform;
}

.strip.settling {
  transition: transform 150ms var(--ease-out);
}

.cell {
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * 0.46);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--steel);
  transition: color 160ms;
}

.cell-back {
  color: #55637a;
  font-size: calc(var(--cell) * 0.36);
}

.cell-window {
  color: var(--text);
}

/* In Combination mode the outer rows do not count: let them step back. */
.cell-window.cell-optional {
  color: #7c889b;
}

.cell-window.cell-valid {
  color: var(--good);
}

/* Preview: the wheel being driven, and everything its chains drag along. */
.drum.driver {
  box-shadow: inset 0 0 0 2px var(--brass), 0 0 16px rgb(224 163 58 / 0.25);
}

.drum.dragged {
  box-shadow: inset 0 0 0 2px var(--steel), 0 0 16px rgb(159 176 198 / 0.2);
}

.previewing .drum:not(.driver):not(.dragged) {
  opacity: 0.45;
}

/* Keyboard: the wheel the arrow keys will turn. */
.drum.selected {
  box-shadow: inset 0 0 0 2px var(--steel);
}

/* Hint: the arrow to press pulses until the player moves. */
@keyframes hint-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgb(224 163 58 / 0.7);
  }
  50% {
    box-shadow: 0 0 0 6px rgb(224 163 58 / 0);
  }
}

.turn.hinted {
  color: var(--brass);
  border-color: var(--brass);
  animation: hint-pulse 1.1s ease-out infinite;
}

.drum.hinted {
  box-shadow: inset 0 0 0 2px rgb(224 163 58 / 0.6);
}

/* The three-row window, framed over the drums. */
.window {
  position: absolute;
  inset: var(--cell) 22px auto 0;
  height: calc(var(--cell) * 3);
  border: 2px solid var(--brass);
  border-radius: 10px;
  pointer-events: none;
  box-shadow: 0 0 0 1px rgb(0 0 0 / 0.5), 0 0 22px rgb(224 163 58 / 0.12) inset;
  transition: border-color 160ms, box-shadow 160ms;
}

.window-solved {
  border-color: var(--good);
  box-shadow: 0 0 0 1px rgb(0 0 0 / 0.5), 0 0 26px rgb(95 214 138 / 0.22) inset;
}

/* The unlock moment: the frame flares and the letters take a bow, column by column. */
@keyframes window-flare {
  0% {
    box-shadow: 0 0 0 1px rgb(0 0 0 / 0.5), 0 0 0 0 rgb(95 214 138 / 0.8);
  }
  100% {
    box-shadow: 0 0 0 1px rgb(0 0 0 / 0.5), 0 0 0 18px rgb(95 214 138 / 0);
  }
}

@keyframes letter-bow {
  0%,
  100% {
    transform: translateY(0) scale(1);
  }
  35% {
    transform: translateY(-14%) scale(1.14);
  }
}

.unlocked .window {
  animation: window-flare 900ms ease-out;
}

.unlocked .cell-window:not(.cell-optional) {
  animation: letter-bow 560ms var(--ease-out);
  animation-delay: calc(var(--i) * 55ms);
}

.rowmark {
  position: absolute;
  right: 0;
  top: calc(var(--cell) * (1 + var(--row)));
  height: var(--cell);
  width: 20px;
  display: grid;
  place-items: center;
  font-size: 14px;
  color: var(--good);
  opacity: 0;
  pointer-events: none;
}

.rowmark.required {
  opacity: 0.35;
}

.rowmark.required::before {
  content: "•";
  position: absolute;
  color: var(--muted);
}

.rowmark.required.valid {
  opacity: 1;
}

.rowmark.required.valid::before {
  content: "";
}

.lock-info,
.hint-note {
  margin: -4px 0 0;
  text-align: center;
  font-size: 13px;
  color: var(--brass);
}

/* Theme clue */
.theme {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 14px;
}

.theme-clue {
  color: var(--brass);
  font-weight: 600;
}

/* Stats + controls */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
}

.stat {
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 8px 4px;
}

.stat span {
  display: block;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat b {
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

button.ghost,
button.primary {
  appearance: none;
  border-radius: 10px;
  padding: 11px 8px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

button.ghost {
  background: var(--panel);
  border: 1px solid var(--edge);
  color: var(--text);
}

button.ghost.armed {
  border-color: #e07a7a;
  color: #e07a7a;
}

button.primary {
  background: var(--brass);
  border: 1px solid var(--brass);
  color: #21180a;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Result */
@keyframes rise-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

.result {
  background: var(--panel);
  border: 1px solid var(--edge);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
  animation: rise-in 260ms var(--ease-out);
}

.result h2 {
  margin: 0 0 4px;
  font-size: 19px;
}

.stars {
  margin: 0 0 8px;
  font-size: 24px;
  letter-spacing: 0.1em;
}

.answer {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--muted);
}

.answer b {
  color: var(--text);
  letter-spacing: 0.08em;
}

.share {
  margin: 0 0 12px;
  padding: 10px;
  background: #0c1017;
  border: 1px solid var(--edge);
  border-radius: 10px;
  text-align: left;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  color: var(--text);
}

.result-actions {
  display: grid;
  gap: 8px;
}

/* Help dialog */
.help {
  width: min(92vw, 440px);
  max-height: 88dvh;
  overflow: auto;
  border: 1px solid var(--edge);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 18px 18px 14px;
  font-size: 14px;
}

.help::backdrop {
  background: rgb(6 8 12 / 0.7);
  backdrop-filter: blur(3px);
}

.help h2 {
  margin: 0 0 8px;
  font-size: 18px;
  color: var(--brass);
}

.help p {
  margin: 8px 0 0;
  color: #c6cfdd;
}

.help b {
  color: var(--text);
}

.help .k-free {
  color: #6b7789;
}

.help .k-drives,
.help .k-pulled {
  color: var(--steel);
}

.help .k-seized {
  color: var(--brass);
}

.help-keys {
  font-size: 12px;
  color: var(--muted);
}

.help form {
  margin-top: 14px;
}

.help button {
  width: 100%;
}

.loading {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

[hidden] {
  display: none !important;
}

@media (prefers-reduced-motion: reduce) {
  .strip.settling,
  .result,
  .unlocked .window,
  .unlocked .cell-window,
  .turn.hinted {
    transition: none;
    animation: none;
  }
}
