/* ----- Design tokens ----- */
:root {
  --bg: #fff7e8;
  --bg-grain: #fdeed0;
  --ink: #2a2438;
  --ink-soft: #5a4f6e;
  --paper: #ffffff;
  --line: #2a2438;

  --c-red:    #e63946;
  --c-green:  #4cb944;
  --c-blue:   #2cb1e8;
  --c-yellow: #ffd23f;

  --c-red-shadow:    #a01f2a;
  --c-green-shadow:  #2f7a2a;
  --c-blue-shadow:   #1a7aa8;
  --c-yellow-shadow: #c99a16;

  --shadow-soft: 0 6px 0 rgba(42, 36, 56, 0.18);
  --shadow-deep: 0 10px 0 rgba(42, 36, 56, 0.28);
  --radius-lg: 14px;
  --radius-md: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Nunito', system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 10%, #ffe0a8 0%, transparent 40%),
    radial-gradient(circle at 85% 90%, #ffd0d0 0%, transparent 45%),
    var(--bg);
  min-height: 100vh;
  overflow-x: hidden;
  /* Prevent text selection during drag — important for kids who press hard */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Subtle paper grain via SVG noise */
body::before {
  content: '';
  position: fixed; inset: 0;
  pointer-events: none;
  opacity: 0.35;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/><feColorMatrix values='0 0 0 0 0.6 0 0 0 0 0.5 0 0 0 0 0.4 0 0 0 0.08 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 720px;
  margin: 0 auto;
  padding: 28px 20px 40px;
  text-align: center;
}

h1 {
  font-family: 'Fraunces', serif;
  font-weight: 900;
  font-size: clamp(2.2rem, 7vw, 3.4rem);
  margin: 0 0 4px;
  letter-spacing: -0.02em;
  color: var(--ink);
  line-height: 1;
}
h1 .swatch {
  display: inline-block;
  width: 0.7em; height: 0.7em;
  border-radius: 50%;
  margin-right: 0.15em;
  vertical-align: -0.05em;
  border: 3px solid var(--ink);
  box-shadow: 3px 3px 0 var(--ink);
  transform: rotate(-6deg);
}
.subtitle {
  font-size: 1rem;
  color: var(--ink-soft);
  margin: 8px 0 22px;
  font-weight: 600;
}

/* ----- Difficulty selector ----- */
.difficulty {
  display: inline-flex;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 6px;
  box-shadow: var(--shadow-soft);
  margin-bottom: 24px;
  gap: 4px;
}
.difficulty button {
  font-family: inherit;
  font-weight: 800;
  font-size: 1rem;
  padding: 10px 20px;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.15s, background 0.2s, color 0.2s;
}
.difficulty button:hover { transform: translateY(-1px); }
.difficulty button.active {
  background: var(--ink);
  color: var(--bg);
}

/* ----- Grid ----- */
.board-wrapper {
  display: flex;
  justify-content: center;
  margin: 0 auto 28px;
  perspective: 1000px;
}
.board {
  --cell: min(18vw, 92px);
  display: grid;
  grid-template-columns: repeat(2, auto);
  grid-template-rows: repeat(2, auto);
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-deep);
}
/* Each .block wraps a 2x2 cluster of cells. Putting the thick black
   dividers on the block elements (instead of on cells) means the thick
   and thin lines never share a corner, so the black lines render cleanly. */
.block {
  display: grid;
  grid-template-columns: repeat(2, var(--cell));
  grid-template-rows: repeat(2, var(--cell));
}
/* Left-column blocks paint the vertical divider on their right edge */
.block[data-block-col="0"] { border-right: 4px solid var(--ink); }
/* Top-row blocks paint the horizontal divider on their bottom edge */
.block[data-block-row="0"] { border-bottom: 4px solid var(--ink); }

.cell {
  background: var(--paper);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* Thin gray lines within a block: cells in the left column of their block
   paint a right border, cells in the top row of their block paint a bottom
   border. Cells on the right/bottom edge of a block don't paint these
   (the block's thick border or the board's outer border handles that edge). */
.cell[data-block-col-pos="0"] { border-right: 2px solid rgba(42, 36, 56, 0.22); }
.cell[data-block-row-pos="0"] { border-bottom: 2px solid rgba(42, 36, 56, 0.22); }

.cell .filling {
  width: 76%;
  height: 76%;
  transition: transform 0.2s, background 0.2s, border-radius 0.2s;
}
/* Givens are permanent puzzle clues: flat, square-ish, locked-in look.
   A subtle inset shadow signals "this is part of the printed puzzle." */
.cell.given .filling {
  border-radius: 6px;
  box-shadow: inset 0 0 0 3px rgba(0,0,0,0.18);
}
/* Player answers are round disks — visually echoing the draggable palette,
   so kids immediately read them as "things I placed and can change." */
.cell.answered .filling {
  border-radius: 50%;
  width: 68%;
  height: 68%;
  border: 3px solid var(--ink);
  box-shadow:
    0 4px 0 rgba(42, 36, 56, 0.25),
    inset -4px -6px 8px rgba(0, 0, 0, 0.15),
    inset 4px 6px 8px rgba(255, 255, 255, 0.35);
  position: relative;
}
/* Glossy highlight on player disks — matches the marbles in the palette */
.cell.answered .filling::after {
  content: '';
  position: absolute;
  top: 12%; left: 18%;
  width: 30%; height: 25%;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 50%;
  filter: blur(2px);
  pointer-events: none;
}
.cell.empty .filling {
  background: transparent;
  border: 3px dashed rgba(42, 36, 56, 0.18);
  border-radius: 10px;
}
/* Drop highlight works for both empty cells and answered cells —
   a soft yellow ring around the whole cell reads as "drop here" either way */
.cell.drop-target {
  background: var(--c-yellow);
}
.cell.drop-target .filling {
  transform: scale(1.05);
}
.cell.placed .filling {
  animation: pop 0.3s ease-out;
}
.cell.selected .filling {
  box-shadow: 0 0 0 4px var(--ink), 0 0 0 7px var(--c-yellow);
  animation: pulse 1.2s infinite;
}

@keyframes pop {
  0%   { transform: scale(0.6); }
  60%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.04); }
}

/* ----- Color palette (draggable disks) ----- */
.palette {
  display: flex;
  justify-content: center;
  gap: clamp(14px, 4vw, 28px);
  margin-bottom: 18px;
  min-height: 90px;
}
.disk {
  --size: clamp(64px, 16vw, 80px);
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  border: 4px solid var(--ink);
  cursor: grab;
  position: relative;
  transition: transform 0.15s;
  touch-action: none; /* required for pointer drag on touch */
  flex-shrink: 0;
}
.disk::after {
  /* glossy highlight to make it look like a marble */
  content: '';
  position: absolute;
  top: 12%; left: 18%;
  width: 30%; height: 25%;
  background: rgba(255,255,255,0.55);
  border-radius: 50%;
  filter: blur(2px);
  pointer-events: none;
}
.disk:hover { transform: translateY(-4px) rotate(-6deg); }
.disk.dragging {
  cursor: grabbing;
  z-index: 1000;
  transform: scale(1.15);
  box-shadow: 0 12px 24px rgba(0,0,0,0.3);
}
.disk.selected {
  transform: translateY(-8px) scale(1.08);
  box-shadow: 0 0 0 5px var(--c-yellow), var(--shadow-soft);
}

.disk-red    { background: var(--c-red);    box-shadow: var(--shadow-soft); }
.disk-green  { background: var(--c-green);  box-shadow: var(--shadow-soft); }
.disk-blue   { background: var(--c-blue);   box-shadow: var(--shadow-soft); }
.disk-yellow { background: var(--c-yellow); box-shadow: var(--shadow-soft); }

.fill-red    { background: var(--c-red); }
.fill-green  { background: var(--c-green); }
.fill-blue   { background: var(--c-blue); }
.fill-yellow { background: var(--c-yellow); }

/* ----- Hint text ----- */
.hint {
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 8px;
}
.hint .small {
  font-size: 0.85rem;
  opacity: 0.7;
}

/* ----- Controls ----- */
.controls {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.controls button {
  font-family: inherit;
  font-weight: 800;
  font-size: 0.95rem;
  padding: 10px 18px;
  border: 3px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
  transition: transform 0.1s, box-shadow 0.1s;
}
.controls button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--ink);
}
/* Subtle variant for secondary actions — no border, no shadow,
   but still discoverable on hover */
.controls button.subtle {
  border-color: transparent;
  background: transparent;
  color: var(--ink-soft);
  box-shadow: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.controls button.subtle:hover {
  color: var(--ink);
  background: rgba(42, 36, 56, 0.06);
}
.controls button.subtle:active {
  transform: none;
  box-shadow: none;
  background: rgba(42, 36, 56, 0.12);
}

/* ----- Win overlay ----- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(42, 36, 56, 0.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s;
  z-index: 100;
}
.overlay.show {
  opacity: 1;
  pointer-events: auto;
}
.overlay-card {
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: 28px;
  padding: 36px 40px;
  text-align: center;
  box-shadow: 0 16px 0 rgba(42, 36, 56, 0.3);
  transform: scale(0.7);
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 90vw;
}
.overlay.show .overlay-card { transform: scale(1); }
.overlay-card .emoji {
  font-size: 5rem;
  display: block;
  animation: bounce 1s infinite;
}
.overlay-card h2 {
  font-family: 'Fraunces', serif;
  font-weight: 900;
  font-size: 2.4rem;
  margin: 8px 0;
}
.overlay-card p {
  color: var(--ink-soft);
  font-weight: 600;
  margin: 0 0 22px;
}
.overlay-card button {
  font-family: inherit;
  font-weight: 800;
  font-size: 1.1rem;
  padding: 14px 28px;
  background: var(--c-yellow);
  color: var(--ink);
  border: 3px solid var(--ink);
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 6px 0 var(--ink);
  transition: transform 0.1s, box-shadow 0.1s;
}
.overlay-card button:hover { transform: translateY(-2px); }
.overlay-card button:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--ink);
}

@keyframes bounce {
  0%, 100% { transform: translateY(0) rotate(-5deg); }
  50%      { transform: translateY(-12px) rotate(5deg); }
}

/* ----- Confetti ----- */
.confetti-piece {
  position: fixed;
  width: 12px;
  height: 18px;
  top: -20px;
  z-index: 99;
  pointer-events: none;
  will-change: transform;
}

/* ----- Footer ----- */
.footer {
  margin-top: 24px;
  color: var(--ink-soft);
  font-size: 0.8rem;
  opacity: 0.7;
}
