:root {
  color-scheme: light;
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-strong: #d3d6da;
  --ink: #172126;
  --muted: #68747d;
  --line: #d8e0e6;
  --tile-empty: #ffffff;
  --tile-border: #cbd5dc;
  --tile-filled: #8d9aa4;
  --correct: #2f9b6d;
  --present: #c49a2c;
  --absent: #68747d;
  --accent: #d64f45;
  --accent-ink: #ffffff;
  --shadow: 0 18px 40px rgba(23, 33, 38, 0.12);
  --board-size: min(86vw, 352px);
  --radius: 8px;
  --keyboard-gap: 5px;
}

body.theme-dark {
  color-scheme: dark;
  --bg: #111518;
  --surface: #191f23;
  --surface-strong: #232b30;
  --ink: #f3f7f8;
  --muted: #a6b0b7;
  --line: #334047;
  --tile-empty: #151a1e;
  --tile-border: #3b474f;
  --tile-filled: #89949b;
  --correct: #3fa47a;
  --present: #d0a440;
  --absent: #5d686f;
  --accent: #e16458;
  --accent-ink: #151a1e;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
}

body.high-contrast {
  --correct: #0072b2;
  --present: #e69f00;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  scroll-behavior: smooth;
  background: var(--bg);
}

body {
  min-height: 100dvh;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  overflow-x: hidden;
}

body.theme-dark {
  background: var(--bg);
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

.app-shell {
  min-height: 100svh;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

.topbar {
  width: min(100%, 920px);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto minmax(260px, 1fr);
  grid-template-areas: "brand mode tools";
  align-items: center;
  gap: 18px;
  padding: 5px 0 9px;
  border-bottom: 1px solid var(--line);
}

.brand-lockup {
  grid-area: brand;
  display: inline-flex;
  align-items: center;
  justify-self: start;
  min-width: 0;
  gap: 8px;
  color: inherit;
  text-decoration: none;
  border-radius: 7px;
}

.brand-lockup:focus-visible {
  outline: 2px solid var(--correct);
  outline-offset: 3px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 7px;
}

.brand-title,
h1,
h2,
p {
  margin: 0;
}

.brand-title {
  min-width: 0;
  font-size: clamp(1.15rem, 2.2vw, 1.38rem);
  line-height: 1.05;
  font-weight: 850;
  white-space: nowrap;
}

h2 {
  font-size: 1.15rem;
}

.mode-switch,
.stats-tabs {
  grid-area: mode;
  display: inline-grid;
  grid-auto-flow: column;
  align-items: center;
  justify-self: center;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}

.mode-tab {
  min-height: 30px;
  border: 0;
  border-radius: 4px;
  padding: 0 11px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
}

.mode-tab.is-active {
  color: var(--ink);
  background: var(--surface-strong);
  box-shadow: none;
}

.toolbar {
  grid-area: tools;
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 5px;
}

.icon-button,
.text-button,
.plain-button,
.danger-button {
  border: 1px solid var(--line);
  color: var(--ink);
  background: var(--surface);
  cursor: pointer;
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease;
}

.icon-button {
  width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  border-radius: var(--radius);
  padding: 0;
}

.icon-button svg,
.text-button svg,
.timer-pill svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:hover,
.text-button:hover,
.danger-button:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.icon-button:active,
.text-button:active,
.danger-button:active,
.key:active {
  transform: translateY(1px);
}

.game-area {
  width: min(100%, 520px);
  margin: 0 auto;
  display: grid;
  min-height: 0;
  grid-template-rows: minmax(0, 1fr) auto auto;
  align-items: center;
  justify-items: center;
  gap: 10px;
  padding-top: 10px;
}

.status-strip {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.eyebrow {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

#statusTitle {
  display: block;
  margin-top: 2px;
  font-size: 1rem;
  line-height: 1.2;
}

.timer-pill {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border-radius: var(--radius);
  padding: 0 10px;
  color: var(--muted);
  background: var(--surface-strong);
  font-size: 0.86rem;
  font-weight: 800;
  white-space: nowrap;
}

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

.toast {
  position: fixed;
  top: max(76px, env(safe-area-inset-top));
  left: 50%;
  z-index: 10;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  max-width: min(100%, 430px);
  padding: 0 12px;
  border-radius: var(--radius);
  color: var(--accent-ink);
  background: var(--accent);
  font-weight: 800;
  line-height: 1.2;
  opacity: 0;
  transform: translate(-50%, -4px);
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.board-wrap {
  width: var(--board-size);
  align-self: center;
}

.board {
  width: 100%;
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 7px;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 7px;
}

.cell {
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  border: 2px solid var(--tile-border);
  border-radius: 4px;
  background: var(--tile-empty);
  color: var(--ink);
  font-size: clamp(1.75rem, 10vw, 2.5rem);
  line-height: 1;
  font-weight: 900;
  text-transform: uppercase;
  user-select: none;
}

.cell.filled {
  border-color: var(--tile-filled);
  animation: pop 120ms ease;
}

.cell.correct,
.cell.present,
.cell.absent {
  color: #ffffff;
  border-color: transparent;
}

.cell.correct {
  background: var(--correct);
}

.cell.present {
  background: var(--present);
}

.cell.absent {
  background: var(--absent);
}

.row.revealed .cell {
  animation: flip 520ms ease both;
  animation-delay: calc(var(--cell-index) * 95ms);
}

.row.shake {
  animation: shake 300ms ease;
}

.row.win .cell {
  animation: bounce 520ms ease both;
  animation-delay: calc(var(--cell-index) * 65ms);
}

.round-actions {
  width: min(100%, 430px);
  min-height: 40px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.round-actions.single-action {
  grid-template-columns: 1fr;
}

.text-button,
.plain-button,
.danger-button {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: var(--radius);
  padding: 0 13px;
  font-size: 0.92rem;
  font-weight: 800;
}

.text-button {
  background: var(--surface);
}

.primary-button {
  color: #ffffff;
  border-color: transparent;
  background: var(--correct);
}

.plain-button {
  background: var(--surface);
}

.text-button[hidden] {
  display: none;
}

.danger-button {
  width: 100%;
  color: #ffffff;
  border-color: transparent;
  background: #b43d36;
}

.keyboard {
  width: min(100%, 520px);
  display: grid;
  gap: var(--keyboard-gap);
  padding-bottom: 2px;
  touch-action: manipulation;
}

.key-row {
  display: grid;
  grid-template-columns: repeat(var(--key-count), minmax(0, 1fr));
  gap: var(--keyboard-gap);
}

.key-row:nth-child(2) {
  padding: 0 calc((100% / 20) + var(--keyboard-gap) / 2);
}

.key {
  min-width: 0;
  height: clamp(46px, 8vh, 56px);
  border: 0;
  border-radius: 5px;
  color: var(--ink);
  background: var(--surface-strong);
  cursor: pointer;
  font-size: clamp(0.86rem, 2.6vw, 1rem);
  font-weight: 900;
  text-transform: uppercase;
  transition: transform 120ms ease, filter 120ms ease;
}

.key.wide {
  grid-column: span 2;
  font-size: 0.76rem;
}

.key svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.key.correct,
.key.present,
.key.absent {
  color: #ffffff;
}

.key.correct {
  background: var(--correct);
}

.key.present {
  background: var(--present);
}

.key.absent {
  background: var(--absent);
}

.page-info,
.site-footer {
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.page-info {
  padding: clamp(40px, 8vw, 78px) max(18px, env(safe-area-inset-right)) clamp(36px, 7vw, 68px) max(18px, env(safe-area-inset-left));
}

.content-inner {
  width: min(100%, 760px);
  margin: 0 auto;
}

.page-info h1 {
  max-width: 620px;
  font-size: clamp(1.8rem, 5vw, 3rem);
  line-height: 1.05;
  font-weight: 900;
}

.page-info p {
  max-width: 660px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.65;
}

.info-lines {
  display: grid;
  gap: 4px;
  margin-top: 22px;
}

.info-lines p {
  margin-top: 0;
}

.info-lines strong {
  color: var(--ink);
}

.site-footer {
  padding: 18px max(18px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  color: var(--muted);
  font-size: 0.86rem;
}

.site-footer .content-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal {
  width: min(92vw, 430px);
  border: 0;
  padding: 0;
  color: var(--ink);
  background: transparent;
}

.result-modal {
  width: min(92vw, 390px);
}

.modal::backdrop {
  background: rgba(10, 14, 16, 0.42);
  backdrop-filter: blur(4px);
}

.modal-panel {
  display: grid;
  gap: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.result-panel {
  position: relative;
  gap: 14px;
  overflow: hidden;
  padding: 22px;
  text-align: center;
}

.result-panel::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 7px;
  background: var(--correct);
}

.result-modal.is-loss .result-panel::before {
  background: var(--absent);
}

.result-hero {
  display: grid;
  gap: 6px;
  padding: 2px 28px 4px;
}

.result-kicker {
  justify-self: center;
  border-radius: 999px;
  padding: 4px 10px;
  color: #ffffff;
  background: var(--correct);
  font-size: 0.72rem;
  font-weight: 900;
  text-transform: uppercase;
}

.result-modal.is-loss .result-kicker {
  background: var(--absent);
}

.result-hero h2 {
  font-size: clamp(1.8rem, 8vw, 2.45rem);
  line-height: 1;
  font-weight: 900;
}

.result-hero p {
  color: var(--muted);
  font-weight: 750;
}

.result-word {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.result-letter {
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  border-radius: 6px;
  color: #ffffff;
  background: var(--correct);
  font-size: clamp(1.35rem, 8vw, 1.9rem);
  font-weight: 900;
  text-transform: uppercase;
}

.result-modal.is-loss .result-letter {
  background: var(--ink);
}

.result-grid {
  display: grid;
  justify-content: center;
  gap: 4px;
}

.result-grid-row {
  display: grid;
  grid-template-columns: repeat(5, 16px);
  gap: 4px;
}

.result-dot {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  background: var(--absent);
}

.result-dot.correct {
  background: var(--correct);
}

.result-dot.present {
  background: var(--present);
}

.result-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
}

.result-stat {
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 9px 4px;
  background: var(--surface);
}

.result-stat strong,
.result-stat span {
  display: block;
}

.result-stat strong {
  font-size: 1.25rem;
  line-height: 1;
}

.result-stat span {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 900;
  text-transform: uppercase;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.result-actions .plain-button {
  grid-column: 1 / -1;
}

.result-actions.single-action {
  grid-template-columns: 1fr;
}

.result-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 7px;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
}

.result-close svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.rule-grid {
  display: grid;
  gap: 12px;
}

.mini-row {
  display: grid;
  grid-template-columns: repeat(5, 34px) minmax(0, 1fr);
  align-items: center;
  gap: 6px;
}

.mini-row .cell {
  width: 34px;
  height: 34px;
  border-radius: 6px;
  font-size: 1rem;
}

.mini-row p {
  margin-left: 4px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.3;
}

.stat-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat-card {
  min-height: 70px;
  display: grid;
  place-items: center;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 4px;
  background: var(--surface-strong);
  text-align: center;
}

.stat-card strong {
  font-size: 1.35rem;
  line-height: 1;
}

.stat-card span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.distribution {
  display: grid;
  gap: 7px;
}

.dist-row {
  display: grid;
  grid-template-columns: 18px 1fr 26px;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.dist-track {
  height: 22px;
  border-radius: 6px;
  background: var(--surface-strong);
  overflow: hidden;
}

.dist-bar {
  min-width: 7%;
  height: 100%;
  border-radius: 6px;
  background: var(--absent);
}

.dist-row.is-best .dist-bar {
  background: var(--correct);
}

.settings-list {
  display: grid;
  gap: 0;
  border-top: 1px solid var(--line);
}

.setting-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 13px 0;
  border-bottom: 1px solid var(--line);
}

.setting-row strong,
.setting-row small {
  display: block;
}

.setting-row small {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.25;
}

.setting-row input {
  appearance: none;
  width: 48px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-strong);
  cursor: pointer;
  position: relative;
  transition: background 160ms ease, border-color 160ms ease;
}

.setting-row input::after {
  content: "";
  position: absolute;
  width: 22px;
  height: 22px;
  top: 2px;
  left: 2px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 1px 5px rgba(23, 33, 38, 0.2);
  transition: transform 160ms ease;
}

.setting-row input:checked {
  border-color: var(--correct);
  background: var(--correct);
}

.setting-row input:checked::after {
  transform: translateX(20px);
}

.modal-actions {
  display: grid;
  gap: 9px;
}

@keyframes pop {
  50% {
    transform: scale(1.06);
  }
}

@keyframes flip {
  0% {
    transform: rotateX(0deg);
  }
  48% {
    color: transparent;
    transform: rotateX(90deg);
  }
  52% {
    transform: rotateX(90deg);
  }
  100% {
    transform: rotateX(0deg);
  }
}

@keyframes shake {
  10%, 90% {
    transform: translateX(-2px);
  }
  20%, 80% {
    transform: translateX(4px);
  }
  30%, 50%, 70% {
    transform: translateX(-6px);
  }
  40%, 60% {
    transform: translateX(6px);
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-9px);
  }
}

body.reduced-motion *,
body.reduced-motion *::before,
body.reduced-motion *::after {
  animation-duration: 1ms !important;
  transition-duration: 1ms !important;
}

@media (max-width: 680px) {
  :root {
    --board-size: min(90vw, 345px);
  }

  .topbar {
    grid-template-columns: minmax(34px, 1fr) auto minmax(104px, 1fr);
    gap: 6px;
  }

  .brand-title {
    display: none;
  }

  .brand-mark {
    width: 32px;
    height: 32px;
  }

  .game-area {
    gap: 10px;
  }

  .site-footer .content-inner {
    display: grid;
  }
}

@media (max-width: 390px) {
  .app-shell {
    padding-left: max(8px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
  }

  .brand-title {
    font-size: 1.18rem;
  }

  .mode-tab {
    min-height: 29px;
    padding: 0 8px;
    font-size: 0.82rem;
  }

  .icon-button {
    width: 30px;
    height: 30px;
  }

  .toolbar {
    gap: 4px;
  }

  .brand-mark {
    width: 30px;
    height: 30px;
  }

  .mini-row {
    grid-template-columns: repeat(5, 30px);
  }

  .mini-row p {
    grid-column: 1 / -1;
    margin: 4px 0 0;
  }
}

@media (max-height: 720px) and (orientation: portrait) {
  :root {
    --board-size: min(82vw, 320px);
    --keyboard-gap: 4px;
  }

  .app-shell {
    padding-top: max(6px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .topbar {
    padding-bottom: 6px;
  }

  .game-area {
    gap: 7px;
  }

  .toast {
    min-height: 27px;
  }

  .key {
    height: 43px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    transition-duration: 1ms !important;
  }
}
