/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-dark: #0a0a1a;
    --bg-panel: #12122a;
    --bg-board: #0d0d20;
    --border-color: #2a2a5a;
    --text-primary: #e0e0ff;
    --text-secondary: #8888bb;
    --neon-blue: #00d4ff;
    --neon-pink: #ff00aa;
    --neon-green: #00ff88;
    --neon-yellow: #ffdd00;
    --neon-purple: #aa44ff;
    --toolbar-h: 44px;
}

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

body {
    position: fixed;
    inset: 0;
    overflow: hidden;
    background: var(--bg-dark);
    color: var(--text-primary);
    font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* ===== Layout shell ===== */
.game-wrapper {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    min-height: 100dvh;
    max-width: 560px;
    margin: 0 auto;
    padding:
        max(6px, env(safe-area-inset-top))
        max(10px, env(safe-area-inset-right))
        max(8px, env(safe-area-inset-bottom))
        max(10px, env(safe-area-inset-left));
    background:
        radial-gradient(circle at 50% 0%, rgba(0, 212, 255, 0.10), transparent 55%),
        radial-gradient(circle at 15% 100%, rgba(170, 68, 255, 0.10), transparent 45%),
        var(--bg-dark);
}

/* ===== Toolbar ===== */
.toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    height: var(--toolbar-h);
    flex: 0 0 auto;
}

.toolbar-spacer { flex: 1; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    height: 36px;
    padding: 0 10px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-panel);
    color: var(--text-primary);
    font-size: 1rem;
    line-height: 1;
    text-decoration: none;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.12s ease, border-color 0.12s ease;
}

.icon-btn:active {
    transform: scale(0.94);
    border-color: var(--neon-blue);
}

.icon-btn-text {
    font-size: 0.82rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}

/* ===== Stage (board + surrounding chrome) ===== */
.stage {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
}

/* Transparent to layout in portrait so the column order is unchanged. */
.side-panel { display: contents; }

.hud { order: 1; }
.progress-wrap { order: 2; }
.board-area { order: 3; }
.action-bar { order: 4; }

/* ===== HUD ===== */
.hud {
    display: flex;
    gap: 6px;
    flex: 0 0 auto;
    margin-top: 4px;
}

.hud-cell {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    padding: 6px 2px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: linear-gradient(180deg, rgba(30, 30, 70, 0.65), rgba(18, 18, 42, 0.9));
}

.hud-cell[hidden] { display: none; }

.hud-label {
    font-size: 0.62rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-secondary);
    white-space: nowrap;
}

.hud-value {
    font-size: 1.05rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
    color: var(--text-primary);
}

.hud-value.is-bump { animation: bump 0.3s ease; }
.hud-value.is-low { color: #ff6b6b; }

@keyframes bump {
    0%   { transform: scale(1); }
    45%  { transform: scale(1.28); color: var(--neon-yellow); }
    100% { transform: scale(1); }
}

/* ===== Progress ===== */
.progress-wrap {
    flex: 0 0 auto;
    margin-top: 6px;
}

.progress-bar {
    position: relative;
    height: 8px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.08);
    overflow: hidden;
}

.progress-fill {
    display: block;
    height: 100%;
    width: 0%;
    border-radius: 999px;
    background: linear-gradient(90deg, var(--neon-blue), var(--neon-green));
    transition: width 0.28s ease;
}

.progress-text {
    margin-top: 3px;
    text-align: center;
    font-size: 0.68rem;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

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

#board {
    display: block;
    border-radius: 18px;
    background: var(--bg-board);
    box-shadow:
        0 0 0 1px var(--border-color),
        0 18px 48px rgba(0, 0, 0, 0.55);
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Combo banner */
.combo-banner {
    position: absolute;
    top: 12%;
    left: 50%;
    transform: translate(-50%, -6px) scale(0.8);
    padding: 6px 18px;
    border-radius: 999px;
    background: rgba(10, 10, 26, 0.82);
    border: 1px solid var(--neon-yellow);
    color: var(--neon-yellow);
    font-size: 1.05rem;
    font-weight: 800;
    letter-spacing: 0.12em;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    text-shadow: 0 0 14px rgba(255, 221, 0, 0.55);
}

.combo-banner.is-show {
    animation: comboPop 0.85s ease forwards;
}

@keyframes comboPop {
    0%   { opacity: 0; transform: translate(-50%, 8px) scale(0.7); }
    22%  { opacity: 1; transform: translate(-50%, -6px) scale(1.12); }
    70%  { opacity: 1; transform: translate(-50%, -6px) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -22px) scale(0.95); }
}

/* Toast */
.toast {
    position: absolute;
    bottom: 10%;
    left: 50%;
    transform: translateX(-50%);
    padding: 7px 16px;
    border-radius: 999px;
    background: rgba(10, 10, 26, 0.9);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    font-size: 0.8rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.toast.is-show { opacity: 1; }

/* ===== Action bar ===== */
.action-bar {
    display: flex;
    gap: 10px;
    flex: 0 0 auto;
}

.btn {
    flex: 1 1 0;
    min-height: 46px;
    padding: 12px 18px;
    border: 1px solid var(--border-color);
    border-radius: 14px;
    background: var(--bg-panel);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.12s ease, box-shadow 0.16s ease, border-color 0.16s ease;
}

.btn:active { transform: scale(0.96); }

.btn-primary {
    border-color: transparent;
    background: linear-gradient(135deg, var(--neon-blue), #0080ff);
    color: #041022;
    box-shadow: 0 8px 22px rgba(0, 160, 255, 0.32);
}

.btn-secondary {
    border-color: var(--neon-purple);
    background: rgba(170, 68, 255, 0.14);
    color: #e6d4ff;
}

.btn-ghost:active { border-color: var(--neon-blue); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }

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

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

.overlay-content {
    width: 100%;
    max-width: 360px;
    max-height: 100%;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 24px 20px;
    border: 1px solid var(--border-color);
    border-radius: 22px;
    background: linear-gradient(180deg, rgba(24, 24, 58, 0.96), rgba(12, 12, 30, 0.98));
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6);
    text-align: center;
    animation: overlayIn 0.24s ease;
}

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

.title {
    font-size: clamp(2rem, 9vw, 2.8rem);
    font-weight: 800;
    letter-spacing: 0.14em;
    background: linear-gradient(135deg, var(--neon-blue), var(--neon-pink) 55%, var(--neon-yellow));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.subtitle {
    margin-bottom: 6px;
    color: var(--text-secondary);
    font-size: 0.86rem;
    letter-spacing: 0.04em;
}

.overlay-title {
    font-size: 1.7rem;
    font-weight: 800;
    letter-spacing: 0.1em;
    color: var(--neon-pink);
}

.overlay-title-win { color: var(--neon-green); }

.overlay-reason {
    margin-top: -4px;
    color: var(--text-secondary);
    font-size: 0.84rem;
}

.result-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 9px 14px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.05);
    font-size: 0.9rem;
    font-variant-numeric: tabular-nums;
}

.result-row span:last-child {
    font-weight: 700;
    color: var(--neon-yellow);
}

.legend {
    margin-top: 8px;
    padding-top: 12px;
    border-top: 1px solid var(--border-color);
    text-align: left;
}

.legend h3 {
    margin-bottom: 8px;
    font-size: 0.72rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--text-secondary);
    text-align: center;
}

.legend-row {
    display: flex;
    gap: 8px;
    align-items: baseline;
    margin-bottom: 6px;
    font-size: 0.76rem;
    color: var(--text-secondary);
    line-height: 1.5;
}

.legend-key {
    flex: 0 0 auto;
    min-width: 82px;
    color: var(--neon-blue);
    font-weight: 700;
}

/* ===== Responsive tweaks ===== */
@media (min-width: 600px) {
    .hud-label { font-size: 0.68rem; }
    .hud-value { font-size: 1.25rem; }
    .progress-text { font-size: 0.74rem; }
}

@media (max-height: 640px) {
    .hud-cell { padding: 4px 2px; }
    .hud-value { font-size: 0.95rem; }
    .board-area { margin: 6px 0; }
    .btn { min-height: 42px; padding: 10px 16px; }
    .overlay-content { padding: 18px 16px; gap: 10px; }
}

@media (max-height: 500px) and (orientation: landscape) {
    .game-wrapper { max-width: none; }
    .legend { display: none; }
    .overlay-content { max-width: 420px; }

    /* A single column would leave the board with ~150px of height. Put the
       HUD and buttons beside it instead so the board gets the full height. */
    .stage {
        flex-direction: row;
        align-items: stretch;
        gap: 12px;
    }

    .board-area {
        order: 0;
        flex: 1 1 auto;
        margin: 0;
    }

    .side-panel {
        order: 1;
        flex: 0 0 clamp(160px, 30vw, 230px);
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 8px;
        min-width: 0;
    }

    .hud { flex-wrap: wrap; }
    .hud-cell { flex: 1 1 44%; }
    .action-bar { flex-wrap: wrap; }
    .btn { min-height: 40px; padding: 8px 12px; font-size: 0.84rem; }
    .toolbar { height: 38px; }
}

@media (prefers-reduced-motion: reduce) {
    .combo-banner.is-show { animation-duration: 0.4s; }
    .overlay-content { animation: none; }
    .hud-value.is-bump { animation: none; }
}
