/* ============================================================
   xword — Material Design 3 stylesheet

   Source color: #0B57D0 (deep blue)
   Type: Roboto Serif (display) · Roboto Flex (body) · Roboto Mono
   Shape: 4/8/12/16/28/full
   Color roles: full M3 token set, light + dark
   ============================================================ */

/* ============ RESET ============ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ============ M3 COLOR TOKENS — LIGHT ============ */
:root {
  /* Primary */
  --md-sys-color-primary: #0B57D0;
  --md-sys-color-on-primary: #FFFFFF;
  --md-sys-color-primary-container: #D8E2FF;
  --md-sys-color-on-primary-container: #001A41;

  /* Secondary */
  --md-sys-color-secondary: #565F71;
  --md-sys-color-on-secondary: #FFFFFF;
  --md-sys-color-secondary-container: #DAE2F9;
  --md-sys-color-on-secondary-container: #131C2B;

  /* Tertiary */
  --md-sys-color-tertiary: #715573;
  --md-sys-color-on-tertiary: #FFFFFF;
  --md-sys-color-tertiary-container: #FBD7FB;
  --md-sys-color-on-tertiary-container: #29132D;

  /* Error */
  --md-sys-color-error: #BA1A1A;
  --md-sys-color-on-error: #FFFFFF;
  --md-sys-color-error-container: #FFDAD6;
  --md-sys-color-on-error-container: #410002;

  /* Surface / background */
  --md-sys-color-background: #FAF9FD;
  --md-sys-color-on-background: #1A1B21;
  --md-sys-color-surface: #FAF9FD;
  --md-sys-color-on-surface: #1A1B21;
  --md-sys-color-surface-variant: #E1E2EC;
  --md-sys-color-on-surface-variant: #45464F;

  /* Outline */
  --md-sys-color-outline: #757780;
  --md-sys-color-outline-variant: #C5C6D0;

  /* Tonal surface containers (elevation 0 → 5 equivalents) */
  --md-sys-color-surface-dim: #DAD9DD;
  --md-sys-color-surface-bright: #FAF9FD;
  --md-sys-color-surface-container-lowest: #FFFFFF;
  --md-sys-color-surface-container-low: #F4F3F7;
  --md-sys-color-surface-container: #EEEDF1;
  --md-sys-color-surface-container-high: #E8E7EB;
  --md-sys-color-surface-container-highest: #E3E2E6;

  /* Inverse + scrim */
  --md-sys-color-inverse-surface: #2F3033;
  --md-sys-color-inverse-on-surface: #F1F0F4;
  --md-sys-color-inverse-primary: #ADC6FF;
  --md-sys-color-scrim: rgba(0, 0, 0, 0.5);

  /* Custom — success / hint (kept consistent with M3 conventions) */
  --md-sys-color-success: #006D3A;
  --md-sys-color-on-success: #FFFFFF;
  --md-sys-color-success-container: #B5F1C4;
  --md-sys-color-on-success-container: #002111;

  --md-sys-color-hint: #7B4E7E;
  --md-sys-color-on-hint: #FFFFFF;
  --md-sys-color-hint-container: #FDD6FA;
  --md-sys-color-on-hint-container: #2D1230;

  /* Crossword-specific cell tokens. Decoupled from M3 surface tokens so
     light and dark modes both keep "paper-card" letter cells (max
     readability). Dark mode = light cells on dark background — feels
     like a piece of paper sitting on a dark table. */
  --xword-cell-bg: #FFFFFF;
  --xword-cell-fg: #1A1B21;
  --xword-cell-block: #1A1B21;
  --xword-cell-num: #45464F;
  --xword-grid-line: #757780;
  --xword-active-bg: #0B57D0;
  --xword-active-fg: #FFFFFF;
  --xword-active-word-bg: #D8E2FF;
  --xword-active-word-fg: #001A41;
  --xword-correct-bg: #B5F1C4;
  --xword-correct-fg: #002111;

  /* ----- M3 typography ----- */
  --md-font-display: 'Roboto Serif', Georgia, serif;
  --md-font-body: 'Roboto Flex', system-ui, sans-serif;
  --md-font-mono: 'Roboto Mono', ui-monospace, monospace;
  --md-icon-font: 'Material Symbols Outlined';

  /* ----- M3 shape ----- */
  --md-shape-xs: 4px;
  --md-shape-sm: 8px;
  --md-shape-md: 12px;
  --md-shape-lg: 16px;
  --md-shape-xl: 28px;
  --md-shape-full: 9999px;

  /* ----- Elevation (subtle in light, more subtle in dark) ----- */
  --md-elev-1: 0 1px 2px rgba(0,0,0,0.05), 0 1px 3px 1px rgba(0,0,0,0.04);
  --md-elev-2: 0 1px 2px rgba(0,0,0,0.07), 0 2px 6px 2px rgba(0,0,0,0.05);
  --md-elev-3: 0 1px 3px rgba(0,0,0,0.08), 0 4px 8px 3px rgba(0,0,0,0.06);
  --md-elev-4: 0 2px 3px rgba(0,0,0,0.10), 0 6px 10px 4px rgba(0,0,0,0.08);
  --md-elev-5: 0 4px 4px rgba(0,0,0,0.12), 0 8px 12px 6px rgba(0,0,0,0.10);

  /* ----- Motion system — one timing scale, reused everywhere.
     Anything spatial uses --ease-spring (with a touch of overshoot).
     Opacity/color uses --ease-flat (no overshoot — fades that overshoot
     read as broken). Duration tokens are intentionally few; reach for the
     next one up only when a transition spans a bigger distance. ----- */
  --xd-dur-fast: 120ms;       /* hover/press feedback — must feel instant */
  --xd-dur-quick: 200ms;      /* small state changes, focus rings */
  --xd-dur-base: 320ms;       /* card hover lifts, panel opens */
  --xd-dur-page: 480ms;       /* view transitions, overlay drops */
  --ease-flat: cubic-bezier(0.2, 0, 0, 1);                        /* M3 emphasized */
  --ease-spring: cubic-bezier(0.2, 1.2, 0.3, 1);                  /* gentle overshoot, settle */
  --ease-spring-soft: cubic-bezier(0.34, 1.56, 0.64, 1);          /* deeper overshoot for big moves */
  --ease-anticipate: cubic-bezier(0.7, -0.4, 0.4, 1.4);           /* tiny counter-move before the main move */
}

/* ============ M3 COLOR TOKENS — DARK ============ */
html[data-theme="dark"] {
  --md-sys-color-primary: #ADC6FF;
  --md-sys-color-on-primary: #002E69;
  --md-sys-color-primary-container: #00428E;
  --md-sys-color-on-primary-container: #D8E2FF;

  --md-sys-color-secondary: #BEC6DC;
  --md-sys-color-on-secondary: #283041;
  --md-sys-color-secondary-container: #3E4759;
  --md-sys-color-on-secondary-container: #DAE2F9;

  --md-sys-color-tertiary: #DEBCDE;
  --md-sys-color-on-tertiary: #402843;
  --md-sys-color-tertiary-container: #583E5B;
  --md-sys-color-on-tertiary-container: #FBD7FB;

  --md-sys-color-error: #FFB4AB;
  --md-sys-color-on-error: #690005;
  --md-sys-color-error-container: #93000A;
  --md-sys-color-on-error-container: #FFDAD6;

  --md-sys-color-background: #111318;
  --md-sys-color-on-background: #E3E2E6;
  --md-sys-color-surface: #111318;
  --md-sys-color-on-surface: #E3E2E6;
  --md-sys-color-surface-variant: #45464F;
  --md-sys-color-on-surface-variant: #C5C6D0;
  --md-sys-color-outline: #8F9099;
  --md-sys-color-outline-variant: #45464F;

  --md-sys-color-surface-dim: #111318;
  --md-sys-color-surface-bright: #36363B;
  --md-sys-color-surface-container-lowest: #0C0E13;
  --md-sys-color-surface-container-low: #1A1B21;
  --md-sys-color-surface-container: #1E1F25;
  --md-sys-color-surface-container-high: #282A30;
  --md-sys-color-surface-container-highest: #33353A;

  --md-sys-color-inverse-surface: #E3E2E6;
  --md-sys-color-inverse-on-surface: #2F3033;
  --md-sys-color-inverse-primary: #0B57D0;
  --md-sys-color-scrim: rgba(0, 0, 0, 0.7);

  --md-sys-color-success: #58DB8B;
  --md-sys-color-on-success: #00391C;
  --md-sys-color-success-container: #00522D;
  --md-sys-color-on-success-container: #71F8A7;

  --md-sys-color-hint: #ECB5EE;
  --md-sys-color-on-hint: #46214B;
  --md-sys-color-hint-container: #5F3863;
  --md-sys-color-on-hint-container: #FDD6FA;

  /* Dark-mode crossword cells: keep them LIGHT for legibility. The board
     reads as a paper page sitting on a dark desk — text and grid lines
     stay punchy, fillable squares are unmistakable. */
  --xword-cell-bg: #ECECEF;
  --xword-cell-fg: #1A1B21;
  --xword-cell-block: #050608;
  --xword-cell-num: #45464F;
  --xword-grid-line: #5A5C63;
  --xword-active-bg: #0B57D0;
  --xword-active-fg: #FFFFFF;
  --xword-active-word-bg: #C8DBFF;
  --xword-active-word-fg: #001A41;
  --xword-correct-bg: #99E5B4;
  --xword-correct-fg: #002111;

  --md-elev-1: 0 1px 2px rgba(0,0,0,0.35), 0 1px 3px 1px rgba(0,0,0,0.18);
  --md-elev-2: 0 1px 2px rgba(0,0,0,0.40), 0 2px 6px 2px rgba(0,0,0,0.22);
  --md-elev-3: 0 1px 3px rgba(0,0,0,0.45), 0 4px 8px 3px rgba(0,0,0,0.26);
  --md-elev-4: 0 2px 3px rgba(0,0,0,0.50), 0 6px 10px 4px rgba(0,0,0,0.30);
  --md-elev-5: 0 4px 4px rgba(0,0,0,0.55), 0 8px 12px 6px rgba(0,0,0,0.32);
}

/* ============ BASE ============ */
html, body {
  font-family: var(--md-font-body);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
body {
  background: var(--md-sys-color-background);
  color: var(--md-sys-color-on-surface);
  min-height: 100vh;
  padding: 24px 16px 24px;
  overscroll-behavior: none;
  transition: background 0.2s ease;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
}

.view { display: none; }
.view.active { display: block; }

a { color: var(--md-sys-color-primary); }
a:hover { text-decoration-thickness: 2px; }

::selection { background: var(--md-sys-color-primary-container); color: var(--md-sys-color-on-primary-container); }

/* ============ MATERIAL SYMBOLS ============ */
.material-symbols-outlined,
.btn .ico.mat,
.ico.mat {
  font-family: 'Material Symbols Outlined';
  font-weight: normal;
  font-style: normal;
  font-size: 18px;
  line-height: 1;
  letter-spacing: normal;
  text-transform: none;
  display: inline-block;
  white-space: nowrap;
  word-wrap: normal;
  direction: ltr;
  -webkit-font-feature-settings: 'liga';
  -webkit-font-smoothing: antialiased;
}

/* ============ MASTHEAD / TOP APP BAR ============ */
.masthead {
  text-align: center;
  padding: 8px 0 24px;
  margin-bottom: 24px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  position: relative;
}
.eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  font-family: var(--md-font-body);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 12px;
}
.eyebrow .dot {
  width: 4px;
  height: 4px;
  background: var(--md-sys-color-primary);
  border-radius: 50%;
}
.masthead h1 {
  font-family: var(--md-font-display);
  font-size: clamp(40px, 7vw, 64px);
  font-weight: 500;
  line-height: 1.05;
  letter-spacing: -0.01em;
  color: var(--md-sys-color-on-surface);
  font-variation-settings: "opsz" 100;
}
.masthead h1 em {
  font-style: italic;
  font-weight: 400;
  color: var(--md-sys-color-primary);
}
.subtitle {
  font-family: var(--md-font-display);
  font-style: italic;
  font-size: 16px;
  font-weight: 400;
  color: var(--md-sys-color-on-surface-variant);
  margin-top: 8px;
}

/* ============ USER BAR / CHIP / MENU ============ */
.user-bar {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 3;
  display: flex;
  align-items: center;
}
.user-bar.inline {
  position: static;
  margin-left: auto;
}
@media (max-width: 760px) {
  .user-bar {
    position: static;
    justify-content: flex-end;
    margin: 0 0 12px;
  }
  .user-bar .user-name { display: none; }
  .user-chip { padding: 4px; }
  .user-menu { right: 0; }
}
.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 4px 16px 4px 4px;
  background: var(--md-sys-color-surface-container);
  color: var(--md-sys-color-on-surface);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-full);
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease;
}
.user-chip:hover {
  background: var(--md-sys-color-surface-container-high);
}
.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--md-sys-color-primary-container) center/cover;
  flex-shrink: 0;
}
.user-name {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.user-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  background-color: var(--md-sys-color-surface-container-high);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-sm);
  padding: 8px 0;
  box-shadow: var(--md-elev-3);
  display: none;
  min-width: 220px;
  z-index: 50;
  isolation: isolate;
}
.user-menu.open { display: block; }
.user-menu-row {
  padding: 8px 16px;
  font-family: var(--md-font-body);
  font-size: 12px;
  color: var(--md-sys-color-on-surface-variant);
}
.user-menu-row.email {
  color: var(--md-sys-color-on-surface);
  font-weight: 500;
  font-size: 14px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  padding: 12px 16px;
  word-break: break-all;
}
.user-menu-row.divider {
  padding: 0;
  margin: 4px 0;
  height: 1px;
  background: var(--md-sys-color-outline-variant);
  border: none;
}
.user-menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 12px 16px;
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  transition: background 0.15s ease;
}
.user-menu button:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
}
.user-menu button.menu-admin {
  color: var(--md-sys-color-primary);
}

/* Login button — M3 outlined style */
.btn-login {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 24px;
  background: var(--md-sys-color-surface-container);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-full);
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  transition: all 0.15s ease;
}
.btn-login:hover {
  background: var(--md-sys-color-surface-container-high);
  box-shadow: var(--md-elev-1);
}

/* ============ SELECTOR-VIEW SPECIFIC ============ */
.selector-intro {
  text-align: center;
  margin-bottom: 24px;
  font-family: var(--md-font-display);
  font-style: italic;
  color: var(--md-sys-color-on-surface-variant);
  font-size: 18px;
}

/* ============ FILTER CHIPS (M3) ============ */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 32px;
}
.filter-chip {
  display: inline-flex;
  align-items: center;
  height: 32px;
  padding: 0 16px;
  background: transparent;
  border: 1px solid var(--md-sys-color-outline);
  border-radius: var(--md-shape-sm);
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  transition: background 0.18s cubic-bezier(0.2, 0, 0, 1),
              color      0.18s cubic-bezier(0.2, 0, 0, 1),
              border-color 0.18s cubic-bezier(0.2, 0, 0, 1),
              transform  0.15s cubic-bezier(0.2, 0, 0, 1);
}
.filter-chip::after {
  content: '';
  position: absolute;
  inset: 0;
  background: currentColor;
  opacity: 0;
  transition: opacity 0.15s cubic-bezier(0.2, 0, 0, 1);
  pointer-events: none;
}
.filter-chip:hover::after { opacity: 0.08; }
.filter-chip:active { transform: scale(0.97); }
.filter-chip.active {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
  border-color: transparent;
}
.filter-chip-count {
  margin-left: 4px;
  font-weight: 400;
  opacity: 0.65;
}

/* ============ DAILY CARD ============ */
.daily-card {
  background: var(--md-sys-color-primary-container);
  color: var(--md-sys-color-on-primary-container);
  border-radius: var(--md-shape-lg);
  padding: 20px 24px;
  margin-bottom: 24px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 16px;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.2, 0, 0, 1);
  box-shadow: var(--md-elev-1);
  position: relative;
  overflow: hidden;
}
.daily-card:hover {
  box-shadow: var(--md-elev-3);
  transform: translateY(-2px);
}
.daily-card-body { min-width: 0; }
.daily-card-eyebrow {
  font-family: var(--md-font-body);
  font-size: 11px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-primary-container);
  opacity: 0.75;
  margin-bottom: 6px;
  font-weight: 500;
}
.daily-card-title {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 24px;
  line-height: 1.15;
  margin-bottom: 4px;
  color: var(--md-sys-color-on-primary-container);
}
.daily-card-meta {
  font-family: var(--md-font-body);
  font-size: 13px;
  color: var(--md-sys-color-on-primary-container);
  opacity: 0.85;
}
.daily-card-streak {
  text-align: right;
  font-family: var(--md-font-body);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-primary-container);
  opacity: 0.85;
  white-space: nowrap;
}
.daily-card-streak-value {
  display: block;
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 40px;
  line-height: 1;
  color: var(--md-sys-color-on-primary-container);
  margin-bottom: 4px;
}
@media (max-width: 480px) {
  .daily-card { grid-template-columns: 1fr; padding: 16px; }
  .daily-card-streak { text-align: left; }
  .daily-card-streak-value { font-size: 28px; }
}

/* ============ STATS BAR (M3 elevated cards) ============ */
.stats-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 24px;
}
.stat {
  background: var(--md-sys-color-surface-container-low);
  border-radius: var(--md-shape-md);
  padding: 16px;
  text-align: center;
  border: 1px solid var(--md-sys-color-outline-variant);
}
.stat-label {
  font-family: var(--md-font-body);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 6px;
}
.stat-value {
  font-family: var(--md-font-display);
  font-size: 28px;
  font-weight: 500;
  line-height: 1;
  color: var(--md-sys-color-on-surface);
  font-variation-settings: "opsz" 24;
}
.stat-value .sub {
  font-size: 14px;
  color: var(--md-sys-color-on-surface-variant);
  font-weight: 400;
  margin-left: 2px;
}
/* Mobile: 4 cards × 28px font overflow narrow viewports (e.g. an iPhone
   SE 320 viewport, even an iPhone 12 at 390). Fold to 2×2 and shrink
   the value font; the labels are short enough to fit either way. The
   stat itself is read at a glance, so a smaller display font does not
   hurt readability. */
@media (max-width: 600px) {
  .stats-bar { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat { padding: 12px 10px; }
  .stat-value { font-size: 22px; }
  .stat-value .sub { font-size: 12px; }
  .stat-label { font-size: 10px; margin-bottom: 4px; }
}

/* ============ PROGRESS BAR ============ */
.progress-track {
  height: 6px;
  background: var(--md-sys-color-surface-container-highest);
  border-radius: var(--md-shape-full);
  overflow: hidden;
  margin-bottom: 32px;
}
.progress-fill {
  position: absolute;
  inset: 0 100% 0 0;
  background: var(--md-sys-color-primary);
  border-radius: var(--md-shape-full);
  transition: right 0.4s cubic-bezier(0.2, 0, 0, 1);
}
.progress-track { position: relative; }

/* ============ MAIN GRID LAYOUT ============ */
.main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  grid-template-areas:
    "board    clues"
    "controls clues";
  gap: 16px 24px;
  align-items: start;
}
.board-wrap { grid-area: board; }
.board-controls { grid-area: controls; }
.clues-panel { grid-area: clues; }
@media (max-width: 900px) {
  .main {
    grid-template-columns: 1fr;
    grid-template-areas:
      "board"
      "clues"
      "controls";
    gap: 12px;
  }
}

/* ============ BACK BAR ============ */
.back-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
  flex-wrap: wrap;
}
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 16px;
  background: none;
  border: none;
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-primary);
  cursor: pointer;
  border-radius: var(--md-shape-full);
  transition: background 0.15s ease;
}
.back-link:hover {
  background: color-mix(in srgb, var(--md-sys-color-primary) 8%, transparent);
}
.puzzle-meta-pills {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}
.pill {
  display: inline-flex;
  align-items: center;
  height: 26px;
  padding: 0 12px;
  background: var(--md-sys-color-surface-container);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-full);
  font-family: var(--md-font-body);
  font-size: 12px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
}

/* ============ BOARD / CROSSWORD GRID ============ */
.board-wrap {
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-lg);
  padding: 16px;
}
.board-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.board-title {
  font-family: var(--md-font-body);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
}
.board-meta {
  font-family: var(--md-font-mono);
  font-size: 12px;
  color: var(--md-sys-color-on-surface-variant);
}

.grid {
  display: grid;
  background: var(--xword-grid-line);
  gap: 1px;
  aspect-ratio: 1 / 1;
  width: 100%;
  border-radius: var(--md-shape-sm);
  overflow: hidden;
}

.cell {
  position: relative;
  background: var(--xword-cell-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.15s cubic-bezier(0.2, 0, 0, 1),
              box-shadow 0.18s cubic-bezier(0.2, 0, 0, 1);
}
/* Mouse-only subtle hover — pointer:fine excludes touch devices to avoid
   the "stuck" highlight after tapping. */
@media (hover: hover) and (pointer: fine) {
  .cell:not(.block):not(.active):not(.active-word):hover {
    background: color-mix(in srgb, var(--xword-active-bg) 6%, var(--xword-cell-bg));
  }
}
/* Active cell glow — quiet outer ring so the cursor reads even in the
   periphery of vision. No animation, just a tasteful aura. */
.cell.active {
  z-index: 2;
  box-shadow:
    0 0 0 2px var(--xword-active-bg),
    0 4px 12px -3px color-mix(in srgb, var(--xword-active-bg) 55%, transparent);
}
.cell.block {
  background: var(--xword-cell-block);
  cursor: default;
}

/* Active word — high-contrast tinted background */
.cell.active-word {
  background: var(--xword-active-word-bg);
}
/* Active cell (the cursor) — bold primary, very high contrast */
.cell.active {
  background: var(--xword-active-bg);
}
/* Correct solved word */
.cell.correct-word {
  background: var(--xword-correct-bg);
}
/* Error during live-validation */
.cell.error {
  background: var(--md-sys-color-error-container) !important;
}
.cell.hinted::after {
  content: '';
  position: absolute;
  top: 4px;
  right: 4px;
  width: 6px;
  height: 6px;
  background: var(--md-sys-color-hint);
  border-radius: 50%;
}

.cell .num {
  position: absolute;
  top: 2px;
  left: 4px;
  font-family: var(--md-font-mono);
  font-size: 9px;
  font-weight: 500;
  color: var(--xword-cell-num);
  line-height: 1;
  pointer-events: none;
}
.cell .letter {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: clamp(16px, 2.5vw, 24px);
  color: var(--xword-cell-fg);
  text-transform: uppercase;
  line-height: 1;
  font-variation-settings: "opsz" 24;
}

.cell.active-word:not(.active) .letter,
.cell.active-word:not(.active) .num { color: var(--xword-active-word-fg); }
.cell.active .letter,
.cell.active .num { color: var(--xword-active-fg); }
.cell.correct-word .letter,
.cell.correct-word .num { color: var(--xword-correct-fg); }
.cell.error .letter { color: var(--md-sys-color-on-error-container); }

.hidden-input {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  border: 0;
  outline: 0;
  background: transparent;
  caret-color: transparent;
  font-size: 16px;             /* prevents iOS auto-zoom on focus */
  -webkit-appearance: none;
  appearance: none;
}

/* ============ CURRENT CLUE BAR ============ */
.current-clue {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--md-sys-color-surface-container);
  border-radius: var(--md-shape-md);
  margin-top: 12px;
  min-height: 60px;
}
.current-clue .badge {
  font-family: var(--md-font-body);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  background: var(--md-sys-color-primary);
  color: var(--md-sys-color-on-primary);
  padding: 4px 12px;
  border-radius: var(--md-shape-full);
  white-space: nowrap;
}
.current-clue .text {
  font-family: var(--md-font-display);
  font-size: 16px;
  font-style: italic;
  color: var(--md-sys-color-on-surface);
  line-height: 1.4;
  flex: 1;
}

/* ============ CONTROLS CARD ============ */
.board-controls {
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-lg);
  padding: 16px;
}

/* ============ M3 BUTTONS ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 0 24px;
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
  border: none;
  border-radius: var(--md-shape-full);
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 0.1px;
  cursor: pointer;
  text-decoration: none;
  position: relative;
  white-space: nowrap;
  overflow: hidden;
  transition: box-shadow 0.2s cubic-bezier(0.2, 0, 0, 1),
              transform   0.15s cubic-bezier(0.2, 0, 0, 1);
}
/* M3 state layer — a tinted overlay that intensifies on interaction.
   Lives inside the button via ::after so it doesn't fight the
   background-color (which is the actual filled/tonal color). */
.btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: currentColor;
  opacity: 0;
  transition: opacity 0.15s cubic-bezier(0.2, 0, 0, 1);
  pointer-events: none;
  border-radius: inherit;
}
.btn:hover::after  { opacity: 0.08; }
.btn:focus-visible::after { opacity: 0.10; }
.btn:active::after { opacity: 0.14; }
.btn:hover { box-shadow: var(--md-elev-1); }
.btn:active { transform: scale(0.98); }
.btn.primary {
  background: var(--md-sys-color-primary);
  color: var(--md-sys-color-on-primary);
}
.btn.primary:hover { box-shadow: var(--md-elev-2); }
.btn-danger {
  background: var(--md-sys-color-error-container);
  color: var(--md-sys-color-on-error-container);
}
.btn-danger:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-error-container) 8%, var(--md-sys-color-error-container));
}
/* Stacked, full-width buttons specific to the Settings drawer. */
.settings-section > .btn {
  width: 100%;
  margin-top: 8px;
}
.settings-section > .btn:first-child { margin-top: 0; }
.btn .ico {
  font-size: 18px;
  line-height: 1;
}

.controls {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.board-controls > .controls { margin-top: 0; }

/* ============ M3 SWITCH ============ */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 12px;
  padding: 12px;
  cursor: pointer;
  border-radius: var(--md-shape-sm);
  transition: background 0.15s ease;
}
.toggle-row:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 4%, transparent);
}
.toggle-row label {
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.toggle-hint {
  font-family: var(--md-font-body);
  font-size: 12px;
  font-weight: 400;
  color: var(--md-sys-color-on-surface-variant);
  text-transform: none;
  letter-spacing: normal;
}

.switch {
  position: relative;
  width: 52px;
  height: 32px;
  background: var(--md-sys-color-surface-container-highest);
  border: 2px solid var(--md-sys-color-outline);
  border-radius: var(--md-shape-full);
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease;
  flex-shrink: 0;
}
.switch::after {
  content: '';
  position: absolute;
  top: 5px;
  left: 5px;
  width: 14px;
  height: 14px;
  background: var(--md-sys-color-outline);
  border-radius: 50%;
  transition: all 0.25s cubic-bezier(0.2, 0, 0, 1);
}
.switch.on {
  background: var(--md-sys-color-primary);
  border-color: var(--md-sys-color-primary);
}
.switch.on::after {
  background: var(--md-sys-color-on-primary);
  width: 22px;
  height: 22px;
  top: 1px;
  left: 25px;
}

/* ============ CLUES PANEL ============ */
.clues-panel {
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-lg);
  max-height: calc(100vh - 60px);
  position: sticky;
  top: 24px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
@media (max-width: 900px) {
  .clues-panel { position: static; max-height: none; }
}
.clues-tabs {
  display: flex;
  flex-shrink: 0;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.clues-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 14px 16px;
  font-family: var(--md-font-body);
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  position: relative;
  transition: color 0.15s ease, background 0.15s ease;
}
.clues-tab:hover {
  color: var(--md-sys-color-on-surface);
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 4%, transparent);
}
.clues-tab.active {
  color: var(--md-sys-color-primary);
}
.clues-tab::after {
  content: '';
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 0;
  height: 3px;
  background: var(--md-sys-color-primary);
  border-radius: 3px 3px 0 0;
  transform: scaleX(0);
  transform-origin: center;
  transition: transform 0.3s cubic-bezier(0.2, 0, 0, 1);
}
.clues-tab.active::after { transform: scaleX(1); }
.clues-tab .count {
  margin-left: 6px;
  font-weight: 400;
  color: var(--md-sys-color-on-surface-variant);
}
.clues-legend {
  padding: 10px 16px;
  font-family: var(--md-font-body);
  font-size: 12px;
  font-style: italic;
  line-height: 1.4;
  color: var(--md-sys-color-on-surface-variant);
  background: color-mix(in srgb, var(--md-sys-color-primary) 4%, transparent);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.clues-legend em {
  font-style: italic;
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
}

.clues-lists {
  overflow-y: auto;
  flex: 1;
  scrollbar-width: thin;
  scrollbar-color: var(--md-sys-color-outline-variant) transparent;
}
.clues-lists::-webkit-scrollbar { width: 6px; }
.clues-lists::-webkit-scrollbar-thumb {
  background: var(--md-sys-color-outline-variant);
  border-radius: 3px;
}

.clues-section { display: none; padding: 8px 0; }
.clues-section.visible { display: block; }
@media (max-width: 900px) {
  .clues-section.visible-mobile { display: block; }
}
.clue-item {
  display: grid;
  grid-template-columns: 32px 1fr;
  gap: 8px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.12s;
  border-left: 3px solid transparent;
}
.clue-item:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 6%, transparent);
}
.clue-item.active {
  background: var(--md-sys-color-primary-container);
  border-left-color: var(--md-sys-color-primary);
}
.clue-item.active .clue-text,
.clue-item.active .clue-num,
.clue-item.active .len { color: var(--md-sys-color-on-primary-container); }
.clue-item.solved {
  color: var(--md-sys-color-on-surface-variant);
  text-decoration: line-through;
  text-decoration-color: var(--md-sys-color-success);
  text-decoration-thickness: 1px;
}
.clue-item.solved .clue-num { color: var(--md-sys-color-success); }
.clue-num {
  font-family: var(--md-font-mono);
  font-size: 12px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  text-align: right;
  padding-top: 2px;
}
.clue-text {
  font-family: var(--md-font-body);
  font-size: 14px;
  line-height: 1.4;
  color: var(--md-sys-color-on-surface);
}
.clue-text .len {
  margin-left: 6px;
  font-family: var(--md-font-mono);
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
}

/* ============ PUZZLE CARDS (selector) ============ */
.section-heading {
  font-family: var(--md-font-body);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin: 24px 0 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.section-heading .count {
  font-family: var(--md-font-mono);
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
}

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
  margin-bottom: 32px;
}
.puzzle-card {
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-md);
  padding: 16px 20px;
  cursor: pointer;
  transition: background var(--xd-dur-base) var(--ease-flat),
              box-shadow var(--xd-dur-base) var(--ease-flat),
              transform var(--xd-dur-base) var(--ease-spring);
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 180px;
  /* The damped cursor tilt (set by app.js on pointer move) writes into
     these vars; the transform composes them with the lift. */
  --tilt-x: 0deg;
  --tilt-y: 0deg;
  --tilt-lift: 0px;
  transform: perspective(900px) rotateX(var(--tilt-x)) rotateY(var(--tilt-y)) translateY(var(--tilt-lift));
  transform-style: preserve-3d;
  will-change: transform;
}
@media (hover: hover) and (pointer: fine) {
  .puzzle-card:hover {
    background: var(--md-sys-color-surface-container);
    box-shadow: var(--md-elev-3);
    --tilt-lift: -3px;
  }
}
.puzzle-card:active { --tilt-lift: -1px; }
.puzzle-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}
.puzzle-card .difficulty {
  font-family: var(--md-font-body);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 4px 10px;
  border-radius: var(--md-shape-full);
  white-space: nowrap;
}
.puzzle-card .difficulty.easy {
  background: var(--md-sys-color-success-container);
  color: var(--md-sys-color-on-success-container);
}
.puzzle-card .difficulty.medium {
  background: var(--md-sys-color-tertiary-container);
  color: var(--md-sys-color-on-tertiary-container);
}
.puzzle-card .difficulty.hard {
  background: var(--md-sys-color-error-container);
  color: var(--md-sys-color-on-error-container);
}
.puzzle-card .theme-tag {
  font-family: var(--md-font-body);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
}
.puzzle-card h3 {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 20px;
  line-height: 1.2;
  color: var(--md-sys-color-on-surface);
}
.puzzle-card p {
  font-family: var(--md-font-body);
  font-size: 13px;
  color: var(--md-sys-color-on-surface-variant);
  line-height: 1.4;
  flex: 1;
}
.puzzle-card-foot {
  display: flex;
  justify-content: space-between;
  font-family: var(--md-font-mono);
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
  border-top: 1px solid var(--md-sys-color-outline-variant);
  padding-top: 10px;
}

.puzzle-card-progress {
  margin-top: -4px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.puzzle-card-progress-bar {
  height: 4px;
  background: var(--md-sys-color-surface-container-highest);
  position: relative;
  overflow: hidden;
  border-radius: var(--md-shape-full);
}
.puzzle-card-progress-bar > span {
  position: absolute;
  inset: 0 100% 0 0;
  background: var(--md-sys-color-primary);
  transition: right 0.4s cubic-bezier(0.2, 0, 0, 1);
  border-radius: var(--md-shape-full);
}
.puzzle-card-progress-bar.solved > span {
  background: var(--md-sys-color-success);
  right: 0 !important;
}
.puzzle-card-progress-label {
  display: flex;
  justify-content: space-between;
  font-family: var(--md-font-mono);
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
}
.puzzle-card-progress-label.solved {
  color: var(--md-sys-color-success);
  font-weight: 500;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  font-family: var(--md-font-display);
  font-style: italic;
  color: var(--md-sys-color-on-surface-variant);
}

@media (max-width: 480px) {
  .puzzle-grid { grid-template-columns: 1fr; gap: 12px; }
  body {
    /* Honour iPhone notch + home-indicator. When installed as a PWA
       the chrome is gone and content has to respect these insets, or
       the user-bar gets covered by the camera island and the footer
       sits under the swipe gesture area. */
    padding: max(16px, env(safe-area-inset-top))
             max(12px, env(safe-area-inset-right))
             max(60px, calc(env(safe-area-inset-bottom) + 24px))
             max(12px, env(safe-area-inset-left));
  }
}

/* ============ MOBILE TOUCH ============ */
@media (max-width: 700px) {
  .board-wrap { padding: 12px; }
  .cell .letter {
    font-size: clamp(14px, 4.2vw, 22px);
  }
  .controls .btn {
    padding: 0 14px;
    font-size: 12px;
    min-height: 44px;
  }
  .clue-item { padding: 14px 16px; }
  /* Filter chips: M3 baseline (32 px) sits below the comfortable thumb
     target. Touch devices get 40 px + slightly bigger font so they
     don't read as "tap precisely" affordances. */
  .filter-chip { height: 40px; padding: 0 14px; font-size: 13px; }
  .back-link, .btn { min-height: 40px; }
  /* The visible switch is 32 px and that is intentional (M3 Switch
     spec). What matters is the TAP area — the entire `.toggle-row` is
     clickable via label delegation, but mobile users tap the switch
     itself. Inflate the hit area via an invisible ::before overlay
     (::after is already the knob). */
  .switch::before {
    content: '';
    position: absolute;
    inset: -8px -10px;
  }
  /* Toggle rows: comfortable vertical padding for thumb scrubbing. */
  .toggle-row { min-height: 48px; }
}

/* ============ WIN OVERLAY ============ */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--md-sys-color-scrim);
  backdrop-filter: blur(8px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  overflow-y: auto; /* a card taller than the screen scrolls instead of being cut off */
  opacity: 0;
  transition: opacity 0.4s;
}
.overlay.show { display: flex; opacity: 1; }
.win-card {
  background: var(--md-sys-color-surface-container-high);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-xl);
  padding: 32px;
  max-width: 460px;
  width: 100%;
  text-align: center;
  box-shadow: var(--md-elev-5);
  transform: scale(0.92);
  transition: transform 0.35s cubic-bezier(0.2, 0, 0, 1);
}
.overlay.show .win-card { transform: scale(1); }
.win-card .eyebrow {
  justify-content: center;
  margin-bottom: 12px;
}
.win-card h2 {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 44px;
  line-height: 1;
  letter-spacing: -0.02em;
  margin-bottom: 8px;
  color: var(--md-sys-color-on-surface);
  font-variation-settings: "opsz" 96;
}
.win-card h2 em {
  font-style: italic;
  font-weight: 400;
  color: var(--md-sys-color-primary);
}
.win-card p {
  font-family: var(--md-font-display);
  font-style: italic;
  font-size: 16px;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 24px;
}
.win-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}
.win-stat {
  background: var(--md-sys-color-surface-container-highest);
  border-radius: var(--md-shape-md);
  padding: 14px 8px;
}
.win-stat-val {
  font-family: var(--md-font-display);
  font-size: 22px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
}
.win-stat-lbl {
  font-family: var(--md-font-body);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-top: 4px;
  font-weight: 500;
}

.win-card .btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}
@media (max-width: 460px) {
  .win-card .btn-row { grid-template-columns: 1fr; }
}

.win-leaderboard {
  text-align: left;
  margin-bottom: 18px;
  font-family: var(--md-font-body);
  font-size: 12px;
}
.win-leaderboard:empty { display: none; }
.win-leaderboard-title {
  font-family: var(--md-font-body);
  font-weight: 500;
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.win-leaderboard-row {
  display: grid;
  grid-template-columns: 24px 24px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px dashed var(--md-sys-color-outline-variant);
}
.win-leaderboard-row:last-child { border-bottom: none; }
.win-leaderboard-row.you {
  background: var(--md-sys-color-primary-container);
  color: var(--md-sys-color-on-primary-container);
  margin: 2px -8px;
  padding: 6px 8px;
  border-radius: var(--md-shape-sm);
}
.win-leaderboard-rank {
  font-family: var(--md-font-mono);
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  text-align: right;
}
.win-leaderboard-rank.podium-1 { color: #FFA000; }
.win-leaderboard-rank.podium-2 { color: #9E9E9E; }
.win-leaderboard-rank.podium-3 { color: #CD7F32; }
.win-leaderboard-avatar {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--md-sys-color-primary-container) center/cover;
  border: 1px solid var(--md-sys-color-outline-variant);
}
.win-leaderboard-name {
  text-align: left;
  font-family: var(--md-font-display);
  font-style: italic;
  font-size: 14px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.win-leaderboard-row.you .win-leaderboard-name { color: var(--md-sys-color-on-primary-container); }
.win-leaderboard-time {
  font-family: var(--md-font-mono);
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
}
.win-leaderboard-row.you .win-leaderboard-time { color: var(--md-sys-color-on-primary-container); }
.win-leaderboard-empty {
  font-family: var(--md-font-display);
  font-style: italic;
  font-size: 13px;
  color: var(--md-sys-color-on-surface-variant);
  text-align: center;
  padding: 8px 0;
}

/* ============ SETTINGS MODAL ============ */
.settings-card {
  background: var(--md-sys-color-surface-container-high);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-xl);
  padding: 28px;
  max-width: 480px;
  width: 100%;
  box-shadow: var(--md-elev-5);
  transform: scale(0.92);
  transition: transform 0.3s cubic-bezier(0.2, 0, 0, 1);
}
.overlay.show .settings-card { transform: scale(1); }
.settings-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.settings-head h2 {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 28px;
  line-height: 1;
  color: var(--md-sys-color-on-surface);
}
.settings-close {
  background: none;
  border: none;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  color: var(--md-sys-color-on-surface-variant);
  padding: 0 8px;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}
.settings-close:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
}
.settings-section { margin-bottom: 20px; }
.settings-section:last-child { margin-bottom: 0; }
.settings-label {
  font-family: var(--md-font-body);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 10px;
  font-weight: 500;
}
.settings-account {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--md-sys-color-surface-container);
  border-radius: var(--md-shape-md);
  margin-bottom: 12px;
}
.settings-account-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--md-sys-color-primary-container) center/cover;
  flex-shrink: 0;
}
.settings-account-text {
  font-family: var(--md-font-body);
  font-size: 13px;
  color: var(--md-sys-color-on-surface-variant);
}
.settings-account-text .name {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 16px;
  color: var(--md-sys-color-on-surface);
  display: block;
  margin-bottom: 2px;
}

.theme-switcher {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  background: var(--md-sys-color-surface-container);
  border-radius: var(--md-shape-full);
}
.theme-option {
  background: none;
  border: none;
  padding: 8px 12px;
  font-family: var(--md-font-body);
  font-size: 13px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  border-radius: var(--md-shape-full);
  transition: background 0.15s ease, color 0.15s ease;
}
.theme-option:hover:not(.active) {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
}
.theme-option.active {
  background: var(--md-sys-color-primary);
  color: var(--md-sys-color-on-primary);
}

/* ============ ADMIN VIEW ============ */
.admin-tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--md-sys-color-surface-container);
  border-radius: var(--md-shape-full);
  margin-bottom: 20px;
  overflow-x: auto;
}
.admin-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 8px 16px;
  font-family: var(--md-font-body);
  font-size: 13px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
  border-radius: var(--md-shape-full);
  white-space: nowrap;
  transition: all 0.15s ease;
}
.admin-tab:hover:not(.active) {
  background: color-mix(in srgb, var(--md-sys-color-on-surface) 8%, transparent);
  color: var(--md-sys-color-on-surface);
}
.admin-tab.active {
  background: var(--md-sys-color-primary);
  color: var(--md-sys-color-on-primary);
}

.admin-content {
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-lg);
  padding: 20px;
  min-height: 200px;
}
.admin-loading {
  font-family: var(--md-font-display);
  font-style: italic;
  color: var(--md-sys-color-on-surface-variant);
  text-align: center;
  padding: 40px;
}
.admin-error {
  background: var(--md-sys-color-error-container);
  color: var(--md-sys-color-on-error-container);
  padding: 14px;
  border-radius: var(--md-shape-md);
  font-family: var(--md-font-body);
  font-size: 14px;
}

.admin-stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.admin-stat-cell {
  background: var(--md-sys-color-surface-container);
  padding: 14px 16px;
  border-radius: var(--md-shape-md);
  border: 1px solid var(--md-sys-color-outline-variant);
}
.admin-stat-label {
  font-family: var(--md-font-body);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 6px;
  font-weight: 500;
}
.admin-stat-value {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 28px;
  line-height: 1;
  color: var(--md-sys-color-on-surface);
}
.admin-stat-value .sub {
  font-size: 13px;
  color: var(--md-sys-color-on-surface-variant);
  font-weight: 400;
  margin-left: 4px;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--md-font-body);
  font-size: 13px;
}
.admin-table th, .admin-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.admin-table th {
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
  background: var(--md-sys-color-surface-container);
}
.admin-table tr:last-child td { border-bottom: none; }
.admin-table tr:hover td { background: color-mix(in srgb, var(--md-sys-color-on-surface) 4%, transparent); }
.admin-table .user-cell {
  display: flex;
  align-items: center;
  gap: 8px;
}
.admin-table .user-cell-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--md-sys-color-primary-container) center/cover;
  flex-shrink: 0;
}
.admin-table .name {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 14px;
  color: var(--md-sys-color-on-surface);
}
.admin-table .small {
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
  display: block;
  margin-top: 2px;
}
.admin-table-wrap { overflow-x: auto; }
.admin-section { margin-bottom: 24px; }
.admin-section h3 {
  font-family: var(--md-font-body);
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.solved-badge {
  display: inline-block;
  padding: 3px 10px;
  background: var(--md-sys-color-success-container);
  color: var(--md-sys-color-on-success-container);
  border-radius: var(--md-shape-full);
  font-weight: 500;
  font-size: 11px;
}
.admin-key-value {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.admin-key-value-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  font-family: var(--md-font-mono);
  font-size: 12px;
}
.admin-key-value-row .k {
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  font-size: 11px;
}
.admin-key-value-row .v {
  color: var(--md-sys-color-on-surface);
}

/* ============ PROFILE VIEW ============ */
.rank-banner {
  background: var(--md-sys-color-primary-container);
  color: var(--md-sys-color-on-primary-container);
  border-radius: var(--md-shape-lg);
  padding: 28px 32px;
  margin-bottom: 24px;
  position: relative;
  box-shadow: var(--md-elev-1);
}
.rank-banner-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 12px;
}
.rank-banner-label {
  font-family: var(--md-font-body);
  font-size: 11px;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-primary-container);
  opacity: 0.75;
  margin-bottom: 4px;
  font-weight: 500;
}
.rank-banner-name {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: clamp(32px, 5vw, 52px);
  line-height: 1;
  color: var(--md-sys-color-on-primary-container);
  font-variation-settings: "opsz" 96;
}
.rank-banner-xp {
  font-family: var(--md-font-mono);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-primary-container);
  text-align: right;
}
.rank-banner-xp .sub {
  display: block;
  font-weight: 400;
  font-size: 10px;
  opacity: 0.75;
  letter-spacing: 0.1em;
  margin-top: 4px;
  text-transform: none;
}
.rank-bar {
  height: 8px;
  background: color-mix(in srgb, var(--md-sys-color-on-primary-container) 20%, transparent);
  border-radius: var(--md-shape-full);
  position: relative;
  overflow: hidden;
}
.rank-bar > span {
  position: absolute;
  inset: 0 100% 0 0;
  background: var(--md-sys-color-on-primary-container);
  border-radius: var(--md-shape-full);
  transition: right 0.6s cubic-bezier(0.2, 0, 0, 1);
}
.rank-bar-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-family: var(--md-font-mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-primary-container);
  opacity: 0.85;
}

.profile-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 32px;
}
.profile-section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
}
.profile-section-head h2 {
  font-family: var(--md-font-body);
  font-weight: 500;
  font-size: 14px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface);
}
.profile-section-head .count {
  font-family: var(--md-font-mono);
  font-size: 11px;
  color: var(--md-sys-color-on-surface-variant);
  letter-spacing: 0.05em;
}

.achievement-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  margin-bottom: 32px;
}
.achievement-card {
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-md);
  padding: 16px;
  display: flex;
  gap: 14px;
  align-items: flex-start;
  transition: all 0.2s cubic-bezier(0.2, 0, 0, 1);
  position: relative;
}
.achievement-card.unlocked {
  background: var(--md-sys-color-surface-container);
  box-shadow: var(--md-elev-1);
}
.achievement-card.locked {
  opacity: 0.5;
}
.achievement-icon {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-family: var(--md-font-display);
  border-radius: 50%;
  background: var(--md-sys-color-surface-container-highest);
  color: var(--md-sys-color-on-surface);
}
.achievement-card.unlocked.tier-bronze .achievement-icon { background: #CD7F32; color: #FFFFFF; }
.achievement-card.unlocked.tier-silver .achievement-icon { background: #C0C0C0; color: #1A1B21; }
.achievement-card.unlocked.tier-gold .achievement-icon { background: #FFA000; color: #1A1B21; }
.achievement-body { flex: 1; min-width: 0; }
.achievement-name {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 15px;
  line-height: 1.2;
  color: var(--md-sys-color-on-surface);
  margin-bottom: 4px;
}
.achievement-desc {
  font-family: var(--md-font-body);
  font-size: 12px;
  color: var(--md-sys-color-on-surface-variant);
  line-height: 1.4;
}
.achievement-tier {
  position: absolute;
  top: 12px;
  right: 12px;
  font-family: var(--md-font-mono);
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--md-sys-color-on-surface-variant);
}
.achievement-card.unlocked.tier-bronze .achievement-tier { color: #8A5520; }
.achievement-card.unlocked.tier-silver .achievement-tier { color: #6A6A6A; }
.achievement-card.unlocked.tier-gold   .achievement-tier { color: #B07000; }
.achievement-xp {
  font-family: var(--md-font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant);
  margin-top: 6px;
}

/* ============ CUSTOM DIALOG (replaces native alert/confirm) ============
   Uses the existing .overlay backdrop. The card itself styles via M3
   tokens so light + dark themes both work without any extra rules.   */
.xdialog-overlay {
  z-index: 200; /* above .settings-card overlay if both ever open */
}
@media (max-width: 600px) {
  /* Forms start near the top, so the on-screen keyboard does not cover the message and button. */
  .xdialog-overlay:has(.auth-form) { align-items: flex-start; padding-top: 16px; }
  .xdialog-overlay:has(.auth-form) .xdialog-card { margin-top: 0; }
}
.xdialog-card {
  background: var(--md-sys-color-surface-container-high);
  color: var(--md-sys-color-on-surface);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-lg);
  padding: 24px;
  width: 100%;
  max-width: 420px;
  margin: auto; /* stays centred, and scrollable when taller than the screen */
  box-shadow: var(--md-elev-5);
  transform: scale(0.92);
  opacity: 0;
  transition: transform 0.22s cubic-bezier(0.2, 0, 0, 1),
              opacity   0.22s ease-out;
  font-family: var(--md-font-body);
}
.overlay.show .xdialog-card { transform: scale(1); opacity: 1; }
.xdialog-title {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 22px;
  line-height: 1.2;
  letter-spacing: -0.01em;
  color: var(--md-sys-color-on-surface);
  margin-bottom: 10px;
}
.xdialog-message {
  font-family: var(--md-font-body);
  font-size: 14px;
  line-height: 1.5;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 22px;
}
.xdialog-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
}
.xdialog-actions .btn {
  flex: 0 0 auto;
  min-width: 96px;
  height: 40px;
  padding: 0 22px;
  border-radius: 999px; /* M3 full-pill action button */
}
/* Inside the dialog we want the louder error tone, not the soft
   error-container the Settings drawer uses. */
.xdialog-actions .btn-danger {
  background: var(--md-sys-color-error);
  color: var(--md-sys-color-on-error);
}
.xdialog-actions .btn-danger:hover {
  background: color-mix(in srgb, var(--md-sys-color-on-error) 8%, var(--md-sys-color-error));
}
.xdialog-actions .xdialog-btn-cancel {
  background: transparent;
  color: var(--md-sys-color-primary);
  border: 1px solid var(--md-sys-color-outline);
}
.xdialog-actions .xdialog-btn-cancel:hover {
  background: color-mix(in srgb, var(--md-sys-color-primary) 8%, transparent);
}
@media (max-width: 380px) {
  .xdialog-actions .btn { min-width: 0; flex: 1 1 0; }
}

/* ---- Share dialog content ---- */
.qr-share {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  margin: 4px 0 22px;
}
.qr-share-canvas {
  background: #FFFFFF;            /* QR must always be light for scanners */
  padding: 12px;
  border-radius: var(--md-shape-md);
  border: 1px solid var(--md-sys-color-outline-variant);
  display: flex;                  /* host the SVG cleanly */
  align-items: center;
  justify-content: center;
}
.qr-share-canvas svg {
  display: block;
  width: 220px;
  height: 220px;
}
.qr-share-url {
  font-family: var(--md-font-mono);
  font-size: 13px;
  color: var(--md-sys-color-on-surface);
  word-break: break-all;
  text-align: center;
  padding: 8px 12px;
  background: var(--md-sys-color-surface-container);
  border-radius: var(--md-shape-sm);
  width: 100%;
}
.qr-share-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
}
.qr-share-buttons .btn { flex: 1 1 auto; min-width: 140px; }
.qr-share-toast {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  background: var(--md-sys-color-inverse-surface, var(--md-sys-color-on-surface));
  color: var(--md-sys-color-inverse-on-surface, var(--md-sys-color-surface));
  font-family: var(--md-font-body);
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s, transform 0.22s;
}
.qr-share-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ============ TOAST ============ */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--md-sys-color-inverse-surface);
  color: var(--md-sys-color-inverse-on-surface);
  padding: 14px 18px;
  border-radius: var(--md-shape-md);
  box-shadow: var(--md-elev-4);
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 280px;
  max-width: 360px;
  pointer-events: auto;
  transform: translateX(120%);
  transition: transform 0.45s cubic-bezier(0.2, 0, 0, 1);
}
.toast.visible { transform: translateX(0); }
.toast-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-family: var(--md-font-display);
  background: var(--md-sys-color-inverse-primary);
  color: var(--md-sys-color-on-primary);
  flex-shrink: 0;
}
.toast.tier-bronze .toast-icon { background: #CD7F32; color: #FFFFFF; }
.toast.tier-silver .toast-icon { background: #C0C0C0; color: #1A1B21; }
.toast.tier-gold .toast-icon { background: #FFA000; color: #1A1B21; }
.toast-body { flex: 1; min-width: 0; }
.toast-eyebrow {
  font-family: var(--md-font-body);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.7;
  margin-bottom: 2px;
}
.toast-name {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 15px;
  line-height: 1.2;
}

/* ============ CONFETTI ============ */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 99;
  overflow: hidden;
}
.confetti span {
  position: absolute;
  width: 8px;
  height: 14px;
  top: -20px;
  border-radius: 1px;
  animation: fall 3.5s linear forwards;
}
@keyframes fall {
  to {
    transform: translateY(110vh) translateX(var(--drift, 0)) rotate(720deg);
    opacity: 0;
  }
}

/* ============ FOOTER ============ */
.footer {
  text-align: center;
  margin-top: 32px;
  padding: 20px 0 8px;
  border-top: 1px solid var(--md-sys-color-outline-variant);
  font-family: var(--md-font-body);
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--md-sys-color-on-surface-variant);
}
.footer a {
  color: var(--md-sys-color-on-surface-variant);
  text-decoration: none;
  transition: color 0.15s ease;
}
.footer a:hover { color: var(--md-sys-color-primary); }

/* ============ LEGAL PAGES ============ */
.legal {
  max-width: 760px;
  margin: 0 auto;
  background: var(--md-sys-color-surface-container-low);
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-shape-lg);
  padding: 40px 48px;
  box-shadow: var(--md-elev-1);
}
.legal section { margin-bottom: 32px; }
.legal section:last-child { margin-bottom: 0; }
.legal h2 {
  font-family: var(--md-font-display);
  font-weight: 500;
  font-size: 20px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  color: var(--md-sys-color-on-surface);
}
.legal p {
  font-family: var(--md-font-body);
  font-size: 15px;
  line-height: 1.65;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 12px;
}
.legal p:last-child { margin-bottom: 0; }
.legal ul {
  margin: 8px 0 12px 24px;
  padding: 0;
}
.legal li {
  font-family: var(--md-font-body);
  font-size: 14px;
  line-height: 1.6;
  color: var(--md-sys-color-on-surface-variant);
  margin-bottom: 4px;
}
.legal a {
  color: var(--md-sys-color-primary);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.legal a:hover { color: color-mix(in srgb, var(--md-sys-color-primary) 80%, var(--md-sys-color-on-surface)); }
.legal code {
  background: var(--md-sys-color-surface-container);
  padding: 2px 6px;
  border-radius: var(--md-shape-xs);
  font-family: var(--md-font-mono);
  font-size: 13px;
  color: var(--md-sys-color-on-surface);
}
.legal strong { color: var(--md-sys-color-on-surface); }
@media (max-width: 700px) { .legal { padding: 28px 22px; } }

/* ============ SYNC INDICATOR ============ */
.sync-indicator {
  font-family: var(--md-font-mono);
  font-size: 11px;
  letter-spacing: 0.05em;
  color: var(--md-sys-color-on-surface-variant);
  padding: 4px 10px;
  border-radius: var(--md-shape-full);
  background: var(--md-sys-color-surface-container);
  transition: opacity 0.3s;
  opacity: 0;
}
.sync-indicator.visible { opacity: 1; }
.sync-indicator.saving {
  color: var(--md-sys-color-primary);
  animation: syncPulse 1.2s ease-in-out infinite;
}
.sync-indicator.saved  { color: var(--md-sys-color-success); animation: none; }
@keyframes syncPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

/* ============ ANIMATIONS ============ */
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}
.cell.correct-flash { animation: pulse 0.4s ease; }

/* Letter type-in — gentle scale-bounce when a letter lands in a cell.
   Triggered by engine.js adding the .type-in class to .letter for ~200ms.
   No opacity step: backwards-fill from opacity:0 caused a one-frame invisibility
   gap between paint() (textContent set) and the rAF that applies the class,
   which on mid-tier mobile reads as "letter disappears and reappears". */
@keyframes typeIn {
  0% { transform: scale(0.55); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}
.cell .letter.type-in {
  animation: typeIn 0.18s cubic-bezier(0.2, 1.2, 0.4, 1) both;
}

/* Hint drop-in — the placed letter falls into the cell with a small bounce. */
@keyframes hintDrop {
  0% { transform: translateY(-12px) scale(0.6); opacity: 0; }
  70% { transform: translateY(2px) scale(1.05); opacity: 1; }
  100% { transform: translateY(0) scale(1); }
}
.cell .letter.hint-drop {
  animation: hintDrop 0.4s cubic-bezier(0.2, 1, 0.3, 1) both;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
}
.cell.error-flash { animation: shake 0.3s ease; }

@keyframes solveWave {
  0%   { transform: scale(1);    background: var(--md-sys-color-surface-container-lowest); }
  40%  { transform: scale(1.18); background: var(--md-sys-color-primary); }
  100% { transform: scale(1);    background: var(--md-sys-color-success-container); }
}
.cell.solve-wave {
  animation: solveWave 0.7s cubic-bezier(0.2, 0, 0, 1) both;
}

/* Word-solve flash — subtle pulse + glow that ripples through the letters
   of a newly completed word. Staggered per cell from engine.js. */
@keyframes wordSolvePulse {
  0%   { transform: scale(1);    box-shadow: 0 0 0 0 rgba(11, 87, 208, 0); }
  40%  { transform: scale(1.07); box-shadow: 0 0 16px 2px rgba(11, 87, 208, 0.55); }
  100% { transform: scale(1);    box-shadow: 0 0 0 0 rgba(11, 87, 208, 0); }
}
.cell.word-solve-flash {
  animation: wordSolvePulse 0.55s cubic-bezier(0.2, 0, 0, 1) both;
  position: relative;
  z-index: 2;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
.masthead, .stats-bar, .progress-track, .board-wrap, .clues-panel,
.board-controls, .puzzle-grid, .filter-bar, .daily-card,
.rank-banner, .admin-content {
  animation: rise 0.5s cubic-bezier(0.2, 0, 0, 1) backwards;
}
/* On back-navigation, suppress the entrance animations — the visitor is
   returning, not arriving. The view-transition slide handles the motion. */
html[data-nav="back"] .masthead,
html[data-nav="back"] .stats-bar,
html[data-nav="back"] .progress-track,
html[data-nav="back"] .board-wrap,
html[data-nav="back"] .clues-panel,
html[data-nav="back"] .board-controls,
html[data-nav="back"] .puzzle-grid,
html[data-nav="back"] .filter-bar,
html[data-nav="back"] .daily-card,
html[data-nav="back"] .rank-banner,
html[data-nav="back"] .admin-content {
  animation: none;
}
.stats-bar { animation-delay: 0.05s; }
.progress-track { animation-delay: 0.1s; }
.daily-card { animation-delay: 0.05s; }
.filter-bar { animation-delay: 0.1s; }
.puzzle-grid { animation-delay: 0.15s; }
.board-wrap { animation-delay: 0.05s; }
.clues-panel { animation-delay: 0.15s; }
.board-controls { animation-delay: 0.1s; }

/* ============ FOCUS RING ============
   One consistent focus ring across every interactive surface. Uses the
   primary token so it tracks the brand and inverts cleanly in dark mode.
   Two-layer outline survives over arbitrary backgrounds (card, chip, cell,
   inverse-surface toast). Hidden when the keyboard isn't the input source. */
:focus { outline: none; }
*:focus-visible {
  outline: 2px solid var(--md-sys-color-primary);
  outline-offset: 2px;
  border-radius: var(--md-shape-xs);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--md-sys-color-primary) 22%, transparent);
}
/* Pill-shaped controls keep their full radius around the ring. */
.btn:focus-visible,
.filter-chip:focus-visible,
.user-chip:focus-visible,
.theme-option:focus-visible,
.back-link:focus-visible,
.admin-tab:focus-visible,
.clues-tab:focus-visible {
  border-radius: var(--md-shape-full);
}
/* The grid uses its own focus model (active cell ring); kill the global. */
.cell:focus-visible,
.hidden-input:focus-visible { outline: none; box-shadow: none; }

/* ============ TYPOGRAPHIC POLISH ============
   text-wrap: balance prevents ragged title widows on display headings;
   text-wrap: pretty handles body copy where balance would slow long blocks. */
.masthead h1,
.win-card h2,
.settings-head h2,
.xdialog-title,
.profile-section-head h2,
.puzzle-card h3,
.daily-card h2,
.rank-banner-name {
  text-wrap: balance;
}
.masthead .subtitle,
.puzzle-card p,
.daily-card .meta,
.empty-state {
  text-wrap: pretty;
}

/* ============ REDUCED MOTION ============
   Strict guard: anything physical (transform, parallax, big reveals)
   gets stripped to a near-instant opacity swap. Color/state-layer
   feedback survives — it's not motion, it's affordance. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  /* Cancel transform-based reveals/scales/tilts so nothing is stuck mid-move. */
  .puzzle-card,
  .puzzle-card:hover,
  .puzzle-card[data-tilt] {
    transform: none !important;
  }
  .overlay .win-card,
  .overlay .settings-card,
  .overlay .xdialog-card {
    transform: scale(1) !important;
  }
  .confetti { display: none !important; }
}

/* ============ STABLE SCROLLBAR GUTTER ============
   Reserve gutter on the SCROLLING ROOT only on devices that actually have
   classic gutter-stealing scrollbars (desktop mice). Touch devices use
   overlay scrollbars, where reserving a gutter just wastes 15 px of
   horizontal space we badly need on a 320 px viewport. The `(pointer:
   coarse)` query is the closest signal for "touch device", and `(hover:
   none)` doubles the gate so a non-touch laptop with a stylus doesn't
   also lose the gutter. */
@media (hover: hover) and (pointer: fine) {
  html { scrollbar-gutter: stable; }
}

/* ============ PROGRESSIVE ENHANCEMENT ============
   Set by the inline boot script. Behavior gated on .js never strands a
   no-JS visitor with a hidden element (we only re-enable JS-only states
   when JS has actually booted). */
html.js .no-js-only { display: none !important; }
html:not(.js) .js-only { display: none !important; }

/* ============ SCROLL LOCK ============
   Applied to <body> while any modal is open. Keeps the scrollbar
   reserved (via scrollbar-gutter above) so there's no layout shift. */
body.scroll-locked {
  overflow: hidden !important;
  touch-action: none;
}

/* ============ VIEW TRANSITIONS ============
   Print-edition page-stack metaphor. Forward (going deeper into a puzzle):
   the old page recedes — it scales down a touch and fades, as if pushed
   slightly away from the camera. The new page rises from below with a
   gentle spring overshoot, like the next sheet being lifted onto the
   stack. Back is the mirror — the page we were on falls down + fades,
   the previous one comes back into focus from a slight push-back.

   No horizontal motion: it kept reading as "window switch", not
   "turning the page". Vertical motion + scale is the editorial-stack
   feel. ::view-transition-group(root) is set to a fixed transform-origin
   so both pseudos pivot around the same anchor — otherwise the
   defaults make the "out" pseudo grow during scale-out.

   The old pseudo lives on top during forward (z-index trick) so the new
   page appears to RISE BEHIND the old one as it lifts off — exactly the
   print-stack physical metaphor. */
@media not (prefers-reduced-motion: reduce) {
  ::view-transition-old(root),
  ::view-transition-new(root) {
    animation-duration: var(--xd-dur-page);
    animation-fill-mode: both;
    transform-origin: 50% 30%;
  }
  ::view-transition-old(root) {
    animation-name: xd-page-recede;
    animation-timing-function: var(--ease-flat);
    z-index: 1;
  }
  ::view-transition-new(root) {
    animation-name: xd-page-rise;
    animation-timing-function: var(--ease-spring);
    z-index: 0;
  }
  html[data-nav="back"]::view-transition-old(root) {
    animation-name: xd-page-fall;
    z-index: 2; /* old page falls IN FRONT on the way back */
  }
  html[data-nav="back"]::view-transition-new(root) {
    animation-name: xd-page-return;
    z-index: 1;
  }
}
/* Forward — old recedes (push away), new rises from below. */
@keyframes xd-page-recede {
  from { opacity: 1; transform: scale(1)    translateY(0); }
  to   { opacity: 0; transform: scale(1.04) translateY(-2%); }
}
@keyframes xd-page-rise {
  0%   { opacity: 0; transform: scale(0.985) translateY(4%); }
  60%  { opacity: 1; }
  100% { opacity: 1; transform: scale(1)     translateY(0); }
}
/* Back — old falls down + fades, new returns from a slight push-back. */
@keyframes xd-page-fall {
  from { opacity: 1; transform: scale(1)    translateY(0); }
  to   { opacity: 0; transform: scale(0.985) translateY(3%); }
}
@keyframes xd-page-return {
  0%   { opacity: 0; transform: scale(1.025) translateY(-1.5%); }
  60%  { opacity: 1; }
  100% { opacity: 1; transform: scale(1)     translateY(0); }
}

/* ============ ON-SCREEN KEYBOARD ============
   Custom QWERTZ keyboard rendered on (pointer: coarse) devices. Replaces
   the iOS/Android virtual keyboard entirely. See assets/engine.js for the
   dispatch path; key markup is generated at game-init time so the same DOM
   container is reused across puzzles.

   Why a custom keyboard at all (research notes, 2026-06-17): mobile virtual
   keyboards on the web are a documented minefield — autocorrect mangles
   first-two-letters via `autocapitalize`, iOS PWAs sometimes refuse to
   open the keyboard, predictive bars insert whole words, Samsung S24
   double-fires Backspace, fonts <16px trigger involuntary zoom. The
   industry standard for word puzzles (NYT Wordle/Crossword/Connections,
   LA Times, Apple News) is a DOM-rendered keyboard with delegated
   pointerdown handlers + a parallel document.keydown for hardware
   keyboards. This is that implementation.

   Hidden on (hover: hover) and (pointer: fine) — desktop uses hardware
   keyboard only. iPad in portrait keeps the on-screen keyboard even with
   an attached BT keyboard (primary pointer is still coarse). Both input
   paths flow through engine.js typeLetter/deleteLetter — single source
   of truth. */

.oskbd {
  display: none;            /* default off — enabled by the coarse-pointer media query below */
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;              /* above page content, below overlays (overlays use z >= 100) */
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
  background: var(--md-sys-color-surface-container);
  border-top: 1px solid var(--md-sys-color-outline-variant);
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.12);
  user-select: none;
  -webkit-user-select: none;
  /* The keyboard is purely a tap target; suppress the browser's tap-flash
     + double-tap-zoom heuristic so presses feel instant. */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.oskbd-row {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-bottom: 4px;
}
.oskbd-row:last-child { margin-bottom: 0; }

.oskbd-key {
  flex: 1 1 0;
  min-width: 0;             /* allow keys to shrink below content size on narrow screens */
  max-width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: var(--md-shape-sm);
  background: var(--md-sys-color-surface-container-high);
  color: var(--md-sys-color-on-surface);
  font-family: var(--md-font-body);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  position: relative;
  transition: transform var(--xd-dur-fast) var(--ease-flat),
              background-color var(--xd-dur-fast) var(--ease-flat);
  /* M3 state-layer hook: a transparent ::before that goes opaque on press. */
}
.oskbd-key::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--md-sys-color-on-surface);
  opacity: 0;
  transition: opacity var(--xd-dur-fast) var(--ease-flat);
  pointer-events: none;
}
.oskbd-key.pressed,
.oskbd-key:active {
  transform: scale(0.94);
  background: var(--md-sys-color-surface-container-highest);
}
.oskbd-key.pressed::before,
.oskbd-key:active::before {
  opacity: 0.12;
}

/* Backspace key — wider, error-tinted, icon glyph. */
.oskbd-key.oskbd-action {
  flex: 1.6 1 0;
  max-width: 72px;
  background: var(--md-sys-color-error-container);
  color: var(--md-sys-color-on-error-container);
  font-size: 20px;
}
.oskbd-key.oskbd-action.pressed,
.oskbd-key.oskbd-action:active {
  background: var(--md-sys-color-error);
  color: var(--md-sys-color-on-error);
}

/* Disabled state — visually muted, no press feedback. Used when no word
   is active so the user knows the keyboard isn't going to do anything. */
.oskbd-key[aria-disabled="true"] {
  opacity: 0.35;
  pointer-events: none;
}

/* Show only on coarse-pointer devices (touchscreens). Desktop / mouse
   users never see the keyboard — they have a hardware keyboard already
   and the on-screen one would just steal vertical real estate. */
@media (pointer: coarse) {
  .oskbd { display: block; }

  /* Make room for the keyboard at the bottom of the page. The grid +
     clue area sit inside #view-game which scrolls with the body; the
     extra padding keeps the footer visible above the keyboard when the
     user scrolls to the bottom. ~210px = 3 rows × ~44px + paddings +
     safe area. Set as a CSS var so engine.js can override after measure. */
  body { --oskbd-h: 210px; }
  #view-game.active { padding-bottom: var(--oskbd-h); }

  /* When scroll-into-view targets a cell, account for the keyboard so
     the cell doesn't land BEHIND it. scroll-margin-bottom shifts the
     "ideal centred position" up by the keyboard height. */
  .cell { scroll-margin-bottom: calc(var(--oskbd-h) + 24px); }
}

/* Three-key-rows on smaller phones: shrink the keys a touch so 11-key
   rows fit without horizontal scroll on a 320px viewport (iPhone SE). */
@media (max-width: 360px) {
  .oskbd-key {
    height: 40px;
    font-size: 16px;
    max-width: 36px;
  }
  .oskbd-key.oskbd-action { max-width: 60px; font-size: 18px; }
  body { --oskbd-h: 192px; }
}

/* Reduced motion: keep the press scale but lose the duration. */
@media (prefers-reduced-motion: reduce) {
  .oskbd-key { transition: none !important; }
  .oskbd-key::before { transition: none !important; }
}

/* ============ PRINT ============ */
@media print {
  body { background: #fff !important; padding: 0; }
  .confetti, .overlay, .toast-container, .oskbd { display: none !important; }
  .user-bar, .back-bar, .stats-bar, .progress-track,
  .current-clue, .controls, .toggle-row, .clues-tabs,
  .footer, #view-selector, #view-admin, #view-profile,
  .puzzle-meta-pills, .sync-indicator,
  .board-header, #hiddenInput, #appVersion { display: none !important; }
  .masthead { border-bottom: 1px solid #000; padding-bottom: 12px; margin-bottom: 16px; }
  .masthead .eyebrow, .subtitle { display: none; }
  .masthead h1, .masthead h1 em { color: #000 !important; }
  .app { max-width: 100%; padding: 12px; }
  .main { grid-template-columns: 1fr !important; gap: 24px; }
  .board-wrap, .clues-panel, .board-controls {
    background: #fff !important;
    border: 1px solid #000 !important;
    box-shadow: none !important;
    padding: 8px;
    page-break-inside: avoid;
  }
  .grid { background: #000 !important; }
  .cell { background: #fff !important; color: #000 !important; }
  .cell.block { background: #000 !important; }
  .cell .letter { display: none; }
  .cell .num { font-size: 7px; color: #000 !important; }
  .clues-panel { max-height: none !important; position: static !important; page-break-inside: avoid; }
  .clues-section { display: block !important; }
  .clue-item { background: none !important; border-left: none !important; padding: 4px 8px; }
  .clue-item.active, .clue-item.solved { background: none !important; }
  .clue-text { color: #000 !important; }
  .clues-legend { display: none; }
}

/* ============ LOGIN / REGISTRATION / PASSWORD DIALOGS ============ */
.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin: 4px 0 20px;
}
.auth-switch { grid-template-columns: repeat(2, 1fr); }
.auth-fields {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.auth-field[hidden] { display: none; }
.auth-field-label {
  font-family: var(--md-font-body);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--md-sys-color-on-surface-variant);
}
.auth-field input {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  font-family: var(--md-font-body);
  font-size: 16px; /* 16px keeps iOS from zooming into the field */
  color: var(--md-sys-color-on-surface);
  background: var(--md-sys-color-surface-container-lowest);
  border: 1px solid var(--md-sys-color-outline);
  border-radius: var(--md-shape-sm);
  outline: none;
}
.auth-field input:focus {
  border-color: var(--md-sys-color-primary);
  box-shadow: 0 0 0 1px var(--md-sys-color-primary);
}
.auth-show {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--md-font-body);
  font-size: 13px;
  color: var(--md-sys-color-on-surface-variant);
  cursor: pointer;
}
.auth-show input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--md-sys-color-primary);
}
.auth-error {
  font-family: var(--md-font-body);
  font-size: 13px;
  line-height: 1.4;
  color: var(--md-sys-color-error);
}
.auth-error:empty { display: none; }
.auth-field-hint {
  font-family: var(--md-font-body);
  font-size: 12px;
  color: var(--md-sys-color-on-surface-variant);
}
.auth-field-hint[hidden] { display: none; }
.auth-submit { width: 100%; }

/* Avatars show the first letter of the name (no profile pictures). */
.user-avatar, .settings-account-avatar, .user-cell-avatar, .win-leaderboard-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--md-font-body);
  font-weight: 600;
  color: var(--md-sys-color-on-primary-container);
  line-height: 1;
}
.user-avatar { font-size: 14px; }
.settings-account-avatar { font-size: 20px; }
.win-leaderboard-avatar { font-size: 11px; }
.user-cell-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--md-sys-color-primary-container);
  font-size: 12px;
  flex-shrink: 0;
}
/* In the account dialogs the form's own button is the primary action;
   the dialog's close button is the quiet "Abbrechen". */
.auth-form ~ .xdialog-actions .xdialog-btn-ok {
  background: transparent;
  color: var(--md-sys-color-primary);
  border: 1px solid var(--md-sys-color-outline);
}
.auth-form ~ .xdialog-actions .xdialog-btn-ok:hover {
  background: color-mix(in srgb, var(--md-sys-color-primary) 8%, transparent);
}
