:root {
  color-scheme: light;
  --ink: #171717;
  --muted: #68635d;
  --paper: #f7f2e8;
  --panel: #fffaf0;
  --stone: #eee4d2;
  --stone-back: #b8d8df;
  --line: #d4c7b4;
  --blue: #7fb0c8;
  --blue-deep: #2f667d;
  --bad: #ad3b31;
  --good: #2d7854;
  --shadow: 0 14px 34px rgba(45, 36, 23, .14);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  position: relative;
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(135deg, #f5ead7 0%, #eef3f0 100%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#penrose {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 0;
  pointer-events: none;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(1500px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 22px 0 28px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  color: var(--blue-deep);
  font-size: clamp(2rem, 4.5vw, 4.2rem);
  font-weight: 900;
  line-height: .98;
}

h2 {
  font-size: 1rem;
}

.topbar-actions,
.card-title,
.number-row,
.toggle-row,
.player-picker {
  display: flex;
  align-items: center;
}

.topbar-actions {
  gap: 10px;
}

.primary-button,
.icon-button,
.tab,
.player-chip {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 4px 0 rgba(55, 45, 30, .14);
}

.primary-button {
  min-height: 42px;
  padding: 0 18px;
  border-radius: 8px;
  font-weight: 800;
  background: var(--blue-deep);
  color: white;
}

.icon-button {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  font-weight: 900;
}

.settings-panel,
.score-card,
.word-card {
  border: 1px solid rgba(115, 100, 78, .3);
  background: rgba(255, 250, 240, .82);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.settings-panel {
  padding: 14px;
  border-radius: 8px;
  margin-bottom: 16px;
}

.mode-tabs {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eadfcd;
  margin-bottom: 12px;
}

.tab {
  min-height: 34px;
  padding: 0 14px;
  border-radius: 6px;
  box-shadow: none;
  background: transparent;
  font-weight: 800;
}

.tab.is-active,
.player-chip.is-active {
  background: var(--blue-deep);
  color: white;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(130px, 1fr));
  gap: 10px;
}

.setting {
  display: grid;
  grid-template-columns: 30px 1fr;
  gap: 5px 8px;
  align-items: center;
  min-width: 0;
  padding: 10px;
  border: 1px solid rgba(126, 111, 88, .28);
  border-radius: 8px;
  background: rgba(255, 255, 255, .36);
}

.setting span:not(.setting-icon) {
  color: var(--muted);
  font-size: .84rem;
  font-weight: 800;
}

.setting input,
.setting select,
.setting strong,
.number-row {
  grid-column: 2;
}

.setting-icon {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  grid-row: 1 / span 3;
  border-radius: 50%;
  background: #efe3cd;
  color: var(--blue-deep);
  font-weight: 900;
  overflow: hidden;
}

.setting-icon img {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--blue-deep);
}

input[type="number"],
select {
  width: 100%;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf7;
  color: var(--ink);
  padding: 0 9px;
}

.number-row {
  gap: 6px;
}

.toggle-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(170px, 1fr));
  gap: 10px;
  padding-top: 12px;
  color: var(--muted);
  font-weight: 800;
}

.flip-switch-row {
  display: flex;
  min-height: 42px;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 8px 10px;
  border: 1px solid rgba(47, 102, 125, .2);
  border-radius: 10px;
  background:
    radial-gradient(circle at 10% 0%, rgba(244, 199, 93, .18), transparent 34%),
    linear-gradient(135deg, rgba(255, 255, 255, .6), rgba(234, 246, 250, .46));
  color: #44575f;
  font-weight: 900;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.65);
}

.flip-switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 28px;
  flex: 0 0 50px;
}

.flip-switch input {
  width: 0;
  height: 0;
  opacity: 0;
}

.slider {
  position: absolute;
  inset: 0;
  border-radius: 30px;
  background: linear-gradient(135deg, #c9c3b8, #aeb9bd);
  cursor: pointer;
  box-shadow: inset 0 2px 5px rgba(34, 29, 22, .18);
  transition: background .3s ease, box-shadow .3s ease;
}

.slider::before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  left: 4px;
  bottom: 4px;
  border-radius: 50%;
  background: linear-gradient(180deg, #ffffff, #f7f2e8);
  box-shadow: 0 2px 6px rgba(34, 29, 22, .22);
  transition: transform .3s ease;
}

.flip-switch input:checked + .slider,
.option-toggle.is-active .slider {
  background: linear-gradient(135deg, #31b76a, #65cf72);
  box-shadow:
    inset 0 2px 5px rgba(24, 90, 54, .18),
    0 0 0 3px rgba(101, 207, 114, .16);
}

.flip-switch input:checked + .slider::before,
.option-toggle.is-active .slider::before {
  transform: translateX(22px);
}

.option-toggle {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.option-toggle.is-active {
  border-color: rgba(47, 102, 125, .32);
  background:
    radial-gradient(circle at 10% 0%, rgba(244, 199, 93, .24), transparent 34%),
    linear-gradient(135deg, rgba(239, 250, 241, .7), rgba(222, 244, 235, .52));
  color: var(--ink);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(210px, 250px) minmax(380px, 1fr) minmax(220px, 280px);
  gap: 16px;
  align-items: start;
}

.score-card,
.word-card {
  border-radius: 8px;
  padding: 14px;
}

.card-title {
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.card-title span {
  border-radius: 999px;
  padding: 4px 9px;
  background: #e6f0f3;
  color: var(--blue-deep);
  font-size: .78rem;
  font-weight: 900;
}

.player-picker {
  gap: 6px;
  margin-bottom: 12px;
}

.player-chip {
  flex: 1;
  min-height: 34px;
  border-radius: 7px;
  font-weight: 800;
  box-shadow: none;
}

.score-row,
.meter-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid rgba(126, 111, 88, .22);
}

.score-row:first-child {
  border-top: 0;
}

.score-row span,
.meter-block span {
  color: var(--muted);
  font-weight: 800;
}

.score-row strong,
.meter-block strong {
  font-size: 1.14rem;
}

#timerReadout {
  min-width: 4.4ch;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#timerReadout.is-running {
  color: var(--blue-deep);
}

#timerReadout.is-expired {
  color: var(--bad);
}

.difficulty-details,
.result-card {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid rgba(126, 111, 88, .22);
}

.difficulty-details h3,
.result-card h3 {
  margin: 0 0 8px;
  font-size: .9rem;
}

.difficulty-details ul {
  display: grid;
  gap: 5px;
  margin: 0;
  padding: 0;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 800;
  list-style: none;
}

.result-card {
  border-radius: 8px;
  background: rgba(127, 176, 200, .18);
  padding: 12px;
}

.result-card p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: .84rem;
  font-weight: 800;
  line-height: 1.35;
}

.board-stage {
  min-width: 0;
}

.play-hud {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(112px, auto) minmax(132px, auto);
  gap: 10px;
  align-items: stretch;
  margin-bottom: 10px;
}

.status-line,
.guess-countdown-pill,
.countdown-pill {
  min-height: 38px;
  display: flex;
  align-items: center;
  border: 1px solid rgba(126, 111, 88, .28);
  border-radius: 8px;
  background: rgba(255, 250, 240, .68);
}

.status-line {
  justify-content: center;
  color: var(--muted);
  font-weight: 800;
  text-align: center;
  padding: 8px 12px;
}

.guess-countdown-pill,
.countdown-pill {
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  color: var(--muted);
  font-weight: 900;
}

.guess-countdown-pill span,
.countdown-pill span {
  font-size: .78rem;
}

.guess-countdown-pill strong,
.countdown-pill strong {
  min-width: 5ch;
  color: var(--ink);
  font-size: 1.24rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.guess-countdown-pill.is-low strong {
  color: var(--bad);
}

.countdown-pill.is-running strong {
  color: var(--blue-deep);
}

.countdown-pill.is-expired strong {
  color: var(--bad);
}

.board {
  --grid-size: 6;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), minmax(0, 1fr));
  gap: clamp(3px, .55vw, 7px);
  width: min(100%, 76vh);
  margin: 0 auto;
  padding: clamp(8px, 1.5vw, 14px);
  border: 1px solid rgba(91, 78, 58, .34);
  border-radius: 8px;
  background: rgba(77, 63, 42, .12);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .42), var(--shadow);
}

.tile {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  perspective: 900px;
  padding: 0;
}

.tile-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform .46s cubic-bezier(.2, .74, .22, 1), filter .2s ease;
}

.tile.is-flipped .tile-inner {
  transform: rotateY(180deg);
}

.tile-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border: 1px solid rgba(104, 88, 62, .42);
  border-radius: 7px;
  backface-visibility: hidden;
  background:
    linear-gradient(145deg, rgba(255,255,255,.68), rgba(196,181,151,.2)),
    var(--stone);
  color: #121212;
  font-size: clamp(.95rem, 2.9vw, 2.05rem);
  font-weight: 900;
  box-shadow: 0 7px 0 rgba(79, 63, 39, .22), 0 11px 18px rgba(56, 45, 31, .18);
  text-transform: uppercase;
  user-select: none;
}

.tile-back {
  transform: rotateY(180deg);
  border-color: rgba(47, 102, 125, .58);
  background:
    linear-gradient(145deg, rgba(255,255,255,.74), rgba(47,102,125,.16) 48%, rgba(47,102,125,.32)),
    var(--stone-back);
  color: #0d2833;
  box-shadow: 0 7px 0 rgba(47, 102, 125, .3), 0 11px 18px rgba(34, 63, 73, .18);
}

.tile-back::after {
  content: "";
  position: absolute;
  inset: 5px;
  border: 2px solid rgba(47, 102, 125, .34);
  border-radius: 5px;
  pointer-events: none;
}

.tile.is-decoy .tile-back {
  border-color: rgba(173, 59, 49, .68);
  background:
    linear-gradient(145deg, rgba(255,255,255,.72), rgba(173,59,49,.15) 48%, rgba(173,59,49,.28)),
    #e5c4b9;
  color: #451711;
  box-shadow: 0 7px 0 rgba(173, 59, 49, .26), 0 11px 18px rgba(74, 30, 24, .16);
}

.tile.is-decoy .tile-back::after {
  border-color: rgba(173, 59, 49, .38);
}

.tile.is-dropped .tile-face {
  background:
    linear-gradient(145deg, rgba(255,255,255,.58), rgba(127,176,200,.32)),
    rgba(127, 176, 200, .66);
  box-shadow: 0 2px 0 rgba(47, 102, 125, .26), 0 6px 10px rgba(56, 45, 31, .1);
}

.tile.is-loss-reveal .tile-face {
  background:
    linear-gradient(145deg, rgba(255,255,255,.64), rgba(197,138,37,.22)),
    rgba(244, 199, 93, .48);
  box-shadow: 0 2px 0 rgba(104, 88, 62, .2), 0 6px 10px rgba(56, 45, 31, .1);
}

.tile.is-wrong {
  animation: nudge .26s ease;
}

.tile:disabled {
  cursor: default;
}

@keyframes nudge {
  20% { transform: translateX(-5px); }
  45% { transform: translateX(5px); }
  70% { transform: translateX(-3px); }
}

.word-list {
  display: grid;
  gap: 8px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.word-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 40px;
  padding: 9px 10px;
  border: 1px solid rgba(126, 111, 88, .23);
  border-radius: 8px;
  background: rgba(255, 255, 255, .42);
  transition: transform .55s ease, background .35s ease, opacity .35s ease;
}

.word-list li.is-found {
  order: -1;
  background: rgba(127, 176, 200, .25);
  opacity: .78;
}

.word-list li.is-missed {
  background: rgba(244, 199, 93, .2);
}

.word-list li.blind-search-note {
  justify-content: center;
  color: var(--muted);
  font-size: .88rem;
  font-weight: 900;
  text-align: center;
}

.word-list li.is-found .word-text {
  text-decoration: line-through;
}

.word-text {
  min-width: 0;
  overflow-wrap: anywhere;
  font-weight: 900;
}

.word-meta {
  color: var(--muted);
  font-size: .76rem;
  font-weight: 900;
  white-space: nowrap;
}

dialog {
  width: min(560px, calc(100vw - 30px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(34, 29, 22, .34);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 10px;
}

.rules-copy {
  color: var(--muted);
  line-height: 1.52;
  font-weight: 650;
}

.game-result-dialog {
  width: min(420px, calc(100vw - 30px));
  border: 1px solid rgba(126, 111, 88, .26);
  border-radius: 14px;
  background:
    radial-gradient(circle at 20% 0%, rgba(244, 199, 93, .3), transparent 36%),
    linear-gradient(135deg, rgba(255, 253, 247, .97), rgba(255, 245, 238, .94));
  text-align: center;
}

.game-result-dialog::backdrop {
  background: rgba(34, 29, 22, .46);
  backdrop-filter: blur(3px);
}

.game-result-content {
  display: grid;
  gap: 12px;
  padding: 10px;
}

.game-result-kicker {
  margin: 0;
  color: var(--gold);
  font-size: .78rem;
  font-weight: 1000;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.game-result-dialog h2 {
  margin: 0;
  color: var(--ink);
  font-family: Georgia, serif;
  font-size: clamp(2rem, 8vw, 3.2rem);
}

.game-result-dialog p {
  margin: 0;
}

#gameResultMessage {
  color: var(--muted);
  font-weight: 800;
  line-height: 1.45;
}

.game-result-dialog.is-loss h2 {
  color: var(--bad);
}

.game-result-dialog .primary-button {
  width: min(180px, 100%);
  margin: 8px auto 0;
}

.dialog-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.dialog-actions .primary-button,
.secondary-dialog-button {
  min-width: 150px;
  min-height: 38px;
  margin: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.secondary-dialog-button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, .55);
  color: var(--muted);
  font-weight: 900;
}

@media (max-width: 1100px) {
  .settings-grid {
    grid-template-columns: repeat(3, minmax(150px, 1fr));
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .score-card,
  .word-card {
    order: 2;
  }

  .board-stage {
    order: 1;
  }

  .board {
    width: min(100%, 680px);
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100vw - 18px, 1500px);
    padding-top: 12px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .settings-grid {
    grid-template-columns: 1fr;
  }

  .toggle-row {
    display: grid;
    grid-template-columns: 1fr;
  }

  .play-hud {
    grid-template-columns: 1fr;
  }

  .countdown-pill {
    min-height: 44px;
  }

  .board {
    padding: 6px;
  }

  .tile-face {
    border-radius: 5px;
  }
}
