:root {
  --color-bg: #eef7f6;
  --color-surface: #ffffff;
  --color-text: #1f3a3a;
  --color-muted: #6b8f8a;
  --color-primary: #ff6b5b;
  --color-primary-dark: #e14f3f;
  --color-secondary: #2a9d8f;
  --color-accent: #ffb703;
  --color-board: #dff3f0;
  --color-tile-bg: #fffaf0;
  --color-tile-text: #1f3a3a;
  --color-tile-border: #cdeae5;
  --color-warn: #ffb703;
  --color-danger: #e63946;
  --color-border: #d9ece9;
  --shadow-soft: 0 6px 16px rgba(31, 58, 58, 0.12);
  --shadow-tile: 0 3px 0 var(--color-tile-border);
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #10201f;
    --color-surface: #17302e;
    --color-text: #eaf6f4;
    --color-muted: #8fb3ae;
    --color-primary: #ff8a7a;
    --color-primary-dark: #ff6b5b;
    --color-secondary: #4fc7b8;
    --color-accent: #ffcc4d;
    --color-board: #1c3735;
    --color-tile-bg: #21403d;
    --color-tile-text: #eaf6f4;
    --color-tile-border: #0f2523;
    --color-warn: #ffcc4d;
    --color-danger: #ff6b6b;
    --color-border: #234745;
    --shadow-soft: 0 6px 16px rgba(0, 0, 0, 0.35);
    --shadow-tile: 0 3px 0 var(--color-tile-border);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

img {
  max-width: 100%;
}

button {
  font: inherit;
  color: inherit;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(14px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  gap: 12px;
}

/* ---------- topbar ---------- */

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  grid-column: 1;
  justify-self: start;
}

.brand-mark {
  border-radius: 8px;
}

.brand-name {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  color: var(--color-primary-dark);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  grid-column: 2;
  justify-self: center;
}

/* ---------- icon buttons ---------- */

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--color-surface);
  box-shadow: var(--shadow-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease;
}

.icon-btn img {
  width: 22px;
  height: 22px;
}

.icon-btn.ghost {
  background: transparent;
  box-shadow: none;
  border: 2px solid var(--color-border);
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn[hidden] {
  display: none !important;
}

/* A single-color icon recolored exactly via mask instead of an approximate
   filter() guess on a black-stroke image — crisp in both themes. */
.icon-mask {
  display: inline-block;
  width: 22px;
  height: 22px;
  background-color: var(--color-secondary);
  mask-size: contain;
  mask-repeat: no-repeat;
  mask-position: center;
  -webkit-mask-size: contain;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-position: center;
}

#demoBtn.active {
  background: var(--color-secondary);
}
#demoBtn.active .icon-mask {
  background-color: #ffffff;
}

.icon-btn:disabled {
  opacity: 0.35;
  pointer-events: none;
}

/* ---------- stage ---------- */

.stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3vh, 26px);
  min-height: 0;
}

/* ---------- timer (collapsible pill + sliding drawer) ---------- */

.timer-control {
  display: flex;
  align-items: center;
  background: var(--color-surface);
  box-shadow: var(--shadow-soft);
  border-radius: 999px;
  flex: 0 0 auto;
  overflow: hidden;
}

.timer-control[hidden] {
  display: none !important;
}

.timer-summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px 10px 22px;
  flex: 0 0 auto;
}

.timer-icon {
  width: 24px;
  height: 24px;
  filter: invert(60%) sepia(30%) saturate(400%) hue-rotate(130deg);
}

.timer-label {
  font-size: clamp(1.6rem, 5vh, 2.2rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--color-secondary);
  min-width: 2.6ch;
  text-align: center;
}

.timer-toggle {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 auto;
}

.timer-toggle img {
  width: 20px;
  height: 20px;
  filter: invert(60%) sepia(10%) saturate(300%) hue-rotate(130deg);
  transition: transform 0.22s ease;
}

.timer-control.drawer-open .timer-toggle img {
  transform: rotate(90deg);
}

.timer-control.locked .timer-summary .timer-toggle {
  opacity: 0.35;
  pointer-events: none;
}

.timer-reshuffle {
  display: flex;
  align-items: center;
  max-width: 0;
  overflow: hidden;
  padding-left: 0;
  transition: max-width 0.28s ease, padding-left 0.28s ease;
}

.timer-control.round-active .timer-reshuffle {
  max-width: 50px;
  padding-left: 8px;
}

.reshuffle-btn .icon-mask {
  background-color: var(--color-primary);
}

.timer-drawer {
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 0;
  overflow: hidden;
  padding-right: 0;
  transition: max-width 0.28s ease, padding-right 0.28s ease;
}

.timer-control.drawer-open .timer-drawer {
  max-width: 260px;
  padding-right: 10px;
}

.timer-drawer .chip {
  width: 34px;
  height: 34px;
  font-size: 0.9rem;
  flex: 0 0 auto;
}

.timer-control.warn .timer-label,
.timer-control.warn .timer-icon {
  color: var(--color-warn);
}
.timer-control.warn .timer-icon {
  filter: invert(70%) sepia(60%) saturate(700%) hue-rotate(0deg);
}

.timer-control.danger {
  animation: pulse 1s ease-in-out infinite;
}
.timer-control.danger .timer-label {
  color: var(--color-danger);
}
.timer-control.danger .timer-icon {
  filter: invert(35%) sepia(70%) saturate(2000%) hue-rotate(330deg);
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.06);
  }
}

/* ---------- demo exit button (replaces the timer pill during a demo) ---------- */

.demo-exit {
  background: var(--color-secondary);
}

.demo-exit img {
  filter: invert(100%);
}

/* ---------- play area (board + demo word list) ---------- */

.play-area {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3vh, 26px);
  min-height: 0;
}

.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

/* ---------- board ---------- */

.board-wrap {
  position: relative;
  width: min(92vw, 92dvh, 480px);
  aspect-ratio: 1;
  flex: 0 1 auto;
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(8px, 2.4vw, 16px);
  position: relative;
  z-index: 0;
  touch-action: none;
}

.path-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
}

.path-overlay[hidden] {
  display: none !important;
}

.path-overlay polyline {
  fill: none;
  stroke: var(--color-primary);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.45;
}

.path-overlay circle {
  fill: var(--color-primary);
  opacity: 0.4;
}

.tile.trace-active {
  background: var(--color-accent);
  color: var(--color-tile-text);
  position: relative;
  transition: background 0.2s ease;
}

.trace-badge {
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 0.7rem;
  font-weight: 800;
  color: var(--color-primary-dark);
  line-height: 1;
}

/* ---------- demo word list ---------- */

.demo-words {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 160px;
  flex: 0 0 auto;
}

.demo-words[hidden] {
  display: none !important;
}

.demo-word {
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--color-surface);
  box-shadow: var(--shadow-soft);
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: 0.02em;
  color: var(--color-tile-border);
  text-align: center;
  transition: background 0.25s ease, color 0.25s ease, transform 0.25s ease;
}

.demo-word.revealed {
  background: var(--color-secondary);
  color: #ffffff;
  transform: scale(1.05);
}

@media (max-width: 620px) {
  .demo-words {
    flex-direction: row;
    flex-wrap: wrap;
    width: min(92vw, 480px);
    justify-content: center;
  }
  .demo-word {
    flex: 1 1 calc(33% - 8px);
  }
}

/* ---------- found words (touch-trace mode) ---------- */

.found-words {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 160px;
  max-height: min(70vh, 480px);
  overflow-y: auto;
  flex: 0 0 auto;
}

.found-words[hidden] {
  display: none !important;
}

.found-word {
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--color-secondary);
  color: #ffffff;
  box-shadow: var(--shadow-soft);
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: 0.02em;
  text-align: center;
  flex: 0 0 auto;
  animation: word-pop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes word-pop {
  0% {
    transform: scale(0.6);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.found-word.pulse {
  animation: word-pulse 0.4s ease;
}

@keyframes word-pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
}

@media (max-width: 620px) {
  .found-words {
    flex-direction: row;
    flex-wrap: wrap;
    width: min(92vw, 480px);
    max-height: 40vh;
    justify-content: center;
  }
  .found-word {
    flex: 1 1 calc(33% - 8px);
  }
}

.tile.trace-success {
  background: var(--color-secondary);
  color: #ffffff;
}

.tile.trace-reject {
  background: var(--color-danger);
  color: #ffffff;
  animation: trace-shake 0.3s ease;
}

@keyframes trace-shake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .found-word,
  .found-word.pulse {
    animation: none;
  }
  .tile.trace-reject {
    animation: none;
  }
}

.tile {
  background: var(--color-tile-bg);
  border-radius: 16px;
  box-shadow: var(--shadow-tile);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.6rem, 8vw, 2.8rem);
  font-weight: 800;
  color: var(--color-tile-text);
  user-select: none;
  transform: translateY(0) scale(1);
}

.tile.tumbling {
  animation: wiggle 0.24s ease-in-out infinite;
}

@keyframes wiggle {
  0%,
  100% {
    transform: rotate(-3deg) scale(1.02);
  }
  50% {
    transform: rotate(3deg) scale(1.02);
  }
}

.tile.settled {
  animation: settle 0.36s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes settle {
  0% {
    transform: scale(1.25);
  }
  100% {
    transform: scale(1);
  }
}

.tile.blank {
  color: var(--color-tile-border);
}

@media (prefers-reduced-motion: reduce) {
  .tile.tumbling {
    animation: none;
    opacity: 0.6;
  }
  .tile.settled {
    animation: none;
  }
  .timer-control.danger {
    animation: none;
  }
  .timer-drawer,
  .timer-toggle img {
    transition: none;
  }
}

/* ---------- board overlay (shuffle button, shown whenever a round isn't in progress) ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  background: color-mix(in srgb, var(--color-bg) 88%, transparent);
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  animation: fadein 0.25s ease;
}

.overlay[hidden] {
  display: none !important;
}

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.overlay-icon {
  width: 64px;
  height: 64px;
  filter: invert(55%) sepia(90%) saturate(500%) hue-rotate(-10deg);
}

/* ---------- minute chips (inside the timer drawer) ---------- */

.chip {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid var(--color-border);
  background: var(--color-surface);
  font-weight: 800;
  font-size: 1.05rem;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease, color 0.12s ease;
}

.chip.selected {
  background: var(--color-secondary);
  border-color: var(--color-secondary);
  color: #ffffff;
}

.chip:active {
  transform: scale(0.92);
}

/* ---------- fab ---------- */

.fab {
  border: none;
  border-radius: 50%;
  background: var(--color-primary);
  box-shadow: var(--shadow-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
}

.fab img {
  filter: invert(100%);
}

.fab-main {
  width: 78px;
  height: 78px;
}

.fab-main img {
  width: 34px;
  height: 34px;
}

.fab:active {
  transform: scale(0.92);
}

.fab:hover {
  background: var(--color-primary-dark);
}

@media (max-width: 380px) {
  .brand-name {
    font-size: 1.05rem;
  }
}
