/* ============================================================
   Queens Game Unlimited — stylesheet
   Design: "royal board" — deep navy surface, warm crown-gold accent.
   No webfonts (system font stack only) to avoid font-swap CLS.
   ============================================================ */

:root,
:root[data-theme="dark"] {
  --bg: #10121a;
  --bg-alt: #151826;
  --surface: #1b1f2e;
  --surface-2: #232841;
  --border: #2c3149;
  --text: #eef0f6;
  --text-muted: #9aa1b8;
  --accent: #e8b34c;
  --accent-strong: #f6c869;
  --accent-contrast: #241a05;
  --danger: #ef5b6b;
  --success: #4cc38a;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --focus-ring: 0 0 0 3px rgba(232, 179, 76, 0.45);

  --c0: #4c6ef5;
  --c1: #f76707;
  --c2: #37b24d;
  --c3: #f03e3e;
  --c4: #ae3ec9;
  --c5: #f5c000;
  --c6: #15aabf;
  --c7: #e64980;
  --c8: #82c91e;
}

:root[data-theme="light"] {
  --bg: #f3f2ed;
  --bg-alt: #ecebe3;
  --surface: #ffffff;
  --surface-2: #f6f4ec;
  --border: #ddd8c8;
  --text: #211d12;
  --text-muted: #6b6556;
  --accent: #b5791c;
  --accent-strong: #96620f;
  --accent-contrast: #fffaf0;
  --danger: #c92a3a;
  --success: #2f9e64;
  --shadow: 0 10px 24px rgba(60, 50, 20, 0.12);
  --focus-ring: 0 0 0 3px rgba(181, 121, 28, 0.35);

  --c0: #7d9bf7;
  --c1: #f2954b;
  --c2: #63c67e;
  --c3: #f2707d;
  --c4: #c877dd;
  --c5: #e0b400;
  --c6: #4bc4d9;
  --c7: #ee7fa5;
  --c8: #a3d454;
}

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

html {
  color-scheme: dark;
  -webkit-text-size-adjust: 100%;
}
html[data-theme="light"] {
  color-scheme: light;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.55;
  transition: background-color 0.25s ease, color 0.25s ease;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

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

/* ---------- Skip link ---------- */
.skip-link {
  position: absolute;
  left: 8px; top: -48px;
  background: var(--accent);
  color: var(--accent-contrast);
  padding: 10px 14px;
  border-radius: 8px;
  z-index: 1000;
  transition: top 0.2s ease;
}
.skip-link:focus { top: 8px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  min-height: 64px;
  background: var(--bg-alt);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
}
.brand .crown-mark { font-size: 1.3rem; }

#chronometer {
  font-family: ui-monospace, "SFMono-Regular", "Cascadia Code", Consolas, "Liberation Mono", monospace;
  font-variant-numeric: tabular-nums;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
  min-width: 118px;
  text-align: center;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--accent-strong);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 8px;
  font-size: 1.05rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, background-color 0.15s ease, border-color 0.15s ease;
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn:focus-visible,
button:focus-visible,
select:focus-visible,
input:focus-visible,
a:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

/* ---------- Layout ---------- */
main { max-width: 960px; margin: 0 auto; padding: 0 16px 48px; }

.game-wrap {
  padding-top: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

/* Reserve square space up-front to prevent CLS before JS builds the grid */
#grid {
  --grid-size: 9;
  width: min(94vw, 72vh, 480px);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: repeat(var(--grid-size), 1fr);
  grid-template-rows: repeat(var(--grid-size), 1fr);
  gap: 2px;
  padding: 6px;
  contain: layout paint;
  overflow: hidden;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  cursor: pointer;
  line-height: 1;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: filter 0.1s ease, box-shadow 0.15s ease;
}
.cell:hover { filter: brightness(1.08); }
.cell .mark {
  pointer-events: none;
  font-size: clamp(22px, 7vw, 42px);
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.55),
    0 0 10px rgba(0, 0, 0, 0.25);
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.35));
}
.cell.adjacent-error {
  box-shadow: inset 0 0 0 3px var(--danger);
}
.cell.adjacent-error .mark {
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.55),
    0 0 12px rgba(239, 91, 107, 0.65);
}

/* ---------- Controls ---------- */
.controls {
  width: min(94vw, 480px);
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: center;
}

.select,
.btn {
  font: inherit;
  font-size: 0.95rem;
  padding: 10px 14px;
  min-height: 42px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.select { min-width: 150px; flex: 1 1 150px; }

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

.btn-secondary:hover { border-color: var(--accent); }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--text-muted);
  min-height: 42px;
}
.toggle-row input { width: 18px; height: 18px; accent-color: var(--accent); }

.puzzle-count {
  width: 100%;
  text-align: center;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin: -2px 0 0;
}

/* ---------- Popups / Panels ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 7, 12, 0.6);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 200;
}
.overlay.open { display: flex; }

.panel {
  width: min(92vw, 420px);
  max-height: 86vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 22px;
}
.panel h2 { margin-top: 0; }

.panel-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.panel-row:last-of-type { border-bottom: none; }

.switch {
  position: relative;
  width: 46px; height: 26px;
  flex: 0 0 auto;
}
.switch input { opacity: 0; width: 100%; height: 100%; position: absolute; margin: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0;
  background: var(--border);
  border-radius: 999px;
  transition: background-color 0.2s ease;
}
.switch .thumb {
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--text);
  transition: transform 0.2s ease;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track + .thumb { transform: translateX(20px); background: var(--accent-contrast); }

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 12px 0;
}
.stat-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  text-align: center;
}
.stat-card .value {
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: 1.3rem;
  color: var(--accent-strong);
  font-variant-numeric: tabular-nums;
}
.stat-card .label { font-size: 0.78rem; color: var(--text-muted); margin-top: 4px; }

.close-x {
  position: absolute;
  top: 14px; right: 14px;
}

.panel-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.share-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.share-btn {
  flex: 1 1 auto;
  text-align: center;
  padding: 10px 14px;
  border-radius: 9px;
  font-weight: 600;
  color: #fff;
}
.share-twitter { background: #1d9bf0; }
.share-reddit { background: #ff4500; }
.share-copy { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }

/* ---------- Content sections ---------- */
.content-section {
  margin-top: 44px;
  padding-top: 8px;
}
.content-section h2 {
  font-size: 1.5rem;
  margin-bottom: 10px;
}
.content-section p { color: var(--text-muted); }
.content-section p a { color: var(--accent); }

.steps-list {
  list-style: none;
  counter-reset: step;
  margin: 18px 0 0;
  padding: 0;
  display: grid;
  gap: 12px;
}
.steps-list li {
  counter-increment: step;
  position: relative;
  padding: 14px 16px 14px 52px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.steps-list li::before {
  content: counter(step);
  position: absolute;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 700;
  font-size: 0.85rem;
  display: flex; align-items: center; justify-content: center;
}
.steps-list strong { color: var(--text); }

.faq-list { margin-top: 16px; display: grid; gap: 10px; }
.faq-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 16px;
}
.faq-item summary {
  cursor: pointer;
  padding: 14px 0;
  font-weight: 600;
  list-style: none;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.faq-item summary::-webkit-details-marker { display: none; }
.faq-item summary::after {
  content: "+";
  color: var(--accent);
  font-size: 1.2rem;
  flex-shrink: 0;
}
.faq-item[open] summary::after { content: "\2212"; }
.faq-item p { margin: 0 0 14px; color: var(--text-muted); }

/* ---------- Footer ---------- */
footer {
  border-top: 1px solid var(--border);
  background: var(--bg-alt);
  margin-top: 48px;
  padding: 22px 16px 30px;
}
.footer-inner {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
}
.footer-links { display: flex; gap: 14px; flex-wrap: wrap; }
.disclaimer { font-size: 0.78rem; opacity: 0.85; }

/* ---------- Responsive ---------- */

/* Small phones */
@media (max-width: 380px) {
  #chronometer { min-width: 96px; font-size: 0.9rem; padding: 6px 8px; }
  .icon-btn { width: 36px; height: 36px; min-width: 36px; font-size: 0.95rem; }
  .select, .btn { padding: 8px 10px; font-size: 0.88rem; }
  .cell .mark { font-size: clamp(18px, 8vw, 32px); }
}

/* Phones in general */
@media (max-width: 600px) {
  .topbar { padding: 8px 10px; gap: 8px; }
  .brand span.brand-text { display: none; }
  main { padding: 0 12px 40px; }
  .content-section h2 { font-size: 1.3rem; }
  .controls { justify-content: stretch; }
  .controls .select { flex: 1 1 100%; }
  .controls .btn { flex: 1 1 48%; }
  .panel { padding: 18px; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
}

/* Small tablets / large phones, portrait */
@media (min-width: 601px) and (max-width: 900px) {
  #grid { width: min(80vw, 72vh, 480px); }
}

/* Tablets and up */
@media (min-width: 700px) {
  #grid { width: min(56vw, 72vh, 480px); }
  .controls { justify-content: center; }
}

/* Landscape phones / short viewports: prioritize height so the board and controls both fit */
@media (max-height: 500px) and (orientation: landscape) {
  .topbar { min-height: 48px; padding: 6px 12px; position: static; }
  .game-wrap { padding-top: 12px; gap: 10px; }
  #grid { width: min(46vh, 60vw, 420px); }
  .content-section { margin-top: 24px; }
}

/* Large desktop screens: keep the board a comfortable, not oversized, size */
@media (min-width: 1200px) {
  #grid { width: min(40vw, 72vh, 520px); }
}

/* Respect safe areas on notched devices */
.topbar {
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
footer {
  padding-bottom: max(30px, env(safe-area-inset-bottom));
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
