/* ==========================================================================
   Tomebound — vivid storybook UI over a three.js canvas
   ========================================================================== */

:root {
    --ink: #2a1650;
    --panel: rgba(38, 20, 78, 0.94);
    --panel-2: rgba(62, 34, 120, 0.92);
    --line: rgba(255, 255, 255, 0.16);
    --text: #fff8ec;
    --dim: #cbbcf0;
    --gold: #ffcf4a;
    --gold-2: #ff9f1a;
    --pink: #ff5fa8;
    --cyan: #3fe0ff;
    --green: #46e07a;
    --red: #ff4a5a;
    --fire: #ff4a3d;
    --water: #2f9bff;
    --leaf: #3fd26a;
    --spark: #ffd23a;
    --board: 480px;
    --radius: 16px;
    --font: 'Trebuchet MS', 'Segoe UI', 'Avenir Next', system-ui, -apple-system, sans-serif;
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { width: 100%; height: 100%; overflow: hidden; background: #2a1650; }
body {
    font-family: var(--font); color: var(--text);
    user-select: none; -webkit-user-select: none; touch-action: none;
    overscroll-behavior: none;
}
#game-canvas { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; z-index: 0; }
[hidden] { display: none !important; }
button { font-family: inherit; color: inherit; cursor: pointer; border: none; background: none; touch-action: manipulation; }

.layer { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
.layer > * { pointer-events: auto; }

/* ------------------------------------------------------------------ Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px;
    min-height: 44px; padding: 10px 20px; border-radius: 999px;
    font-size: 16px; font-weight: 800; letter-spacing: 0.3px;
    background: linear-gradient(180deg, #7b5cff, #5432d8);
    box-shadow: 0 4px 0 #34198f, 0 8px 18px rgba(0,0,0,0.3), inset 0 2px 0 rgba(255,255,255,0.35);
    text-shadow: 0 2px 0 rgba(0,0,0,0.25);
    transition: transform 0.08s, filter 0.15s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #34198f, 0 4px 10px rgba(0,0,0,0.3), inset 0 2px 0 rgba(255,255,255,0.35); }
.btn.primary { background: linear-gradient(180deg, #ffdd55, #ff9f1a); color: #4a2400; text-shadow: 0 1px 0 rgba(255,255,255,0.5); box-shadow: 0 4px 0 #b8600a, 0 8px 18px rgba(0,0,0,0.3), inset 0 2px 0 rgba(255,255,255,0.6); }
.btn.green { background: linear-gradient(180deg, #6ef59a, #22b65a); color: #053016; text-shadow: none; box-shadow: 0 4px 0 #137a3a, 0 8px 18px rgba(0,0,0,0.3), inset 0 2px 0 rgba(255,255,255,0.5); }
.btn.pink { background: linear-gradient(180deg, #ff86c0, #ff3d8a); box-shadow: 0 4px 0 #a81f58, 0 8px 18px rgba(0,0,0,0.3), inset 0 2px 0 rgba(255,255,255,0.4); }
.btn.ghost { background: rgba(255,255,255,0.1); box-shadow: inset 0 0 0 2px rgba(255,255,255,0.25); }
.btn.big { min-height: 56px; font-size: 20px; padding: 12px 34px; }
.btn.small { min-height: 44px; padding: 6px 14px; font-size: 14px; }
.btn[disabled], .btn.disabled { filter: grayscale(0.85) brightness(0.75); cursor: not-allowed; }
.mini { min-height: 32px; padding: 4px 12px; border-radius: 999px; font-size: 13px; font-weight: 700; background: rgba(255,255,255,0.12); }

.corner-btn {
    width: 44px; height: 44px; border-radius: 50%;
    background: rgba(30, 14, 64, 0.75); border: 2px solid rgba(255,255,255,0.25);
    font-size: 20px; font-weight: 800; color: #fff;
    box-shadow: 0 4px 12px rgba(0,0,0,0.35);
}
.corner-btn.off { opacity: 0.55; text-decoration: line-through; }
#corner { position: fixed; top: calc(8px + var(--safe-t)); right: 8px; z-index: 60; display: flex; gap: 8px; }

/* ------------------------------------------------------------------ Title */
#title { display: flex; align-items: center; justify-content: center; z-index: 30; }
.title-wrap { text-align: center; padding: 20px; pointer-events: auto; animation: rise 0.8s cubic-bezier(.2,1.4,.4,1) both; }
.logo { font-size: clamp(48px, 13vw, 120px); font-weight: 900; line-height: 0.9; letter-spacing: -2px; filter: drop-shadow(0 8px 0 rgba(42,22,80,0.6)) drop-shadow(0 18px 30px rgba(0,0,0,0.4)); }
.logo-a { color: #fff; -webkit-text-stroke: 3px #5a2ad8; paint-order: stroke fill; }
.logo-b { background: linear-gradient(180deg, #ffe46a 10%, #ff8a1a 90%); -webkit-background-clip: text; background-clip: text; color: transparent; -webkit-text-stroke: 3px #a8380a; paint-order: stroke fill; }
.tagline { margin-top: 10px; font-size: clamp(16px, 4vw, 26px); font-weight: 800; color: #fff; text-shadow: 0 3px 0 #5a2ad8, 0 6px 16px rgba(0,0,0,0.4); letter-spacing: 1px; }
#title-buttons { margin-top: 28px; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.title-foot { margin-top: 26px; font-size: 14px; font-weight: 700; color: #fff; opacity: 0.9; text-shadow: 0 2px 6px rgba(0,0,0,0.6); }

@keyframes rise { from { opacity: 0; transform: translateY(30px) scale(0.92); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(0.6); opacity: 0; } 70% { transform: scale(1.06); opacity: 1; } 100% { transform: scale(1); } }
@keyframes wiggle { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-6deg); } 75% { transform: rotate(6deg); } }
@keyframes glow { 0%,100% { box-shadow: 0 0 0 2px rgba(255,220,90,0.9), 0 0 16px rgba(255,200,60,0.7); } 50% { box-shadow: 0 0 0 2px rgba(255,240,150,1), 0 0 28px rgba(255,220,80,1); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(4px); } }

/* ------------------------------------------------------------------ Town HUD */
#topbar {
    position: absolute; top: calc(6px + var(--safe-t)); left: 98px; right: 110px;
    display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap;
}
.badge {
    position: relative; display: flex; align-items: center; gap: 8px;
    min-height: 48px; padding: 4px 26px 10px 6px; border-radius: 999px;
    background: var(--panel); border: 2px solid rgba(255,207,74,0.6);
    box-shadow: 0 6px 16px rgba(0,0,0,0.3);
}
.hb-icon { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; background: radial-gradient(circle at 35% 30%, #a98bff, #5432d8); }
.hb-text { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.1; }
#hb-name { font-weight: 800; font-size: 14px; }
#hb-level { font-size: 12px; color: var(--gold); font-weight: 800; }
.hb-xp { position: absolute; left: 50px; right: 14px; bottom: 5px; height: 5px; border-radius: 3px; background: rgba(255,255,255,0.15); overflow: hidden; }
#hb-xp-fill { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #b46bff, #ff7ae0); transition: width 0.4s; }
.pip { position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px; background: var(--pink); color: #fff; font-size: 12px; font-weight: 900; display: grid; place-items: center; box-shadow: 0 0 0 2px #2a1650; animation: pop 0.4s both; }

#topbar { pointer-events: none; }
#topbar > *, #res-bar > * { pointer-events: auto; }
#res-bar { display: flex; gap: 6px; flex-wrap: wrap; pointer-events: none; }
.res {
    display: flex; align-items: center; gap: 4px; min-height: 34px; padding: 4px 10px 4px 6px;
    border-radius: 999px; background: var(--panel); border: 1px solid var(--line);
    font-weight: 800; font-size: 14px; box-shadow: 0 4px 10px rgba(0,0,0,0.25);
}
.res .ri { font-size: 17px; }
.res.bump { animation: bumpPulse 0.35s; }
@keyframes bumpPulse { 0%,100% { transform: scale(1); } 45% { transform: scale(1.12); } }

#nav {
    position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(10px + var(--safe-b));
    display: flex; gap: 8px; padding: 8px; border-radius: 26px;
    background: rgba(30, 14, 64, 0.82); border: 2px solid rgba(255,255,255,0.15);
    box-shadow: 0 10px 30px rgba(0,0,0,0.35);
}
.nav-btn {
    position: relative; width: 74px; min-height: 62px; border-radius: 18px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    background: linear-gradient(180deg, #7b5cff, #5432d8);
    box-shadow: 0 3px 0 #34198f, inset 0 2px 0 rgba(255,255,255,0.3);
    transition: transform 0.08s;
}
.nav-btn:active { transform: translateY(2px); }
.nav-btn .ico { font-size: 24px; line-height: 1; }
.nav-btn .lbl { font-size: 11px; font-weight: 800; }
.nav-btn.primary { background: linear-gradient(180deg, #ff86c0, #ff3d8a); box-shadow: 0 3px 0 #a81f58, inset 0 2px 0 rgba(255,255,255,0.35); }
.nav-btn.gold { background: linear-gradient(180deg, #ffdd55, #ff9f1a); color: #4a2400; box-shadow: 0 3px 0 #b8600a, inset 0 2px 0 rgba(255,255,255,0.5); }
#town-hint {
    position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(100px + var(--safe-b));
    padding: 10px 18px; border-radius: 999px; background: var(--panel); border: 2px solid var(--gold);
    font-weight: 800; text-align: center; max-width: 90vw; box-shadow: 0 6px 16px rgba(0,0,0,0.35);
    display: flex; gap: 10px; align-items: center;
}

/* Basket bubbles over buildings (positioned from 3D) */
#bubbles { position: fixed; inset: 0; pointer-events: none; z-index: 9; }
.bubble {
    position: absolute; transform: translate(-50%, -100%); pointer-events: auto;
    min-width: 44px; min-height: 44px; padding: 4px 10px; border-radius: 999px;
    display: flex; align-items: center; gap: 4px;
    background: #fff; color: #3a1a6a; font-weight: 900; font-size: 13px;
    box-shadow: 0 4px 0 rgba(58,26,106,0.35), 0 6px 14px rgba(0,0,0,0.3);
    border: 2px solid var(--gold);
}
.bubble .bi { font-size: 18px; }
.bubble.full { background: linear-gradient(180deg, #fff6c2, #ffd35a); animation: wiggle 1.2s infinite; }
.bubble::after { content: ''; position: absolute; left: 50%; bottom: -8px; margin-left: -6px; border: 6px solid transparent; border-top-color: var(--gold); }
.tag3d {
    position: absolute; transform: translate(-50%, -50%); pointer-events: none;
    padding: 2px 8px; border-radius: 8px; background: rgba(30,14,64,0.7); font-size: 11px; font-weight: 800; white-space: nowrap;
}

/* ------------------------------------------------------------------ Panels */
#panel-root { position: fixed; inset: 0; z-index: 40; pointer-events: none; }
.scrim { position: absolute; inset: 0; background: rgba(20, 8, 44, 0.55); pointer-events: auto; animation: fadein 0.2s both; }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.panel {
    /* Centred 26px low so the top edge clears the "← Games" link and the corner buttons. */
    position: absolute; left: 50%; top: calc(50% + 26px); transform: translate(-50%, -50%);
    width: min(760px, calc(100vw - 20px)); max-height: calc(100vh - 64px - var(--safe-t) - var(--safe-b));
    display: flex; flex-direction: column;
    background: linear-gradient(180deg, rgba(70, 38, 140, 0.97), rgba(38, 20, 78, 0.98));
    border: 3px solid rgba(255, 207, 74, 0.75); border-radius: 24px;
    box-shadow: 0 0 0 4px rgba(42,22,80,0.6), 0 20px 60px rgba(0,0,0,0.5);
    pointer-events: auto; animation: panelIn 0.28s cubic-bezier(.2,1.3,.4,1) both;
}
.panel.narrow { width: min(460px, calc(100vw - 20px)); }
.panel.wide { width: min(980px, calc(100vw - 20px)); }
@keyframes panelIn { from { opacity: 0; transform: translate(-50%, -46%) scale(0.94); } to { opacity: 1; transform: translate(-50%, -50%) scale(1); } }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px; border-bottom: 2px solid var(--line); }
.panel-head .ph-icon { font-size: 30px; }
.panel-head h2 { flex: 1; font-size: 22px; font-weight: 900; text-shadow: 0 2px 0 rgba(0,0,0,0.3); }
.panel-head .sub { font-size: 13px; color: var(--dim); font-weight: 700; }
.panel-close { width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,0.12); font-size: 20px; font-weight: 900; }
.panel-body { padding: 14px 16px 16px; overflow-y: auto; overscroll-behavior: contain; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
.panel-foot { padding: 10px 16px 16px; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; border-top: 2px solid var(--line); }
.tabs { display: flex; gap: 6px; padding: 8px 16px 0; flex-wrap: wrap; }
.tab { min-height: 40px; padding: 6px 14px; border-radius: 12px 12px 0 0; background: rgba(255,255,255,0.08); font-weight: 800; font-size: 14px; }
.tab.on { background: rgba(255,207,74,0.25); color: var(--gold); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.tile {
    position: relative; padding: 12px; border-radius: 16px; background: rgba(255,255,255,0.08);
    border: 2px solid var(--line); text-align: left; display: flex; flex-direction: column; gap: 6px;
}
.tile.click { cursor: pointer; transition: transform 0.1s, border-color 0.15s; }
.tile.click:hover { border-color: var(--gold); transform: translateY(-2px); }
.tile.locked { opacity: 0.55; }
.tile.ready { border-color: var(--green); }
.tile h3 { font-size: 16px; font-weight: 900; display: flex; gap: 6px 8px; align-items: center; flex-wrap: wrap; }
.spell > span { min-width: 0; }
.tile .t-icon { font-size: 26px; }
.tile p, .muted { font-size: 13px; color: var(--dim); line-height: 1.35; }
.tile .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.cost { display: inline-flex; gap: 6px; flex-wrap: wrap; font-size: 13px; font-weight: 800; }
.cost span.short { color: #ff8a9a; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; background: rgba(255,255,255,0.12); font-size: 12px; font-weight: 800; }
.chip.gold { background: rgba(255,207,74,0.25); color: var(--gold); }
.chip.green { background: rgba(70,224,122,0.2); color: #8cffb0; }
.chip.red { background: rgba(255,74,90,0.22); color: #ffa0aa; }
.bar { height: 10px; border-radius: 5px; background: rgba(255,255,255,0.14); overflow: hidden; }
.bar > i { display: block; height: 100%; background: linear-gradient(90deg, #46e07a, #b6ff6a); border-radius: 5px; }
.section-title { font-size: 15px; font-weight: 900; color: var(--gold); margin: 14px 0 8px; letter-spacing: 0.5px; text-transform: uppercase; }
.section-title:first-child { margin-top: 0; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 14px; }
.kv b { color: var(--gold); }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 640px) { .split { grid-template-columns: 1fr; } }

/* Items */
.item { border-left: 5px solid var(--rc, #ddd); }
.item .iname { font-weight: 900; color: var(--rc, #fff); font-size: 14px; }
.item ul { list-style: none; font-size: 12.5px; color: #e8dcff; line-height: 1.45; }
.item .delta { font-size: 12px; font-weight: 900; }
.delta.up { color: #7dffa4; } .delta.down { color: #ff8a9a; }
.slot-empty { color: var(--dim); font-style: italic; font-size: 13px; }

/* Stats */
.stat-row { display: grid; grid-template-columns: 28px 1fr auto 44px; gap: 8px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.stat-row .sv { font-weight: 900; font-size: 16px; }
.plus { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(180deg, #6ef59a, #22b65a); font-size: 22px; font-weight: 900; color: #053016; }
.plus[disabled] { filter: grayscale(1); opacity: 0.4; }

/* Spellbook */
.spell { display: grid; grid-template-columns: 40px 1fr; gap: 10px; align-items: start; }
.spell .s-icon { font-size: 28px; width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: rgba(255,255,255,0.12); }
.spell.slotted { border-color: var(--cyan); background: rgba(63,224,255,0.12); }
.pips { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.pipc { display: inline-flex; align-items: center; gap: 2px; padding: 1px 7px; border-radius: 999px; font-size: 12px; font-weight: 900; color: #1a0a2a; }
.pipc.fire { background: var(--fire); color: #fff; } .pipc.water { background: var(--water); color: #fff; } .pipc.leaf { background: var(--leaf); } .pipc.spark { background: var(--spark); }

/* Library */
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.book {
    position: relative; min-height: 150px; padding: 12px 10px; border-radius: 10px 16px 16px 10px;
    background: linear-gradient(90deg, rgba(0,0,0,0.25) 0 8px, var(--bc, #555) 8px);
    box-shadow: inset -6px 0 0 rgba(255,255,255,0.15), 0 6px 14px rgba(0,0,0,0.35);
    display: flex; flex-direction: column; gap: 6px; text-align: left;
}
.book.missing { --bc: #3a2a5a; filter: grayscale(0.7); opacity: 0.6; }
.book .b-icon { font-size: 32px; filter: drop-shadow(0 2px 2px rgba(0,0,0,0.4)); }
.book .b-title { font-weight: 900; font-size: 14px; color: #fff; text-shadow: 0 2px 2px rgba(0,0,0,0.5); }
.book .b-tier { font-size: 12px; font-weight: 900; color: #fff; }
.book .b-bonus { font-size: 12px; color: #fff; background: rgba(0,0,0,0.3); padding: 4px 6px; border-radius: 6px; }

/* Wing map */
.wings { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
.wing-card { min-height: 120px; padding: 12px; border-radius: 18px; text-align: left; color: #fff; display: flex; flex-direction: column; gap: 4px; border: 3px solid rgba(255,255,255,0.3); box-shadow: 0 6px 16px rgba(0,0,0,0.35); text-shadow: 0 2px 2px rgba(0,0,0,0.5); }
.wing-card .w-icon { font-size: 34px; }
.wing-card .w-name { font-weight: 900; font-size: 16px; }
.wing-card .w-sub { font-size: 12px; font-weight: 800; opacity: 0.95; }
.wing-card.locked { filter: grayscale(0.9) brightness(0.6); }
.wing-card.current { border-color: var(--gold); animation: glow 1.6s infinite; }
.map-wrap { position: relative; width: 100%; border-radius: 18px; overflow: hidden; }
.map-svg { display: block; width: 100%; height: auto; }
.node-btn { cursor: pointer; }
.node-btn:focus { outline: none; }
.map-legend { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; font-weight: 800; color: var(--dim); margin-top: 8px; }

/* Monster preview */
.foe-preview { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 640px) { .foe-preview { grid-template-columns: 1fr; } }

/* ------------------------------------------------------------------ Battle HUD */
#battle-grid {
    position: absolute; inset: 0; padding: calc(56px + var(--safe-t)) 12px calc(12px + var(--safe-b));
    display: grid; grid-template-columns: minmax(220px, 1fr) var(--board) minmax(220px, 1fr);
    gap: 14px; align-items: center; pointer-events: none;
}
#battle-grid > * { pointer-events: auto; }
#board-wrap { position: relative; width: var(--board); height: var(--board); align-self: center; justify-self: center; }
#board-area { position: absolute; inset: 0; border-radius: 18px; touch-action: none; }
#e-side { display: flex; flex-direction: column; gap: 10px; height: 100%; justify-content: center; }
#foe-area { flex: 1 1 auto; min-height: 180px; pointer-events: none; }
.card {
    padding: 10px 12px; border-radius: 20px; background: var(--panel);
    border: 3px solid rgba(255,255,255,0.18); box-shadow: 0 10px 26px rgba(0,0,0,0.35);
    display: flex; flex-direction: column; gap: 8px; max-height: 100%; overflow: hidden;
}
.card.active { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(255,207,74,0.35), 0 10px 26px rgba(0,0,0,0.35); }
.card.hit { animation: shake 0.35s; }
.card-head { display: flex; align-items: center; gap: 8px; }
.c-icon { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-size: 20px; background: radial-gradient(circle at 35% 30%, #a98bff, #5432d8); flex: none; }
.side-e .c-icon { background: radial-gradient(circle at 35% 30%, #ff9a9a, #c8324a); }
.c-name { flex: 1; font-weight: 900; font-size: 15px; line-height: 1.1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-lvl { font-weight: 900; font-size: 12px; color: var(--gold); }
.hpbar { position: relative; height: 22px; border-radius: 11px; background: rgba(0,0,0,0.4); overflow: hidden; box-shadow: inset 0 2px 4px rgba(0,0,0,0.4); }
.hp-fill, .hp-lag, .shield-fill { position: absolute; top: 0; bottom: 0; left: 0; border-radius: 11px; }
.hp-lag { background: #ffe9a8; transition: width 0.6s 0.25s; }
.hp-fill { background: linear-gradient(180deg, #7dff9a, #22b65a); transition: width 0.25s; }
.side-e .hp-fill { background: linear-gradient(180deg, #ff8a8a, #e0304a); }
.shield-fill { background: repeating-linear-gradient(45deg, rgba(120,220,255,0.75) 0 6px, rgba(80,170,255,0.75) 6px 12px); transition: width 0.25s; }
.hp-text { position: absolute; inset: 0; display: grid; place-items: center; font-size: 12.5px; font-weight: 900; text-shadow: 0 1px 2px rgba(0,0,0,0.8); }
.mana { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.mbar { position: relative; height: 20px; border-radius: 10px; background: rgba(0,0,0,0.38); overflow: hidden; }
.mbar i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 10px; transition: width 0.3s; }
.mbar.fire i { background: linear-gradient(180deg, #ff8a6a, var(--fire)); }
.mbar.water i { background: linear-gradient(180deg, #7ac4ff, var(--water)); }
.mbar.leaf i { background: linear-gradient(180deg, #8cf5a8, var(--leaf)); }
.mbar.spark i { background: linear-gradient(180deg, #fff09a, var(--spark)); }
.mbar span { position: absolute; inset: 0; display: grid; place-items: center; font-size: 11px; font-weight: 900; text-shadow: 0 1px 2px rgba(0,0,0,0.9); }
.mbar.pulse { animation: pop 0.3s; }
.status { display: flex; gap: 4px; flex-wrap: wrap; min-height: 0; }
.status .chip { font-size: 11px; }
.spells { display: flex; flex-direction: column; gap: 6px; overflow-y: auto; }
.spell-btn {
    position: relative; display: grid; grid-template-columns: 34px 1fr; gap: 8px; align-items: center;
    min-height: 48px; padding: 6px 10px 6px 6px; border-radius: 14px; text-align: left;
    background: rgba(255,255,255,0.08); border: 2px solid rgba(255,255,255,0.12);
}
.spell-btn .si { font-size: 22px; width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: rgba(0,0,0,0.25); }
.spell-btn .sn { font-weight: 900; font-size: 13.5px; line-height: 1.15; }
.spell-btn .sd { font-size: 11.5px; color: var(--dim); line-height: 1.2; }
.spell-btn .key { position: absolute; top: 3px; right: 6px; font-size: 10px; font-weight: 900; opacity: 0.6; }
.spell-btn.ready { border-color: var(--gold); background: rgba(255,207,74,0.16); animation: glow 1.4s infinite; }
.spell-btn.ready .si { background: rgba(255,207,74,0.35); }
.spells.enemy .spell-btn { cursor: default; min-height: 40px; }
.spells.enemy .spell-btn.ready { animation: none; border-color: #ff8a9a; background: rgba(255,74,90,0.14); }
.potions { display: flex; gap: 6px; flex-wrap: wrap; }
.pot-btn { position: relative; width: 48px; height: 48px; border-radius: 14px; font-size: 24px; background: rgba(255,255,255,0.1); border: 2px solid rgba(255,255,255,0.18); }
.pot-btn .n { position: absolute; right: -4px; bottom: -4px; min-width: 20px; height: 20px; border-radius: 10px; background: var(--gold); color: #3a1a00; font-size: 12px; font-weight: 900; display: grid; place-items: center; }
.weak { font-size: 12px; font-weight: 800; color: var(--dim); }

#turn-banner {
    position: absolute; left: 50%; top: -40px; transform: translateX(-50%);
    padding: 5px 18px; border-radius: 999px; font-weight: 900; font-size: 15px; white-space: nowrap;
    background: linear-gradient(180deg, #6ef59a, #22b65a); color: #053016; box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    transition: background 0.2s;
}
#turn-banner.enemy { background: linear-gradient(180deg, #ff8a9a, #e0304a); color: #fff; }
#loot-line { position: absolute; top: calc(8px + var(--safe-t)); left: 104px; display: flex; gap: 8px; font-weight: 900; }
#loot-line span { padding: 4px 12px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); }
#battle-menu { position: absolute; top: calc(8px + var(--safe-t)); right: 110px; }

/* Portrait battle: monster + enemy card on top, board, then the hero card */
body.portrait #battle-grid {
    grid-template-columns: 1fr; grid-template-rows: auto var(--board) minmax(0, 1fr);
    padding: calc(52px + var(--safe-t)) 8px calc(8px + var(--safe-b)); gap: 8px; align-items: stretch;
}
body.portrait #e-side { order: 0; flex-direction: row; height: var(--foeh, 150px); }
body.portrait #foe-area { flex: 0 0 38%; min-height: 0; }
body.portrait #e-card { flex: 1; padding: 8px 10px; gap: 5px; }
body.portrait #e-spells, body.portrait #e-weak { display: none; }
body.portrait #board-wrap { order: 1; }
body.portrait #p-card { order: 2; padding: 8px 10px; gap: 6px; overflow-y: auto; }
body.portrait #p-spells { flex-direction: row; flex-wrap: wrap; overflow: visible; }
body.portrait #p-spells .spell-btn { flex: 1 1 calc(50% - 6px); min-width: 0; }
body.portrait #p-spells .spell-btn .sd { display: none; }
body.portrait #turn-banner { top: auto; bottom: calc(100% + 2px); font-size: 12px; padding: 3px 12px; }
body.portrait #turn-banner { display: none; }
body.portrait #loot-line { top: calc(10px + var(--safe-t)); font-size: 13px; left: 100px; }
body.portrait #battle-menu { right: 8px; top: calc(56px + var(--safe-t)); width: 40px; height: 40px; display: none; }
body.portrait .hpbar { height: 18px; }
body.portrait .mbar { height: 16px; }

/* ------------------------------------------------------------------ Floating text, banner, toasts */
#floats { position: fixed; inset: 0; pointer-events: none; z-index: 35; overflow: hidden; }
.float {
    position: absolute; transform: translate(-50%, -50%); font-weight: 900; font-size: 26px; white-space: nowrap;
    text-shadow: 0 3px 0 rgba(0,0,0,0.45), 0 0 12px rgba(0,0,0,0.4);
    animation: floatUp 1.1s ease-out forwards;
}
.float.big { font-size: 38px; }
.float.small { font-size: 18px; }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, -30%) scale(0.5); } 15% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); } 30% { transform: translate(-50%, -70%) scale(1); } 100% { opacity: 0; transform: translate(-50%, -170%) scale(1); } }
#banner {
    position: fixed; left: 50%; top: 42%; z-index: 36; pointer-events: none;
    font-size: clamp(32px, 8vw, 72px); font-weight: 900; white-space: nowrap;
    color: #fff; -webkit-text-stroke: 3px #5a2ad8; paint-order: stroke fill;
    text-shadow: 0 6px 0 rgba(42,22,80,0.6), 0 12px 30px rgba(0,0,0,0.4);
    animation: bannerIn 1.3s cubic-bezier(.2,1.4,.4,1) forwards;
}
#banner.gold { background: linear-gradient(180deg, #ffe46a, #ff8a1a); -webkit-background-clip: text; background-clip: text; color: transparent; -webkit-text-stroke: 3px #a8380a; }
@keyframes bannerIn { 0% { opacity: 0; transform: translate(-50%, -50%) scale(0.3) rotate(-8deg); } 20% { opacity: 1; transform: translate(-50%, -50%) scale(1.1) rotate(2deg); } 35% { transform: translate(-50%, -50%) scale(1) rotate(0); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -60%) scale(1); } }
#toasts { position: fixed; left: 50%; transform: translateX(-50%); top: calc(64px + var(--safe-t)); z-index: 50; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { padding: 8px 16px; border-radius: 999px; background: var(--panel); border: 2px solid var(--gold); font-weight: 800; font-size: 14px; box-shadow: 0 6px 16px rgba(0,0,0,0.35); animation: pop 0.3s both; max-width: 92vw; text-align: center; }
.toast.out { transition: opacity 0.4s, transform 0.4s; opacity: 0; transform: translateY(-10px); }

/* ------------------------------------------------------------------ Dialogue */
#dialog { position: fixed; inset: 0; z-index: 45; display: flex; align-items: flex-end; justify-content: center; padding: 16px 12px calc(16px + var(--safe-b)); background: linear-gradient(180deg, transparent 40%, rgba(20,8,44,0.6)); }
.dlg-box {
    width: min(760px, 100%); display: flex; gap: 14px; align-items: flex-start;
    padding: 16px; border-radius: 24px; cursor: pointer;
    background: linear-gradient(180deg, rgba(255,250,235,0.98), rgba(255,236,200,0.98)); color: #3a1a5a;
    border: 4px solid #5a2ad8; box-shadow: 0 0 0 4px rgba(255,207,74,0.8), 0 18px 40px rgba(0,0,0,0.45);
    animation: rise 0.3s both;
}
.dlg-portrait { flex: none; width: 72px; height: 72px; border-radius: 20px; display: grid; place-items: center; font-size: 44px; background: radial-gradient(circle at 35% 30%, #fff1b0, #ffb84a); box-shadow: inset 0 -4px 0 rgba(0,0,0,0.12); }
.dlg-body { flex: 1; min-width: 0; }
.dlg-name { font-weight: 900; font-size: 16px; color: #5a2ad8; margin-bottom: 4px; }
.dlg-text { font-size: 17px; line-height: 1.45; min-height: 3em; font-weight: 600; }
.dlg-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.dlg-foot .mini { background: rgba(90,42,216,0.12); color: #5a2ad8; min-height: 36px; }
.dlg-next { font-size: 18px; color: #5a2ad8; animation: bob 0.8s infinite; }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(4px); } }

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

/* ------------------------------------------------------------------ Help */
.help h3 { color: var(--gold); margin: 12px 0 6px; font-size: 16px; }
.help p, .help li { font-size: 14px; line-height: 1.5; color: #f0e6ff; }
.help ul { padding-left: 20px; }
.gem-legend { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.gem-legend div { padding: 6px 10px; border-radius: 10px; background: rgba(255,255,255,0.08); font-size: 13px; }

/* ------------------------------------------------------------------ Phones */
@media (max-width: 700px) {
    #topbar { left: 8px; right: 104px; top: calc(46px + var(--safe-t)); gap: 6px; }
    .badge { min-height: 44px; }
    .res { min-height: 30px; padding: 3px 8px 3px 5px; font-size: 12.5px; }
    .res .ri { font-size: 15px; }
    #nav { gap: 5px; padding: 6px; border-radius: 20px; width: calc(100vw - 12px); justify-content: space-between; }
    .nav-btn { width: auto; flex: 1; min-height: 56px; border-radius: 14px; }
    .nav-btn .ico { font-size: 21px; }
    .nav-btn .lbl { font-size: 10px; }
    .panel { border-radius: 20px; }
    .panel-head h2 { font-size: 19px; }
    .grid { grid-template-columns: 1fr; }
    .dlg-portrait { width: 56px; height: 56px; font-size: 34px; }
    .dlg-text { font-size: 15.5px; }
}
@media (max-height: 520px) and (orientation: landscape) {
    #topbar { top: calc(4px + var(--safe-t)); }
    #town-hint { bottom: auto; top: calc(52px + var(--safe-t)); padding: 6px 14px; font-size: 13px; }
    #nav { bottom: 4px; padding: 4px; }
    .nav-btn { min-height: 48px; width: 64px; }
    .nav-btn .lbl { display: none; }
    #battle-grid { padding-top: calc(8px + var(--safe-t)); gap: 8px; grid-template-columns: minmax(180px, 1fr) var(--board) minmax(180px, 1fr); }
    #loot-line { display: none; }
    .card { padding: 6px 8px; gap: 5px; border-radius: 14px; }
    .spell-btn { min-height: 44px; padding: 4px 6px 4px 4px; }
    .spell-btn .sd { display: none; }
    #turn-banner { display: none; }
    #battle-menu { right: 8px; top: auto; bottom: 8px; }
    .card-head .c-icon { width: 28px; height: 28px; font-size: 16px; }
}
