* { margin: 0; padding: 0; box-sizing: border-box; touch-action: manipulation; }

:root {
  --tile-size: clamp(40px, 10vw, 62px);
  --tile-gap: clamp(3px, 0.8vw, 5px);
  --tile-font: clamp(1.2rem, 4vw, 2rem);
  --gold: #d4a017;
  --red: #c0392b;
  --locked-bg: #2a2a2c;
  --active-border: #565658;
  --bg: #121213;
  --text: #ffffff;
  --border: #3a3a3c;
  --scramble-bg: #3a3a3c;
  --ctrl-bg: #818384;
  --muted: #888;
}

[data-theme="light"] {
  --locked-bg: #e0e0e0;
  --active-border: #878a8c;
  --bg: #f7f7f8;
  --text: #1a1a1b;
  --border: #d3d6da;
  --scramble-bg: #d3d6da;
  --ctrl-bg: #878a8c;
  --muted: #666;
}

body {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
  padding: 1.5rem 1rem 0;
  overflow-x: hidden;
}

/* Header */
header {
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem 0;
  position: relative;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.75rem;
  gap: 0.4rem;
}

.header-nav {
  position: absolute;
  right: 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  z-index: 10;
}

.header-nav-left {
  position: absolute;
  left: 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  z-index: 10;
}

.header-nav button,
.header-nav-left button {
  all: unset;
  cursor: pointer;
  font-size: 1.4rem;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

h1 {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(4px, 1vw, 8px);
}

.title-row {
  display: flex;
  gap: clamp(2px, 0.5vw, 4px);
}

.title-tile {
  display: flex;
  align-items: center;
  justify-content: center;
  width: clamp(28px, 6.5vw, 40px);
  height: clamp(28px, 6.5vw, 40px);
  border-radius: 8px;
  font-size: clamp(0.85rem, 3.2vw, 1.25rem);
  font-weight: bold;
  border: 2px solid #000;
}

.title-tile.red { background: var(--red); color: #ffffff; }
.title-tile.gold { background: var(--gold); color: #000; }


#timer {
  font-size: clamp(1.2rem, 4.5vw, 1.8rem);
  font-weight: bold;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.1em;
  color: var(--gold);
  margin-bottom: 0.5rem;
}

[data-theme="light"] #timer {
  color: var(--red);
}

[data-theme="light"] #hint {
  color: #000;
}

[data-theme="light"] .instruction {
  color: var(--red);
}

/* Toast */
#toast {
  color: #ffffff;
  padding: 0.4rem 1.2rem;
  border-radius: 6px;
  font-weight: bold;
  font-size: clamp(0.85rem, 3vw, 1rem);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
  min-height: 2rem;
  line-height: 1.4;
}

#toast.correct { background: #2e7d32; }
#toast.incorrect { background: var(--red); }

#toast.show {
  opacity: 1;
}

/* Game area */
#game {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 500px;
  flex: 1;
}

/* Clue area (fixed height to prevent layout shift) */
#clue-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: calc(var(--tile-size) + 2.8em + 1rem);
  justify-content: center;
}

/* Hint */
#hint {
  font-size: clamp(1rem, 3.5vw, 1.3rem);
  font-weight: bold;
  color: #ffffff;
  text-align: center;
  margin-bottom: 0.5rem;
  min-height: 2.8em;
  font-style: italic;
  max-width: 100%;
}

#victory {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(2px, 0.5vw, 4px);
  margin-bottom: 0.75rem;
  max-width: 100%;
}

#victory .title-tile {
  width: clamp(28px, 6.5vw, 40px);
  height: clamp(28px, 6.5vw, 40px);
  font-size: clamp(0.85rem, 3.2vw, 1.25rem);
}

#victory .title-space {
  width: clamp(8px, 2vw, 14px);
}

/* Scrambled letters */
#scramble {
  display: flex;
  gap: var(--tile-gap);
  margin-bottom: 1rem;
  min-height: var(--tile-size);
}

.scramble-tile {
  width: var(--tile-size);
  height: var(--tile-size);
  background: var(--scramble-bg);
  border: 2px solid #000;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--tile-font);
  font-weight: bold;
  text-transform: uppercase;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  transition: opacity 0.15s, transform 0.15s;
}

.scramble-tile.used {
  opacity: 0.25;
  pointer-events: none;
  transform: scale(0.9);
}

.instruction {
  font-size: clamp(0.8rem, 3.2vw, 1.3rem);
  font-weight: bold;
  color: var(--gold);
  text-align: center;
  font-style: italic;
  margin-top: 0.5rem;
  white-space: nowrap;
}

/* Board */
#board {
  display: flex;
  flex-direction: column;
  gap: var(--tile-gap);
  margin-bottom: 0.75rem;
}

.row {
  display: flex;
  gap: var(--tile-gap);
}

.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  border: 2px solid var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--tile-font);
  font-weight: bold;
  text-transform: uppercase;
  transition: background-color 0.3s, border-color 0.3s;
}

/* Row states */
.row.locked .tile {
  background: var(--locked-bg);
  border-color: var(--locked-bg);
  color: #888;
}

.row.active .tile {
  background: transparent;
  border-color: var(--active-border);
  color: var(--text);
}

.row.active .tile.filled {
  border-color: #888;
}

/* Solved tile colours */
.tile.gold {
  background: var(--gold);
  border-color: #000;
  color: #000;
}

.tile.red {
  background: var(--red);
  border-color: #000;
  color: #ffffff;
}

/* Divider */
#divider {
  width: 80%;
  height: 1px;
  background: var(--border);
  margin: 0.75rem 0;
}

/* Final row */
#final-label {
  font-size: clamp(0.7rem, 2.5vw, 0.85rem);
  letter-spacing: 0.2em;
  color: var(--muted);
  margin-bottom: 0.4rem;
}

#final-row {
  margin-bottom: 0.75rem;
}

/* Gold tiles tappable in final round */
.tile.gold.tappable {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s, transform 0.15s;
}

.tile.gold.tappable.used {
  opacity: 0.4;
  pointer-events: none;
  transform: scale(0.9);
}

/* Controls (Enter + Backspace) */
#controls {
  display: flex;
  gap: clamp(8px, 2vw, 12px);
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 500px;
  padding: 1.5rem 1rem calc(1.5rem + env(safe-area-inset-bottom, 0px));
  margin-top: auto;
  position: relative;
}

.ctrl-btn {
  height: clamp(40px, 7vh, 50px);
  padding: 0 1.2rem;
  border: none;
  border-radius: 4px;
  background: var(--ctrl-bg);
  color: #ffffff;
  font-size: clamp(0.8rem, 2.5vw, 1rem);
  font-weight: bold;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* Shuffle button */
#btn-shuffle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  color: var(--text);
  align-items: center;
  justify-content: center;
}

/* Home button */
#btn-home {
  position: fixed;
  top: 1.5rem;
  left: 1rem;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  -webkit-tap-highlight-color: transparent;
  z-index: 10;
}

/* Animations */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.row.shake {
  animation: shake 0.3s ease;
}

@keyframes flip {
  0% { transform: scaleY(1); }
  50% { transform: scaleY(0); }
  100% { transform: scaleY(1); }
}

.tile.flip {
  animation: flip 0.25s ease;
}

@keyframes shuffle-flip {
  0% { transform: scaleY(1); }
  50% { transform: scaleY(0); }
  100% { transform: scaleY(1); }
}

.scramble-tile.shuffle-flip {
  animation: shuffle-flip 0.15s ease;
}

/* Responsive tweaks */
@media (max-width: 360px) {
  :root {
    --tile-size: 38px;
    --tile-gap: 3px;
  }
}

/* Rotate overlay for phones in landscape */

/* Instructions Modal */
#instructions-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}
#instructions-modal-overlay.show {
  display: flex;
}
#instructions-modal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem 1.5rem;
  max-width: clamp(320px, 50vw, 480px);
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  position: relative;
}
#instructions-modal h2 {
  font-size: 1.1rem;
  color: var(--text);
}
#instructions-modal p {
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.5;
}
#instructions-modal .modal-close,
#stats-modal .modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
.example-hint {
  font-size: 0.9rem;
  font-weight: bold;
  font-style: italic;
  color: var(--text);
}
.example-row {
  display: flex;
  gap: 3px;
  justify-content: center;
}
.example-tile {
  width: 28px;
  height: 28px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: bold;
  border: 2px solid #000;
}
.example-tile.scramble {
  background: var(--scramble-bg);
  color: var(--text);
}
.example-tile.gold {
  background: var(--gold);
  color: #000;
}
.example-tile.red {
  background: var(--red);
  color: #fff;
}
.example-tile.empty {
  background: transparent;
  border-color: var(--active-border);
}
.example-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin: 0 auto;
  width: fit-content;
}
.example-divider {
  display: none;
}
.example-label {
  font-size: 0.65rem;
  letter-spacing: 0.2em;
  color: var(--muted);
  margin-top: -0.5rem;
  margin-bottom: -0.8rem;
}
.timer-list {
  list-style: none;
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.8;
  margin-top: -0.8rem;
}
.gold-text {
  color: var(--gold);
}

/* Stats modal */
#stats-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2000;
  align-items: center;
  justify-content: center;
}
#stats-modal-overlay.show {
  display: flex;
}
#stats-modal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem 1.5rem;
  max-width: clamp(320px, 50vw, 480px);
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  position: relative;
}
#stats-modal h2, #stats-modal h3 {
  color: var(--text);
  margin: 0;
}
#stats-modal h2 {
  font-size: 1.1rem;
  letter-spacing: 0.1em;
}
#stats-modal h3 {
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  margin-top: 0.5rem;
}
#stats-summary {
  display: flex;
  justify-content: center;
  gap: 1rem;
}
.stat-box {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.stat-value {
  font-size: 1.4rem;
  font-weight: bold;
  color: var(--text);
}
.stat-label {
  font-size: 0.65rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
#speed-distribution {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  text-align: left;
}
.speed-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.speed-label {
  font-size: 0.75rem;
  color: var(--text);
  min-width: 6.5rem;
  white-space: nowrap;
}
.speed-time {
  color: var(--muted);
  font-size: 0.65rem;
  margin-left: 0.3rem;
}
.speed-bar-container {
  flex: 1;
  height: 1.4rem;
  position: relative;
}
.speed-bar {
  height: 100%;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 0.4rem;
  min-width: 1.4rem;
  font-size: 0.7rem;
  font-weight: bold;
  color: #fff;
}
.speed-bar.blitzed { background: var(--gold); color: #000; }
.speed-bar.fast { background: #e67e22; }
.speed-bar.completed { background: #2e7d32; }
.speed-bar.out-of-time { background: var(--red); }
#today-result {
  font-size: 0.85rem;
  color: var(--text);
  padding: 0.5rem;
  border-radius: 8px;
  background: var(--scramble-bg);
}
#today-result:empty {
  display: none;
}
.share-btn {
  background: #2e7d32;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.75rem 1.5rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.05em;
}
.share-btn:active {
  opacity: 0.8;
}

.stats-tab {
  all: unset;
  cursor: pointer;
  padding: 0.4rem 1rem;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  border: 1px solid var(--muted);
  -webkit-tap-highlight-color: transparent;
}
.stats-tab.active {
  color: var(--text);
  border-color: var(--text);
  background: rgba(255,255,255,0.1);
}
[data-theme="light"] .stats-tab.active {
  background: rgba(0,0,0,0.05);
}

#rotate-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 9999;
  align-items: center;
  justify-content: center;
}

.rotate-message {
  font-size: 1.2rem;
  font-weight: bold;
  color: var(--text);
  text-align: center;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2rem;
}

.rotate-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

@media (orientation: landscape) and (max-height: 500px) {
  #rotate-overlay {
    display: flex;
  }
}

@media (min-width: 768px) {
  body { padding: 2rem 1rem 0.5rem; }
  header { margin-bottom: 1rem; }
  #timer { margin-bottom: 1rem; }
  #scramble { margin-bottom: 1.25rem; }
}

@media (min-width: 768px) and (orientation: landscape) {
  :root {
    --tile-size: clamp(38px, 7vw, 52px);
    --tile-gap: 4px;
    --tile-font: clamp(1.1rem, 3vw, 1.7rem);
  }
  body { padding: 1rem 1rem 0; }
  header { margin-bottom: 0.5rem; padding: 0.4rem 0; }
  #timer { margin-bottom: 1rem; }
  #clue-area { height: calc(var(--tile-size) + 2.6em + 0.75rem); }
  #board { margin-bottom: 0.5rem; }
  #divider { margin: 0.5rem 0; }
  #final-label { margin-bottom: 0.3rem; }
  #final-row { margin-bottom: 0.5rem; }
  #controls { padding: 0.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom, 0px)); }
}

/* Countdown overlay */
#countdown-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.countdown-number {
  font-size: clamp(4rem, 20vw, 8rem);
  font-weight: 700;
  color: var(--gold);
}

.countdown-pop {
  animation: countdown-pop 0.6s ease-out;
}

@keyframes countdown-pop {
  0% { transform: scale(1.4); opacity: 0.3; }
  50% { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.9); opacity: 0.7; }
}
