:root {
  --ink: #2B2130;
  --paper: #FFFDF6;
  --muted: #6B6270;
  --line: #2B2130;
  --accent: #FBE70C;
  --accent-ink: #171318;
  --accent-soft: #FFF9B0;
  --second: #0C5CB6;
  --pixel: "Pixelify Sans", ui-rounded, system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #86D05E; color: var(--ink); }
body { font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-tap-highlight-color: transparent; }
canvas#town { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; outline: none; touch-action: none; image-rendering: pixelated; }

button, input { font: inherit; color: inherit; }
a { color: inherit; }

/* ---------- hud ---------- */
.hud {
  position: fixed; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: max(12px, env(safe-area-inset-top)) 16px 0;
  pointer-events: none;
}
.hud > * { pointer-events: auto; }
.spacer { flex: 1; }
.chip {
  font-family: var(--pixel); font-weight: 500; font-size: 15px;
  background: #fff; border: 2px solid var(--line); box-shadow: 3px 3px 0 rgba(43,33,48,.25);
  padding: 5px 11px; text-decoration: none; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px;
}
.chip.brand { font-weight: 700; background: var(--accent); color: var(--accent-ink); }
#online::before { content: ""; width: 8px; height: 8px; background: #B5ADB8; display: inline-block; }
#online.live::before { background: #2E9E6A; box-shadow: 0 0 0 2px #C9F2DA; }
.btn-chip { cursor: pointer; }
.btn-chip:hover { transform: translate(-1px,-1px); box-shadow: 4px 4px 0 rgba(43,33,48,.3); }
.btn-chip.accent { background: var(--ink); color: #fff; }

/* ---------- chat ---------- */
.chat { position: fixed; left: 16px; bottom: 16px; z-index: 4; width: min(360px, calc(100vw - 32px)); pointer-events: none; }
#chatLog { list-style: none; margin: 0 0 8px; padding: 0; display: flex; flex-direction: column; gap: 4px; }
#chatLog li {
  align-self: flex-start; max-width: 100%;
  background: rgba(255,255,255,.92); border: 2px solid var(--line); padding: 3px 8px; font-size: 14px;
  transition: opacity 1s;
}
#chatLog li.old { opacity: 0; }
#chatLog b { font-family: var(--pixel); }
#chatLog b.mine { background: var(--accent); color: var(--accent-ink); padding: 0 3px; }
#chatBox { pointer-events: auto; }
#chatInput {
  width: 100%; border: 2px solid var(--line); background: rgba(255,255,255,.95); padding: 9px 12px;
  box-shadow: 3px 3px 0 rgba(43,33,48,.25); outline: none;
}
#chatInput:focus { background: #fff; box-shadow: 3px 3px 0 var(--ink); }

/* ---------- toasts ---------- */
#toasts { position: fixed; top: 70px; left: 50%; transform: translateX(-50%); z-index: 20; display: flex; flex-direction: column; gap: 8px; align-items: center; width: calc(100vw - 32px); pointer-events: none; }
.toast {
  font-family: var(--pixel); font-size: 16px; background: #fff; border: 2px solid var(--line);
  box-shadow: 4px 4px 0 rgba(43,33,48,.3); padding: 8px 14px; text-align: center; max-width: 520px;
  animation: pop .25s ease-out; transition: opacity .5s, transform .5s;
}
.toast.big { background: var(--accent); color: var(--accent-ink); font-size: 19px; font-weight: 700; padding: 12px 18px; }
.toast.out { opacity: 0; transform: translateY(-8px); }
@keyframes pop { from { transform: scale(.8); opacity: 0; } }

.confetti { position: fixed; top: -12px; width: 9px; height: 9px; z-index: 30; pointer-events: none; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(540deg); } }

/* ---------- talk box ---------- */
#dialog {
  position: fixed; left: 50%; bottom: max(18px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 15;
  width: min(680px, calc(100vw - 32px)); min-height: 108px;
  background: #fff; border: 3px solid var(--line); box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--line), 6px 10px 0 6px rgba(43,33,48,.25);
  padding: 22px 22px 18px; cursor: pointer;
}
#dialogName {
  position: absolute; top: -16px; left: 16px; font-family: var(--pixel); font-weight: 700; font-size: 16px;
  background: var(--accent); color: var(--accent-ink); border: 3px solid var(--line); padding: 1px 10px;
}
#dialogText { margin: 0; font-family: var(--pixel); font-size: 20px; line-height: 1.4; }
#dialogMore { position: absolute; right: 14px; bottom: 8px; font-family: var(--pixel); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- modals ---------- */
.modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 16px; background: rgba(43,33,48,.28); }
.modal[hidden], #dialog[hidden], #joy[hidden], [hidden] { display: none !important; }
.sheet {
  width: min(560px, 100%); max-height: calc(100dvh - 32px); overflow: auto;
  background: var(--paper); border: 3px solid var(--line); box-shadow: 8px 8px 0 rgba(43,33,48,.35);
  padding: 24px 24px 20px; animation: pop .18s ease-out;
}
.sheet h2 { font-family: var(--pixel); font-weight: 700; font-size: 28px; line-height: 1.1; margin: 4px 0 6px; }
.sheet h2 small { font-size: 15px; color: var(--muted); font-weight: 500; }
.sheet h3 { font-family: var(--pixel); font-weight: 700; font-size: 17px; margin: 20px 0 8px; display: flex; justify-content: space-between; align-items: baseline; }
.sheet h3 small { color: var(--muted); font-weight: 500; }
.sheet p { margin: 8px 0; }
.sheet .icon { display: inline-block; margin-right: 4px; }
.eyebrow { font-family: var(--pixel); font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.tagline { font-weight: 600; }
.fine { font-size: 12.5px; color: var(--muted); margin-top: 14px !important; }
.empty { color: var(--muted); font-style: italic; }

.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 20px; }
.btn {
  font-family: var(--pixel); font-weight: 700; font-size: 17px; cursor: pointer; text-decoration: none;
  background: var(--accent); color: var(--accent-ink); border: 3px solid var(--line); padding: 8px 16px;
  box-shadow: 4px 4px 0 var(--line); display: inline-flex; align-items: center; gap: 6px;
}
.btn:hover { transform: translate(-1px,-1px); box-shadow: 5px 5px 0 var(--line); }
.btn:active { transform: translate(3px,3px); box-shadow: 1px 1px 0 var(--line); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: 4px 4px 0 var(--line); }
.btn.ghost { background: #fff; color: var(--ink); }
.link { background: none; border: 0; padding: 0; cursor: pointer; font-family: var(--pixel); font-size: 14px; text-decoration: underline; text-underline-offset: 3px; white-space: nowrap; }

.quests { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.quests label { display: flex; gap: 10px; align-items: flex-start; background: #fff; border: 2px solid var(--line); padding: 9px 11px; cursor: pointer; }
.quests input, .check input { appearance: none; flex: none; width: 20px; height: 20px; border: 2px solid var(--line); background: #fff; margin: 1px 0 0; cursor: pointer; display: grid; place-items: center; }
.quests input:checked, .check input:checked { background: var(--accent); }
.quests input:checked::after, .check input:checked::after { content: "✓"; font-weight: 800; font-size: 14px; line-height: 1; color: var(--accent-ink); }
.quests input:checked + span { text-decoration: line-through; color: var(--muted); }
.links { display: grid; gap: 6px; }
.links a { background: var(--accent-soft); border: 2px solid var(--line); padding: 8px 11px; font-weight: 600; text-decoration: none; }
.links a:hover { background: #fff; }

.field { display: grid; gap: 4px; margin: 0 0 12px; }
.field span { font-family: var(--pixel); font-weight: 700; font-size: 15px; }
.field em { font-style: normal; font-weight: 400; color: var(--muted); }
.field input { border: 2px solid var(--line); background: #fff; padding: 9px 11px; outline: none; width: 100%; }
.field input:focus { box-shadow: 3px 3px 0 var(--accent), 3px 3px 0 2px var(--line); }
.check { display: flex; gap: 10px; align-items: center; font-weight: 600; cursor: pointer; }

.ob-grid { display: grid; grid-template-columns: 150px 1fr; gap: 18px; margin-top: 16px; }
.ob-look { display: grid; justify-items: center; gap: 8px; background: #fff; border: 2px solid var(--line); padding: 10px 8px; align-content: start; }
.ob-look canvas, .card-head canvas { image-rendering: pixelated; background: #A8DE86; border: 2px solid var(--line); }
.look-row { display: grid; grid-template-columns: 26px 1fr 26px; align-items: center; width: 100%; text-align: center; font-family: var(--pixel); font-size: 14px; }
.look-row button { border: 2px solid var(--line); background: var(--accent); color: var(--accent-ink); cursor: pointer; padding: 0; height: 24px; font-size: 11px; }
@media (max-width: 520px) {
  .ob-grid { grid-template-columns: 1fr; }
  .ob-look { grid-template-columns: auto 1fr; justify-items: stretch; }
  .sheet { padding: 20px 16px 16px; }
  .sheet h2 { font-size: 24px; }
}

.card-head { display: flex; gap: 16px; align-items: center; }
.card-head canvas { width: 72px; height: 84px; flex: none; }

.ladder { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin: 14px 0 4px; }
.ladder div { text-align: center; border: 2px solid #CFC8D2; background: #fff; padding: 6px 2px; opacity: .55; }
.ladder div.on { opacity: 1; border-color: var(--line); }
.ladder div.now { background: var(--accent); color: var(--accent-ink); box-shadow: 3px 3px 0 var(--line); }
.ladder span { display: block; font-size: 24px; }
.ladder small { font-family: var(--pixel); font-size: 12px; }
.bar { height: 12px; border: 2px solid var(--line); background: #fff; margin: 8px 0; }
.bar.small { height: 9px; margin: 0 0 8px; }
.bar i { display: block; height: 100%; background: var(--accent); border-right: 2px solid var(--line); }
.bar i[style="width:0%"] { border: 0; }
.phase ul { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.phase li { display: flex; justify-content: space-between; gap: 10px; align-items: center; background: #fff; border: 2px solid var(--line); padding: 7px 10px; }
.phase li small { color: var(--muted); font-family: var(--pixel); }

.people, .wins { list-style: none; padding: 0; margin: 10px 0; display: grid; gap: 6px; }
.person { width: 100%; text-align: left; display: grid; grid-template-columns: 30px 1fr; column-gap: 8px; background: #fff; border: 2px solid var(--line); padding: 7px 10px; cursor: pointer; }
.person .pet { grid-row: span 2; font-size: 22px; align-self: center; }
.person b { font-family: var(--pixel); }
.person small, .wins small { color: var(--muted); }
.wins li { background: #fff; border: 2px solid var(--line); padding: 8px 11px; display: grid; }
.wins b { font-family: var(--pixel); }

/* ---------- touch ---------- */
.touch-controls { display: none; }
body.touch .touch-controls { display: block; }
body.touch .map-btn { display: inline-flex; }
.round {
  position: fixed; z-index: 6; width: 54px; height: 54px; border-radius: 50%;
  border: 3px solid var(--line); background: #fff; box-shadow: 3px 3px 0 rgba(43,33,48,.3);
  font-family: var(--pixel); font-weight: 700; font-size: 22px; touch-action: manipulation;
}
#btnA { right: 20px; bottom: max(26px, env(safe-area-inset-bottom)); width: 72px; height: 72px; background: var(--accent); color: var(--accent-ink); font-size: 30px; }
#chatBtn { right: 102px; bottom: max(30px, env(safe-area-inset-bottom)); }
#joy { position: fixed; z-index: 6; width: 110px; height: 110px; margin: -55px 0 0 -55px; border-radius: 50%; border: 3px solid rgba(43,33,48,.5); background: rgba(255,255,255,.25); pointer-events: none; }
#joyKnob { position: absolute; left: 50%; top: 50%; width: 46px; height: 46px; margin: -23px 0 0 -23px; border-radius: 50%; background: var(--accent); border: 3px solid var(--line); }
body.touch .chat { bottom: 112px; }
body.touch #chatBox[hidden] { display: none; }
body.touch .hud .chip { font-size: 13px; padding: 4px 8px; }
@media (max-width: 560px) {
  .hud { gap: 6px; }
  .hud .spacer { flex-basis: 100%; height: 0; }
  #online { display: none; }
  .chip { font-size: 13px; padding: 4px 8px; }
}
@media (max-width: 560px) { #toasts { top: 104px; } }

/* ---------- v2: help, zoom, choices, battle, fade ---------- */
.zoom { display: inline-flex; gap: 4px; }
.zoom .chip { width: 34px; justify-content: center; padding: 5px 0; font-weight: 700; }
#help {
  position: fixed; top: 64px; left: 16px; z-index: 4; font-family: var(--pixel); font-size: 13px; line-height: 1.9;
  background: rgba(255,255,255,.92); border: 2px solid var(--line); box-shadow: 3px 3px 0 rgba(43,33,48,.25); padding: 6px 30px 6px 10px;
}
#help kbd { font-family: var(--pixel); font-size: 11px; background: #fff; border: 1.5px solid var(--line); border-bottom-width: 3px; padding: 0 4px; margin: 0 2px 0 6px; }
#help div > kbd:first-child { margin-left: 0; }
#helpClose { position: absolute; top: 2px; right: 4px; background: none; border: 0; cursor: pointer; font-size: 12px; }

#dialogChoices {
  position: absolute; right: 18px; bottom: calc(100% + 10px); display: grid; min-width: 120px;
  background: #fff; border: 3px solid var(--line); box-shadow: 0 0 0 3px #fff, 0 0 0 6px var(--line); padding: 6px 4px;
}
#dialogChoices button { font-family: var(--pixel); font-size: 19px; background: none; border: 0; text-align: left; padding: 3px 10px; cursor: pointer; }
#dialogChoices button::before { content: "▶ "; visibility: hidden; }
#dialogChoices button.on::before { visibility: visible; }
#dialogChoices button:hover { background: var(--accent-soft); }

#battle { position: fixed; inset: 0; z-index: 25; display: grid; place-items: center; background: rgba(43,33,48,.35); padding: 16px; }
.battle-frame { width: min(720px, 100%); border: 4px solid var(--line); background: #fff; box-shadow: 8px 8px 0 rgba(43,33,48,.4); animation: pop .2s ease-out; }
#battleCv { display: block; width: 100%; aspect-ratio: 240 / 150; image-rendering: pixelated; }
#battleBox { position: relative; border-top: 4px solid var(--line); background: #FFFDF6; padding: 14px 18px 16px; min-height: 96px; cursor: pointer; }
#battleText { margin: 0; font-family: var(--pixel); font-size: 20px; line-height: 1.35; }
#battleMore { position: absolute; right: 14px; bottom: 8px; color: #D9473F; font-family: var(--pixel); animation: blink 1s steps(2) infinite; }
#battleMenu { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
#battleMenu button {
  font-family: var(--pixel); font-size: 15px; text-align: left; line-height: 1.25; cursor: pointer;
  background: #fff; border: 3px solid var(--line); padding: 8px 10px 8px 24px; position: relative;
}
#battleMenu button.on, #battleMenu button:hover { background: var(--accent-soft); }
#battleMenu button.on::before { content: "▶"; position: absolute; left: 7px; font-size: 11px; top: 50%; transform: translateY(-50%); }
#battleMenu button.run { grid-column: span 2; text-align: center; padding-left: 10px; background: #F2EFE8; }
@media (max-width: 560px) {
  #battleMenu { grid-template-columns: 1fr; }
  #battleMenu button.run { grid-column: auto; }
  #battleText { font-size: 17px; }
  #help { display: none; }
}

#fade { position: fixed; inset: 0; z-index: 30; background: #FFFDF6; opacity: 0; pointer-events: none; transition: opacity .2s; }
#fade.on { opacity: 1; }

.ladder canvas { display: block; margin: 0 auto; width: 40px; height: 40px; image-rendering: pixelated; }
.stats { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 4px; }
.stats span { font-family: var(--pixel); font-size: 14px; background: #fff; border: 2px solid var(--line); padding: 3px 8px; }
#chatLog .pet { font-size: 12px; }
