/* ==========================================================================
   Checkmate Quest — kid-friendly, warm, focused
   ========================================================================== */

:root {
  /* Typography */
  --font-display: "Fraunces", Georgia, serif;
  --font-body: "Plus Jakarta Sans", -apple-system, BlinkMacSystemFont, sans-serif;

  /* Color — warm playful */
  --bg: #fff8ec;
  --bg-deep: #f3e6c8;
  --ink: #2a1c0f;
  --ink-soft: #6b573e;
  --paper: #ffffff;
  --line: #e6d6b3;
  --accent: #ff7a3d;    /* warm orange */
  --accent-2: #ffc857;  /* gold */
  --good: #2dbf6e;
  --bad: #e0405a;
  --heart: #ff3e6c;

  /* Board */
  --sq-light: #f4dfb1;
  --sq-dark: #b08762;
  --sq-highlight: #fff48a;

  /* Radii */
  --r-sm: 10px;
  --r-md: 16px;
  --r-lg: 24px;

  /* Motion */
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 20% -10%, #ffe7c1 0%, transparent 60%),
    radial-gradient(1000px 500px at 110% 110%, #ffd6b9 0%, transparent 55%),
    var(--bg);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
  /* iPad / touch tuning */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation; /* kill 300ms double-tap zoom */
  overscroll-behavior: none;
}

body { overflow-y: auto; }

/* Pieces and squares should never be selectable on iPad */
.board, .board * {
  -webkit-user-select: none;
  user-select: none;
}
.board img {
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  cursor: pointer;
}
.board .square-55d63 { cursor: pointer; }

.app {
  max-width: 760px;
  margin: 0 auto;
  padding: clamp(16px, 2.5vw, 28px);
  display: flex;
  flex-direction: column;
  gap: clamp(14px, 2vw, 22px);
  min-height: 100dvh;
}

/* iPad landscape */
@media (min-width: 900px) and (orientation: landscape) {
  .app {
    max-width: 1040px;
    gap: 18px;
    padding: 18px 24px;
  }
}

/* Big desktop / large touchscreen */
@media (min-width: 1200px) {
  .app {
    max-width: 1180px;
    gap: 22px;
    padding: 22px 28px;
  }
}
@media (min-width: 1600px) {
  .app {
    max-width: 1360px;
  }
}

/* ---------- HUD ---------- */

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 14px 18px;
  box-shadow: 0 6px 20px -12px rgba(60, 30, 10, 0.18);
  width: 100%;
  margin: 0 auto;
}

.hud-left {
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.home-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--ink);
  background: var(--bg-deep);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px 8px 10px;
  font-weight: 700;
  font-size: 14px;
  min-height: 40px;
  transition: background 160ms var(--ease), transform 120ms var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.home-btn:hover { background: #ecdcb6; }
.home-btn:active { transform: translateY(1px); }
.home-btn svg { display: block; color: var(--accent); }
@media (max-width: 600px) {
  .home-btn-label { display: none; }
  .home-btn { padding: 8px 10px; }
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 18px;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.brand-mark {
  font-size: 24px;
  color: var(--accent);
  filter: drop-shadow(0 1px 0 rgba(255,122,61,0.25));
}
@media (max-width: 520px) {
  .brand-name { display: none; }
}

.hud-center {
  display: inline-flex;
  gap: clamp(10px, 3vw, 22px);
  align-items: center;
  justify-content: center;
}
.stat { text-align: center; min-width: 56px; }
.stat-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
}
.stat-value {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 28px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums lining-nums;
  color: var(--ink);
  transition: transform 200ms var(--ease), color 200ms var(--ease);
}
@media (min-width: 768px) { .stat-value { font-size: 32px; } }
.stat-value.pulse {
  animation: stat-pulse 700ms var(--ease);
  color: var(--accent);
}
@keyframes stat-pulse {
  0%   { transform: scale(1); }
  35%  { transform: scale(1.45); }
  100% { transform: scale(1); }
}
.stat-value.big-pulse {
  animation: stat-big-pulse 1100ms var(--ease);
  color: var(--accent);
}
@keyframes stat-big-pulse {
  0%   { transform: scale(1) rotate(0); }
  20%  { transform: scale(1.55) rotate(-3deg); color: var(--accent); }
  45%  { transform: scale(1.25) rotate(2deg); color: #ff4d18; }
  70%  { transform: scale(1.4) rotate(-1deg); }
  100% { transform: scale(1) rotate(0); }
}
.stat-value.big-pulse, .stat-value.pulse {
  position: relative;
  z-index: 2;
}

.hearts {
  display: inline-flex;
  gap: 6px;
  justify-content: flex-end;
}
.heart {
  font-size: 30px;
  line-height: 1;
  color: var(--heart);
  filter: drop-shadow(0 2px 0 rgba(255, 62, 108, 0.28));
  transition: transform 220ms var(--ease), opacity 380ms var(--ease), filter 220ms var(--ease);
}
@media (min-width: 768px) { .heart { font-size: 34px; } }
.heart.lost {
  opacity: 0.18;
  filter: grayscale(1);
  transform: scale(0.85);
  color: #a8a092;
}
.heart.shake { animation: heart-shake 460ms var(--ease); }
@keyframes heart-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px) rotate(-8deg); }
  40% { transform: translateX(6px) rotate(8deg); }
  60% { transform: translateX(-4px) rotate(-4deg); }
  80% { transform: translateX(4px) rotate(4deg); }
}

/* ---------- Puzzle ---------- */

.stage { display: flex; justify-content: center; }

.puzzle {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: clamp(14px, 3vw, 22px);
  width: 100%;
  max-width: 740px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 10px 30px -18px rgba(60, 30, 10, 0.22);
}
@media (min-width: 900px) and (orientation: landscape) {
  .puzzle { max-width: 880px; }
}
@media (min-width: 1200px) {
  .puzzle { max-width: 980px; }
}
@media (min-width: 1600px) {
  .puzzle { max-width: 1080px; }
}

.puzzle-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.turn-tag {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 999px;
  background: linear-gradient(135deg, #fff3da, #ffe2bd);
  border: 1px solid var(--line);
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
}
@media (min-width: 768px) { .turn-tag { font-size: 15px; padding: 12px 20px; } }
.ghost-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 10px 18px;
  min-height: 40px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 160ms var(--ease), color 160ms var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.ghost-btn:hover { background: var(--bg-deep); color: var(--ink); }

.board {
  width: 100%;
  /* Cap based on viewport so the board uses screen real estate well */
  max-width: min(640px, 88vmin);
  aspect-ratio: 1 / 1;
  margin: 0 auto;
  border-radius: var(--r-md);
  overflow: hidden;
  box-shadow: 0 6px 0 rgba(60, 30, 10, 0.12), 0 18px 40px -22px rgba(60, 30, 10, 0.35);
  background: var(--bg-deep);
  touch-action: manipulation;
  cursor: pointer;
}

/* Make the board grow on bigger screens — kill the white space */
@media (min-width: 900px) and (orientation: landscape) {
  .board { max-width: min(780px, 78vmin); }
}
@media (min-width: 1200px) {
  .board { max-width: min(880px, 80vmin); }
}
@media (min-width: 1600px) {
  .board { max-width: min(980px, 78vmin); }
}

/* chessboard.js overrides for theme */
.board .white-1e1d7,
.board .black-3c85d {
  border-radius: 0;
}
.board .white-1e1d7 { background: var(--sq-light); color: #7a5a30; }
.board .black-3c85d { background: var(--sq-dark); color: #f1dfb6; }

/* Highlighting */
.sq-selected { box-shadow: inset 0 0 0 4px var(--accent) !important; }
.sq-target {
  position: relative;
}
.sq-target::after {
  content: "";
  position: absolute;
  inset: 25%;
  border-radius: 50%;
  background: rgba(255, 122, 61, 0.45);
  pointer-events: none;
}
.sq-target.sq-capture::after {
  inset: 6%;
  background: transparent;
  border: 5px solid rgba(255, 122, 61, 0.7);
}
.sq-mate { box-shadow: inset 0 0 0 5px var(--good) !important; }
.sq-wrong { box-shadow: inset 0 0 0 5px var(--bad) !important; }

.puzzle-footer { min-height: 28px; }
.message {
  text-align: center;
  font-weight: 600;
  font-size: 16px;
  color: var(--ink-soft);
  transition: color 200ms var(--ease);
  min-height: 22px;
}
@media (min-width: 768px) { .message { font-size: 17px; } }
.message.good { color: var(--good); }
.message.bad { color: var(--bad); }

/* ---------- FX layer ---------- */

.fx-canvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  pointer-events: none;
  z-index: 50;
}

.affirmation {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 60;
  font-family: var(--font-display);
  font-weight: 800;
  letter-spacing: -0.02em;
  opacity: 0;
  text-align: center;
  padding: 0 24px;
  /* Cap so big words never overflow the viewport */
  max-width: 95vw;
  line-height: 0.95;
  word-break: break-word;
}
.affirmation.playing {
  animation: affirm 1500ms var(--ease) forwards;
}
@keyframes affirm {
  0%   { opacity: 0; transform: scale(0.6) translateY(20px); }
  20%  { opacity: 1; transform: scale(1.08) translateY(0); }
  60%  { opacity: 1; transform: scale(1) translateY(-6px); }
  100% { opacity: 0; transform: scale(1.12) translateY(-40px); }
}

/* ---------- Modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(40, 20, 6, 0.5);
  backdrop-filter: blur(8px);
  z-index: 100;
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--paper);
  border-radius: var(--r-lg);
  padding: clamp(24px, 5vw, 40px);
  text-align: center;
  max-width: 420px;
  width: 100%;
  border: 1px solid var(--line);
  box-shadow: 0 30px 60px -20px rgba(0,0,0,0.4);
  animation: modal-in 500ms var(--ease);
}
@keyframes modal-in {
  from { opacity: 0; transform: scale(0.9) translateY(20px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-eyebrow {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--bad);
  margin-bottom: 8px;
}
.modal-title {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 40px;
  line-height: 1;
  margin: 0 0 16px;
  color: var(--ink);
}
.modal-stat {
  font-size: 18px;
  font-weight: 600;
  margin: 4px 0;
  color: var(--ink);
}
.modal-stat-sub {
  font-size: 14px;
  color: var(--ink-soft);
  margin: 4px 0 24px;
}
.modal-stat span {
  font-family: var(--font-display);
  font-weight: 800;
  color: var(--accent);
}

.primary-btn {
  background: var(--accent);
  color: white;
  border: none;
  font: inherit;
  font-weight: 700;
  font-size: 16px;
  padding: 14px 28px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 4px 0 #d35a22, 0 8px 18px -6px rgba(255, 122, 61, 0.6);
  transition: transform 120ms var(--ease), box-shadow 120ms var(--ease);
}
.primary-btn:hover { transform: translateY(-1px); }
.primary-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #d35a22, 0 4px 10px -4px rgba(255, 122, 61, 0.6); }

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
