*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --bg: #15110b;
    --panel: #2a2116;
    --line: #6a5334;
    --text: #fff2df;
    --muted: #cbb392;
    --accent: #ffbe6b;
    --board: #201a11;
    --cao: #d4553f;
    --general: #c08a3e;
    --soldier: #7a8f5a;
}

html, body { height: 100%; overscroll-behavior: none; }

body {
    position: fixed;
    inset: 0;
    overflow: hidden;
    color: var(--text);
    font-family: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Segoe UI', sans-serif;
    background:
        radial-gradient(circle at 50% 0%, rgba(255, 190, 107, 0.13), transparent 55%),
        linear-gradient(180deg, #0d0a06, #1a140c);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
}

.shell {
    display: flex; flex-direction: column;
    height: 100%; min-height: 100dvh;
    max-width: 480px; margin: 0 auto;
    padding:
        max(6px, env(safe-area-inset-top))
        max(8px, env(safe-area-inset-right))
        max(8px, env(safe-area-inset-bottom))
        max(8px, env(safe-area-inset-left));
}

.toolbar { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; height: 38px; }
.toolbar-spacer { flex: 1; }

.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 34px; height: 31px; padding: 0 10px;
    border: 1px solid var(--line); border-radius: 9px;
    background: var(--panel); color: var(--text);
    font-family: inherit; font-size: 0.88rem; line-height: 1;
    text-decoration: none; cursor: pointer;
    transition: transform 0.12s ease, border-color 0.12s ease;
}

.icon-btn-text { font-size: 0.76rem; font-weight: 600; }
.icon-btn:active { transform: scale(0.94); border-color: var(--accent); }

.hud { display: flex; gap: 6px; flex: 0 0 auto; margin: 5px 0; }

.hud span {
    flex: 1; display: flex; flex-direction: column; align-items: center;
    padding: 4px 2px; border-radius: 9px; background: rgba(255, 255, 255, 0.05);
}

.hud b { font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.hud i { font-style: normal; font-size: 0.58rem; letter-spacing: 0.08em; color: var(--muted); }

/* ===== Board ===== */
.board-area { flex: 1 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; }

.board {
    position: relative;
    border-radius: 10px;
    background: var(--board);
    box-shadow: inset 0 0 0 2px #4a3a24, 0 12px 32px rgba(0, 0, 0, 0.5);
    touch-action: none;
}

.exit {
    position: absolute;
    border-top: 3px dashed var(--accent);
    opacity: 0.55;
    pointer-events: none;
}

.piece {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 7px;
    font-weight: 800;
    line-height: 1.05;
    text-align: center;
    color: #22150a;
    box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.2), 0 2px 5px rgba(0, 0, 0, 0.4);
    transition: transform 0.11s ease;
    will-change: transform;
    cursor: grab;
}

.piece.is-cao { background: linear-gradient(180deg, #e8705a, var(--cao)); color: #33110b; }
.piece.is-general { background: linear-gradient(180deg, #dba75a, var(--general)); }
.piece.is-soldier { background: linear-gradient(180deg, #9db977, var(--soldier)); }
.piece.is-dragging { transition: none; filter: brightness(1.12); z-index: 10; }

/* ===== Tools ===== */
.tools { display: flex; gap: 5px; flex: 0 0 auto; margin-top: 8px; }

.tool {
    flex: 1; min-height: 48px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
    border: 1px solid var(--line); border-radius: 11px;
    background: var(--panel); color: var(--text);
    font-family: inherit; cursor: pointer; transition: transform 0.1s ease;
}

.tool i { font-style: normal; font-size: 1rem; line-height: 1; }
.tool span { font-size: 0.58rem; color: var(--muted); }
.tool:active { transform: scale(0.95); }
.tool:disabled { opacity: 0.4; }

.hint { margin-top: 5px; text-align: center; font-size: 0.64rem; color: var(--muted); }

/* ===== Overlays ===== */
.overlay {
    position: absolute; inset: 0; display: none;
    align-items: center; justify-content: center; padding: 18px;
    background: rgba(8, 5, 3, 0.9);
    backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px);
    z-index: 50;
}

.overlay.is-open { display: flex; }

.panel {
    width: 100%; max-width: 330px; max-height: 100%; overflow-y: auto;
    display: flex; flex-direction: column; gap: 10px;
    padding: 20px 16px;
    border: 1px solid var(--line); border-radius: 20px;
    background: linear-gradient(180deg, #35291a, #1d160d);
    text-align: center;
    animation: pop 0.22s ease;
}

.panel-wide { max-width: 380px; }

@keyframes pop { from { opacity: 0; transform: translateY(12px) scale(0.97); } to { opacity: 1; transform: none; } }

.panel-title { font-size: 1.45rem; font-weight: 800; letter-spacing: 0.08em; color: var(--accent); }

.grid-levels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }

.level-cell {
    aspect-ratio: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
    border: 1px solid var(--line); border-radius: 10px;
    background: rgba(255, 255, 255, 0.05); color: var(--text);
    font-family: inherit; font-size: 0.9rem; font-weight: 700; cursor: pointer;
}

.level-cell small { font-size: 0.5rem; color: var(--accent); }
.level-cell.is-done { border-color: #8fd28f; background: rgba(143, 210, 143, 0.14); }
.level-cell.is-current { border-color: var(--accent); }

.btn {
    min-height: 46px; padding: 12px 20px;
    border: 1px solid var(--line); border-radius: 13px;
    background: rgba(255, 255, 255, 0.05); color: var(--text);
    font-family: inherit; font-size: 0.92rem; font-weight: 700;
    text-decoration: none; display: flex; align-items: center; justify-content: center;
    cursor: pointer;
}

.btn:active { transform: scale(0.97); }
.btn-primary { border-color: transparent; background: linear-gradient(135deg, #ffd08a, #f0a03c); color: #2a1600; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.stat { padding: 9px 6px; border-radius: 11px; background: rgba(255, 255, 255, 0.06); }
.stat b { display: block; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: 0.62rem; color: var(--muted); }

@media (max-height: 680px) {
    .hud { margin: 3px 0; }
    .tool { min-height: 42px; }
    .hint { display: none; }
}

@media (prefers-reduced-motion: reduce) { .panel { animation: none; } .piece { transition: none; } }
