/* Base styling. Every colour, edge and letter face is a token from themes.css. */

:root {
  --cell: 44px;
  --gap: 4px;
  --toggle-h: 52px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

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

body {
  background: var(--bg);
  background-image: var(--bg-image);
  color: var(--ink);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; opacity: 0.4; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

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

.app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           calc(var(--toggle-h) + max(8px, env(safe-area-inset-bottom))) max(16px, env(safe-area-inset-left));
  max-width: 900px; margin: 0 auto;
  gap: 10px;
}

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.title {
  margin: 0; font: var(--title-font); letter-spacing: var(--title-track); color: var(--title);
}
.top-actions { display: flex; gap: 8px; }
.icon-button {
  width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 700; font-size: 1.15rem;
  display: grid; place-items: center;
}
.swatch {
  width: 22px; height: 22px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent) 50%, var(--bg) 50%);
  border: 2px solid var(--accent);
}

/* Input */
.letters-form { display: flex; gap: 8px; }
.input-wrap { position: relative; flex: 1; min-width: 0; }
#letters {
  width: 100%; height: 48px; padding: 0 44px 0 14px;
  border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--field); color: var(--ink);
  font: var(--tile-weight) 1.25rem/1 var(--tile-font); letter-spacing: 0.12em; text-transform: uppercase;
}
#letters::placeholder { color: var(--ink-soft); letter-spacing: 0.02em; font-weight: 400; font-family: system-ui, sans-serif; font-size: 1rem; text-transform: none; }
.clear-input {
  position: absolute; right: 2px; top: 2px; width: 44px; height: 44px;
  border: 0; background: none; font-size: 1.6rem; color: var(--ink-soft);
}
.go {
  height: 48px; min-width: 64px; padding: 0 18px;
  border-radius: var(--radius); border: 0;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 1.05rem;
}

/* Board */
.board {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: space-evenly;
  gap: 12px;
}
.board.landscape { flex-direction: row; }

.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 7), var(--cell));
  grid-auto-rows: var(--cell);
  gap: var(--gap);
  flex: none;
}
.cell {
  width: var(--cell); height: var(--cell); padding: 0;
  border-radius: calc(var(--cell) * var(--tile-round));
  border: 1.5px var(--cell-style) var(--cell-line); background: var(--cell-bg);
  touch-action: none;
}
.cell.drop-target { border-style: solid; border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, transparent); }

.tile {
  display: grid; place-items: center;
  border-radius: calc(var(--tile, 44px) * var(--tile-round));
  background: var(--tile-bg); color: var(--tile-ink);
  border: 0; padding: 0;
  font-family: var(--tile-font); font-weight: var(--tile-weight);
  box-shadow: var(--tile-shadow);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.cell.tile { border: 0; border-radius: calc(var(--cell) * var(--tile-round)); font-size: calc(var(--cell) * 0.55); }
.tile.selected { outline: 3px solid var(--select); outline-offset: 2px; }
.tile.dragging { opacity: 0.3; }

.circle-wrap {
  flex: none; display: grid; place-items: center;
  width: var(--circle, 300px); height: var(--circle, 300px);
}
.circle {
  position: relative; width: 100%; height: 100%;
  border-radius: 50%;
  background: var(--disc);
  border: 1.5px solid var(--line);
}
.circle-wrap.drop-target .circle { border-color: var(--accent); }
.circle .tile {
  position: absolute; left: 50%; top: 50%;
  width: var(--tile); height: var(--tile); margin: calc(var(--tile) / -2) 0 0 calc(var(--tile) / -2);
  font-size: calc(var(--tile) * 0.55);
  transition: transform 320ms cubic-bezier(.3, .7, .3, 1);
}
.spin {
  position: absolute; left: 50%; top: 50%;
  width: var(--centre, 56px); height: var(--centre, 56px);
  margin: calc(var(--centre, 56px) / -2) 0 0 calc(var(--centre, 56px) / -2);
  border-radius: 50%; border: 2px solid var(--accent); background: transparent;
  display: grid; place-items: center;
}
.spin svg { width: 55%; height: 55%; fill: var(--accent); }
.circle.whirling .spin svg { animation: spin 0.45s linear infinite; }
.circle.blurred .tile { filter: blur(0.7px); }
@keyframes spin { to { transform: rotate(360deg); } }

.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  width: var(--tile); height: var(--tile); font-size: calc(var(--tile) * 0.55);
  transform: translate(-50%, -50%) scale(1.1);
}

/* Actions */
.actions { display: flex; justify-content: center; gap: 8px; }
.action {
  flex: 1; max-width: 130px; height: 44px; padding: 0 4px; white-space: nowrap;
  border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface);
  font-weight: 600;
}

/* Words panel */
.words-panel {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  max-width: 900px; margin: 0 auto;
  background: var(--panel); color: var(--ink);
  border-radius: 16px 16px 0 0; border: 1px solid var(--line); border-bottom: 0;
  box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.35);
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; flex-direction: column;
  max-height: 75vh; max-height: 75dvh;
}
.words-toggle {
  flex: none; height: var(--toggle-h); width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  border: 0; background: none; font-weight: 700; font-size: 1.05rem; color: var(--accent-text);
}
.chevron {
  width: 10px; height: 10px; border-right: 2px solid currentColor; border-top: 2px solid currentColor;
  transform: rotate(-45deg) translate(-2px, 2px); transition: transform 200ms;
}
.words-toggle[aria-expanded="true"] .chevron { transform: rotate(135deg) translate(-2px, 2px); }
.words-body { display: flex; flex-direction: column; min-height: 0; flex: 1; padding: 0 16px 12px; }
.words-body[hidden] { display: none; }

.tabs { display: flex; gap: 6px; flex: none; }
.tabs [role="tab"] {
  flex: 1; height: 40px; border-radius: 8px; border: 1px solid var(--line); background: none;
  font-weight: 600; color: var(--ink-soft);
}
.tabs [role="tab"][aria-selected="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.words-status { flex: none; min-height: 1.2em; margin: 8px 0 4px; color: var(--ink-soft); font-size: 0.92rem; }
.words-status button { margin-left: 8px; padding: 4px 12px; border-radius: 6px; border: 1px solid var(--accent); background: none; color: var(--accent-text); }

.results { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.results[hidden] { display: none; }
.results-note { margin: 0 0 6px; color: var(--ink-soft); font-size: 0.9rem; }
.length-group h3 { margin: 12px 0 6px; font-size: 0.85rem; font-weight: 600; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.06em; }
.length-group:first-child h3 { margin-top: 2px; }
.word-list { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; }
.word-list li, .phrase-list li {
  font-family: var(--tile-font); font-weight: var(--tile-weight); letter-spacing: 0.05em;
  padding: 4px 9px; border-radius: 6px; background: var(--soft-fill);
}
.phrase-list { display: flex; flex-direction: column; gap: 6px; margin: 0; padding: 0; list-style: none; }
.multi-status { color: var(--ink-soft); font-size: 0.9rem; margin: 10px 0; }
.find-more {
  height: 44px; padding: 0 20px; margin: 4px 0 8px; border-radius: var(--radius);
  border: 1px solid var(--accent); background: none; color: var(--accent-text); font-weight: 700;
}

/* Help dialog */
.help {
  max-width: min(520px, calc(100vw - 32px)); max-height: 85dvh; overflow: auto;
  border: 1px solid var(--line); border-radius: 16px;
  background: var(--panel); color: var(--ink); padding: 20px 22px;
}
.help::backdrop { background: rgba(0, 0, 0, 0.55); }
.help h2 { margin-top: 0; font-family: var(--tile-font); font-weight: var(--tile-weight); color: var(--title); }
.help ul { padding-left: 1.1em; line-height: 1.5; }
.help li + li { margin-top: 6px; }
.help-small { color: var(--ink-soft); font-size: 0.85rem; }
.help form { text-align: right; }

@media (prefers-reduced-motion: reduce) {
  .circle .tile, .chevron { transition: none; }
  .circle.whirling .spin svg { animation: none; }
}

@media (max-height: 560px) {
  .app { gap: 6px; }
  .title { font-size: 1.15rem; }
  .icon-button { width: 40px; height: 40px; }
}

/* Sign-in page (hosted copy only) */
.login {
  min-height: 100vh; min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px 16px; text-align: center;
}
.login-icon { border-radius: 22px; }
.login-form { display: flex; flex-direction: column; gap: 10px; width: min(320px, 100%); text-align: left; }
.login-form label { font-weight: 600; color: var(--ink-soft); }
.login-form input {
  height: 48px; padding: 0 14px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--field); color: var(--ink); font-size: 1.1rem;
}
.login-error { margin: 0; color: var(--accent-text); font-weight: 600; }

/* Clue bar: camera, clue and Solve in one field */
.clue-bar {
  display: flex; align-items: center; gap: 2px;
  min-height: 48px; padding: 4px;
  border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--field);
}
.clue-bar:focus-within { border-color: var(--accent); }
.clue-camera {
  flex: none; width: 40px; height: 40px; border: 0; border-radius: 8px; background: none;
  display: grid; place-items: center;
}
.clue-camera svg { width: 22px; height: 22px; fill: var(--ink-soft); }
.clue-camera:hover svg, .clue-camera[aria-busy="true"] svg { fill: var(--accent-text); }
/* One 20px line centred in a 40px field; grows to four lines for long clues.
   16px text stops iOS zooming in when the field is tapped. */
#clue {
  flex: 1; min-width: 0; height: 40px; max-height: 100px; padding: 10px 6px; margin: 0; resize: none;
  overflow-y: hidden; scrollbar-width: none; -webkit-appearance: none; appearance: none;
  border: 0; border-radius: 0; background: none; color: var(--ink); outline: none;
  font: 16px/20px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
#clue::placeholder { color: var(--ink-soft); }
.clue-solve {
  flex: none; height: 40px; padding: 0 16px; border: 0; border-radius: max(4px, calc(var(--radius) - 2px));
  background: var(--solve-bg); color: var(--solve-ink);
  font-weight: 700; font-size: 0.98rem;
}
.clue-solve:hover:not(:disabled) { filter: brightness(1.1); }

/* Answer card */
.answer-card {
  position: relative; flex: none; display: flex; flex-direction: column;
  max-height: 30vh; max-height: 30dvh;
  padding: 12px 44px 12px 14px; border-radius: calc(var(--radius) + 4px);
  background: var(--card-bg);
  border: 1.5px solid var(--card-line);
}
.answer-card[hidden] { display: none; }
.answer-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; scrollbar-width: thin; padding-right: 6px; }
.answer-actions:empty { display: none; }
.answer-actions { flex: none; padding-top: 10px; }
.answer-close {
  position: absolute; top: 4px; right: 4px; width: 36px; height: 36px;
  border: 0; border-radius: 50%; background: none; font-size: 1.35rem; color: var(--ink-soft);
}
.answer-head { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; }
.answer-word { margin: 0; font: var(--tile-weight) 1.4rem/1.15 var(--tile-font); letter-spacing: 0.08em; color: var(--answer); }
.answer-chip {
  padding: 2px 8px; border-radius: 999px; border: 1px solid var(--chip-line); background: var(--chip-bg);
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--chip-ink);
}
.answer-explain { margin: 6px 0 0; font-size: 0.93rem; line-height: 1.45; }
.answer-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 8px; font-size: 0.82rem; color: var(--ink-soft); }
.answer-alt { padding: 2px 8px; border-radius: 6px; background: var(--soft-fill); color: var(--ink); font: var(--tile-weight) 0.85rem var(--tile-font); letter-spacing: 0.05em; }
.answer-ruled { text-decoration: line-through; opacity: 0.7; }
.answer-retry {
  height: 36px; padding: 0 14px; border-radius: 18px;
  border: 1px solid var(--line); background: none; color: var(--ink); font-weight: 600; font-size: 0.88rem;
}
.answer-retry:hover { border-color: var(--accent); color: var(--accent-text); }
.answer-msg { margin: 0; font-size: 0.95rem; line-height: 1.4; color: var(--ink-soft); }

/* Working indicators: spinner + elapsed time in the card, spinners on the busy button */
@keyframes pulse { 50% { opacity: 0.25; } }
.spinner {
  flex: none; display: inline-block; width: 20px; height: 20px; border-radius: 50%;
  border: 2.5px solid color-mix(in srgb, var(--accent) 25%, transparent);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
.answer-msg.busy { display: flex; align-items: center; gap: 10px; padding: 4px 0; color: var(--ink); }
.answer-elapsed { margin-left: auto; font-variant-numeric: tabular-nums; font-size: 0.85rem; color: var(--ink-soft); }
.clue-solve[aria-busy="true"], .clue-camera[aria-busy="true"] { position: relative; opacity: 1; }
.clue-solve[aria-busy="true"] { color: transparent; }
.clue-camera[aria-busy="true"] svg { visibility: hidden; }
/* The spinner takes the colour the button's own text would have, so it shows on
   solid Solve buttons (new themes) as well as tinted ones (classic themes). */
.clue-solve[aria-busy="true"]::after, .clue-camera[aria-busy="true"]::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 18px; height: 18px; margin: -9px 0 0 -9px;
  border-radius: 50%; border: 2.5px solid color-mix(in srgb, var(--spin-ink) 30%, transparent);
  border-top-color: var(--spin-ink); animation: spin 0.8s linear infinite;
}
.clue-solve[aria-busy="true"] { --spin-ink: var(--solve-ink); }
.clue-camera[aria-busy="true"] { --spin-ink: var(--accent-text); }
@media (prefers-reduced-motion: reduce) {
  .spinner, .clue-solve[aria-busy="true"]::after, .clue-camera[aria-busy="true"]::after {
    animation: pulse 1.6s ease-in-out infinite; border-color: var(--accent);
  }
  .clue-solve[aria-busy="true"]::after, .clue-camera[aria-busy="true"]::after { border-color: var(--spin-ink);
  }
}

/* In-app camera viewfinder (no shutter sound, no "Use Photo" step) */
.camera-sheet {
  position: fixed; inset: 0; z-index: 100; background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
}
.camera-sheet[hidden] { display: none; }
.camera-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.camera-frame {
  position: absolute; left: 6%; right: 6%; top: 34%; height: 22%;
  border: 2px solid rgba(255, 255, 255, 0.9); border-radius: 14px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35);
  pointer-events: none;
}
.camera-hint {
  position: absolute; left: 16px; right: 16px; top: calc(34% - 44px); margin: 0;
  text-align: center; color: #fff; font-weight: 600; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.camera-controls {
  position: relative; width: 100%; max-width: 480px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 24px calc(24px + env(safe-area-inset-bottom));
}
.camera-text { min-width: 72px; height: 44px; border: 0; background: none; color: #fff; font-weight: 600; font-size: 1.05rem; }
.camera-shutter {
  width: 72px; height: 72px; border-radius: 50%; padding: 0;
  background: #fff; border: 4px solid rgba(255, 255, 255, 0.45); background-clip: padding-box;
  box-shadow: 0 0 0 3px #fff;
}
.camera-shutter:active { transform: scale(0.94); }

/* Colour picker */
.picker {
  width: min(400px, calc(100vw - 32px)); max-height: 85dvh; overflow: auto;
  border: 1px solid var(--line); border-radius: calc(var(--radius) + 6px);
  background: var(--panel); color: var(--ink); padding: 18px 18px 16px;
}
.picker::backdrop { background: rgba(0, 0, 0, 0.55); }
.picker h2 { margin: 0 0 6px; font: var(--title-font); letter-spacing: var(--title-track); color: var(--title); }
.picker-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.picker-group {
  grid-column: 1 / -1; margin: 10px 0 0; font-size: 0.75rem; font-weight: 700;
  letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft);
}
.picker-option {
  display: flex; flex-direction: column; align-items: stretch; gap: 6px; padding: 6px 6px 8px;
  border-radius: var(--radius); border: 1.5px solid var(--line); background: var(--surface);
  text-align: left; font-size: 0.85rem; font-weight: 600; line-height: 1.2;
}
.picker-option[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.picker-preview {
  position: relative; height: 42px; border-radius: max(3px, calc(var(--radius) - 3px));
  display: flex; align-items: center; justify-content: center;
  border: 1px solid rgba(128, 128, 128, 0.35);
}
.picker-tile {
  width: 28px; height: 28px; border-radius: 5px; display: grid; place-items: center;
  font: 700 18px/1 Georgia, serif; box-shadow: 0 2px 0 rgba(0, 0, 0, 0.35);
}
.picker-dot { position: absolute; right: 7px; bottom: 7px; width: 9px; height: 9px; border-radius: 50%; }
.picker form { margin-top: 14px; text-align: right; }
