:root {
    --bg: #0b0d12;
    --panel: rgba(18, 21, 30, 0.86);
    --panel-solid: #12151e;
    --line: rgba(255, 255, 255, 0.08);
    --line-strong: rgba(255, 255, 255, 0.16);
    --text: rgba(255, 255, 255, 0.92);
    --muted: rgba(235, 235, 245, 0.56);
    --brand: #5c73e7;
    --brand-strong: #3451b2;
    --brand-soft: rgba(100, 108, 255, 0.16);
    --hp: #e5484d;
    --hp-bg: rgba(229, 72, 77, 0.18);
    --xp: #4cc38a;
    --warn: #f5a524;
    --radius: 10px;
    --font: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.4 var(--font);
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

#app { position: fixed; inset: 0; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

#view {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    outline: none;
}

.hidden { display: none !important; }

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

.icon-btn {
    min-width: 34px;
    height: 34px;
    padding: 0 10px;
    border-radius: 8px;
    border: 1px solid var(--line-strong);
    background: var(--panel);
    backdrop-filter: blur(6px);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.04em;
}
.icon-btn:hover { border-color: var(--brand); }
.icon-btn.active { background: var(--brand-strong); border-color: var(--brand); }

.primary {
    margin-top: 18px;
    padding: 11px 22px;
    border-radius: 8px;
    border: 1px solid var(--brand);
    background: var(--brand-strong);
    font-weight: 600;
    font-size: 15px;
}
.primary:hover { background: var(--brand); }
.hint { color: var(--muted); font-weight: 400; font-size: 0.85em; }
.k { color: var(--muted); }

/* ------------------------------------------------------------------ HUD */

#hud {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    pointer-events: none;
    padding: 0 max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
    font-variant-numeric: tabular-nums;
}
.xpbar {
    height: 6px;
    margin: 0 calc(-1 * max(14px, env(safe-area-inset-left)));
    background: rgba(76, 195, 138, 0.12);
}
.xpfill {
    height: 100%;
    background: linear-gradient(90deg, #2f9e6b, var(--xp));
    transform-origin: left;
    transform: scaleX(0);
    transition: transform 120ms linear;
    box-shadow: 0 0 10px rgba(76, 195, 138, 0.6);
}
.hud-row {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 12px;
    margin-top: 10px;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.hud-left { display: flex; align-items: center; gap: 10px; }
.level {
    padding: 3px 9px;
    border-radius: 6px;
    background: var(--brand-strong);
    font-weight: 700;
    font-size: 13px;
    text-shadow: none;
}
.hpbar {
    position: relative;
    width: min(220px, 40vw);
    height: 16px;
    border-radius: 5px;
    background: var(--hp-bg);
    border: 1px solid rgba(229, 72, 77, 0.45);
    overflow: hidden;
}
.hpfill {
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, #b8282e, var(--hp));
    transform-origin: left;
    transition: transform 100ms ease-out;
}
.hpbar span {
    position: absolute;
    inset: 0;
    font-size: 11px;
    font-weight: 600;
    line-height: 14px;
    text-align: center;
}
.hpbar.hit { animation: hpHit 260ms ease-out; }
.hpbar.low .hpfill { animation: hpLow 0.8s ease-in-out infinite alternate; }
@keyframes hpHit { 0% { box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.7); } 100% { box-shadow: 0 0 0 0 transparent; } }
@keyframes hpLow { from { filter: brightness(1); } to { filter: brightness(1.6); } }

.hud-center { font-size: 22px; font-weight: 700; letter-spacing: 0.02em; }
.hud-right { display: flex; justify-content: flex-end; gap: 16px; font-weight: 600; }

.hud-buttons {
    position: absolute;
    right: max(14px, env(safe-area-inset-right));
    top: 46px;
    display: flex;
    gap: 8px;
    pointer-events: auto;
}

/* ------------------------------------------------------------------ modals */

.modal {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(6, 8, 12, 0.55);
    backdrop-filter: blur(2px);
    padding: 16px;
    z-index: 5;
    animation: fadeIn 140ms ease-out;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal-inner {
    width: min(760px, 100%);
    text-align: center;
}
.modal-inner.small {
    width: min(380px, 100%);
    padding: 26px 26px 22px;
    border-radius: 14px;
    background: var(--panel);
    border: 1px solid var(--line-strong);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.modal h2 { margin: 0; font-size: 28px; letter-spacing: -0.01em; }
.modal .sub { margin: 6px 0 0; color: var(--muted); }
.controls { margin: 14px 0 0; color: var(--muted); font-size: 12px; }

#levelup h2 { color: #fff; text-shadow: 0 0 24px rgba(92, 115, 231, 0.8); }
.cards {
    display: grid;
    grid-template-columns: repeat(var(--n, 3), 1fr);
    gap: 14px;
    max-width: calc(var(--n, 3) * 250px);
    margin: 20px auto 0;
}
.cards.cooldown { pointer-events: none; }
.card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 150px;
    padding: 22px 16px 18px;
    border-radius: 12px;
    border: 1px solid var(--line-strong);
    background: linear-gradient(180deg, rgba(52, 81, 178, 0.28), var(--panel-solid) 70%);
    text-align: left;
    transition: transform 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
    animation: cardIn 220ms ease-out backwards;
}
.card:nth-child(2) { animation-delay: 50ms; }
.card:nth-child(3) { animation-delay: 100ms; }
@keyframes cardIn { from { opacity: 0; transform: translateY(14px); } }
.card:hover, .card:focus-visible {
    transform: translateY(-3px);
    border-color: var(--brand);
    box-shadow: 0 10px 30px rgba(52, 81, 178, 0.35);
    outline: none;
}
.card-key {
    position: absolute;
    top: 10px;
    right: 12px;
    width: 22px;
    height: 22px;
    border-radius: 6px;
    border: 1px solid var(--line-strong);
    font: 600 12px/20px var(--mono);
    text-align: center;
    color: var(--muted);
}
.card-name { font-size: 17px; font-weight: 700; padding-right: 26px; }
.card-level { font: 600 12px var(--mono); color: var(--brand); margin-top: -4px; }
.card-level.new { color: var(--warn); }
.card-level.heal { color: var(--xp); }
.card.heal { background: linear-gradient(180deg, rgba(76, 195, 138, 0.22), var(--panel-solid) 70%); }
.card.heal:hover, .card.heal:focus-visible { border-color: var(--xp); box-shadow: 0 10px 30px rgba(76, 195, 138, 0.25); }

/* Upgrades taken: one row per upgrade, a pip per level, gold when maxed. */
.upgrades {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 3px;
    margin-top: 10px;
    font-size: 11px;
    font-weight: 600;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.upg {
    display: grid;
    grid-template-columns: 104px auto;
    align-items: center;
    gap: 8px;
    padding: 2px 8px 2px 6px;
    border-radius: 5px;
    background: rgba(11, 13, 18, 0.55);
}
.upg-name { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pips { display: flex; gap: 3px; }
.pips i { width: 7px; height: 7px; border-radius: 2px; background: rgba(255, 255, 255, 0.14); }
.pips i.on { background: var(--brand); box-shadow: 0 0 6px rgba(92, 115, 231, 0.6); }
.upg.max .pips i.on { background: var(--warn); box-shadow: 0 0 6px rgba(245, 165, 36, 0.55); }
.card-desc { color: var(--muted); font-size: 13px; }

.stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 18px;
    font-variant-numeric: tabular-nums;
}
.stats div {
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.04);
}
.stats span:last-child { font-size: 20px; font-weight: 700; }
#gameover h2 { color: var(--hp); }

/* ------------------------------------------------------------------ joystick */

#stick {
    position: fixed;
    width: 112px;
    height: 112px;
    margin: -56px 0 0 -56px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.18);
    background: rgba(255, 255, 255, 0.05);
    pointer-events: none;
    z-index: 4;
}
#stick-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: rgba(92, 115, 231, 0.55);
    border: 2px solid rgba(255, 255, 255, 0.35);
    transform: translate(-50%, -50%);
}

/* ------------------------------------------------------------------ inspector */

#overlay {
    position: absolute;
    top: 88px;
    right: max(14px, env(safe-area-inset-right));
    bottom: 14px;
    width: 340px;
    display: flex;
    flex-direction: column;
    border-radius: var(--radius);
    background: var(--panel);
    border: 1px solid var(--line-strong);
    backdrop-filter: blur(8px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
    font-size: 12px;
    z-index: 3;
    font-variant-numeric: tabular-nums;
}
#overlay header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 8px 8px 14px;
    border-bottom: 1px solid var(--line);
}
#overlay header strong { font-size: 13px; }
#overlay header .icon-btn { height: 26px; min-width: 26px; padding: 0; font-size: 16px; border: 0; background: transparent; }
.ov-scroll { overflow-y: auto; padding: 4px 14px 14px; scrollbar-width: thin; }
#overlay section { padding: 10px 0; border-bottom: 1px solid var(--line); }
#overlay section:last-child { border-bottom: 0; }
#overlay h3 {
    margin: 0 0 8px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
}
#overlay h3 .note { text-transform: none; letter-spacing: 0; font-weight: 400; float: right; }

.ov-metrics { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.ov-metrics div { display: flex; flex-direction: column; }
.ov-metrics div span:last-child { font-size: 16px; font-weight: 700; }

.ov-stress label[for="stress"] { display: flex; justify-content: space-between; font-weight: 600; }
#stress-val { font-family: var(--mono); }
#stress-val.hot { color: var(--warn); }
#stress { width: 100%; margin: 8px 0 4px; accent-color: var(--brand); }
.ov-row-check { display: flex; align-items: center; gap: 12px; }
.ov-row-check label { flex: 1; display: flex; align-items: center; gap: 6px; }
.link-btn { padding: 0; border: 0; background: none; color: var(--brand); font-size: 12px; }
.link-btn:hover { text-decoration: underline; }

.ov-phase {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 8px 0 4px;
    font-weight: 600;
}
.ov-phase:first-child { margin-top: 0; }
.ov-phase code { margin-left: auto; color: var(--muted); font: 11px var(--mono); }
.ov-sys {
    display: grid;
    grid-template-columns: 16px 110px 1fr 48px;
    align-items: center;
    gap: 6px;
    height: 20px;
    padding-left: 10px;
}
.ov-sys input, .ov-phase input { margin: 0; accent-color: var(--brand); }
.ov-sys a {
    color: var(--text);
    text-decoration: none;
    font-family: var(--mono);
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.ov-sys a:hover { color: var(--brand); text-decoration: underline; }
.ov-sys.off a, .ov-sys.off .ms { color: var(--muted); text-decoration: line-through; }
.ov-bar {
    position: relative;
    height: 8px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.05);
    overflow: hidden;
}
.ov-bar .fill {
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, var(--brand-strong), var(--brand));
    transform-origin: left;
    transform: scaleX(0);
    transition: transform 200ms linear;
}
.ov-bar .peak {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 2px;
    margin-left: -1px;
    background: var(--warn);
    opacity: 0.8;
}
.ov-sys .ms { text-align: right; font-family: var(--mono); font-size: 11px; }

.ov-table { display: flex; flex-direction: column; gap: 2px; }
.ov-tr {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 10px;
    font-family: var(--mono);
    font-size: 11px;
}
.ov-tr span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ov-tr span:not(:first-child) { text-align: right; min-width: 44px; color: var(--muted); }
.ov-tr span:nth-child(2) { color: var(--text); }

/* ------------------------------------------------------------------ loading */

#loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(ellipse at center, #141a2e 0%, var(--bg) 70%);
    z-index: 10;
    transition: opacity 350ms ease;
    padding: 16px;
}
#loading.done { opacity: 0; pointer-events: none; }
.loading-inner { width: min(360px, 100%); text-align: center; }
.brand { font-weight: 700; font-size: 15px; color: var(--muted); letter-spacing: 0.02em; }
.brand span { color: var(--brand); }
.loading-title { font-size: 40px; font-weight: 800; letter-spacing: -0.02em; margin: 2px 0 18px; }
.progress { height: 4px; border-radius: 2px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
#progress-fill {
    height: 100%;
    background: var(--brand);
    transform-origin: left;
    transform: scaleX(0.03);
    transition: transform 200ms ease;
}
#loading-text { margin-top: 12px; color: var(--muted); font-size: 13px; min-height: 1.4em; }
#loading.error .progress { display: none; }
#loading.error #loading-text { color: var(--text); font-size: 15px; }
#loading pre {
    text-align: left;
    max-height: 30vh;
    overflow: auto;
    font: 11px var(--mono);
    color: var(--muted);
    white-space: pre-wrap;
}

/* ------------------------------------------------------------------ narrow / embed */

@media (max-width: 720px) {
    .hud-row { grid-template-columns: 1fr auto; }
    .hud-center { grid-column: 2; grid-row: 1; font-size: 18px; }
    .hud-right { grid-column: 1 / -1; justify-content: flex-start; font-size: 12px; gap: 12px; }
    .hud-buttons { top: 70px; }
    .cards { grid-template-columns: 1fr; gap: 10px; }
    .upgrades { font-size: 10px; gap: 2px; margin-top: 6px; }
    .upg { grid-template-columns: 78px auto; gap: 6px; padding: 1px 6px 1px 5px; }
    .pips i { width: 5px; height: 5px; }
    .card { min-height: 0; padding: 14px 14px 12px; }
    .modal h2 { font-size: 22px; }
    /* Bottom sheet that stops short of the screen centre, where the player is. */
    #overlay { top: auto; left: 10px; right: 10px; width: auto; height: calc(50% - 44px); bottom: 10px; }
}

body.embed .hud-row { margin-top: 6px; font-size: 12px; }
body.embed .hud-center { font-size: 16px; }
body.embed .hpbar { width: min(150px, 34vw); height: 13px; }
body.embed .hpbar span { font-size: 10px; line-height: 11px; }
body.embed .level { font-size: 11px; padding: 2px 7px; }
body.embed .hud-buttons { top: 38px; }
body.embed .icon-btn { height: 28px; min-width: 28px; font-size: 11px; }
body.embed #overlay { top: 74px; width: 300px; }
body.embed .card { min-height: 110px; padding: 16px 12px 12px; }
body.embed .upgrades { font-size: 10px; gap: 2px; margin-top: 6px; }
body.embed .upg { grid-template-columns: 82px auto; padding: 1px 6px 1px 5px; }
body.embed .pips i { width: 5px; height: 5px; }
body.embed .modal h2 { font-size: 22px; }
@media (max-width: 720px) {
    /* The embed rules above outrank the narrow layout: restore it (stats row wraps, inspector is a bottom sheet). */
    body.embed .hud-buttons { top: 66px; }
    body.embed #overlay { top: auto; width: auto; }
}

/* Touch-only devices: no keyboard hints, finger-sized buttons (after the embed rules on purpose). */
@media (hover: none) and (pointer: coarse) {
    .keys-only { display: none; }
    .icon-btn, body.embed .icon-btn { min-width: 40px; height: 40px; }
}
