:root {
    color-scheme: dark;
    --bg: #050b14;
    --panel: rgba(8, 19, 32, 0.95);
    --ink: #f3f7ff;
    --muted: #9cb0c4;
    --cyan: #65f4e8;
    --violet: #9e7bff;
    --gold: #ffd878;
    --danger: #ff6685;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: var(--bg);
    color: var(--ink);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    overscroll-behavior: none;
}

body {
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

button { font: inherit; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }
.hidden { display: none !important; }

#game-shell {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-rows: 58px minmax(0, 1fr) auto;
    background:
        radial-gradient(circle at 50% 30%, rgba(41, 77, 108, 0.3), transparent 45%),
        linear-gradient(160deg, #081522, #03070d 70%);
}

.hud {
    z-index: 5;
    grid-row: 1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 52px minmax(0, 1fr) minmax(0, 1fr);
    align-items: center;
    width: min(720px, 100%);
    margin: 0 auto;
    padding: 5px 8px;
}

.hud-item { text-align: center; min-width: 0; }
.hud-item span {
    display: block;
    color: var(--muted);
    font-size: 0.65rem;
    font-weight: 800;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}
.hud-item strong { color: var(--gold); font-size: 1.05rem; }

.icon-button {
    justify-self: center;
    width: 42px;
    height: 42px;
    border: 1px solid rgba(101, 244, 232, 0.5);
    border-radius: 50%;
    background: rgba(101, 244, 232, 0.08);
    color: var(--cyan);
    font-weight: 900;
    cursor: pointer;
}

#play-area {
    position: relative;
    grid-row: 2;
    min-height: 0;
    width: min(760px, 100%);
    margin: 0 auto;
    overflow: hidden;
}

#game-canvas {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}

.screen {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: grid;
    place-items: center;
    padding: 16px;
    background: rgba(3, 8, 14, 0.75);
    backdrop-filter: blur(5px);
}

.panel {
    width: min(390px, 94vw);
    padding: 26px 22px;
    text-align: center;
    border: 1px solid rgba(101, 244, 232, 0.28);
    border-radius: 22px;
    background: var(--panel);
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55), inset 0 0 40px rgba(101, 244, 232, 0.04);
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--cyan);
    font-size: 0.72rem;
    font-weight: 900;
    letter-spacing: 0.28em;
}

h1, h2 { margin: 0 0 18px; line-height: 1; text-transform: uppercase; }
h1 { font-size: clamp(2.5rem, 12vw, 4.8rem); letter-spacing: -0.05em; text-shadow: 0 0 25px rgba(158, 123, 255, 0.55); }
h2 { font-size: clamp(2rem, 9vw, 3.2rem); }
.tagline { color: var(--muted); line-height: 1.5; }
.record { color: var(--muted); }
.record strong { color: var(--gold); font-size: 1.2rem; }
.controls-hint { margin: 16px 0 0; color: #7890a6; font-size: 0.75rem; line-height: 1.4; }

.knight-mark {
    position: relative;
    width: 50px;
    height: 46px;
    margin: -6px auto 12px;
    border: 4px solid #c9d8e8;
    border-radius: 18px 18px 10px 10px;
    box-shadow: 0 0 18px rgba(101, 244, 232, 0.35);
}
.knight-mark::before {
    content: "";
    position: absolute;
    inset: 13px 6px 8px;
    border-radius: 5px;
    background: #07111d;
}
.knight-mark span::before, .knight-mark span::after {
    content: "";
    position: absolute;
    top: 22px;
    width: 7px;
    height: 4px;
    border-radius: 50%;
    background: var(--cyan);
    box-shadow: 0 0 9px var(--cyan);
}
.knight-mark span::before { left: 13px; }
.knight-mark span::after { right: 13px; }

.primary-button, .secondary-button {
    display: block;
    width: 100%;
    min-height: 48px;
    margin: 10px 0;
    padding: 11px 18px;
    border-radius: 999px;
    font-weight: 850;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    cursor: pointer;
}
.primary-button { border: 0; background: linear-gradient(100deg, var(--cyan), #8ac8ff); color: #06101b; box-shadow: 0 0 20px rgba(101, 244, 232, 0.25); }
.secondary-button { border: 1px solid rgba(255, 255, 255, 0.22); background: rgba(255,255,255,0.04); color: var(--ink); }
.primary-button:active, .secondary-button:active, .ability-button:active { transform: translateY(2px); }

.ability-bar {
    z-index: 6;
    grid-row: 3;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    width: min(720px, 100%);
    margin: 0 auto;
    padding: 8px 10px calc(8px + var(--safe-bottom));
}
.ability-bar.single { grid-template-columns: minmax(0, 360px); justify-content: center; }

.ability-button {
    position: relative;
    min-height: 62px;
    overflow: hidden;
    display: grid;
    grid-template-columns: 46px 1fr;
    align-items: center;
    padding: 7px 12px;
    border: 1px solid rgba(101, 244, 232, 0.38);
    border-radius: 16px;
    background: rgba(10, 25, 41, 0.94);
    color: var(--ink);
    text-align: left;
    cursor: pointer;
}
.ability-button.targeted { border-color: var(--gold); box-shadow: 0 0 16px rgba(255, 216, 120, 0.35); }
.ability-button.unavailable { filter: saturate(0.5); color: #91a0af; }
.ability-icon { z-index: 2; color: var(--cyan); font-size: 2rem; text-align: center; text-shadow: 0 0 12px var(--cyan); }
.phase-icon { color: var(--violet); text-shadow: 0 0 12px var(--violet); }
.ability-copy { z-index: 2; min-width: 0; }
.ability-copy strong, .ability-copy small { display: block; }
.ability-copy strong { font-size: 0.85rem; }
.ability-copy small { color: var(--muted); font-size: 0.6rem; }
.cooldown { position: absolute; inset: 0; pointer-events: none; }
.cooldown span { position: absolute; inset: auto 0 0; height: 0; background: rgba(2, 6, 12, 0.76); }
.cooldown-label { position: absolute; z-index: 3; right: 9px; top: 7px; color: var(--gold); font-size: 0.75rem; font-style: normal; font-weight: 900; }

.toast {
    position: absolute;
    z-index: 8;
    top: 12px;
    left: 50%;
    max-width: 80%;
    padding: 8px 14px;
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 999px;
    background: rgba(4, 10, 18, 0.9);
    color: var(--ink);
    font-size: 0.8rem;
    font-weight: 800;
    opacity: 0;
    transform: translate(-50%, -12px);
    transition: opacity 0.18s, transform 0.18s;
    pointer-events: none;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 780px) and (min-height: 650px) {
    #game-shell { grid-template-rows: 62px minmax(0, 1fr) 78px; }
    #play-area { border-inline: 1px solid rgba(101, 244, 232, 0.08); }
}

@media (max-height: 560px) and (orientation: landscape) {
    #game-shell { grid-template-rows: 48px minmax(0, 1fr); grid-template-columns: 1fr 190px; }
    .hud { grid-column: 1 / -1; grid-row: 1; }
    #play-area { grid-column: 1; grid-row: 2; width: 100%; }
    .ability-bar { grid-column: 2; grid-row: 2; grid-template-columns: 1fr; align-content: center; padding: 12px; }
    .ability-bar.single { grid-template-columns: 1fr; }
    .panel { padding: 16px 18px; }
    h1 { font-size: 2.5rem; }
    .knight-mark, .controls-hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
