@font-face { font-family: 'Pixelify Sans'; src: url(../fonts/pixelify-latin.woff2) format('woff2'); font-weight: 400 700; font-display: swap; }
@font-face { font-family: 'Press Start 2P'; src: url(../fonts/pressstart-latin.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Mitr'; src: url(../fonts/mitr-400-thai.woff2) format('woff2'); font-weight: 400; unicode-range: U+0E01-0E5B, U+200C-200D, U+25CC; font-display: swap; }
@font-face { font-family: 'Mitr'; src: url(../fonts/mitr-400-latin.woff2) format('woff2'); font-weight: 400; unicode-range: U+0000-00FF, U+2000-206F; font-display: swap; }
@font-face { font-family: 'Mitr'; src: url(../fonts/mitr-500-thai.woff2) format('woff2'); font-weight: 500; unicode-range: U+0E01-0E5B, U+200C-200D, U+25CC; font-display: swap; }
@font-face { font-family: 'Mitr'; src: url(../fonts/mitr-500-latin.woff2) format('woff2'); font-weight: 500; unicode-range: U+0000-00FF, U+2000-206F; font-display: swap; }

:root {
  --bg: #120d24;
  --bg2: #1c1438;
  --panel: #241a46;
  --panel-hi: #2f2358;
  --ink: #07050f;
  --edge: #4a3a86;
  --text: #f4ecd8;
  --muted: #a99fd0;
  --gold: #f2c94c;
  --gold-dk: #b4882a;
  --xp: #5fe08a;
  --xp-dk: #2f9a55;
  --danger: #e0445a;
  --done: #3f8f5e;
  --ui: 'Pixelify Sans', 'Mitr', system-ui, sans-serif;
  --pixel: 'Press Start 2P', 'Mitr', monospace;
  --thai: 'Mitr', system-ui, sans-serif;
  --px: 4px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
html { background: var(--bg); }
body {
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg2) 60%, #2a1838 100%) fixed;
  color: var(--text);
  font-family: var(--ui);
  font-size: 17px;
  line-height: 1.4;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }

.stars {
  position: fixed; inset: 0; pointer-events: none; z-index: 0; opacity: .7;
  background-image:
    radial-gradient(1px 1px at 12% 8%, #fff 99%, transparent),
    radial-gradient(1px 1px at 78% 14%, #fff 99%, transparent),
    radial-gradient(2px 2px at 40% 22%, #f2c94c 99%, transparent),
    radial-gradient(1px 1px at 88% 40%, #fff 99%, transparent),
    radial-gradient(1px 1px at 6% 52%, #fff 99%, transparent),
    radial-gradient(2px 2px at 64% 66%, #b7a8ff 99%, transparent),
    radial-gradient(1px 1px at 24% 80%, #fff 99%, transparent),
    radial-gradient(1px 1px at 92% 88%, #fff 99%, transparent);
  animation: twinkle 4s steps(2) infinite;
}
@keyframes twinkle { 50% { opacity: .35; } }

#app {
  position: relative; z-index: 1;
  max-width: 480px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 32px);
}

/* Pixel frame: notched corners via stacked shadows. */
.px {
  background: var(--panel);
  box-shadow:
    0 calc(var(--px) * -1) 0 0 var(--edge), 0 var(--px) 0 0 var(--edge),
    calc(var(--px) * -1) 0 0 0 var(--edge), var(--px) 0 0 0 var(--edge),
    0 calc(var(--px) * 2) 0 0 var(--ink);
  margin: var(--px);
}
.px.gold { --edge: var(--gold); }

.btn {
  display: block; width: calc(100% - 8px); border: 0; padding: 14px 16px;
  background: var(--gold); color: var(--ink); --edge: var(--gold-dk);
  font-family: var(--pixel); font-size: 12px; letter-spacing: .5px; text-transform: uppercase;
  transition: transform .06s;
}
.btn:active { transform: translateY(3px); }
.btn.ghost { background: var(--panel-hi); color: var(--text); --edge: var(--edge); }
.btn.danger { background: var(--danger); color: #fff; --edge: #8a1a2c; }
.btn[disabled] { opacity: .45; }

/* Header */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 14px; }
.trip { font-family: var(--pixel); font-size: 9px; line-height: 1.6; color: var(--gold); letter-spacing: .5px; }
.trip small { display: block; font-family: var(--thai); font-size: 12px; color: var(--muted); letter-spacing: 0; }
.icon-btn { background: var(--panel); border: 0; width: 40px; height: 40px; font-size: 18px; --edge: var(--edge); flex: none; }

/* Hero panel */
.hero { padding: 16px; display: grid; grid-template-columns: 116px 1fr; gap: 14px; align-items: center; }
.avatar { width: 116px; height: 116px; background: radial-gradient(circle at 50% 60%, #3a2c70 0, #241a46 70%); position: relative; }
.avatar svg { width: 100%; height: 100%; display: block; animation: bob 1.6s steps(2) infinite; }
@keyframes bob { 50% { transform: translateY(-3px); } }
.hero-name { font-family: var(--pixel); font-size: 13px; line-height: 1.5; word-break: break-word; }
.hero-meta { color: var(--muted); font-size: 15px; }
.hero-title { color: var(--gold); font-size: 16px; font-weight: 700; margin-top: 2px; }
.lv { display: inline-block; background: var(--gold); color: var(--ink); font-family: var(--pixel); font-size: 9px; padding: 4px 6px; margin-right: 6px; vertical-align: 2px; }

.xpbar { grid-column: 1 / -1; }
.xpbar-track { height: 16px; background: var(--ink); padding: 3px; }
.xpbar-fill { height: 100%; background: linear-gradient(180deg, var(--xp) 0 55%, var(--xp-dk) 55%); transition: width .6s steps(12); }
.xpbar-text { display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); margin-top: 4px; }
.xpbar-text b { color: var(--xp); font-weight: 700; }

.badges { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; }
.badge { background: var(--panel-hi); padding: 3px 8px; font-size: 14px; }

/* Quest log */
.section-title { display: flex; justify-content: space-between; align-items: baseline; font-family: var(--pixel); font-size: 11px; color: var(--gold); margin: 26px 4px 10px; }
.section-title span { font-family: var(--ui); font-size: 15px; color: var(--muted); }

.quests { display: grid; gap: 12px; }
.quest { width: calc(100% - 8px); text-align: left; border: 0; padding: 12px 14px; display: grid; grid-template-columns: 40px 1fr auto; gap: 12px; align-items: center; }
.quest:active { background: var(--panel-hi); }
.quest-icon { width: 40px; height: 40px; display: grid; place-items: center; background: var(--ink); font-family: var(--pixel); font-size: 12px; color: var(--muted); }
.quest-title { font-family: var(--thai); font-weight: 500; font-size: 17px; line-height: 1.3; }
.quest-sub { font-size: 14px; color: var(--muted); }
.quest-xp { font-family: var(--pixel); font-size: 9px; color: var(--gold); text-align: right; white-space: nowrap; }
.quest.boss .quest-icon { background: #4a1426; color: var(--danger); }
.quest.done { --edge: var(--done); background: #1d2f2c; }
.quest.done .quest-icon, .quest.final:not(.locked) .quest-icon { background: var(--done); color: #fff; font-family: var(--ui); font-size: 22px; }
.quest.final:not(.locked) .quest-icon { background: var(--danger); }
.quest.done .quest-xp { color: var(--xp); }
.quest.locked { opacity: .55; }
.quest.final { --edge: var(--danger); }

.flex-btn { margin-top: 26px; }
.foot { text-align: center; color: var(--muted); font-size: 13px; margin-top: 18px; }

/* Create hero */
.create h1 { font-family: var(--pixel); font-size: 15px; line-height: 1.6; color: var(--gold); text-align: center; margin: 18px 0 4px; }
.create .lead { text-align: center; color: var(--muted); margin: 0 0 20px; }
.field { padding: 14px; margin-bottom: 18px; }
.field label { display: block; font-family: var(--pixel); font-size: 9px; color: var(--muted); margin-bottom: 8px; }
.field input { width: 100%; background: var(--ink); color: var(--text); border: 0; padding: 12px; font-family: var(--ui); font-size: 18px; outline: none; border-radius: 0; }
.field input:focus { box-shadow: inset 0 0 0 2px var(--gold); }
.classes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 22px; }
.class-card { border: 0; padding: 10px 8px 12px; text-align: center; }
.class-card svg { width: 96px; height: 96px; display: block; margin: 0 auto 4px; }
.class-card b { display: block; font-family: var(--pixel); font-size: 10px; margin-bottom: 4px; }
.class-card small { color: var(--muted); font-size: 13px; line-height: 1.2; display: block; }
.class-card[aria-pressed="true"] { --edge: var(--gold); background: var(--panel-hi); }

/* Modal + sheet */
.modal { position: fixed; inset: 0; z-index: 10; background: rgba(5, 3, 12, .72); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.modal[hidden] { display: none; }
.modal.center { align-items: center; }
@keyframes fade { from { opacity: 0; } }
.sheet { width: 100%; max-width: 480px; max-height: 88dvh; overflow-y: auto; background: var(--bg2); padding: 22px 18px calc(env(safe-area-inset-bottom) + 22px); box-shadow: 0 -4px 0 var(--gold); animation: rise .2s steps(5); }
@keyframes rise { from { transform: translateY(40%); } }
.sheet h2 { font-family: var(--thai); font-weight: 500; font-size: 22px; line-height: 1.3; margin: 0; }
.sheet .sub { color: var(--gold); font-size: 15px; margin-bottom: 14px; }
.sheet .story { font-family: var(--thai); font-size: 16px; line-height: 1.65; white-space: pre-line; background: var(--panel); padding: 14px; margin: 4px 4px 20px; }
.sheet .label { font-family: var(--pixel); font-size: 9px; color: var(--muted); margin: 0 4px 10px; }
.options { display: grid; gap: 10px; margin-bottom: 16px; }
.options.grid4 { grid-template-columns: repeat(4, 1fr); }
.opt { border: 0; width: calc(100% - 8px); display: flex; justify-content: space-between; align-items: center; padding: 14px; text-align: left; font-size: 17px; }
.opt b { font-family: var(--pixel); font-size: 10px; color: var(--gold); }
.grid4 .opt { flex-direction: column; gap: 6px; padding: 12px 4px; text-align: center; }
.result { padding: 14px; margin-bottom: 16px; --edge: var(--done); background: #1d2f2c; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.result b { font-family: var(--pixel); font-size: 11px; color: var(--xp); }
.stack { display: grid; gap: 12px; }

/* Level up */
.levelup { text-align: center; padding: 24px 18px; width: calc(100% - 40px); max-width: 360px; animation: pop .3s steps(4); }
@keyframes pop { from { transform: scale(.6); } }
.levelup h2 { font-family: var(--pixel); font-size: 18px; color: var(--gold); margin: 0 0 6px; text-shadow: 3px 3px 0 var(--ink); }
.levelup .rays { width: 180px; height: 180px; margin: 10px auto; position: relative; background: repeating-conic-gradient(rgba(242, 201, 76, .22) 0 15deg, transparent 15deg 30deg); border-radius: 50%; animation: spin 8s linear infinite; }
.levelup .rays svg { position: absolute; inset: 20px; width: 140px; height: 140px; animation: counter 8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes counter { to { transform: rotate(-360deg); } }
.levelup p { margin: 4px 0; }
.levelup .new-title { color: var(--gold); font-size: 19px; font-weight: 700; }
.levelup .evolved { font-family: var(--pixel); font-size: 9px; color: var(--xp); margin-top: 10px; line-height: 1.6; }
.levelup .btn { margin-top: 18px; }

/* Hero card (flex) */
.card-wrap { width: calc(100% - 32px); max-width: 380px; max-height: 94dvh; overflow-y: auto; animation: pop .25s steps(4); }
.herocard { --edge: var(--gold); background: linear-gradient(180deg, #2f2358, #1c1438); padding: 18px 16px 16px; text-align: center; }
.herocard .trip { text-align: center; }
.herocard .portrait { width: 180px; height: 180px; margin: 10px auto 6px; background: radial-gradient(circle at 50% 62%, #4a3a90 0, transparent 70%); }
.herocard .portrait svg { width: 100%; height: 100%; }
.herocard .name { font-family: var(--pixel); font-size: 15px; line-height: 1.5; word-break: break-word; }
.herocard .title { color: var(--gold); font-weight: 700; font-size: 17px; }
.herocard .meta { color: var(--muted); font-size: 15px; }
.statrow { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 4px; }
.stat { background: var(--ink); padding: 8px 4px; }
.stat b { display: block; font-family: var(--pixel); font-size: 14px; color: var(--gold); margin-bottom: 4px; }
.stat small { font-size: 12px; color: var(--muted); }
.herocard .badges { justify-content: center; margin: 4px 0 10px; }
.qlist { text-align: left; font-size: 14px; margin: 0 4px; }
.qlist div { display: flex; justify-content: space-between; gap: 8px; padding: 4px 0; border-bottom: 1px dashed #3a2f66; }
.qlist div span:first-child { font-family: var(--thai); }
.qlist .no { color: #5f5690; }
.card-actions { margin-top: 14px; }

.confirm { padding: 22px 18px; width: calc(100% - 40px); max-width: 360px; text-align: center; }
.confirm h2 { font-family: var(--pixel); font-size: 13px; color: var(--danger); line-height: 1.6; }
.confirm p { color: var(--muted); }

/* Toast */
.toast { position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 18px); transform: translate(-50%, -40px); opacity: 0; visibility: hidden; z-index: 20; background: var(--xp); color: var(--ink); font-family: var(--pixel); font-size: 12px; padding: 12px 16px; box-shadow: 0 4px 0 var(--xp-dk); transition: transform .25s steps(5), opacity .25s, visibility .25s; white-space: nowrap; }
.toast.show { transform: translate(-50%, 0); opacity: 1; visibility: visible; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
.maplink { text-align: center; text-decoration: none; margin-bottom: 20px; }
