:root {
  color-scheme: dark;

  --bg: #130f1c;
  --surface: #1d1729;
  --surface-2: #251d34;
  --border: #2a2338;

  --text-primary: #f6f3fb;
  --text-secondary: #cabfe0;
  --text-muted: #8f80ab;

  --accent: #a855f7;
  --accent-strong: #9333ea;
  --accent-soft: #c4b5fd;
  --accent-on: #ffffff;
  --danger: #dc2626;

  --square-light: #d9d2e0;
  --square-dark: #4b3a66;
  --square-highlight: #f6d76b;
}

:root[data-theme="light"] {
  color-scheme: light;

  --bg: #faf8ff;
  --surface: #ffffff;
  --surface-2: #f3effa;
  --border: #e3ddf0;

  --text-primary: #241c33;
  --text-secondary: #5c5270;
  --text-muted: #817598;

  --accent: #9333ea;
  --accent-strong: #7e22ce;
  --accent-soft: #7c3aed;
  --accent-on: #ffffff;
  --danger: #dc2626;

  --square-light: #f4f0fa;
  --square-dark: #b7a0dd;
  --square-highlight: #f0b429;
}

/* Board square themes — independent of light/dark app theme, so these come
   after it in source order and only ever touch --square-*. */
:root[data-board-theme="green"] {
  --square-light: #eeeed2;
  --square-dark: #769656;
  --square-highlight: #f6d76b;
}

:root[data-board-theme="blue"] {
  --square-light: #dee3e6;
  --square-dark: #4b7399;
  --square-highlight: #f6d76b;
}

:root[data-board-theme="slate"] {
  --square-light: #e3e3e3;
  --square-dark: #6b6b6b;
  --square-highlight: #f0b429;
}

/* Board textures — a subtle background-image layered on top of whichever
   --square-* color theme is active above, so texture and color are two
   independent choices instead of one combined picker. */
:root[data-board-texture="wood"] .square.light {
  background-image: repeating-linear-gradient(
    118deg,
    rgba(90, 55, 10, 0.09) 0px,
    rgba(90, 55, 10, 0.09) 2px,
    transparent 2px,
    transparent 7px
  );
}
:root[data-board-texture="wood"] .square.dark {
  background-image: repeating-linear-gradient(
    118deg,
    rgba(0, 0, 0, 0.14) 0px,
    rgba(0, 0, 0, 0.14) 2px,
    transparent 2px,
    transparent 7px
  );
}

:root[data-board-texture="marble"] .square.light {
  background-image:
    radial-gradient(circle at 22% 28%, rgba(255, 255, 255, 0.35), transparent 42%),
    radial-gradient(circle at 72% 68%, rgba(0, 0, 0, 0.08), transparent 38%);
}
:root[data-board-texture="marble"] .square.dark {
  background-image:
    radial-gradient(circle at 30% 72%, rgba(255, 255, 255, 0.12), transparent 42%),
    radial-gradient(circle at 76% 22%, rgba(0, 0, 0, 0.18), transparent 38%);
}

:root[data-board-texture="felt"] .square.light,
:root[data-board-texture="felt"] .square.dark {
  background-image: radial-gradient(rgba(0, 0, 0, 0.09) 1px, transparent 1.3px);
  background-size: 5px 5px;
}

/* Piece accent — a colored glow behind each piece, independent of board
   theme/texture. Uses drop-shadow (not a fill recolor) so it works
   regardless of the underlying piece artwork's own colors. */
:root[data-piece-accent="gold"] .piece-img {
  filter: drop-shadow(0 0 3px #f0b429cc);
}
:root[data-piece-accent="ice"] .piece-img {
  filter: drop-shadow(0 0 3px #38bdf8cc);
}
:root[data-piece-accent="ember"] .piece-img {
  filter: drop-shadow(0 0 3px #f97316cc);
}
:root[data-piece-accent="jade"] .piece-img {
  filter: drop-shadow(0 0 3px #34d399cc);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: "Fredoka", system-ui, -apple-system, "Segoe UI", sans-serif;
  /* Native-app-wrapper polish, not needed for a plain browser tab: kills
     the grey/blue flash Safari/Chrome put on every tap (-webkit-tap-
     highlight-color), and the rubber-band/pull-to-refresh bounce at the
     top and bottom of the page (overscroll-behavior-y) — both read as
     "this is a website" the instant this is running inside an app shell. */
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

/* Buttons/nav/board chrome shouldn't show a text-selection caret or a
   long-press "Copy/Share" callout — normal, readable content (game
   reviews, dashboard text, chat messages) is deliberately NOT included
   here and stays selectable. */
.btn,
.sidebar,
.board,
.piece-img,
.page-header,
.card h2,
.card h3 {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

a {
  color: var(--accent-soft);
}

.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.2rem 0.9rem;
  gap: 0.3rem;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text-primary);
  padding: 0.3rem 0.6rem 1.2rem;
  background: none;
  border: none;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  width: 100%;
  text-align: left;
}

.sidebar-brand .brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  object-fit: cover;
  flex-shrink: 0;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.55rem 0.7rem;
  border-radius: 6px;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
}

.nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.nav-link:hover {
  background: var(--surface-2);
  color: var(--text-primary);
}

.nav-link.active {
  background: var(--accent);
  color: var(--accent-on);
}

.app-main {
  flex: 1;
  padding: 0.9rem 2rem 1.5rem;
  min-width: 0;
  overflow-y: auto;
}

.page-header {
  font-size: 1.3rem;
  font-weight: 700;
  margin: 0 0 1.4rem;
  color: var(--text-primary);
}

.page-header .stage-tag {
  font-size: 0.85rem;
  font-weight: normal;
  color: var(--text-muted);
  margin-left: 0.5rem;
}

.btn {
  padding: 0.5rem 1.2rem;
  font-size: 0.92rem;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-family: inherit;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

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

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-on);
}

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

.btn-danger:hover {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.2rem 1.4rem;
}

/* !important is deliberate: .hidden is a utility meant to always win, but as
   a single-class selector it ties in specificity with any other single-class
   rule that sets `display` (e.g. .modal-overlay { display: flex }) — a tie
   is broken by cascade order, which silently picks the wrong one depending on
   stylesheet load order. This bit .modal-overlay and .puzzle-board-wrap both:
   adding "hidden" to an element that also had its own `display` rule did
   nothing, because the other rule loaded later and won the tie. */
.hidden {
  display: none !important;
}

.muted-inline {
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* Shared info popover — click-to-open detail card used by the dashboard's
   tactic chips, the openings collection page, and account.html's
   achievement badges. Each page keeps its own small copy of the JS that
   builds/positions one of these rather than a shared module — watch for
   this CSS class if renaming it, since a rename here needs to land in all
   of dashboard.js, openings.js, and account.html's inline script. */
.info-popover {
  position: fixed;
  z-index: 50;
  max-width: 280px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  animation: info-popover-in 0.15s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes info-popover-in {
  from {
    opacity: 0;
    transform: translateY(-4px) scale(0.97);
  }
}

@media (prefers-reduced-motion: reduce) {
  .info-popover {
    animation: none;
  }
}

.info-popover strong {
  display: block;
  margin-bottom: 0.35rem;
  color: var(--text-primary);
  font-size: 0.9rem;
}

.info-popover p {
  margin: 0 0 0.5rem;
  line-height: 1.4;
}

.info-popover .info-meta {
  margin: 0 0 0.5rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.info-popover a.learn-more-link {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent-soft);
  text-decoration: none;
}

.info-popover a.learn-more-link:hover {
  text-decoration: underline;
}

/* Shared grid/list entrance — referenced by name (not a utility class, so
   no template changes needed) from each page's own card/tile rule:
   .stat-tile, .achievement-badge, .opening-card, .puzzle-card, .game-card.
   Staggered only for the first handful of items — a long list (50+
   puzzles) doesn't need every item waiting its turn through an ever-longer
   delay, and anything past the fold is invisible when the delay would run
   anyway. */
@keyframes grid-item-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .stat-tile,
  .achievement-badge,
  .opening-card,
  .puzzle-card,
  .game-card {
    animation: none !important;
  }
}

@media (max-width: 720px) {
  .app-shell {
    flex-direction: column;
  }

  /* Bottom tab bar instead of a side rail — fixed so it's always reachable,
     icon-only (labels hidden below) so all 5 items fit one row with no
     scrolling, regardless of screen width. */
  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    z-index: 50;
    width: 100%;
    /* The 60px bar itself, PLUS the iPhone home-indicator strip (0 on
       Android and on any browser that doesn't need it — env() resolves to
       0 without viewport-fit=cover, which is exactly the "not standalone,
       no notch" case). Padding rather than adding to height so the icons
       stay vertically centered in the visible 60px, not pushed up into it. */
    height: calc(60px + env(safe-area-inset-bottom));
    padding-top: 0;
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    flex-direction: row;
    align-items: stretch;
    justify-content: space-around;
    gap: 0;
    border-right: none;
    border-top: 1px solid var(--border);
  }

  .sidebar-brand {
    display: none;
  }

  .nav-link {
    flex: 1;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 0.2rem;
    padding: 0.4rem 0.2rem;
    border-radius: 0;
    font-size: 0;
  }

  .nav-label {
    display: none;
  }

  .nav-icon {
    width: 22px;
    height: 22px;
  }

  /* Room for the fixed bottom bar (now taller by the home-indicator inset
     on iOS, see .sidebar above) so content never sits behind it —
     .play-page's own height:100% already respects this since it's a
     percentage of .app-main's padded content box, no separate override
     needed there. padding-top covers the same notch/status-bar area at
     the other edge, since .app-shell stacks column-wise here and nothing
     else claims that space on mobile. */
  .app-main {
    padding-top: calc(0.9rem + env(safe-area-inset-top));
    padding-bottom: calc(60px + env(safe-area-inset-bottom) + 0.9rem);
    padding-left: calc(2rem + env(safe-area-inset-left));
    padding-right: calc(2rem + env(safe-area-inset-right));
  }
}
