@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@600;700;800&family=DM+Sans:wght@400;500;600;700;800&display=swap');

:root {
    --ink: #f4ecd7; --muted: #c0b693; --page: #15130f; --panel: #29251e;
    --panel-border: #655437; --function: #f3e5b9; --function-border: #a97b34;
    --enemy: #7c302d; --enemy-border: #d09a59; --dead: #376b4c;
    --dead-border: #2d7038; --preview: #d9f7d8; --preview-border: #24a148;
    --shadow: 0 12px 30px rgb(0 0 0 / 30%);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0; overflow: hidden; background: var(--page); color: var(--ink);
    font-family: 'DM Sans', sans-serif; text-align: center;
    background-image: radial-gradient(ellipse at 50% -25%, #514127 0, transparent 54%),
        repeating-linear-gradient(135deg, rgb(255 255 255 / 1.4%) 0 1px, transparent 1px 5px);
}
button, a { font: inherit; }
a { color: inherit; text-decoration: none; }

.game-shell {
    width: min(1180px, 100%); max-width: 100vw; min-width: 0; height: 100dvh; margin: 0 auto;
    padding: clamp(.55rem, 1.5vw, 1rem); display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto; gap: clamp(.45rem, 1.2vh, .8rem);
}
.game-header { min-width: 0; display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.responsive-title {
    min-width: 0; margin: 0; overflow: hidden; text-overflow: ellipsis;
    font: 800 clamp(1.35rem, 3vw, 2.15rem)/1 'Cinzel', serif; letter-spacing: .045em;
    color: #f0d58e; text-shadow: 0 2px 14px rgb(218 167 75 / 24%); white-space: nowrap;
}
#stats-bar {
    min-width: 0; overflow: hidden; text-overflow: ellipsis;
    padding: .42rem .75rem; border: 1px solid var(--panel-border); border-radius: 999px;
    background: #201d17; color: #ead9aa; font-size: clamp(.68rem, 1.3vw, .88rem); white-space: nowrap;
}
#stats-bar strong { display: flex; gap: .7rem; }
.battle-board {
    min-height: 0; display: grid; grid-template-columns: minmax(250px, .75fr) minmax(0, 1.25fr);
    gap: clamp(.5rem, 1.4vw, 1rem);
}
.game-panel, .green-box {
    min-width: 0; min-height: 0; padding: clamp(.55rem, 1.4vw, .9rem);
    border: 1px solid var(--panel-border); border-radius: 16px;
    background: linear-gradient(145deg, #302a20, #211f1b 68%); box-shadow: var(--shadow);
}
.game-panel {
    display: grid; grid-template-rows: auto minmax(0, 1fr); gap: .55rem; overflow: hidden;
}
.panel-heading {
    display: flex; align-items: baseline; justify-content: space-between; gap: .6rem; min-width: 0;
}
.section-title {
    font: 700 clamp(.82rem, 1.7vw, 1.08rem)/1.2 'Cinzel', serif; color: #efd99e;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.panel-heading small { color: var(--muted); font-size: clamp(.58rem, 1vw, .72rem); white-space: nowrap; }
.preview-legend span {
    display: inline-block; width: .62rem; height: .62rem; margin-right: .2rem;
    border: 2px solid var(--preview-border); border-radius: 3px; background: var(--preview); vertical-align: -.06rem;
}
.card-grid, .content-container {
    min-width: 0; min-height: 0; display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: repeat(2, minmax(0, 1fr));
    gap: clamp(.4rem, 1.1vw, .7rem);
}
.card {
    min-width: 0; min-height: 0; height: 100%; padding: clamp(.4rem, 1.3vw, .8rem);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: clamp(.15rem, .7vh, .4rem); border-radius: 12px;
    font-size: clamp(.68rem, 1.35vw, .92rem); font-weight: 800; line-height: 1.15;
    text-align: center; box-shadow: 0 8px 16px rgb(0 0 0 / 28%); user-select: none;
    position: relative; isolation: isolate;
}
.function-card {
    border: 2px solid var(--function-border); outline: 1px solid rgb(255 241 194 / 48%); outline-offset: -5px;
    color: #342512; background: radial-gradient(ellipse at 50% 34%, #fff9dc, #ead7a5 74%, #d0ae69);
    cursor: pointer; overflow: hidden;
    transition: transform 140ms ease, background 140ms ease, box-shadow 140ms ease;
}
.function-card:is(:hover, :focus-visible, .is-previewing) {
    z-index: 1; outline: 1px solid #fff1bf; background: radial-gradient(ellipse at 50% 34%, #fffef0, #f3e4b6 74%, #ddbd7a);
    box-shadow: 0 7px 16px rgb(75 54 25 / 24%); transform: translateY(-3px);
}
.function-icon, .enemy-icon { font-size: clamp(1.05rem, 2.8vw, 1.8rem); line-height: 1; }
.function-card::after, .enemy-card::after {
    content: ""; position: absolute; z-index: -1; inset: 5px; border: 1px solid currentColor;
    border-radius: 7px; opacity: .2; pointer-events: none;
}
.function-card::before {
    content: "FUNCTION"; position: absolute; top: .48rem; left: .65rem;
    color: #805d2b; font: 800 .48rem 'Cinzel', serif; letter-spacing: .18em; opacity: .75;
}
.function-card small { max-width: 90%; font: 800 clamp(.7rem, 1.45vw, .96rem)/1.15 'Cinzel', serif; }
.enemy-card {
    border: 2px solid var(--enemy-border); outline: 1px solid rgb(255 232 179 / 35%); outline-offset: -5px;
    color: #fff0ce; background: radial-gradient(ellipse at 50% 36%, #a04a3e, #682724 75%, #421c1b);
    transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}
.neutralized-card { border-color: #83b586; background: radial-gradient(ellipse at 50% 36%, #579267, #294f37 75%, #1e382a); opacity: .78; }
.enemy-card .enemy-icon { filter: drop-shadow(0 3px 3px rgb(0 0 0 / 38%)); }
.enemy-card small { color: #f4dfb1; font: 700 clamp(.7rem, 1.3vw, .9rem) 'Cinzel', serif; }
.enemy-card.preview-kill {
    border: 3px solid var(--preview-border); background: var(--preview);
    box-shadow: 0 0 0 3px rgb(36 161 72 / 18%), 0 7px 16px rgb(28 84 44 / 22%);
    transform: translateY(-2px);
}
.enemy-card.preview-change:not(.preview-kill) { background: #f4aa72; border-color: #a65221; }
.enemy-hp {
    display: flex; align-items: center; justify-content: center; gap: clamp(.25rem, .7vw, .55rem);
    width: 100%; color: #073b29; font-size: clamp(1.25rem, 5.4vw, 3.45rem);
    font-weight: 900; line-height: 1; letter-spacing: -.04em; white-space: nowrap;
    text-shadow: 0 2px 3px rgb(255 255 255 / 45%);
}
.preview-arrow { color: rgb(37 43 39 / 48%); font-size: .52em; }
.preview-result { color: #116b2d; }
.preview-unknown { font-size: .52em; letter-spacing: 0; }
.preview-badge {
    position: absolute; top: .35rem; right: .4rem; padding: .15rem .38rem;
    border-radius: 999px; background: var(--preview-border); color: white;
    font-size: clamp(.5rem, .9vw, .65rem); font-weight: 800; letter-spacing: .04em;
}
footer {
    display: flex; align-items: center; justify-content: center; gap: .45rem;
    color: var(--muted); font-size: .72rem;
}
#toggle-history { padding: 0; border: 0; background: transparent; color: inherit; cursor: pointer; }
#toggle-history:hover { color: var(--ink); }
#previous-waves-container {
    position: fixed; z-index: 20; inset: auto 1rem 2.4rem 1rem; max-width: 980px;
    max-height: 72dvh; margin: auto; overflow: auto;
}
.modal {
    display: none; position: fixed; z-index: 50; inset: 0; padding: .75rem;
    align-items: center; justify-content: center; overflow: auto; background: rgb(0 0 0 / 72%);
}
.modal-content {
    width: min(560px, 100%); max-height: calc(100dvh - 1.5rem); padding: clamp(.75rem, 2vw, 1.2rem);
    overflow: auto; border: 3px solid #8b6f47; border-radius: 14px;
    background: linear-gradient(145deg, #3b3222, #211e19); color: var(--ink);
    box-shadow: 0 18px 55px rgb(0 0 0 / 65%);
}
.modal-content h2, .modal-content h3 { margin: .2rem 0 .6rem; }
.modal-content h2 { font-size: clamp(1.15rem, 4vw, 1.55rem); }
.modal-content h3 { font-size: clamp(.85rem, 3vw, 1.1rem); }
#shop-function-card {
    width: min(150px, 42vw); height: 115px; margin: .55rem auto .8rem;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .35rem;
}
#replace-options { height: min(230px, 36dvh); padding: 0; }
#skip-button {
    width: 100%; margin-top: .7rem; padding: .6rem 1rem; border: 2px solid #8b3a23;
    border-radius: 8px; background: #b85c38; color: white; font-weight: 800; cursor: pointer;
}
#skip-button:hover { background: #923d26; }

@media (max-width: 720px) {
    .game-shell { padding: .45rem; gap: .38rem; }
    .game-header { gap: .45rem; }
    .responsive-title { font-size: clamp(1.05rem, 5.3vw, 1.45rem); }
    #stats-bar { padding: .3rem .46rem; }
    #stats-bar strong { gap: .38rem; }
    .battle-board {
        grid-template-columns: 1fr; grid-template-rows: minmax(0, 1.08fr) minmax(0, .92fr); gap: .42rem;
    }
    .enemy-panel { grid-row: 1; }
    .function-panel { grid-row: 2; }
    .game-panel { padding: .45rem; gap: .35rem; border-radius: 10px; }
    .card-grid { gap: .34rem; }
    .card { padding: .3rem; border-radius: 8px; }
    .function-card::before { top: .34rem; left: .4rem; font-size: .4rem; }
    .enemy-hp { font-size: clamp(1.1rem, 8vw, 2rem); }
    .function-icon, .enemy-icon { font-size: clamp(.95rem, 5vw, 1.35rem); }
    .preview-legend { display: none; }
    footer { font-size: .62rem; }
}

@media (max-height: 560px) and (orientation: landscape) {
    .game-shell { padding: .35rem .55rem; gap: .3rem; }
    .responsive-title { font-size: 1.05rem; }
    .battle-board { grid-template-columns: minmax(210px, .72fr) minmax(0, 1.28fr); grid-template-rows: 1fr; }
    .function-panel, .enemy-panel { grid-row: auto; }
    .game-panel { padding: .38rem; gap: .3rem; }
    .panel-heading small, footer { font-size: .56rem; }
}

@media (prefers-reduced-motion: reduce) {
    .function-card, .enemy-card { transition: none; }
}
