/* Meowdoku theme — cat-cafe warm. Owns ALL visual styling. No framework. */

/* ---------- tokens ---------- */
:root {
  --paper: #FDFBF7;
  --surface: #F5EEE3;
  --surface-2: #EFE6D6;
  --ink: #3D3229;
  --ink-soft: #8A7B6C;
  --accent: #E07A3F;
  --accent-deep: #C05F28;
  --accent-soft: #F6C9A4;
  --line: #E7DDCC;
  --line-strong: #B9A88E;
  --c-given: #F3EADA;
  --c-peer: #F6EEDF;
  --c-same: #FADFC4;
  --c-selected: #F9D3B0;
  --c-bad: #CF4B3E;
  --c-bad-bg: #F9DBD4;
  --shadow: 0 1px 2px rgba(61, 50, 41, 0.08), 0 4px 14px rgba(61, 50, 41, 0.07);
  --radius: 14px;
  --press: 90ms;
  --snap: 130ms;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #221C17;
    --surface: #2B241D;
    --surface-2: #332B22;
    --ink: #F0E7DA;
    --ink-soft: #A2907C;
    --accent: #F0904D;
    --accent-deep: #F7A76B;
    --accent-soft: #6E4626;
    --line: #383026;
    --line-strong: #5C4D3B;
    --c-given: #2F2820;
    --c-peer: #322A21;
    --c-same: #4C3521;
    --c-selected: #5A3C20;
    --c-bad: #E56B5B;
    --c-bad-bg: #4A2A24;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}

/* explicit theme override wins both directions */
body[data-theme="light"] {
  --paper: #FDFBF7;
  --surface: #F5EEE3;
  --surface-2: #EFE6D6;
  --ink: #3D3229;
  --ink-soft: #8A7B6C;
  --accent: #E07A3F;
  --accent-deep: #C05F28;
  --accent-soft: #F6C9A4;
  --line: #E7DDCC;
  --line-strong: #B9A88E;
  --c-given: #F3EADA;
  --c-peer: #F6EEDF;
  --c-same: #FADFC4;
  --c-selected: #F9D3B0;
  --c-bad: #CF4B3E;
  --c-bad-bg: #F9DBD4;
  --shadow: 0 1px 2px rgba(61, 50, 41, 0.08), 0 4px 14px rgba(61, 50, 41, 0.07);
}

body[data-theme="dark"] {
  --paper: #221C17;
  --surface: #2B241D;
  --surface-2: #332B22;
  --ink: #F0E7DA;
  --ink-soft: #A2907C;
  --accent: #F0904D;
  --accent-deep: #F7A76B;
  --accent-soft: #6E4626;
  --line: #383026;
  --line-strong: #5C4D3B;
  --c-given: #2F2820;
  --c-peer: #322A21;
  --c-same: #4C3521;
  --c-selected: #5A3C20;
  --c-bad: #E56B5B;
  --c-bad-bg: #4A2A24;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 4px 16px rgba(0, 0, 0, 0.3);
}

/* ---------- base ---------- */
* { box-sizing: border-box; margin: 0; padding: 0; }

html { height: 100%; }

body {
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}

button {
  font-family: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.app {
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: clamp(6px, 1.4vh, 14px);
  padding: calc(6px + env(safe-area-inset-top))
           calc(12px + env(safe-area-inset-right))
           calc(10px + env(safe-area-inset-bottom))
           calc(12px + env(safe-area-inset-left));
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: clamp(4px, 1.8vw, 10px);
  min-height: 44px;
}

@media (max-width: 374px) {
  .brand { font-size: 1rem; }
  .brand svg, .brand img { width: 22px; height: 22px; }
  #timer { padding: 4px 8px; font-size: 0.9rem; }
  #btn-new { padding: 5px 9px; }
  .icon-btn { width: 34px; height: 34px; }
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.01em;
}

.brand svg, .brand img { width: 26px; height: 26px; display: block; }

#timer {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 1rem;
  color: var(--ink-soft);
  background: var(--surface);
  padding: 5px 12px;
  border-radius: 999px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--press) ease;
}

.icon-btn svg { width: 20px; height: 20px; }

.icon-btn:active { transform: scale(0.92); }

.icon-btn.off { opacity: 0.5; color: var(--ink-soft); }

/* universal "on" pill — #btn-note, seg chips, icon toggles */
button.on,
#size-seg button.on, #diff-seg button.on,
#btn-note.on {
  background: var(--accent);
  color: #FFF6EC;
  font-weight: 700;
}

#btn-new {
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--accent-deep);
  transition: transform var(--press) ease, background var(--snap) ease;
}

#btn-new:active { transform: scale(0.94); }

#mistakes {
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--c-bad);
  min-width: 1em;
  text-align: center;
}

#mistakes:empty { display: none; }

#mistakes.placed { animation: meow-pop 140ms ease-out; }

/* ---------- segmented controls ---------- */
.segs {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

#size-seg, #diff-seg {
  display: flex;
  background: var(--surface);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

#size-seg button, #diff-seg button {
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--ink-soft);
  transition: transform var(--press) ease, background var(--snap) ease, color var(--snap) ease;
}

#size-seg button:active, #diff-seg button:active { transform: scale(0.94); }

#size-seg button.active, #diff-seg button.active,
#size-seg button[aria-pressed="true"], #diff-seg button[aria-pressed="true"] {
  background: var(--accent);
  color: #FFF6EC;
}

/* ---------- board ---------- */
.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* size board off the wrapper itself where container units exist */
@supports (width: 1cqw) {
  .board-wrap { container-type: size; }
  #board { width: min(100cqw, 100cqh, 520px); }
}

#board {
  --n: 9;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  width: min(100vw - 24px, calc(100vh - 19.5rem), 520px);
  width: min(100vw - 24px, calc(100dvh - 19.5rem), 520px);
  aspect-ratio: 1;
  flex: 0 0 auto;
  margin: auto;
  background: var(--paper);
  border: 2px solid var(--line-strong);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  min-width: 0;
  min-height: 0;
  transition: transform var(--press) ease, background var(--snap) ease;
}

.cell:active { transform: scale(0.92); }

.cell.box-edge-r { border-right: 2px solid var(--line-strong); }
.cell.box-edge-b { border-bottom: 2px solid var(--line-strong); }

.cell-glyph {
  width: 76%;
  height: 76%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.cell-glyph svg { width: 100%; height: 100%; display: block; }

/* plain-number fallback glyph (cats.js failed) */
.fallback-num {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  font-size: clamp(14px, 6.2vmin, 30px);
  line-height: 1;
  color: var(--cat, var(--ink));
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

/* state ladder — order = priority: given < peer < same < selected < conflict */
.cell.given { background: var(--c-given); }
.cell.given .cell-glyph { filter: saturate(0.3) contrast(1.1); opacity: 0.85; } /* locked, inked */
.cell.filled .cell-glyph { opacity: 0.95; } /* the player's own cat — full color, lighter hand */
.cell.filled .notes { display: none; }
.cell.shake { animation: meow-shake 140ms ease; }
.cell.peer { background: var(--c-peer); }
.cell.same { background: var(--c-same); box-shadow: inset 0 0 0 2px var(--accent-soft); }
.cell.selected { background: var(--c-selected); box-shadow: inset 0 0 0 2.5px var(--accent); }
.cell.conflict { background: var(--c-bad-bg); box-shadow: inset 0 0 0 2px var(--c-bad); animation: meow-shake 140ms ease; }
.cell.placed .cell-glyph { animation: meow-pop 140ms ease-out; }

/* pencil notes: 3x3 micro-grid */
.notes {
  position: absolute;
  inset: 6%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}

.notes i {
  font-style: normal;
  font-size: clamp(6px, 1.1vh, 10px);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
}

.notes i svg { width: 90%; height: 90%; opacity: 0.75; }

/* notes sub-grid follows box dims: 3x3 boxes (9x9) default, 2x3 (6x6), 2x2 (4x4) */
.board[data-n="6"] .notes { grid-template-rows: repeat(2, 1fr); }
.board[data-n="4"] .notes {
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
}

/* ---------- keypad ---------- */
#pad {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  padding-bottom: 2px;
}

.pad-key {
  position: relative;
  width: clamp(42px, 9.6vw, 52px);
  height: clamp(46px, 10.5vw, 56px);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--press) ease, background var(--snap) ease;
}

.pad-key svg { width: 68%; height: 68%; pointer-events: none; }

.pad-key:active { transform: scale(0.9) translateY(1px); background: var(--surface-2); }

.pad-key[disabled] { opacity: 0.35; pointer-events: none; }

.pad-key.done { opacity: 0.35; filter: grayscale(0.65); } /* fully placed — dimmed, still tappable */

.pad-count {
  position: absolute;
  right: 3px;
  bottom: 2px;
  font-size: 0.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  pointer-events: none;
}

#btn-note, #btn-undo, #btn-hint, #btn-erase {
  height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  gap: 6px;
  transition: transform var(--press) ease, background var(--snap) ease, color var(--snap) ease;
}

#btn-note svg, #btn-undo svg, #btn-hint svg, #btn-erase svg { width: 18px; height: 18px; }

#btn-note:active, #btn-undo:active, #btn-hint:active, #btn-erase:active { transform: scale(0.92); }

#btn-note.active, #btn-note[aria-pressed="true"] { background: var(--accent); color: #FFF6EC; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  background: var(--ink);
  color: var(--paper);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--snap) ease, transform var(--snap) ease;
  z-index: 50;
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- win overlay ---------- */
.win {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(34, 28, 23, 0.55);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  padding: 24px;
  z-index: 100;
}

.win.show, .win.open { display: flex; }

.win-card {
  background: var(--paper);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 28px 26px calc(24px + env(safe-area-inset-bottom));
  width: min(100%, 340px);
  text-align: center;
  animation: meow-pop 140ms ease-out;
}

.win-card h2, #win-title { font-size: 1.5rem; font-weight: 800; letter-spacing: -0.02em; margin-bottom: 10px; }

#win-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

#win-stats > div { display: flex; flex-direction: column; gap: 2px; }

#win-stats b {
  font-size: 1.15rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

#win-stats span {
  font-size: 0.58rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-soft);
}

.win-card .stats {
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  margin-bottom: 20px;
}

#btn-again {
  width: 100%;
  height: 48px;
  border-radius: 14px;
  background: var(--accent);
  color: #FFF6EC;
  font-size: 1rem;
  font-weight: 700;
  transition: transform var(--press) ease, background var(--snap) ease;
}

#btn-again:active { transform: scale(0.96); background: var(--accent-deep); }

[hidden] { display: none !important; }

/* ---------- motion ---------- */
@keyframes meow-pop {
  0% { transform: scale(0.55); }
  60% { transform: scale(1.14); }
  100% { transform: scale(1); }
}

@keyframes meow-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  50% { transform: translateX(3px); }
  75% { transform: translateX(-2px); }
}

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