:root {
    --night: #1b1f3b;
    --cream: #fff6e5;
    --sun: #ffcc3d;
    --grass: #4cc46a;
    --ink: #2b2b2b;
    --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--night);
    color: var(--cream);
    font-family: var(--font);
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation; /* no double-tap zoom on the tablet */
    user-select: none;
    -webkit-user-select: none;
}

#game-root {
    position: relative;
    width: 100%;
    height: 100%;
}

#game-container {
    position: absolute;
    inset: 0;
}

#game-container canvas {
    display: block;
}

/* The UI layer is positioned by ui.ts to exactly cover the scaled canvas,
   so CSS inside it can use % units that match the game's 1280x720 space. */
#ui {
    position: absolute;
    left: 0;
    top: 0;
    width: 0;
    height: 0;
    pointer-events: none; /* children opt back in */
}

#ui > * {
    pointer-events: auto;
}

/* ---- Shared UI pieces ---- */

.btn {
    font: inherit;
    font-weight: 700;
    color: var(--ink);
    background: var(--sun);
    border: 0;
    border-bottom: 6px solid #d9a51b;
    border-radius: 18px;
    padding: 0.6em 1.6em;
    cursor: pointer;
    transition: transform 80ms ease, border-bottom-width 80ms ease;
}

.btn:active {
    transform: translateY(4px);
    border-bottom-width: 2px;
}

.btn:focus-visible {
    outline: 4px solid var(--cream);
    outline-offset: 2px;
}

/* ---- Title screen ---- */

.title-ui {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    padding-bottom: 7%;
    gap: 1.2em;
}

.title-ui .btn {
    font-size: clamp(18px, 3.4cqw, 40px);
    animation: breathe 1.6s ease-in-out infinite;
}

.title-ui .credit {
    font-size: clamp(11px, 1.6cqw, 18px);
    opacity: 0.8;
}

#ui { container-type: size; }

@keyframes breathe {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

/* ---- Toast ---- */

.toast {
    position: absolute;
    left: 50%;
    top: 12%;
    transform: translateX(-50%);
    background: rgba(27, 31, 59, 0.92);
    border: 3px solid var(--sun);
    border-radius: 16px;
    padding: 0.6em 1.2em;
    font-size: clamp(14px, 2.4cqw, 28px);
    font-weight: 700;
    animation: pop 220ms ease-out;
}

@keyframes pop {
    from { transform: translateX(-50%) scale(0.8); opacity: 0; }
    to { transform: translateX(-50%) scale(1); opacity: 1; }
}

/* ---- Shared panels / dialog ---- */

.panel, .dialog {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(27, 31, 59, 0.94);
    border: 4px solid var(--sun);
    border-radius: 24px;
    padding: 3cqh 4cqw;
    text-align: center;
    max-width: 80cqw;
}

.panel { top: 50%; transform: translate(-50%, -50%); }
.dialog { top: 5cqh; width: 70cqw; }
.dialog.end { bottom: auto; top: 50%; transform: translate(-50%, -50%); }

.panel h1, .dialog h1 {
    margin: 0 0 0.4em;
    font-size: clamp(22px, 5cqh, 48px);
}

.dialog p {
    margin: 0.3em 0 0.8em;
    font-size: clamp(16px, 3.6cqh, 30px);
    line-height: 1.3;
}

.dialog p.big {
    font-size: clamp(28px, 8cqh, 72px);
    font-weight: 900;
    color: var(--sun);
    margin: 0.1em 0;
}

.dialog .btn, .panel .btn {
    font-size: clamp(18px, 4cqh, 36px);
}

/* ---- Name entry ---- */

.name-entry .row {
    display: flex;
    gap: 1em;
    justify-content: center;
    align-items: stretch;
    margin-top: 1em;
}

.name-input {
    font: inherit;
    font-size: clamp(18px, 4cqh, 36px);
    font-weight: 700;
    padding: 0.4em 0.8em;
    border-radius: 16px;
    border: 4px solid var(--cream);
    background: var(--cream);
    color: var(--ink);
    width: 12em;
    max-width: 50cqw;
    outline: none;
}

.name-input:focus { border-color: var(--sun); }

.profile-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.8em;
    justify-content: center;
    margin-bottom: 0.8em;
}

.name-entry .or {
    opacity: 0.75;
    font-size: clamp(13px, 2.6cqh, 20px);
}

/* ---- Question card + number pad (charge round, recharge, finishing spell) ---- */

.question {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 0.6cqh;
}

.eq {
    font-size: clamp(28px, 10cqh, 80px);
    font-weight: 900;
    display: flex;
    align-items: baseline;
    gap: 0.25em;
}

.answer {
    min-width: 1.8em;
    text-align: center;
    background: var(--cream);
    color: var(--ink);
    border-radius: 0.25em;
    padding: 0 0.2em;
    transition: background 150ms ease, color 150ms ease;
}

.answer.pop { animation: digit 140ms ease-out; }
.answer.right { background: var(--grass); color: var(--night); animation: cheer 360ms ease-out; }
.answer.shown { background: #cfd3ea; }

@keyframes digit { from { transform: scale(1.18); } to { transform: scale(1); } }
@keyframes cheer {
    0% { transform: scale(1); }
    40% { transform: scale(1.25) rotate(-4deg); }
    100% { transform: scale(1) rotate(0); }
}
@keyframes panelpop {
    from { transform: scale(0.85); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.feedback {
    min-height: 1.4em;
    font-size: clamp(14px, 3.4cqh, 26px);
    font-weight: 700;
    text-align: center;
}

.feedback.good { color: var(--sun); }
.feedback.soft { color: var(--cream); opacity: 0.9; }

.pad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-auto-rows: 1fr;
    gap: 0.9cqh;
}

.key {
    font: inherit;
    font-weight: 800;
    font-size: clamp(18px, 5cqh, 40px);
    min-height: 7.5cqh;
    color: var(--ink);
    background: var(--cream);
    border: 0;
    border-bottom: 5px solid #cbbfa6;
    border-radius: 14px;
    cursor: pointer;
    touch-action: manipulation;
}

.key:active { transform: translateY(3px); border-bottom-width: 2px; }
.key.back { background: #e0d6c2; }
.key.go { background: var(--grass); border-bottom-color: #2f8f4a; color: var(--night); }

/* ---- Charge round ---- */

#ui > .charge-ui { pointer-events: none; }

.charge-ui {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: flex-end;
    align-items: stretch;
    padding: 2cqh 2cqw;
}

.charge-panel {
    pointer-events: auto;
    width: 52cqw;
    display: flex;
    flex-direction: column;
    gap: 1.4cqh;
    background: rgba(27, 31, 59, 0.9);
    border: 4px solid var(--sun);
    border-radius: 24px;
    padding: 2.4cqh 2cqw;
}

.charge-panel h1 { margin: 0; font-size: clamp(20px, 5.4cqh, 44px); text-align: center; }
.charge-panel .sub { margin: 0; text-align: center; opacity: 0.85; font-size: clamp(12px, 2.6cqh, 20px); }
.charge-panel .pad { flex: 1; }

.charge-status {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1cqw;
}

.dots { display: flex; gap: 0.8cqw; }

.dot {
    width: 3.2cqh;
    height: 3.2cqh;
    border-radius: 50%;
    border: 3px solid var(--cream);
    opacity: 0.6;
    transition: all 200ms ease;
}

.dot.current { opacity: 1; transform: scale(1.25); border-color: var(--sun); }
.dot.hit { opacity: 1; background: var(--sun); border-color: var(--sun); box-shadow: 0 0 1cqh var(--sun); }
.dot.miss { opacity: 0.8; background: #8d92b5; border-color: #8d92b5; }

.wand-row {
    display: flex;
    align-items: center;
    gap: 0.4em;
    font-weight: 900;
    font-size: clamp(18px, 4.6cqh, 38px);
}

.wand-row .count { min-width: 1.2em; text-align: center; color: var(--sun); }
.wand-row .count.bump { animation: cheer 300ms ease-out; }
.wand-row .count.empty { color: #8d92b5; }
.wand-row .stars { color: var(--sun); letter-spacing: 0.05em; }

.dialog.charge-done { top: auto; bottom: 6cqh; left: auto; right: 3cqw; transform: none; width: 50cqw; }

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

/* Taps on the open battlefield reach the game canvas. */
#ui > .battle-ui { pointer-events: none; }

.battle-ui {
    position: absolute;
    inset: 0;
}

.battle-ui button, .battle-overlay { pointer-events: auto; }

.hud-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    padding: 2cqh 2cqw 0;
    gap: 2cqw;
}

.hp { width: 30cqw; }
.hp.right { text-align: right; }

.label {
    font-weight: 700;
    font-size: clamp(12px, 3cqh, 24px);
    margin-bottom: 0.2em;
    text-shadow: 0 2px 0 var(--night);
}

.bar {
    height: 3.6cqh;
    background: rgba(0, 0, 0, 0.45);
    border: 3px solid var(--cream);
    border-radius: 999px;
    overflow: hidden;
}

.fill {
    height: 100%;
    width: 100%;
    border-radius: 999px;
    transition: width 320ms ease;
}

.fill.hero { background: var(--grass); }
.fill.enemy { background: #e63b2e; float: right; }
.fill.enemy.low { background: var(--sun); animation: blinkbar 600ms ease-in-out infinite; }

@keyframes blinkbar { 50% { opacity: 0.55; } }

.wand { text-align: center; }
.wand .wand-row {
    background: rgba(27, 31, 59, 0.75);
    border: 3px solid var(--sun);
    border-radius: 999px;
    padding: 0.1em 0.7em;
}

.battle-hint {
    position: absolute;
    left: 50%;
    top: 15cqh;
    transform: translateX(-50%);
    background: rgba(27, 31, 59, 0.85);
    border-radius: 999px;
    padding: 0.4em 1.1em;
    font-weight: 800;
    font-size: clamp(15px, 3.6cqh, 28px);
    white-space: nowrap;
}

.battle-hint.hidden, .round-btn.hidden { display: none; }

.round-btn {
    position: absolute;
    bottom: 4cqh;
    width: 20cqh;
    height: 20cqh;
    border-radius: 50%;
    border: 5px solid var(--cream);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font: inherit;
    font-weight: 900;
    color: var(--cream);
    cursor: pointer;
    touch-action: manipulation;
    transition: transform 80ms ease, box-shadow 150ms ease, background 150ms ease;
}

.round-btn .icon { font-size: 7cqh; line-height: 1; }
.round-btn .txt { font-size: clamp(11px, 2.6cqh, 20px); }
.round-btn:active { transform: scale(0.92); }

.round-btn.shield { right: 3cqw; background: #2f6fde; }
.round-btn.shield.ready { background: #e63b2e; animation: urgent 360ms ease-in-out infinite; box-shadow: 0 0 4cqh #ff5a4e; }
.round-btn.shield.up { background: #9fd8ff; color: var(--night); box-shadow: 0 0 5cqh #9fd8ff; }
.round-btn.shield.cooldown { opacity: 0.55; }

.round-btn.recharge { left: 3cqw; background: #7c6cf0; animation: urgent 700ms ease-in-out infinite; box-shadow: 0 0 3cqh var(--sun); }

@keyframes urgent { 50% { transform: scale(1.1); } }

.battle-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(20, 24, 51, 0.45);
}

.overlay-panel {
    width: 56cqw;
    background: rgba(27, 31, 59, 0.96);
    border: 4px solid var(--sun);
    border-radius: 24px;
    padding: 2.4cqh 2.4cqw;
    display: flex;
    flex-direction: column;
    gap: 1.4cqh;
    animation: panelpop 220ms ease-out;
}

.battle-overlay.finish .overlay-panel { box-shadow: 0 0 6cqh rgba(255, 204, 61, 0.5); }
.overlay-panel h2 { margin: 0; text-align: center; font-size: clamp(22px, 6cqh, 48px); color: var(--sun); }
.overlay-sub { text-align: center; opacity: 0.85; font-size: clamp(13px, 2.8cqh, 22px); }
.overlay-panel .pad { height: 34cqh; }

.btn.big { font-size: clamp(22px, 5.4cqh, 44px); }
.btn.ghost { background: transparent; color: var(--cream); border: 3px solid var(--cream); border-bottom-width: 6px; }
.dialog .row { display: flex; gap: 1em; justify-content: center; flex-wrap: wrap; }

/* ---- Tablet app feel ---- */

html, body {
    overscroll-behavior: none;          /* no pull-to-refresh or rubber-banding */
    -webkit-touch-callout: none;        /* no long-press menus on images/buttons */
}

input, textarea {
    user-select: text;
    -webkit-user-select: text;
}

#rotate {
    display: none;
}

/* A touch device held upright: ask to turn it. The game keeps its state underneath. */
@media (orientation: portrait) and (pointer: coarse) {
    #rotate {
        position: fixed;
        inset: 0;
        z-index: 10;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 0.6em;
        background: var(--night);
        color: var(--cream);
        font-size: clamp(22px, 5vw, 40px);
        font-weight: 800;
        text-align: center;
        padding: 1em;
    }

    #rotate .rotate-icon {
        font-size: 3em;
        animation: tilt 2s ease-in-out infinite;
    }
}

@keyframes tilt {
    0%, 20% { transform: rotate(0deg); }
    50%, 80% { transform: rotate(-90deg); }
    100% { transform: rotate(0deg); }
}
