:root {
  --bg: #eef3f9;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --text: #122033;
  --muted: #66758a;
  --line: #dce4ee;
  --brand: #3758d6;
  --brand-strong: #2742b0;
  --accent: #f0b429;
  --success: #138a62;
  --cell: #f7f9fc;
  --cell-selected: #dfe6ff;
  --shadow: 0 18px 55px rgba(18, 32, 51, 0.12);
  --radius: 18px;
}

[data-theme="dark"] {
  --bg: #07101d;
  --surface: #0f1b2d;
  --surface-2: #142238;
  --text: #eef4ff;
  --muted: #9fb0c7;
  --line: #263853;
  --brand: #7088ff;
  --brand-strong: #8ea0ff;
  --accent: #ffd166;
  --success: #54d3a7;
  --cell: #132238;
  --cell-selected: #243b69;
  --shadow: 0 18px 55px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background:
    radial-gradient(circle at top left, color-mix(in srgb, var(--brand) 13%, transparent), transparent 28rem),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background-color 180ms ease, color 180ms ease;
  overscroll-behavior: none;
}
button { font: inherit; }
button, .letter-cell { -webkit-tap-highlight-color: transparent; }

.app-shell {
  width: min(1180px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: clamp(10px, 1.8vw, 22px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
}

.topbar {
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.eyebrow { margin: 0 0 2px; color: var(--brand); font-size: 0.68rem; font-weight: 800; letter-spacing: 0.16em; }
h1 { margin: 0; font-size: clamp(1.35rem, 3vw, 2rem); line-height: 1; }
.icon-button {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); cursor: pointer;
  box-shadow: 0 6px 18px rgba(18, 32, 51, 0.08);
}

.game-card {
  min-height: 0;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(10px, 1.7vw, 18px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
}
.game-toolbar {
  display: grid;
  grid-template-columns: repeat(2, minmax(70px, 120px)) 1fr;
  align-items: stretch;
  gap: 8px;
}
.stat {
  min-height: 50px;
  padding: 7px 10px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.stat span { color: var(--muted); font-size: 0.7rem; }
.stat strong { font-size: 1rem; }
.primary-button, .secondary-button {
  min-height: 44px;
  border-radius: 12px;
  padding: 0 16px;
  border: 0;
  font-weight: 800;
  cursor: pointer;
}
.primary-button { justify-self: end; background: var(--brand); color: white; }
.primary-button:hover { background: var(--brand-strong); }
.secondary-button { background: var(--surface-2); border: 1px solid var(--line); color: var(--text); }
.wide { width: 100%; justify-self: stretch; }

.game-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(230px, 0.42fr);
  gap: clamp(10px, 2vw, 20px);
  align-items: center;
}
.board-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  gap: 6px;
}
.word-grid {
  --grid-size: 10;
  width: min(100%, 68dvh, 650px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), 1fr);
  gap: clamp(2px, 0.45vw, 5px);
  touch-action: none;
  user-select: none;
}
.letter-cell {
  min-width: 0;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: clamp(5px, 1vw, 10px);
  border: 1px solid var(--line);
  background: var(--cell);
  color: var(--text);
  font-size: clamp(0.72rem, 2.2vw, 1.35rem);
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition: transform 100ms ease, background 100ms ease, border-color 100ms ease;
}
.letter-cell.selecting { background: var(--cell-selected); border-color: var(--brand); transform: scale(0.96); }
.letter-cell.found { background: color-mix(in srgb, var(--success) 24%, var(--surface)); border-color: var(--success); color: var(--text); }
.letter-cell.hint { animation: hintPulse 900ms ease 2; }
@keyframes hintPulse { 50% { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); transform: scale(0.9); } }
.hint-text { margin: 0; color: var(--muted); font-size: 0.76rem; text-align: center; }

.word-panel {
  min-height: 0;
  max-height: 100%;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: clamp(10px, 1.5vw, 16px);
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 10px;
}
.panel-heading { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.panel-heading h2 { margin: 0; font-size: clamp(0.95rem, 2vw, 1.15rem); }
.word-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  align-content: start;
}
.word-chip {
  min-width: 0;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: clamp(0.72rem, 1.8vw, 0.9rem);
  font-weight: 750;
  text-align: center;
  overflow-wrap: anywhere;
}
.word-chip.found { color: var(--muted); text-decoration: line-through; background: color-mix(in srgb, var(--success) 12%, var(--surface)); }

.modal {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center;
  padding: 20px; background: rgba(3, 8, 16, 0.66); backdrop-filter: blur(8px);
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(390px, 100%); padding: 28px; border-radius: 22px; text-align: center;
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.modal-card h2 { margin: 12px 0 8px; }
.modal-card p { margin: 0 0 20px; color: var(--muted); }
.win-icon {
  width: 64px; height: 64px; margin: 0 auto; border-radius: 50%; display: grid; place-items: center;
  background: color-mix(in srgb, var(--success) 18%, var(--surface)); color: var(--success); font-size: 2rem; font-weight: 900;
}

@media (max-width: 760px) {
  .app-shell { padding: 8px; gap: 8px; }
  .topbar { min-height: 42px; }
  .icon-button { width: 40px; height: 40px; }
  .game-card { padding: 8px; gap: 8px; }
  .game-toolbar { grid-template-columns: 1fr 1fr auto; }
  .stat { min-height: 44px; padding: 5px 8px; }
  .primary-button { min-height: 44px; padding-inline: 12px; }
  .game-layout { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; align-items: start; gap: 8px; }
  .word-grid { width: min(100%, 58dvh); }
  .word-panel { padding: 8px; gap: 8px; }
  .word-list { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
  .word-chip { padding: 6px 4px; font-size: clamp(0.62rem, 2.5vw, 0.78rem); }
  .panel-heading h2 { font-size: 0.9rem; }
  .secondary-button { min-height: 36px; padding: 0 12px; }
  .hint-text { font-size: 0.68rem; }
}

@media (max-width: 430px), (max-height: 720px) {
  .eyebrow { display: none; }
  h1 { font-size: 1.22rem; }
  .app-shell { min-height: 100dvh; }
  .game-toolbar { gap: 5px; }
  .stat { min-height: 39px; border-radius: 9px; }
  .stat span { font-size: 0.6rem; }
  .stat strong { font-size: 0.86rem; }
  .primary-button { min-height: 39px; border-radius: 9px; font-size: 0.78rem; padding-inline: 10px; }
  .word-grid { width: min(100%, 54dvh); gap: 2px; }
  .letter-cell { border-radius: 5px; font-size: clamp(0.68rem, 3.5vw, 1rem); }
  .word-list { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  .word-chip { padding: 5px 2px; }
  .hint-text { display: none; }
}

@media (orientation: landscape) and (max-height: 620px) {
  .app-shell { grid-template-rows: auto minmax(0, 1fr); }
  .topbar { min-height: 36px; }
  .game-layout { grid-template-columns: minmax(0, 1fr) minmax(190px, 0.42fr); grid-template-rows: 1fr; }
  .word-grid { width: min(100%, 72dvh); }
  .word-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

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