:root {
  --font: ui-rounded, "SF Pro Rounded", "Avenir Next", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  --ink: #2c3f31;
  --ink-soft: #56695b;
  --cream: #fff8ea;
  --cream-2: #f6ecd6;
  --leaf: #3f6b4a;
  --leaf-deep: #2d4c36;
  --sand: #e3c48f;
  --terracotta: #c2643d;
  --gold: #f2c35a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  color-scheme: light;
  font-family: var(--font);
  color: var(--ink);
  background: #9cc27c;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { width: 100%; height: 100%; height: 100dvh; margin: 0; overflow: hidden; }
body { overscroll-behavior: none; touch-action: none; background: #9cc27c; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
#app { position: fixed; inset: 0; overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
[hidden] { display: none !important; }

#game-shell, #game { position: absolute; inset: 0; }
#game canvas { display: block; width: 100% !important; height: 100% !important; }
#tint { position: absolute; inset: 0; pointer-events: none; z-index: 2; mix-blend-mode: multiply; background-color: #fff; transition: background-color 1s linear; }
#flash { position: absolute; inset: 0; pointer-events: none; z-index: 25; background: #fff; opacity: 0; }
#flash.go { animation: flash .5s ease-out; }
#light {
  position: absolute; inset: 0; pointer-events: none; z-index: 2;
  background:
    radial-gradient(120% 80% at 18% 0%, rgba(255, 214, 140, .22), transparent 60%),
    radial-gradient(140% 100% at 50% 50%, transparent 62%, rgba(60, 50, 25, .22) 100%);
}

/* ---------- title / panels ---------- */
.panel-screen, .modal {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center;
  padding: calc(20px + var(--safe-top)) calc(16px + var(--safe-right)) calc(20px + var(--safe-bottom)) calc(16px + var(--safe-left));
  overflow: auto;
}
#start-screen {
  background:
    radial-gradient(90% 60% at 20% 10%, rgba(255, 230, 170, .75), transparent 60%),
    radial-gradient(70% 50% at 90% 90%, rgba(120, 170, 200, .45), transparent 60%),
    linear-gradient(170deg, #b8d69b 0%, #d9e5b4 45%, #f2d7a2 100%);
}
.modal { background: rgba(40, 55, 40, .45); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
#ending { background: rgba(40, 55, 40, .35); animation: fade .4s ease both; }

.title-card {
  width: min(100%, 440px);
  padding: 26px 22px 22px;
  border-radius: 28px;
  background: var(--cream);
  border: 2px solid #fff;
  box-shadow: 0 2px 0 rgba(255, 255, 255, .8) inset, 0 20px 50px rgba(45, 70, 50, .28), 0 0 0 6px rgba(255, 248, 234, .35);
  text-align: center;
  animation: rise .5s cubic-bezier(.2, .9, .3, 1.2) both;
}
.eyebrow { color: #7a8b69; text-transform: uppercase; letter-spacing: .16em; font-size: 11px; font-weight: 800; }
h1, h2 { margin: 10px 0 12px; color: var(--leaf-deep); line-height: .96; letter-spacing: -.03em; text-wrap: balance; }
h1 { font-size: clamp(38px, 11vw, 58px); }
h2 { font-size: clamp(30px, 8vw, 44px); }
.title-card p { margin: 14px auto 18px; max-width: 34ch; line-height: 1.45; color: var(--ink-soft); }
.title-card .tiny { margin: 12px 0 0; font-size: 11px; opacity: .7; }
.start-note { min-height: 1em; margin: 10px 0 0 !important; font-size: 13px; font-weight: 700; color: #7a8b69 !important; }
.family-row { display: flex; justify-content: center; gap: 6px; margin: 6px 0; }
.family-row canvas { width: 64px; height: 64px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 6px 14px rgba(45, 70, 50, .2); }
.family-row canvas:nth-child(odd) { transform: translateY(4px) rotate(-3deg); }
.family-row canvas:nth-child(even) { transform: rotate(3deg); }
.family-row.big canvas { width: 70px; height: 70px; }

.primary-button {
  min-height: 52px; border: 0; border-radius: 999px; padding: 0 26px;
  background: var(--leaf); color: #fff; font-weight: 800; font-size: 17px;
  box-shadow: 0 6px 0 var(--leaf-deep), 0 14px 22px rgba(39, 71, 49, .22);
  transition: transform .08s, box-shadow .08s;
}
.primary-button:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--leaf-deep); }
.secondary-button {
  min-height: 48px; border: 2px solid #e2d6bb; border-radius: 16px; padding: 0 16px;
  background: #fff; color: var(--ink); font-weight: 800; font-size: 15px;
  transition: transform .08s;
}
.secondary-button:active { transform: scale(.97); }
.restart-button { margin-top: 12px; min-height: 48px; font-size: 16px; }
.secondary-button.danger { color: #a3412a; border-color: #efcdbf; }
.menu-actions { display: grid; gap: 10px; margin: 16px 0; }
.reset-confirm p { margin: 4px 0 10px !important; font-size: 14px; }
.reset-confirm .row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.controls-help { font-size: 13px; }

/* ---------- HUD ---------- */
#hud { position: absolute; inset: 0; pointer-events: none; z-index: 8; }
#top-hud {
  position: absolute;
  top: calc(10px + var(--safe-top)); left: calc(10px + var(--safe-left)); right: calc(10px + var(--safe-right));
  display: flex; gap: 8px; justify-content: space-between; align-items: flex-start;
}
.glass-card {
  background: rgba(255, 248, 234, .92);
  border: 2px solid rgba(255, 255, 255, .9);
  border-radius: 18px;
  box-shadow: 0 6px 18px rgba(43, 68, 50, .18);
}
.quest-card { padding: 9px 12px 10px; min-width: 0; flex: 1 1 auto; max-width: 300px; }
.chapter-label { font-size: 10px; text-transform: uppercase; letter-spacing: .12em; font-weight: 800; color: #8a7a52; }
.quest-label { margin-top: 3px; font-size: 13px; line-height: 1.2; font-weight: 800; color: var(--ink); }
.quest-label.pop { animation: pop .5s ease; }
.pips { display: flex; gap: 5px; margin-top: 6px; }
.pip { width: 14px; height: 18px; border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%; background: rgba(120, 110, 90, .18); transform: rotate(-20deg); transition: background .3s, transform .3s; }
.pip.on { background: #fff; box-shadow: inset -3px 0 0 #e7d9b6, 0 0 0 2px var(--gold); transform: rotate(-20deg) scale(1.1); }
.top-right { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.character-switcher { pointer-events: auto; display: flex; gap: 2px; padding: 4px; }
.character-button {
  position: relative; width: 46px; height: 46px; padding: 3px; border: 0; border-radius: 14px; background: transparent;
  transition: transform .12s, background .2s;
}
.character-button canvas { width: 40px; height: 40px; display: block; border-radius: 50%; }
.character-button span { display: none; }
.character-button:active { transform: scale(.9); }
.character-button.active { background: #fff; box-shadow: inset 0 0 0 2px var(--gold); }
.mini-row { display: flex; gap: 6px; pointer-events: auto; }
.round-button {
  width: 44px; height: 44px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .9);
  background: rgba(255, 248, 234, .92); color: var(--ink-soft); display: grid; place-items: center;
  box-shadow: 0 4px 12px rgba(43, 68, 50, .15); transition: transform .1s;
}
.round-button svg { width: 22px; height: 22px; }
.round-button:active { transform: scale(.9); }
#mute-button .cross { display: none; }
#mute-button.muted .cross { display: block; }
#mute-button.muted .wave { display: none; }

.area-title {
  position: absolute; left: 0; right: 0; top: 24%; text-align: center; opacity: 0; pointer-events: none;
  color: #fff8ea; text-shadow: 0 2px 0 rgba(45, 60, 40, .45), 0 6px 22px rgba(30, 40, 25, .45);
}
.area-title .area-name { display: inline-block; max-width: calc(100% - 32px); font-weight: 900; font-size: clamp(20px, 6vw, 36px); letter-spacing: .2em; padding: 8px 20px 8px calc(20px + .2em); border-radius: 999px; background: rgba(44, 63, 49, .5); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
.area-title .area-sub { margin-top: 8px; font-weight: 700; font-size: 13px; letter-spacing: .12em; text-transform: uppercase; opacity: .9; }
.area-title.show { animation: title 3s ease both; }

.toast {
  position: absolute; left: 50%; top: calc(var(--safe-top) + 138px); transform: translate(-50%, -10px);
  padding: 9px 16px; border-radius: 999px; background: rgba(44, 63, 49, .92); color: #fff8ea;
  font-size: 14px; font-weight: 800; max-width: calc(100% - 32px); text-align: center; opacity: 0; transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.prompt {
  position: absolute; display: flex; align-items: center; gap: 8px;
  left: 50%; bottom: calc(26px + var(--safe-bottom)); transform: translate(-50%, 8px);
  padding: 6px 14px 6px 6px; border-radius: 999px;
  background: rgba(255, 248, 234, .95); border: 2px solid #fff; box-shadow: 0 6px 18px rgba(43, 68, 50, .2);
  font-weight: 800; font-size: 14px; white-space: nowrap; opacity: 0; transition: opacity .18s, transform .18s;
}
.prompt.show { opacity: 1; transform: translate(-50%, 0); }
.prompt-key { min-width: 28px; height: 28px; padding: 0 8px; border-radius: 9px; display: grid; place-items: center; background: var(--leaf); color: #fff; font-size: 13px; }
.prompt-key.verb { background: var(--gold); color: #4b3a14; }

/* touch controls */
.joy-zone { position: absolute; left: 0; bottom: 0; width: 55%; height: 48%; pointer-events: auto; touch-action: none; }
.joy-base {
  position: fixed; left: 0; top: 0; width: 120px; height: 120px; border-radius: 50%;
  background: rgba(255, 248, 234, .22); border: 2px solid rgba(255, 255, 255, .55);
  box-shadow: 0 8px 20px rgba(43, 68, 50, .14); transition: background .15s, opacity .2s; opacity: .75;
}
.joy-base.live { background: rgba(255, 248, 234, .32); opacity: 1; }
.joy-knob {
  position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px; border-radius: 50%;
  background: var(--cream); border: 2px solid #fff; box-shadow: 0 5px 14px rgba(43, 68, 50, .22);
}
.action-button {
  pointer-events: auto; position: absolute;
  right: calc(20px + var(--safe-right)); bottom: calc(28px + var(--safe-bottom));
  width: 84px; height: 84px; border-radius: 50%; border: 3px solid #fff;
  background: rgba(255, 248, 234, .82); color: #6b5a33;
  display: grid; place-items: center; align-content: center;
  box-shadow: 0 8px 20px rgba(84, 67, 38, .2); transition: transform .08s, background .2s;
}
.action-glyph { font-weight: 900; font-size: 26px; line-height: 1; }
.action-label { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; min-height: 0; }
.action-button.ready { background: var(--gold); color: #4b3a14; animation: ready 1.6s ease-in-out infinite; }
.action-button.pressed { transform: scale(.9); }

@media (pointer: coarse) {
  .prompt { left: auto; right: calc(14px + var(--safe-right)); bottom: calc(124px + var(--safe-bottom)); transform: translateY(8px); }
  .prompt.show { transform: translateY(0); }
}
@media (hover: hover) and (pointer: fine) {
  .joy-zone, .action-button { display: none; }
}

/* ---------- dialogue ---------- */
.dialogue {
  position: absolute; z-index: 20;
  left: calc(10px + var(--safe-left)); right: calc(10px + var(--safe-right)); bottom: calc(12px + var(--safe-bottom));
  max-width: 620px; margin: 0 auto; min-height: 112px; padding: 14px 16px 18px 14px;
  display: grid; grid-template-columns: 64px 1fr; gap: 12px; align-items: start;
  background: var(--cream); border: 2px solid #fff; border-radius: 22px;
  box-shadow: 0 16px 40px rgba(39, 62, 45, .3), 0 0 0 5px rgba(255, 248, 234, .35);
  opacity: 0; transform: translateY(16px); pointer-events: none; transition: opacity .18s, transform .22s cubic-bezier(.2, .9, .3, 1.2);
}
.dialogue.show { opacity: 1; transform: translateY(0); pointer-events: auto; }
.dialogue.narration { grid-template-columns: 1fr; background: #fbf3df; }
.dialogue.narration .dialogue-portrait { display: none; }
.dialogue.narration .dialogue-text { font-style: italic; color: #5b5a48; }
.dialogue-portrait canvas { width: 64px; height: 64px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 4px 10px rgba(45, 70, 50, .2); }
.dialogue-name { font-weight: 900; color: var(--leaf); font-size: 15px; }
.dialogue-text { margin-top: 3px; line-height: 1.38; color: var(--ink); font-size: 16px; min-height: 2.8em; }
.dialogue-more { position: absolute; right: 16px; bottom: 8px; width: 0; height: 0; border-left: 7px solid transparent; border-right: 7px solid transparent; border-top: 9px solid var(--terracotta); opacity: 0; }
.dialogue.done .dialogue-more { opacity: 1; animation: nudge .9s ease-in-out infinite; }
#app.talking .joy-zone, #app.talking .action-button, #app.talking .prompt { opacity: 0; pointer-events: none; }
#app.fishing .joy-zone { opacity: .25; pointer-events: none; }
#app.talking .joy-base { opacity: 0; }

/* ---------- catch card ---------- */
.catch-card {
  position: absolute; z-index: 21; left: 50%; top: 20%; width: min(300px, calc(100% - 40px));
  padding: 14px 16px 16px; border-radius: 22px; background: var(--cream); border: 2px solid #fff; text-align: center;
  box-shadow: 0 18px 40px rgba(39, 62, 45, .3), 0 0 0 5px rgba(255, 248, 234, .35);
  opacity: 0; transform: translate(-50%, 16px) scale(.94); pointer-events: none; transition: opacity .2s, transform .3s cubic-bezier(.2, .9, .3, 1.3);
}
.catch-card.show { opacity: 1; transform: translate(-50%, 0) scale(1); pointer-events: auto; }
.catch-art { display: grid; place-items: center; height: 90px; background: radial-gradient(closest-side, rgba(95, 167, 182, .28), transparent); }
.catch-art canvas { width: 150px; height: 94px; }
.catch-name { font-weight: 900; font-size: 20px; color: var(--leaf-deep); }
.catch-meta { margin-top: 2px; font-size: 13px; font-weight: 800; color: #8a7a52; }
.catch-meta .new { display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 999px; background: var(--terracotta); color: #fff; font-size: 11px; letter-spacing: .06em; }
.catch-blurb { margin: 8px 0 0; font-size: 14px; line-height: 1.35; color: var(--ink-soft); }

/* ---------- album ---------- */
.album-card { width: min(100%, 560px); max-height: calc(100dvh - 40px); display: flex; flex-direction: column; text-align: left; padding: 18px 16px 16px; }
.album-head { display: flex; align-items: center; justify-content: space-between; }
.album-head h2 { margin: 0; font-size: 28px; }
.tabs { display: flex; gap: 6px; margin: 12px 0 10px; }
.tab { min-height: 44px; flex: 1; border: 2px solid #eadfc6; background: #fff; border-radius: 14px; font-weight: 800; color: var(--ink-soft); }
.tab.active { background: var(--leaf); border-color: var(--leaf); color: #fff; }
.album-body { overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y; min-height: 200px; }
.album-empty { padding: 30px 10px; text-align: center; color: var(--ink-soft); }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; padding: 4px; }
.polaroid { background: #fff; padding: 7px 7px 10px; border-radius: 4px; box-shadow: 0 6px 14px rgba(45, 60, 40, .18); transform: rotate(var(--r, -1.5deg)); }
.polaroid img { width: 100%; display: block; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 2px; background: #ddd; }
.polaroid figcaption { margin-top: 6px; font-size: 12px; font-weight: 800; color: var(--ink-soft); text-align: center; }
.log-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; padding: 4px; }
.log-item { background: #fff; border: 2px solid #eee3cb; border-radius: 16px; padding: 8px; text-align: center; }
.log-item canvas { width: 100%; max-width: 130px; height: auto; }
.log-item b { display: block; font-size: 13px; color: var(--ink); }
.log-item small { font-size: 11px; color: #8a7a52; font-weight: 700; }
.log-item.unknown b { color: #b0a48a; }
.log-summary { margin: 0 4px 10px; font-weight: 800; color: #8a7a52; font-size: 13px; }
.menu-actions .row.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ---------- karting ---------- */
#app.karting #top-hud, #app.karting .prompt, #app.karting .action-button, #app.karting #light { display: none; }
#app.karting #tint { background-color: #fff !important; }
.kart-hud { position: absolute; z-index: 9; top: calc(10px + var(--safe-top)); left: calc(10px + var(--safe-left)); right: calc(10px + var(--safe-right)); display: flex; align-items: flex-start; gap: 10px; pointer-events: none; }
.kart-pos { font-weight: 900; font-size: clamp(44px, 12vw, 64px); line-height: .9; color: #fff; text-shadow: 0 3px 0 #c2643d, 0 6px 16px rgba(0, 0, 0, .35); }
.kart-pos sup { font-size: .42em; }
.kart-box { margin-left: auto; display: grid; justify-items: end; gap: 1px; padding: 8px 12px; border-radius: 14px; background: rgba(20, 22, 27, .78); color: #fff; font-variant-numeric: tabular-nums; }
.kart-box b { font-size: 18px; } .kart-box span { font-size: 14px; } .kart-box small { font-size: 11px; color: #39d98a; font-weight: 800; }
.kart-leave { pointer-events: auto; }
.kart-fpv { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 1; display: block; }
.kart-cam { pointer-events: auto; min-height: 48px; padding: 5px 14px; border-radius: 14px; border: 2px solid rgba(255, 255, 255, .5); background: rgba(20, 22, 27, .78); color: #fff; display: grid; justify-items: center; transition: transform .08s; }
.kart-cam:active { transform: scale(.94); }
.kart-cam span { font-size: 10px; letter-spacing: .12em; text-transform: uppercase; opacity: .75; font-weight: 700; }
.kart-cam b { font-size: 15px; }
.kart-lights { position: absolute; z-index: 9; left: 50%; top: 30%; transform: translateX(-50%); display: flex; gap: 10px; padding: 12px 14px; border-radius: 16px; background: #15171b; box-shadow: 0 10px 30px rgba(0, 0, 0, .4); }
.kart-lights i { width: 34px; height: 34px; border-radius: 50%; background: #3a1414; box-shadow: inset 0 0 0 3px #2a2c31; transition: background .08s; }
.kart-lights i.on { background: #ff3b30; box-shadow: 0 0 18px #ff3b30, inset 0 0 0 3px #2a2c31; }
.kart-lights i.go { background: #39d98a; box-shadow: 0 0 18px #39d98a, inset 0 0 0 3px #2a2c31; }
.kart-card { width: min(100%, 420px); }
.kart-standings { display: grid; gap: 6px; margin: 10px 0; }
.kart-row { display: grid; grid-template-columns: 44px 40px 1fr auto; align-items: center; gap: 10px; padding: 6px 12px; border-radius: 14px; background: #fff; text-align: left; font-variant-numeric: tabular-nums; }
.kart-row.me { outline: 3px solid var(--gold); }
.kart-row b { font-size: 18px; color: var(--terracotta); } .kart-row canvas { width: 40px; height: 40px; border-radius: 50%; } .kart-row span { font-weight: 800; } .kart-row small { color: var(--ink-soft); font-weight: 700; }
.kart-sub { margin: 6px auto 14px !important; font-weight: 700; }
.kart-buttons { display: grid; gap: 10px; }

/* ---------- wider screens ---------- */
@media (min-width: 760px) {
  .character-button { width: 58px; height: 62px; }
  .character-button canvas { width: 44px; height: 44px; margin: 0 auto; }
  .character-button span { display: block; margin-top: 1px; font-size: 9px; font-weight: 800; color: var(--ink-soft); }
  .quest-card { padding: 12px 16px; }
  .quest-label { font-size: 15px; }
}
@media (max-width: 380px) {
  .character-button { width: 44px; height: 44px; }
  .character-button canvas { width: 36px; height: 36px; }
  .quest-label { font-size: 12px; }
}
/* landscape phones: tighten vertical space */
@media (orientation: landscape) and (max-height: 520px) {
  .area-title { top: 18%; }
  .action-button { width: 72px; height: 72px; bottom: calc(14px + var(--safe-bottom)); }
  .joy-zone { height: 60%; width: 45%; }
  .dialogue { min-height: 90px; padding: 10px 14px 14px 10px; grid-template-columns: 52px 1fr; }
  .dialogue-portrait canvas { width: 52px; height: 52px; }
  .dialogue-text { font-size: 15px; }
  .toast { top: calc(var(--safe-top) + 14px); }
  .top-right { flex-direction: row-reverse; align-items: flex-start; }
}

/* ---------- motion ---------- */
@keyframes rise { from { opacity: 0; transform: translateY(18px) scale(.97); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.05); color: var(--terracotta); } 100% { transform: scale(1); } }
@keyframes title { 0% { opacity: 0; transform: translateY(8px); letter-spacing: .3em; } 15% { opacity: 1; transform: none; } 75% { opacity: 1; } 100% { opacity: 0; transform: translateY(-6px); } }
@keyframes ready { 0%, 100% { box-shadow: 0 8px 20px rgba(84, 67, 38, .2), 0 0 0 0 rgba(242, 195, 90, .55); } 50% { box-shadow: 0 8px 20px rgba(84, 67, 38, .2), 0 0 0 10px rgba(242, 195, 90, 0); } }
@keyframes flash { 0% { opacity: .9; } 100% { opacity: 0; } }
@keyframes nudge { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(3px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
