/* ---------- Themes ---------- */
:root, [data-theme="cozy"] {
  --paper: #FBF6EC;
  --paper-2: #F4EBDA;
  --card: #FFFDF8;
  --ink: #2B2522;
  --ink-soft: #6F655E;
  --ink-faint: #CFC3B1;
  --dot: #E2D6C2;
  --accent: #F28C6B;
  --good: #5FAF7A;
  --bad: #E06A6A;
  --c-memory: #FFC9B0;
  --c-math: #FFE08A;
  --c-logic: #CFC2F2;
  --c-spatial: #B9E6C9;
  --c-focus: #B5D8F5;
  --c-chem: #A8E0D6;
  --c-fun: #F8C3DD;
  --c-duo: #F6A9A0;
  --shadow: 3px 3px 0 var(--ink);
  color-scheme: light;
}
[data-theme="night"] {
  --paper: #1E2026;
  --paper-2: #262931;
  --card: #2A2D36;
  --ink: #EDE7DA;
  --ink-soft: #A7A196;
  --ink-faint: #4A4D57;
  --dot: #34373F;
  --accent: #FF9F7F;
  --good: #7FD39A;
  --bad: #FF8A8A;
  --c-memory: #8C5A48;
  --c-math: #8A7430;
  --c-logic: #5E5288;
  --c-spatial: #3F7A57;
  --c-focus: #3E6688;
  --c-chem: #357A6E;
  --c-fun: #8A4A6A;
  --c-duo: #8C4B45;
  --shadow: 3px 3px 0 #000;
  color-scheme: dark;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background-color: var(--paper);
  color: var(--ink);
  font-family: "Patrick Hand", "Noteworthy", "Chalkboard SE", "Comic Sans MS", cursive;
  font-size: 19px;
  line-height: 1.35;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
[data-font="clean"] body, body[data-font="clean"] { font-family: -apple-system, system-ui, "Segoe UI", sans-serif; font-size: 17px; }
[data-paper="dots"] body { background-image: radial-gradient(var(--dot) 1.4px, transparent 1.6px); background-size: 22px 22px; }
[data-paper="lines"] body { background-image: linear-gradient(var(--dot) 1px, transparent 1px); background-size: 100% 30px; }
[data-paper="grid"] body { background-image: linear-gradient(var(--dot) 1px, transparent 1px), linear-gradient(90deg, var(--dot) 1px, transparent 1px); background-size: 22px 22px; }

#app {
  max-width: 520px; margin: 0 auto; min-height: 100%;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 90px);
}
#app.no-nav { padding-bottom: calc(env(safe-area-inset-bottom) + 20px); }
h1, h2, h3 { font-weight: normal; margin: 0 0 8px; line-height: 1.1; }
h1 { font-size: 34px; } h2 { font-size: 26px; } h3 { font-size: 21px; }
p { margin: 0 0 10px; }
.muted { color: var(--ink-soft); }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; }
.stack > * + * { margin-top: 12px; }
.hidden { display: none !important; }

/* hand-drawn borders */
.sketch {
  border: 2.5px solid var(--ink);
  border-radius: 255px 18px 225px 18px / 18px 225px 18px 255px;
}
.sketch.alt { border-radius: 18px 225px 18px 255px / 255px 18px 225px 18px; }

.card {
  background: var(--card);
  padding: 16px;
  box-shadow: var(--shadow);
}

/* ---------- Buttons ---------- */
button { font: inherit; color: inherit; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 8px 18px;
  background: var(--card); color: var(--ink);
  border: 2.5px solid var(--ink);
  border-radius: 255px 18px 225px 18px / 18px 225px 18px 255px;
  box-shadow: var(--shadow);
  font-size: 20px; cursor: pointer;
  transition: transform .08s;
}
.btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }
.btn.primary { background: var(--accent); color: #2B2522; }
.btn.big { min-height: 64px; font-size: 26px; width: 100%; }
.btn.small { min-height: 38px; font-size: 17px; padding: 4px 12px; }
.btn.ghost { box-shadow: none; background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%;
  border: 2.5px solid var(--ink); background: var(--card);
  font-size: 22px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}

/* ---------- Top bar & nav ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.ink-count { display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px; background: var(--card); font-size: 20px; }
.ink-drop { width: 16px; height: 20px; display: inline-block; }
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  display: flex; justify-content: center; gap: 6px;
  background: var(--paper); border-top: 2.5px solid var(--ink);
}
.nav button {
  flex: 1; max-width: 120px; background: none; border: none; padding: 6px 2px;
  font-size: 16px; color: var(--ink-soft); display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.nav button .ico { font-size: 24px; line-height: 1; }
.nav button.on { color: var(--ink); }
.nav button.on .ico { transform: scale(1.15) rotate(-6deg); }

/* ---------- Companion ---------- */
.companion-wrap { display: flex; align-items: flex-end; gap: 10px; }
.companion { width: 120px; height: 120px; flex: none; animation: bob 3.2s ease-in-out infinite; }
.companion.lg { width: 180px; height: 180px; }
.companion.sm { width: 64px; height: 64px; }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-1deg); } 50% { transform: translateY(-4px) rotate(1deg); } }
.bubble {
  position: relative; background: var(--card); padding: 10px 14px; margin-bottom: 30px; flex: 1;
  font-size: 18px;
}
.bubble::after {
  content: ""; position: absolute; left: -12px; bottom: 14px; width: 16px; height: 16px;
  background: var(--card); border-left: 2.5px solid var(--ink); border-bottom: 2.5px solid var(--ink);
  transform: rotate(45deg) skew(10deg, 10deg);
}

/* ---------- Home ---------- */
.shelf-title { margin: 22px 0 10px; display: flex; align-items: center; gap: 8px; font-size: 22px; }
.shelf-title .tag { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--ink); }
.game-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.game-card {
  position: relative; padding: 12px; min-height: 120px; text-align: left; cursor: pointer;
  background: var(--card); box-shadow: var(--shadow);
  display: flex; flex-direction: column; justify-content: space-between;
}
.game-card:active { transform: translate(2px,2px); box-shadow: 1px 1px 0 var(--ink); }
.game-card .glyph {
  width: 54px; height: 54px; border-radius: 50%; border: 2.5px solid var(--ink);
  display: flex; align-items: center; justify-content: center; font-size: 22px; transform: rotate(-4deg);
  margin-bottom: 8px; white-space: nowrap;
}
.game-card .name { font-size: 20px; line-height: 1.05; }
.game-card .best { font-size: 15px; color: var(--ink-soft); }
.game-card .duo-tag { position: absolute; top: 10px; right: 10px; font-size: 14px; padding: 0 6px; border: 2px solid var(--ink); border-radius: 10px; }
.chaos-btn {
  width: 100%; padding: 16px 18px; text-align: left; cursor: pointer;
  background: var(--c-fun); box-shadow: var(--shadow); display: flex; align-items: center; gap: 14px;
}
.chaos-btn .bolt { font-size: 40px; transform: rotate(-10deg); }
.chaos-btn .t1 { font-size: 28px; line-height: 1; }
.chaos-btn .t2 { font-size: 16px; color: #2B2522; opacity: .75; }
[data-theme="night"] .chaos-btn .t2 { color: var(--ink); }
.quests { margin-top: 14px; padding: 12px 14px; background: var(--card); }
.quest { display: flex; align-items: center; gap: 10px; padding: 4px 0; font-size: 18px; }
.quest .box { width: 22px; height: 22px; border: 2.5px solid var(--ink); border-radius: 5px 8px 4px 7px; flex: none; display:flex; align-items:center; justify-content:center; font-size: 16px; }
.quest.done { color: var(--ink-soft); text-decoration: line-through; }

/* ---------- Game runner ---------- */
.game-top { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.game-top .title { flex: 1; font-size: 22px; }
.game-top .score { font-size: 22px; min-width: 40px; text-align: right; }
.stage { position: relative; min-height: 60vh; }
.info-line { text-align: center; color: var(--ink-soft); min-height: 26px; }
.timer-bar { height: 10px; border: 2px solid var(--ink); border-radius: 8px; overflow: hidden; margin: 6px 0 12px; background: var(--card); }
.timer-bar > div { height: 100%; background: var(--accent); width: 100%; transform-origin: left; }
.big-display { font-size: 46px; text-align: center; letter-spacing: 3px; padding: 26px 8px; min-height: 120px; display: flex; align-items: center; justify-content: center; word-break: break-all; }
.options { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.options.one { grid-template-columns: 1fr; }
.opt {
  min-height: 64px; padding: 10px; font-size: 22px; background: var(--card); cursor: pointer;
  border: 2.5px solid var(--ink); border-radius: 255px 18px 225px 18px / 18px 225px 18px 255px; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center; text-align: center;
}
.opt:active { transform: translate(2px,2px); box-shadow: 1px 1px 0 var(--ink); }
.opt.right { background: var(--good); color: #1f2a22; }
.opt.wrong { background: var(--bad); color: #2a1f1f; }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.keypad button { min-height: 58px; font-size: 26px; background: var(--card); border: 2.5px solid var(--ink); border-radius: 14px 18px 12px 16px; box-shadow: var(--shadow); cursor: pointer; }
.keypad button:active { transform: translate(2px,2px); box-shadow: 1px 1px 0 var(--ink); }
.answer-line { font-size: 36px; text-align: center; min-height: 54px; border-bottom: 2.5px dashed var(--ink-faint); margin: 8px 30px; letter-spacing: 2px; word-break: break-all; }
.flash-ok { animation: flashok .4s; }
.flash-bad { animation: shake .4s; }
@keyframes flashok { 0% { background: var(--good); } 100% { background: transparent; } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-8px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(5px); } }
.pop-in { animation: popin .25s ease-out; }
@keyframes popin { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* intro / result */
.intro { text-align: center; padding-top: 10px; }
.intro .glyph-lg { width: 96px; height: 96px; margin: 10px auto 14px; border-radius: 50%; border: 3px solid var(--ink); display: flex; align-items: center; justify-content: center; font-size: 38px; transform: rotate(-5deg); }
.result-score { font-size: 64px; line-height: 1; margin: 6px 0; }
.badge-new { display: inline-block; padding: 2px 10px; background: var(--c-math); color: #2B2522; border: 2px solid var(--ink); border-radius: 12px; transform: rotate(-3deg); }

/* ---------- Forms ---------- */
.field { display: block; margin: 10px 0; }
.field > span { display: block; margin-bottom: 6px; color: var(--ink-soft); }
input[type="text"] {
  width: 100%; font: inherit; font-size: 24px; padding: 10px 14px; color: var(--ink); background: var(--card);
  border: 2.5px solid var(--ink); border-radius: 255px 18px 225px 18px / 18px 225px 18px 255px; outline: none;
  -webkit-user-select: text; user-select: text;
}
textarea { width: 100%; min-height: 90px; font-family: monospace; font-size: 12px; background: var(--card); color: var(--ink); border: 2px solid var(--ink); border-radius: 10px; padding: 8px; -webkit-user-select: text; user-select: text; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg button { padding: 6px 14px; min-height: 42px; border: 2.5px solid var(--ink); background: var(--card); border-radius: 14px 18px 12px 16px; cursor: pointer; font-size: 18px; }
.seg button.on { background: var(--accent); color: #2B2522; }
.swatch { width: 38px; height: 38px; border-radius: 50%; border: 2.5px solid var(--ink); cursor: pointer; }
.swatch.on { outline: 3px dashed var(--ink); outline-offset: 3px; }

/* ---------- Hatch & species ---------- */
.hatch-blot { width: 220px; height: 220px; margin: 30px auto 10px; display: block; cursor: pointer; }
.hatch-blot.wiggle { animation: shake .4s; }
.species-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.species-card { background: var(--card); padding: 8px; text-align: center; cursor: pointer; box-shadow: var(--shadow); }
.species-card.on { background: var(--accent); }
.species-card svg { width: 100px; height: 100px; }

/* ---------- Shop / stats ---------- */
.shop-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.shop-item { background: var(--card); padding: 8px 4px; text-align: center; cursor: pointer; font-size: 16px; box-shadow: var(--shadow); }
.shop-item .ic { font-size: 30px; display: block; }
.shop-item.worn { background: var(--c-math); color: #2B2522; }
.radar { width: 100%; max-width: 340px; display: block; margin: 0 auto; }
.rec-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 2px dashed var(--ink-faint); }

/* toast */
.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 14px); transform: translateX(-50%); z-index: 50;
  background: var(--card); padding: 8px 16px; box-shadow: var(--shadow); font-size: 18px; animation: popin .25s;
  max-width: 90vw; text-align: center;
}

/* micro (chaos) */
.micro-instr { text-align: center; font-size: 34px; margin: 4px 0 8px; transform: rotate(-2deg); }
.micro-stage { position: relative; height: 56vh; min-height: 340px; overflow: hidden; display: flex; flex-direction: column; justify-content: center; gap: 12px; }
.micro-stage.mirror { transform: scaleX(-1); }
.micro-stage.upside { transform: rotate(180deg); }
.micro-stage.tiny .opt, .micro-stage.tiny .tap-thing { transform: scale(.65); }
.lives { font-size: 22px; letter-spacing: 2px; }
.tap-thing { position: absolute; display: flex; align-items: center; justify-content: center; cursor: pointer; font-size: 30px; border: 2.5px solid var(--ink); border-radius: 50%; background: var(--card); }

/* split duel */
.duel { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--paper); z-index: 30; }
.duel-half { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 14px; position: relative; }
.duel-half.top { transform: rotate(180deg); border-bottom: 3px dashed var(--ink); }
.duel-half .q { font-size: 30px; text-align: center; min-height: 44px; }
.duel-half .opts { display: flex; gap: 12px; width: 100%; max-width: 420px; }
.duel-half .opts .opt { flex: 1; }
.duel-score { font-size: 26px; }
.duel-quit { position: absolute; z-index: 31; right: 12px; top: 50%; transform: translateY(-50%); }

/* nonogram */
.nono { display: grid; gap: 0; margin: 0 auto; touch-action: none; }
.nono .cell { border: 1px solid var(--ink-faint); background: var(--card); aspect-ratio: 1; display: flex; align-items: center; justify-content: center; font-size: 18px; color: var(--ink-soft); }
.nono .cell.on { background: var(--ink); }
.nono .clue { font-size: 15px; display: flex; align-items: flex-end; justify-content: center; line-height: 1.05; color: var(--ink); text-align: center; padding: 2px; }
.nono .clue.row { justify-content: flex-end; align-items: center; padding-right: 5px; }
.nono .clue.done { color: var(--ink-faint); }
.nono .cell.b-r { border-right: 2px solid var(--ink); }
.nono .cell.b-b { border-bottom: 2px solid var(--ink); }

/* make 24 */
.m24-nums { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 16px 0; }
.m24-nums .opt { min-height: 86px; font-size: 34px; }
.m24-nums .opt.sel { background: var(--c-math); color: #2B2522; }
.m24-ops { display: flex; gap: 10px; justify-content: center; }
.m24-ops .opt { width: 64px; min-height: 60px; font-size: 30px; }
.m24-ops .opt.sel { background: var(--c-math); color: #2B2522; }

/* bubble wrap */
.bubbles { display: grid; gap: 8px; touch-action: manipulation; }
.bub { aspect-ratio: 1; border-radius: 50%; border: 2.5px solid var(--ink); cursor: pointer; position: relative; display: flex; align-items: center; justify-content: center; font-size: 14px; transition: transform .1s; }
.bub:not(.popped) { box-shadow: inset -4px -5px 0 rgba(0,0,0,.08), inset 4px 4px 0 rgba(255,255,255,.55); }
.bub.popped { transform: scale(.82); opacity: .35; border-style: dashed; }

/* svg game boards */
.board-svg { width: 100%; display: block; }
.opt-svg { padding: 6px; }
.opt-svg svg { width: 100%; height: auto; display: block; }

/* party (2–4 players on one phone) */
.party { position: fixed; inset: 0; z-index: 30; background: var(--paper); display: grid; touch-action: none; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }
.party.p2 { grid-template-rows: 1fr 1fr; }
.party.p3, .party.p4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.party.p3 .pzone:first-child { grid-column: 1 / 3; }
.pzone {
  position: relative; display: flex; flex-direction: column; margin: 5px; padding: 10px; overflow: hidden; min-width: 0;
  border: 3px solid var(--pc); border-radius: 18px 26px 16px 24px;
  background: color-mix(in srgb, var(--pc) 12%, var(--paper)); transition: background .2s, opacity .2s;
}
.pzone.flip { transform: rotate(180deg); }
.pzone.won { background: color-mix(in srgb, var(--pc) 50%, var(--paper)); }
.pzone.lost { opacity: .4; }
.pz-head { display: flex; justify-content: space-between; align-items: center; font-size: 19px; }
.pz-name::before { content: "●"; color: var(--pc); margin-right: 6px; }
.pz-score { font-size: 28px; }
.pz-body { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; min-height: 0; }
.pz-big { font-size: 34px; line-height: 1.1; }
.pz-opts { display: grid; gap: 8px; width: 100%; }
.pz-opts .opt { min-height: 54px; font-size: 22px; padding: 4px; }
.party-quit { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 31; width: 40px; height: 40px; font-size: 18px; }
.light { width: 110px; height: 110px; border-radius: 50%; border: 3px solid var(--ink); background: var(--bad); }
.track { position: relative; width: 100%; height: 46px; border: 2.5px solid var(--ink); border-radius: 12px; background: var(--card); overflow: hidden; }
.track .target { position: absolute; top: 0; bottom: 0; left: 50%; transform: translateX(-50%); background: var(--good); opacity: .55; }
.track .marker { position: absolute; top: 3px; bottom: 3px; width: 8px; margin-left: -4px; border-radius: 4px; background: var(--ink); }
.rope { position: relative; width: 46px; flex: 1; max-height: 260px; border-left: 3px dashed var(--ink-faint); margin-left: 23px; }
.rope .knot { position: absolute; left: -17px; width: 30px; height: 30px; border-radius: 50%; background: var(--pc); border: 3px solid var(--ink); margin-top: -15px; }

.new-tag { position: absolute; top: -10px; left: -6px; padding: 0 8px; font-size: 14px; background: var(--accent); color: #2B2522; border: 2px solid var(--ink); border-radius: 10px; transform: rotate(-8deg); z-index: 2; }
