:root {
  --primary: #ffb84d;
  --primary-strong: #e07000;
  --success: #18a957;
  --warning: #f59e0b;
  --danger: #e54848;
  --ink: #2f2a24;
  --muted: #6f6557;
  --bg: #fffdf5;
  --surface: #ffffff;
  --surface-alt: #fff3d6;
  --border: #ecd9b0;
  --focus: #2f2a24;
  --shadow: 0 8px 24px rgba(48, 32, 8, 0.14);
  --radius: 18px;
}

body.dark {
  --primary: #ffb84d;
  --primary-strong: #ffcc6f;
  --success: #31c06f;
  --warning: #f7b949;
  --danger: #ff7676;
  --ink: #efe7d6;
  --muted: #b0a891;
  --bg: #191714;
  --surface: #24211c;
  --surface-alt: #2f2a24;
  --border: #3d382f;
  --focus: #ffdd9e;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: "Nunito", system-ui, sans-serif;
  background: radial-gradient(circle at top right, var(--surface-alt), var(--bg) 44%);
  color: var(--ink);
}

body.dyslexic {
  font-family: "OpenDyslexic", "Comic Sans MS", "Trebuchet MS", sans-serif;
}

button,
select,
input,
textarea,
a {
  font: inherit;
}

.skip-link {
  position: absolute;
  left: -999px;
  top: -999px;
}

.skip-link:focus {
  left: 12px;
  top: 12px;
  background: var(--surface);
  padding: 10px 14px;
  border-radius: 10px;
  border: 2px solid var(--focus);
  z-index: 50;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--primary);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.14);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex-wrap: nowrap;
}

.back-link,
.ghost-btn {
  color: var(--ink);
  text-decoration: none;
  background: rgba(255, 255, 255, 0.45);
  border: none;
  padding: 8px 10px;
  border-radius: 999px;
  font-weight: 800;
  min-height: 42px;
  font-size: clamp(14px, 3.8vw, 17px);
  white-space: nowrap;
  cursor: pointer;
}

.layout {
  width: min(1200px, 100%);
  margin: 10px auto;
  padding: 0 10px 16px;
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr;
}

.game-panel,
.side-panel .card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.game-panel {
  padding: 10px;
  display: grid;
  gap: 10px;
}

.duel-banner {
  background: linear-gradient(90deg, #ffe7b7, #ffd180);
  color: #4d2d00;
  padding: 9px 12px;
  border-radius: 12px;
  font-weight: 800;
}

.hidden {
  display: none !important;
}

.hud {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.stat {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-alt);
  border-radius: 12px;
  padding: 6px 8px;
  min-height: 50px;
}

.timer-wrap {
  grid-column: span 2;
}

.stat-label {
  color: var(--muted);
  font-weight: 700;
  font-size: 13px;
}

.stat-value {
  font-family: "Space Grotesk", "Nunito", sans-serif;
  font-size: 20px;
  font-weight: 700;
}

.timer-ring {
  width: 52px;
  height: 52px;
  transform: rotate(-90deg);
}

.timer-ring-bg,
.timer-ring-progress {
  fill: none;
  stroke-width: 8;
}

.timer-ring-bg {
  stroke: rgba(0, 0, 0, 0.13);
}

.timer-ring-progress {
  stroke: var(--success);
  stroke-linecap: round;
  transition: stroke 160ms linear;
}

.timer-warning .timer-ring-progress {
  stroke: var(--warning);
}

.timer-danger .timer-ring-progress {
  stroke: var(--danger);
}

.bonus {
  background: var(--surface-alt);
  border-radius: 12px;
  padding: 10px;
}

.bonus-head {
  display: flex;
  justify-content: space-between;
  font-weight: 800;
  font-size: 14px;
  margin-bottom: 8px;
}

.bonus-track {
  width: 100%;
  height: 14px;
  background: rgba(0, 0, 0, 0.08);
  border-radius: 99px;
  overflow: hidden;
}

.bonus-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #7ddf96, #23b264);
  transition: width 240ms ease;
}

.found-panel,
.build-panel {
  background: var(--surface-alt);
  border-radius: 14px;
  padding: 12px;
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.panel-head h2,
.build-panel h2,
.side-panel h2 {
  margin: 0;
  font-size: 18px;
}

.panel-head p {
  margin: 0;
  font-size: 14px;
  color: var(--muted);
  font-weight: 800;
}

.found-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  max-height: 160px;
  overflow: auto;
}

.word-pill {
  border-radius: 999px;
  padding: 7px 10px;
  font-weight: 800;
  letter-spacing: 0.02em;
  text-align: center;
  background: rgba(0, 0, 0, 0.07);
  color: var(--muted);
}

.word-pill.found {
  background: #1fa95f;
  color: #fff;
}

.slot-row {
  min-height: 70px;
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.slot-letter,
.letter-tile {
  min-width: 48px;
  min-height: 48px;
  border: none;
  border-radius: 12px;
  font-size: 24px;
  font-family: "Space Grotesk", "Nunito", sans-serif;
  font-weight: 700;
  cursor: pointer;
  transform: translateY(0);
  transition: transform 140ms ease, box-shadow 140ms ease, opacity 140ms ease;
}

.slot-letter {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.16);
}

.letter-tray {
  margin: 8px auto 10px;
  width: min(340px, 100%);
  display: grid;
  grid-template-columns: repeat(4, minmax(48px, 1fr));
  gap: 8px;
}

.letter-tile {
  background: linear-gradient(180deg, #ffd890, var(--primary));
  color: #3b2a00;
  box-shadow: 0 6px 12px rgba(224, 112, 0, 0.24);
}

.letter-tile:hover,
.slot-letter:hover {
  transform: translateY(-3px);
}

.letter-tile.used {
  opacity: 0.28;
  pointer-events: none;
}

.actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

@media (max-width: 959px) {
  .hud {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .timer-wrap {
    grid-column: span 1;
  }

  .timer-ring {
    width: 24px;
    height: 24px;
  }

  .stat {
    min-height: 44px;
    padding: 5px 6px;
    gap: 4px;
  }

  .stat-label {
    font-size: 10px;
  }

  .stat-value {
    font-size: 15px;
    line-height: 1;
  }

  .actions #undoBtn {
    order: 1;
  }

  .actions #submitBtn {
    order: 2;
  }

  .actions #shuffleBtn {
    order: 3;
  }

  .actions #clearBtn {
    order: 4;
  }
}

.action-btn {
  border: none;
  border-radius: 999px;
  min-height: 48px;
  padding: 10px 12px;
  font-weight: 900;
  letter-spacing: 0.01em;
  cursor: pointer;
  background: #efe8d4;
  color: #443929;
}

.action-btn.primary {
  background: var(--primary);
  color: #3e2b00;
}

.action-btn.link-btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.status-toast {
  margin: 0;
  min-height: 24px;
  font-weight: 800;
  color: var(--muted);
}

.status-toast.ok {
  color: var(--success);
}

.status-toast.warn {
  color: var(--warning);
}

.status-toast.error {
  color: var(--danger);
}

.status-toast.burst {
  animation: pulse 320ms ease;
}

.side-panel {
  display: grid;
  gap: 10px;
}

.side-panel .card {
  padding: 12px;
}

.side-panel p {
  margin: 8px 0;
  color: var(--muted);
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
  font-weight: 700;
}

.setting-row select,
.setting-row input {
  min-height: 38px;
}

.tips {
  margin: 8px 0 0;
  padding-left: 18px;
  color: var(--muted);
}

.tips li {
  margin-bottom: 6px;
}

.results-modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.56);
  padding: 12px;
  z-index: 40;
}

.results-card {
  width: min(660px, 100%);
  background: var(--surface);
  border-radius: 18px;
  padding: 16px;
  border: 1px solid var(--border);
}

.results-card h2 {
  margin-top: 0;
  margin-bottom: 8px;
  font-size: 26px;
}

.results-card p {
  margin: 6px 0;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 12px;
}

.duel-outcome {
  margin-top: 8px;
  font-weight: 800;
  color: var(--primary-strong);
}

#shareText {
  width: 100%;
  min-height: 148px;
  margin-top: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 10px;
  background: var(--surface-alt);
  color: var(--ink);
}

.share-actions,
.result-actions,
.share-links {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.result-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.share-links a {
  background: var(--surface-alt);
  border-radius: 999px;
  min-height: 44px;
  text-decoration: none;
  color: var(--ink);
  font-weight: 800;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}

/* ── Puzzle Picker ─── */
.puzzle-picker-card {
  width: min(860px, 100%);
}

.picker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}

.picker-header h2 {
  margin: 0;
  font-size: 22px;
}

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

.puzzle-picker-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.picker-card {
  background: var(--surface-alt);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 140ms ease, transform 140ms ease;
  font: inherit;
  color: var(--ink);
}

.picker-card:hover,
.picker-card:focus-visible {
  border-color: var(--primary-strong);
  transform: translateY(-2px);
}

.picker-card-emoji {
  font-size: 28px;
  line-height: 1;
}

.picker-card-theme {
  font-weight: 800;
  font-size: 15px;
}

.picker-card-letters {
  font-family: "Space Grotesk", "Nunito", sans-serif;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.diff-badge {
  display: inline-block;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  align-self: flex-start;
}

.diff-badge.easy {
  background: #d1fae5;
  color: #065f46;
}

.diff-badge.medium {
  background: #fef3c7;
  color: #92400e;
}

.diff-badge.hard {
  background: #fee2e2;
  color: #991b1b;
}

body.dark .diff-badge.easy {
  background: #064e3b;
  color: #6ee7b7;
}

body.dark .diff-badge.medium {
  background: #78350f;
  color: #fde68a;
}

body.dark .diff-badge.hard {
  background: #7f1d1d;
  color: #fca5a5;
}

.no-script {
  text-align: center;
  padding: 18px;
  color: var(--danger);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  border: 0;
  padding: 0;
}

.shake {
  animation: shake 280ms ease;
}

body.colorblind .word-pill.found {
  background-image: repeating-linear-gradient(
    45deg,
    rgba(255, 255, 255, 0.2),
    rgba(255, 255, 255, 0.2) 6px,
    rgba(255, 255, 255, 0.34) 6px,
    rgba(255, 255, 255, 0.34) 12px
  );
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

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

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

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
    scroll-behavior: auto !important;
  }
}

@media (max-width: 520px) {
  /* Compact HUD so action buttons stay visible on small screens */
  .hud {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 5px;
  }

  .timer-wrap {
    grid-column: span 1;
  }

  .timer-ring {
    display: none;
  }

  .stat {
    min-height: 42px;
    padding: 5px;
    gap: 3px;
    justify-content: center;
  }

  .timer-wrap > div {
    width: 100%;
  }

  .stat-label {
    font-size: 10px;
    white-space: nowrap;
  }

  .stat-value {
    font-size: 14px;
    line-height: 1.05;
    white-space: nowrap;
  }

  .found-list {
    max-height: 140px;
  }

  .slot-row {
    min-height: 64px;
    padding: 8px;
  }

  .actions {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .action-btn {
    font-size: 13px;
    padding: 8px 6px;
    min-height: 42px;
  }

  .found-list {
    max-height: 190px;
  }

  .results-card {
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
  }

  .stat {
    flex-direction: column;
    gap: 2px;
  }

  .timer-wrap > div {
    text-align: center;
    width: 100%;
  }

  .letter-tray {
    grid-template-columns: repeat(4, minmax(44px, 1fr));
    gap: 6px;
  }

  .slot-letter,
  .letter-tile {
    min-width: 44px;
    min-height: 48px;
    font-size: 22px;
  }
}

@media (max-width: 400px) {
  .details-link {
    display: none;
  }

  .back-link,
  .ghost-btn {
    font-size: 13px;
    padding: 7px 8px;
  }
}

@media (min-width: 960px) {
  .topbar {
    padding: 14px 16px;
    gap: 12px;
  }

  .back-link,
  .ghost-btn {
    min-height: 48px;
    padding: 10px 14px;
    font-size: 20px;
  }

  .stat {
    min-height: 56px;
    padding: 7px 10px;
  }

  .stat-value {
    font-size: 23px;
  }

  .layout {
    margin: 18px auto;
    padding: 0 12px 20px;
    gap: 16px;
    grid-template-columns: 1fr 310px;
    align-items: start;
  }

  .game-panel {
    padding: 14px;
    gap: 14px;
  }

  .hud {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .timer-wrap {
    grid-column: span 1;
  }

  .found-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    max-height: 220px;
  }

  .actions {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .puzzle-picker-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
