/* ============================================================
   FlickerPic — Game Styles
   Applies to all game screens (home, game, results, stats, past).
   Imports design tokens from /css/styles.css via :root vars.
   ============================================================ */

/* ── App Shell ─────────────────────────────────────────────── */

.app {
  width: 100%;
  max-width: 420px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
}

.app-header {
  padding: 14px 16px;
  text-align: center;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.logo--sm {
  font-size: 1.4rem;
  text-decoration: none;
}

/* ── Settings Toggle ───────────────────────────────────────── */

.settings-row {
  padding: 0 4px;
}

.toggle-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--color-text-muted);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--color-border);
  border-radius: 11px;
  transition: background 0.2s;
}

.toggle-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-track {
  background: var(--color-primary);
}

.toggle-switch input:checked + .toggle-track::after {
  transform: translateX(18px);
}

/* ── Screen System ─────────────────────────────────────────── */

.screen {
  display: none;
  flex: 1;
  flex-direction: column;
  gap: 16px;
  padding: 20px 16px;
  overflow-y: auto;
}

.screen.active {
  display: flex;
}

.screen-title {
  font-size: 1.3rem;
  font-weight: 700;
  text-align: center;
}

.subsection-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text-muted);
  margin-top: 8px;
}

/* ── Buttons (game-specific overrides) ─────────────────────── */

.btn--primary {
  background: var(--color-primary);
  color: #fff;
}

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

.btn--secondary {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-text);
}

.btn--secondary:hover {
  background: var(--color-surface-hover);
}

.btn--ghost {
  background: transparent;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  padding: 8px 16px;
}

.btn--ghost:hover {
  color: var(--color-error);
}

/* Give-up confirmation state */
.btn--confirm {
  color: var(--color-error) !important;
  font-weight: 600;
}

.btn--lg {
  padding: 16px 24px;
  font-size: 1.1rem;
  border-radius: var(--radius);
}

.screen-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ── Ad Slots ──────────────────────────────────────────────── */

.ad-slot {
  margin-top: auto;
  padding: 20px 16px;
  min-height: 100px;
  background: rgba(30, 36, 74, 0.6);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.ad-slot--lg {
  min-height: 250px;
}

.ad-label {
  font-size: 0.8rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  opacity: 0.6;
}

/* ── Home Screen ───────────────────────────────────────────── */

.home-hero {
  text-align: center;
  padding: 24px 0 8px;
}

.puzzle-number {
  font-size: 1.5rem;
  font-weight: 700;
}

.puzzle-date {
  font-size: 0.9rem;
  color: var(--color-text-muted);
  margin-top: 4px;
}

/* ── Game Screen ───────────────────────────────────────────── */

.game-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-text-muted);
}

.game-timer {
  font-variant-numeric: tabular-nums;
  font-size: 1.1rem;
  color: var(--color-text);
  padding: 4px 10px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
}

.game-guesses {
  font-variant-numeric: tabular-nums;
  font-size: 1rem;
  color: var(--color-text-muted);
  padding: 4px 10px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
}

/* Image frame */
.image-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--color-surface);
  flex-shrink: 0;
}

.image-frame canvas {
  width: 100%;
  height: 100%;
  display: block;
}

#impressionist-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  transition: opacity 0.4s ease-out;
}

/* Subtle jitter animation — occasional twitch like an unstable painting */
@keyframes painting-jitter {
  0%    { transform: translate(0, 0) rotate(0deg);        }
  92%   { transform: translate(0, 0) rotate(0deg);        }
  93%   { transform: translate(0.7px, -0.5px) rotate(0.2deg);  }
  94.5% { transform: translate(-0.5px, 0.4px) rotate(-0.15deg);}
  96%   { transform: translate(0.3px, 0.6px) rotate(0.1deg);   }
  97.5% { transform: translate(-0.6px, -0.3px) rotate(-0.12deg);}
  99%   { transform: translate(0.2px, -0.2px) rotate(0.05deg); }
  100%  { transform: translate(0, 0) rotate(0deg);        }
}

.image-frame.jitter canvas {
  animation: painting-jitter 3s ease-in-out infinite;
}

/* Larger shake burst that leads into obfuscation phase transitions */
@keyframes painting-shake {
  0%   { transform: translate(0, 0) rotate(0deg);              }
  8%   { transform: translate(-4px, 3px) rotate(-4deg);        }
  16%  { transform: translate(3.5px, -3.5px) rotate(3.2deg);   }
  24%  { transform: translate(-3px, -2px) rotate(-2.8deg);     }
  32%  { transform: translate(2.5px, 3px) rotate(2.4deg);      }
  42%  { transform: translate(-3.5px, 1.5px) rotate(-1.8deg);  }
  52%  { transform: translate(2px, -2.5px) rotate(1.4deg);     }
  62%  { transform: translate(-1.5px, 2px) rotate(-1deg);      }
  72%  { transform: translate(1.2px, -1px) rotate(0.7deg);     }
  82%  { transform: translate(-0.8px, 0.6px) rotate(-0.4deg);  }
  92%  { transform: translate(0.3px, -0.3px) rotate(0.15deg);  }
  100% { transform: translate(0, 0) rotate(0deg);              }
}

.image-frame.phase-shake canvas {
  animation: painting-shake 0.6s ease-out;
}

.image-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  z-index: 2;
}

/* Pre-game countdown overlay */
.pregame-countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  z-index: 3;
}

.pregame-number {
  font-size: 6rem;
  font-weight: 800;
  color: var(--color-primary);
  line-height: 1;
  animation: countdown-pop 1s ease-out forwards;
}

@keyframes countdown-pop {
  0%   { transform: scale(0.4); opacity: 0; }
  20%  { transform: scale(1.15); opacity: 1; }
  40%  { transform: scale(0.95); opacity: 1; }
  55%  { transform: scale(1.0); opacity: 1; }
  80%  { transform: scale(1.0); opacity: 1; }
  100% { transform: scale(1.4); opacity: 0; }
}

/* Hint bar */
.hint-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  min-height: 62px; /* 2 lines of 0.9rem text + padding — prevents layout shift */
}

.hint-icon {
  flex-shrink: 0;
}

.hint-text {
  flex: 1;
  color: var(--color-text);
  transition: color 0.3s ease;
}

.hint-text.hint-new {
  color: #ffd93d;
}

.hint-count {
  flex-shrink: 0;
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: 0.8rem;
}

/* Word blanks */
.word-blanks {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
  padding: 8px 0;
  min-height: 92px; /* 2 rows of 36px letters + gap + padding — prevents layout shift */
}

/* Each word is an inline-flex group so wrapping never splits mid-word */
.word-group {
  display: inline-flex;
  gap: 2px;
}

.blank-letter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 36px;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--color-text-muted);
  border-bottom: 2px solid var(--color-border);
}

.blank-letter.revealed {
  color: var(--color-text);
  border-bottom-color: var(--color-primary);
}

/* Flash animation when a letter is newly revealed */
@keyframes letter-flash {
  0%   { color: var(--color-primary); transform: scale(1.3); }
  100% { color: var(--color-text); transform: scale(1); }
}

.blank-letter.letter-flash {
  animation: letter-flash 0.4s ease-out forwards;
}

.blank-space {
  width: 16px;
  height: 36px;
}

/* Guess input row */
.guess-row {
  display: flex;
  gap: 8px;
}

.guess-input {
  flex: 1;
  padding: 12px 14px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  font-size: 1rem;
  outline: none;
  transition: border-color var(--transition);
}

.guess-input:focus {
  border-color: var(--color-primary);
}

.guess-input.shake {
  animation: shake 0.4s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

/* Wrong guesses */
.wrong-guesses {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 0;
}

.wrong-guess {
  display: inline-block;
  padding: 4px 10px;
  background: rgba(248, 113, 113, 0.15);
  border: 1px solid rgba(248, 113, 113, 0.3);
  border-radius: 20px;
  font-size: 0.8rem;
  color: var(--color-error);
}

/* ── Results Screen ────────────────────────────────────────── */

.result-banner {
  text-align: center;
  padding: 16px 0;
}

.result-banner--win .result-emoji {
  font-size: 4rem;
  animation: win-pop 0.6s ease-out;
}

@keyframes win-pop {
  0%   { transform: scale(0); }
  60%  { transform: scale(1.3); }
  80%  { transform: scale(0.9); }
  100% { transform: scale(1); }
}

.result-banner--win .result-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-success);
}

.result-banner--lose .result-emoji {
  font-size: 4rem;
  animation: lose-shake 0.5s ease-in-out;
}

@keyframes lose-shake {
  0%, 100% { transform: translateX(0) rotate(0deg); }
  15% { transform: translateX(-10px) rotate(-5deg); }
  30% { transform: translateX(10px) rotate(5deg); }
  45% { transform: translateX(-8px) rotate(-3deg); }
  60% { transform: translateX(8px) rotate(3deg); }
  75% { transform: translateX(-4px); }
  90% { transform: translateX(4px); }
}

.result-banner--lose .result-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-error);
}

.result-answer {
  text-align: center;
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

/* Result row: thumbnail image + stats side-by-side */
.result-row {
  display: flex;
  gap: 12px;
  align-items: stretch;
}

.result-image-frame {
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--color-surface);
  flex-shrink: 0;
}

.result-image-frame--sm {
  width: 200px;
  height: 200px;
}

.result-image-frame canvas {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.result-stats {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  justify-content: space-between;
}

.ad-slot--results {
  min-height: 100px;
  margin-top: 0;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  padding: 4px 12px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  min-width: 32px;
}

.stat-label {
  font-size: 0.75rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Countdown */
.countdown-box {
  text-align: center;
  padding: 16px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
}

.countdown-label {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-bottom: 4px;
}

.countdown-digits {
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.05em;
  background: linear-gradient(135deg, var(--color-primary), #c084fc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ── Stats Screen ──────────────────────────────────────────── */

.stats-overview {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 14px 4px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
}

.stat-card-value {
  font-size: 1.4rem;
  font-weight: 700;
}

.stat-card-label {
  font-size: 0.7rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 2px;
}

/* Distribution chart */
.distribution-chart {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.dist-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dist-label {
  width: 60px;
  font-size: 0.8rem;
  color: var(--color-text-muted);
  text-align: right;
  flex-shrink: 0;
}

.dist-bar-track {
  flex: 1;
  height: 24px;
  background: var(--color-surface);
  border-radius: 4px;
  overflow: hidden;
}

.dist-bar {
  height: 100%;
  background: var(--color-primary);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: #fff;
  min-width: 24px;
  transition: width 0.4s ease;
}

/* ── Past Puzzles Screen ───────────────────────────────────── */

.past-puzzles-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--color-text-muted);
  font-size: 0.95rem;
}

.past-puzzle-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--color-surface);
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
}

.past-puzzle-icon {
  font-size: 1.3rem;
  flex-shrink: 0;
}

.past-puzzle-info {
  flex: 1;
  min-width: 0;
}

.past-puzzle-title {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.past-puzzle-meta {
  font-size: 0.8rem;
  color: var(--color-text-muted);
  margin-top: 2px;
}

/* ── Responsive ────────────────────────────────────────────── */

@media (max-width: 420px) {
  .screen {
    padding: 16px 12px;
  }

  .blank-letter {
    width: 24px;
    height: 32px;
    font-size: 1rem;
  }

  .stats-overview {
    gap: 6px;
  }

  .stat-card-value {
    font-size: 1.2rem;
  }
}

@media (min-width: 421px) {
  body {
    display: flex;
    justify-content: center;
  }
}

/* ── Soft Keyboard (touch devices only) ────────────────────── */

.soft-keyboard {
  display: none; /* hidden by default; shown only on touch devices */
  flex-direction: column;
  gap: 6px;
  padding: 6px 0 4px;
  flex-shrink: 0;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.key {
  flex: 1;
  max-width: 36px;
  height: 44px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
}

.key:active {
  background: var(--color-primary);
  color: #fff;
}

.key--action {
  flex: 1.5;
  max-width: 54px;
  font-size: 1.1rem;
}

.key--space {
  flex: 1;
  max-width: 220px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
}

.soft-keyboard.keyboard-disabled .key {
  opacity: 0.4;
  pointer-events: none;
}

/* Wrong guesses: collapse from flow when empty */
#screen-game .wrong-guesses:empty {
  display: none;
}

/* ── Touch-device compact game layout ──────────────────────── */
/* Ensures all game elements (image, hints, input, keyboard)
   fit within the viewport on most phones without scrolling.  */

@media (pointer: coarse) {
  /* Show the soft keyboard */
  .soft-keyboard {
    display: flex;
  }

  /* Compact header */
  .app-header {
    padding: 8px 12px;
  }

  .logo--sm {
    font-size: 1.2rem;
  }

  /* ── Game screen: fill viewport, no scroll ── */

  #screen-game.active {
    gap: 4px;
    padding: 6px 10px 2px;
    overflow: hidden;
  }

  /* Game bar */
  #screen-game .game-bar {
    font-size: 0.85rem;
  }

  #screen-game .game-timer {
    font-size: 0.95rem;
    padding: 2px 8px;
  }

  #screen-game .game-guesses {
    font-size: 0.85rem;
    padding: 2px 8px;
  }

  /* Image frame: responsive square that fits the viewport.
     Width = min(container, viewport-height minus other UI elements).
     aspect-ratio derives height = width → always a perfect square.
     The 440px constant accounts for header, bar, hint (2-line),
     blanks (2-row), input, give-up, keyboard, wrong-guesses, and gaps. */
  #screen-game .image-frame {
    flex: 0 0 auto;
    width: clamp(120px, calc(100dvh - 440px), 100%);
    aspect-ratio: 1 / 1;
    align-self: center;
  }

  /* Hint bar */
  #screen-game .hint-bar {
    padding: 5px 10px;
    font-size: 0.8rem;
    gap: 6px;
    min-height: 50px; /* 2 lines of 0.8rem text + padding — prevents layout shift */
  }

  #screen-game .hint-count {
    font-size: 0.72rem;
  }

  /* Word blanks */
  #screen-game .word-blanks {
    padding: 2px 0;
    min-height: 55px; /* 2 rows of 24px letters + gap + padding — prevents layout shift */
    gap: 3px;
  }

  #screen-game .blank-letter {
    width: 20px;
    height: 24px;
    font-size: 0.85rem;
    letter-spacing: 1px;
  }

  #screen-game .blank-space {
    width: 10px;
    height: 24px;
  }

  /* Guess row */
  #screen-game .guess-row {
    gap: 6px;
  }

  #screen-game .guess-input {
    padding: 8px 10px;
    font-size: 0.9rem;
  }

  #screen-game .guess-row .btn--primary {
    padding: 8px 14px;
    font-size: 0.9rem;
  }

  /* Wrong guesses: single-line horizontal scroll */
  #screen-game .wrong-guesses {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 4px;
  }

  #screen-game .wrong-guess {
    white-space: nowrap;
    font-size: 0.72rem;
    padding: 2px 8px;
  }

  /* Give Up: ultra-compact */
  #screen-game #btn-giveup {
    padding: 2px 8px;
    font-size: 0.72rem;
  }

  /* ── Compact keyboard ── */

  .soft-keyboard {
    gap: clamp(2px, 0.4dvh, 4px);
    padding: clamp(2px, 0.4dvh, 6px) 0;
    padding-bottom: max(2px, env(safe-area-inset-bottom, 0px));
  }

  .keyboard-row {
    gap: 3px;
  }

  .key {
    height: clamp(28px, 4.8dvh, 42px);
    max-width: 34px;
    font-size: clamp(0.65rem, 1.6dvh, 0.85rem);
    border-radius: 6px;
  }

  .key--action {
    max-width: 50px;
    font-size: clamp(0.8rem, 2dvh, 1.1rem);
  }

  .key--space {
    max-width: 200px;
    height: clamp(22px, 3.5dvh, 34px);
    font-size: 0.65rem;
  }
}
