:root {
  color-scheme: light dark;
  --page: #f3f4f7;
  --ink: #1d2029;
  --muted: #6a7180;
  --tray: #dde1e9;
  --tray-edge: #c9cfdb;
  --tile: #ffffff;
  --tile-edge: #cbd5e1;
  --tile-hover: #f1f5f9;
  --focus: #f08a3c;
  --success: #2e8540;
  --selection: rgba(59, 130, 246, 0.35);
  --selection-border: #2563eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #14161c;
    --ink: #e8e9ee;
    --muted: #9aa0ad;
    --tray: #1f232c;
    --tray-edge: #2d323e;
    --tile: #262c38;
    --tile-edge: #3d4659;
    --tile-hover: #333b4b;
    --selection: rgba(96, 165, 250, 0.4);
    --selection-border: #60a5fa;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 1rem;
  display: flex;
  justify-content: center;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--page);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

button, select { font: inherit; }

.layout {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: min(100%, 640px);
}

.topbar, .game-header, .hud, .actions, footer {
  display: flex;
  align-items: center;
}

.topbar, .game-header, footer { justify-content: space-between; }
.topbar { min-height: 1.75rem; gap: 1rem; }
.home-link { color: inherit; font-weight: 650; text-decoration: none; }
.home-link:hover { text-decoration: underline; }
.account { display: flex; align-items: center; gap: 0.65rem; text-align: right; }
.account a { color: inherit; font-weight: 600; }
.account button { border: 0; padding: 0; color: inherit; background: transparent; text-decoration: underline; cursor: pointer; }

.game-header { gap: 1rem; }
h1, h2, p { margin: 0; }
h1 { font-size: clamp(1.5rem, 5vw, 2.2rem); line-height: 1; }
.eyebrow {
  margin-bottom: 0.25rem;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 750;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.button, .primary-button, .puzzle-card, .pack-tabs button, .close-button {
  border: 1px solid var(--tray-edge);
  border-radius: 8px;
  background: var(--tile);
  color: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
}

.button { padding: 0.5rem 0.85rem; font-size: 0.95rem; }
.button:hover, .pack-tabs button:hover, .puzzle-card:hover {
  background: var(--tile-hover);
  border-color: var(--muted);
}
.button:active { transform: scale(0.98); }

.primary-button {
  padding: 0.65rem 1.25rem;
  background: var(--focus);
  color: #fff;
  border-color: transparent;
  font-size: 1rem;
}
.primary-button:hover { opacity: 0.92; }

.hud {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  background: var(--tray);
  border-radius: 10px;
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
}
.hud-item span { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; font-weight: 650; }
.hud-item strong { font-size: 1.15rem; font-variant-numeric: tabular-nums; }

.selection-status {
  min-height: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-size: 0.95rem;
}
.selection-preview {
  font-family: monospace;
  font-weight: 750;
  letter-spacing: 0.15em;
  color: var(--focus);
}

.board-shell {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--tray);
  border-radius: 12px;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.06);
}

.grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-size, 12), 1fr);
  grid-template-rows: repeat(var(--grid-size, 12), 1fr);
  width: 100%;
  height: 100%;
  gap: 2px;
  touch-action: none;
}

.grid-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  background: var(--tile);
  border-radius: 4px;
  font-size: clamp(0.85rem, 3.2vw, 1.35rem);
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.1s ease, transform 0.08s ease, color 0.1s ease;
}

.grid-cell:hover {
  background: var(--tile-hover);
}

.grid-cell.selecting {
  background: var(--selection) !important;
  outline: 2px solid var(--selection-border);
  z-index: 2;
  transform: scale(1.04);
}

.grid-cell.found {
  background: var(--found-bg, rgba(59, 130, 246, 0.25));
  box-shadow: inset 0 0 0 2px var(--found-border, #3b82f6);
  z-index: 1;
}

.word-list-section {
  background: var(--tray);
  border-radius: 10px;
  padding: 1rem;
}

.word-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 0.5rem;
}

.word-item {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.6rem;
  background: var(--tile);
  border: 1px solid var(--tile-edge);
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  transition: all 0.2s ease;
}

.word-item.found {
  background: var(--word-bg, var(--tray));
  border-color: var(--word-accent, var(--muted));
  color: var(--muted);
}

.word-item.found .word-label {
  text-decoration: line-through;
  opacity: 0.8;
}

.word-check {
  font-size: 0.95rem;
  line-height: 1;
  color: var(--word-accent, var(--muted));
}

.actions {
  justify-content: space-between;
  gap: 1rem;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
}
.mobile-hint { display: none; }
@media (max-width: 600px) {
  .desktop-hint { display: none; }
  .mobile-hint { display: inline; }
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(20, 22, 28, 0.88);
  backdrop-filter: blur(4px);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  color: #fff;
  z-index: 10;
  text-align: center;
  padding: 2rem;
}
.overlay h2 { font-size: 2rem; }
.save-note { font-size: 0.9rem; color: #aeb6c5; }

.leaderboard-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.5rem;
}
.leaderboard-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
}
.leaderboard-label { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.rank-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.rank-list li {
  display: grid;
  grid-template-columns: 2.5rem 1fr auto;
  align-items: center;
  padding: 0.45rem 0.75rem;
  background: var(--tray);
  border-radius: 6px;
  font-size: 0.9rem;
}
.rank-list li.me {
  background: var(--focus);
  color: #fff;
  font-weight: 700;
}
.rank-position { color: var(--muted); font-size: 0.8rem; font-weight: 650; }
.rank-list li.me .rank-position { color: rgba(255, 255, 255, 0.85); }
.rank-name { font-weight: 600; }
.rank-score { font-variant-numeric: tabular-nums; font-weight: 700; }
.my-rank { font-size: 0.85rem; margin-top: 0.25rem; }
.muted { color: var(--muted); font-size: 0.9rem; }

.puzzle-dialog {
  padding: 1.5rem;
  background: var(--page);
  color: var(--ink);
  border: 1px solid var(--tray-edge);
  border-radius: 14px;
  max-width: 520px;
  width: 90vw;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.25);
}
.puzzle-dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}
.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1rem;
}
.close-button {
  background: transparent;
  border: 0;
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem 0.5rem;
}

.pack-tabs {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  padding-bottom: 0.5rem;
  margin-bottom: 0.5rem;
}
.pack-tab {
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
  white-space: nowrap;
}
.pack-tab.active {
  background: var(--ink);
  color: var(--page);
  border-color: transparent;
}
.pack-description {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 1rem;
}

.puzzle-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  max-height: 320px;
  overflow-y: auto;
}

.puzzle-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  text-align: left;
}
.puzzle-card.current {
  border-color: var(--focus);
  background: var(--tile-hover);
}
.puzzle-card.completed .card-title {
  color: var(--success);
}
.card-title { font-weight: 700; font-size: 0.95rem; }
.card-meta { font-size: 0.8rem; color: var(--muted); margin-left: 0.5rem; flex: 1; }
.card-status { font-size: 0.8rem; font-weight: 650; }

footer {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 1rem;
}
