:root {
    --ink: #3b2f2f;
    --paper: #fffdf7;
    --page: #fdf3e1;
    --accent: #e8435a;
    --good: #4caf50;
    --warn: #e0522b;
    --font: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

* {
    box-sizing: border-box;
}

html {
    background: var(--page);
}

body {
    margin: 0;
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0px));
    background: var(--page);
    color: var(--ink);
    font-family: var(--font);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

button {
    font-family: inherit;
    color: inherit;
}

.page {
    max-width: 820px;
    margin: 0 auto;
}

.banner {
    text-align: center;
}

.banner img {
    width: min(100%, 520px);
    height: auto;
}

/* ---------- 状态栏 ---------- */
.hud {
    display: flex;
    align-items: center;
    gap: 12px 20px;
    flex-wrap: wrap;
    margin: 6px 0 10px;
    padding: 10px 14px;
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: 16px;
    box-shadow: 4px 4px 0 var(--ink);
    font-size: 16px;
}

.hud strong {
    font-size: 22px;
    font-variant-numeric: tabular-nums;
}

.hud-icon {
    width: 24px;
    height: 24px;
    vertical-align: -6px;
}

.hud-beans {
    flex: 1 1 260px;
}

.hud-total {
    color: #8a7a6a;
}

.meter {
    position: relative;
    height: 14px;
    margin: 8px 0 4px;
    background: #f1e6d3;
    border: 2px solid var(--ink);
    border-radius: 999px;
    overflow: visible;
}

.meter-fill {
    height: 100%;
    width: 100%;
    border-radius: 999px;
    background: var(--good);
    transition: width 0.2s ease, background-color 0.3s ease;
}

.meter-fill[data-level='danger'] {
    background: var(--warn);
}

.meter-mark {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    color: #f5b800;
    font-style: normal;
    font-size: 16px;
    text-shadow: 0 0 2px var(--ink), 0 0 2px var(--ink);
    pointer-events: none;
}

.hud-time.urgent strong {
    color: var(--warn);
    animation: pulse 0.5s ease-in-out infinite alternate;
    display: inline-block;
}

@keyframes pulse {
    to {
        transform: scale(1.25);
    }
}

/* ---------- 画面 ---------- */
.stage {
    position: relative;
    border: 3px solid var(--ink);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 5px 5px 0 var(--ink);
    background: var(--paper);
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 4 / 3;
    touch-action: none;
}

.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 16px;
    background: rgba(255, 253, 247, 0.92);
    text-align: center;
}

.overlay[hidden] {
    display: none;
}

.cover {
    max-width: 70%;
    max-height: 58%;
    object-fit: contain;
    mix-blend-mode: multiply;   /* 去掉封面图自带的白底 */
}

.howto {
    margin: 0;
    font-size: 15px;
    line-height: 1.6;
    color: #6b5b4b;
}

.big-button {
    padding: 12px 36px;
    border: 3px solid var(--ink);
    border-radius: 16px;
    background: var(--accent);
    color: #fff;
    font-size: 22px;
    font-weight: bold;
    box-shadow: 4px 4px 0 var(--ink);
    cursor: pointer;
}

.big-button:active {
    transform: translate(3px, 3px);
    box-shadow: 1px 1px 0 var(--ink);
}

.big-button:disabled {
    opacity: 0.5;
    cursor: default;
}

/* 结算底板透一点，让彩纸露出来 */
.overlay.result {
    background: rgba(255, 253, 247, 0.74);
}

.result h2 {
    margin: 0;
    font-size: 34px;
}

.result p {
    margin: 0;
    font-size: 18px;
}

.result strong {
    font-size: 24px;
    color: var(--accent);
}

.stars {
    font-size: 54px;
    line-height: 1;
    letter-spacing: 6px;
    color: #f5b800;
    text-shadow: 2px 2px 0 var(--ink);
}

/* ---------- 触屏方向键：只在触屏设备上显示 ---------- */
.dpad {
    display: none;
    grid-template-columns: repeat(3, 64px);
    grid-template-rows: repeat(3, 64px);
    gap: 6px;
    justify-content: center;
    margin: 14px auto 0;
}

@media (pointer: coarse) {
    .dpad {
        display: grid;
    }
}

.dpad button {
    border: 3px solid var(--ink);
    border-radius: 16px;
    background: var(--paper);
    font-size: 24px;
    box-shadow: 3px 3px 0 var(--ink);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.dpad button:active {
    background: #ffe3e8;
    transform: translate(2px, 2px);
    box-shadow: 1px 1px 0 var(--ink);
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

/* ---------- 工具栏 ---------- */
.toolbar {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin-top: 16px;
}

.toolbar button {
    padding: 8px 14px;
    border: 2px solid var(--ink);
    border-radius: 12px;
    background: var(--paper);
    font-size: 15px;
    box-shadow: 2px 2px 0 var(--ink);
    cursor: pointer;
}

.toolbar button:active {
    transform: translate(2px, 2px);
    box-shadow: none;
}

/* ---------- 关于弹窗 ---------- */
.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(40, 30, 20, 0.45);
}

.modal[hidden] {
    display: none;
}

.modal-card {
    position: relative;
    width: min(100%, 460px);
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    padding: 22px 20px;
    background: var(--paper);
    border: 3px solid var(--ink);
    border-radius: 18px;
    box-shadow: 6px 6px 0 var(--ink);
    line-height: 1.6;
}

.modal-card h2 {
    margin: 0 0 10px;
}

.modal-close {
    position: absolute;
    top: 8px;
    right: 10px;
    width: 36px;
    height: 36px;
    border: none;
    background: none;
    font-size: 28px;
    cursor: pointer;
}

.rules {
    padding-left: 20px;
}

.credits {
    padding-top: 8px;
    border-top: 2px dashed #d9c9b0;
}

.credits p {
    margin: 4px 0;
}

@media (max-width: 520px) {
    .hud {
        font-size: 14px;
    }

    .hud strong {
        font-size: 18px;
    }

    .big-button {
        font-size: 18px;
        padding: 10px 26px;
    }

    .howto {
        font-size: 13px;
    }

    .result h2 {
        font-size: 24px;
    }

    .stars {
        font-size: 38px;
    }
}
