/* 2048 Master Edition Stylesheet */
:root {
    --bg: #BBADA0;
    --grid-bg: #CDC1B4;
    --slot-bg: #C4B6A8;
    --header-box: #8F7A66;
    --header-lbl: #EEE4DA;
    --header-val: #FFFFFF;
    --card-bg: #C6B8AB;
    --card-border: #B0A396;
    --title-color: #776E65;
    --sub-color: #8C8278;
    --btn-bot-active: #ED8E46;
    --btn-bot-idle: #8F7A66;
    --btn-general: #9B8C7D;
    --text-dark: #776E65;
    --text-light: #F9F6F2;
    --banner-bg: #EDC22E;
    --banner-text: #FFFFFF;
    --overlay-bg: rgba(238, 228, 218, 0.88);
    --badge-human-bg: #4A8505;
    --badge-bot-bg: #D96A22;
}

body.dark-theme {
    --bg: #1A1C24;
    --grid-bg: #282C38;
    --slot-bg: #303644;
    --header-box: #323848;
    --header-lbl: #AFB6C6;
    --header-val: #FFFFFF;
    --card-bg: #222630;
    --card-border: #343A4A;
    --title-color: #F5F7FC;
    --sub-color: #969EB2;
    --btn-bot-active: #EB7D37;
    --btn-bot-idle: #3C4458;
    --btn-general: #323848;
    --text-dark: #1E2028;
    --text-light: #F9F6F2;
    --banner-bg: #9ED18E;
    --banner-text: #192D19;
    --overlay-bg: rgba(20, 22, 28, 0.90);
    --badge-human-bg: #3D7A04;
    --badge-bot-bg: #C55818;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

body {
    background-color: var(--bg);
    color: var(--title-color);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 16px 8px;
    transition: background-color 0.25s ease, color 0.25s ease;
    overflow-x: hidden;
}

.game-container {
    width: 480px;
    max-width: 96vw;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* Header */
.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.title-group h1 {
    font-size: 42px;
    font-weight: 800;
    line-height: 1;
    letter-spacing: -1px;
}

.subtitle {
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 1px;
    color: var(--sub-color);
    margin-top: 4px;
}

.scores-group {
    display: flex;
    gap: 8px;
}

.score-box {
    background: var(--header-box);
    border-radius: 6px;
    padding: 6px 14px;
    text-align: center;
    min-width: 86px;
}

.score-lbl {
    font-size: 11px;
    font-weight: 700;
    color: var(--header-lbl);
    letter-spacing: 0.5px;
}

.score-val {
    font-size: 20px;
    font-weight: 800;
    color: var(--header-val);
    line-height: 1.2;
}

/* 4-Category Leaderboards Card */
.leaderboard-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 9px 12px;
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.lb-section {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.lb-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.lb-title {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.5px;
    color: var(--title-color);
}

.lb-status {
    font-size: 10px;
    font-weight: 700;
    color: #4CAF50;
}

.lb-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.lb-item {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 5px;
    padding: 4px 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

body.dark-theme .lb-item {
    background: rgba(255, 255, 255, 0.04);
}

.lb-badge {
    font-size: 9px;
    font-weight: 800;
    color: #FFFFFF;
    padding: 2px 5px;
    border-radius: 3px;
    letter-spacing: 0.5px;
    flex-shrink: 0;
}

.human-badge { background: var(--badge-human-bg); }
.bot-badge { background: var(--badge-bot-bg); }

.lb-name {
    font-size: 12px;
    font-weight: 700;
    color: var(--title-color);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 105px;
}

.lb-score {
    font-size: 13px;
    font-weight: 800;
    color: var(--title-color);
    margin-left: auto;
    flex-shrink: 0;
}

.session-section {
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    padding-top: 5px;
}

body.dark-theme .session-section {
    border-top-color: rgba(255, 255, 255, 0.06);
}

.session-item {
    padding: 3px 8px;
}

.lb-tag {
    font-size: 11px;
    font-weight: 600;
    color: var(--sub-color);
}

.lb-val {
    font-size: 12px;
    font-weight: 800;
    color: var(--title-color);
}

/* Action Buttons Bar */
.controls-bar {
    display: flex;
    gap: 6px;
}

.btn {
    background: var(--btn-general);
    color: #FFFFFF;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 700;
    padding: 8px 12px;
    cursor: pointer;
    transition: transform 0.08s ease, filter 0.15s ease, background-color 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.btn-bot {
    background: var(--btn-bot-idle);
    flex: 1.6;
    font-size: 14px;
}

.btn-bot.active {
    background: var(--btn-bot-active);
}

.btn-step { flex: 1; }
.btn-undo { flex: 1; }
.btn-speed { flex: 1.1; font-size: 12px; }
.btn-reset { flex: 0.9; }

/* Grid Board */
.board-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    background: var(--grid-bg);
    border-radius: 8px;
    padding: 9px;
}

.grid-board {
    position: relative;
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 9px;
}

.grid-cell {
    background: var(--slot-bg);
    border-radius: 6px;
    width: 100%;
    height: 100%;
}

/* Tile Layer */
.tile-container {
    position: absolute;
    top: 9px;
    left: 9px;
    right: 9px;
    bottom: 9px;
    pointer-events: none;
}

.tile {
    position: absolute;
    width: calc((100% - 27px) / 4);
    height: calc((100% - 27px) / 4);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 32px;
    transition: left 75ms cubic-bezier(0.25, 1, 0.5, 1), top 75ms cubic-bezier(0.25, 1, 0.5, 1);
    will-change: left, top;
}

.tile-inner {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
}

.tile-new .tile-inner {
    animation: pop 160ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.tile-merged .tile-inner {
    animation: merge-pop 180ms ease-in-out;
}

@keyframes pop {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes merge-pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.18); }
    100% { transform: scale(1); }
}

/* Tile Colors */
.tile-2 .tile-inner { background: #F6F0E8; color: #776E65; }
.tile-4 .tile-inner { background: #F2CE8E; color: #776E65; }
.tile-8 .tile-inner { background: #F2B179; color: #F9F6F2; }
.tile-16 .tile-inner { background: #F59563; color: #F9F6F2; }
.tile-32 .tile-inner { background: #F67C5F; color: #F9F6F2; }
.tile-64 .tile-inner { background: #F65E3B; color: #F9F6F2; }
.tile-128 .tile-inner { background: #EDCF72; color: #F9F6F2; font-size: 28px; }
.tile-256 .tile-inner { background: #EDCC61; color: #F9F6F2; font-size: 28px; }
.tile-512 .tile-inner { background: #EDC850; color: #F9F6F2; font-size: 28px; }
.tile-1024 .tile-inner { background: #EDC53F; color: #F9F6F2; font-size: 22px; }
.tile-2048 .tile-inner { background: #EDC22E; color: #F9F6F2; font-size: 22px; }
.tile-4096 .tile-inner { background: #9ED18E; color: #1E331E; font-size: 22px; }
.tile-8192 .tile-inner { background: #73BA9B; color: #F9F6F2; font-size: 22px; }
.tile-16384 .tile-inner { background: #5AA3A8; color: #F9F6F2; font-size: 18px; }
.tile-32768 .tile-inner { background: #557EAC; color: #F9F6F2; font-size: 18px; }
.tile-65536 .tile-inner { background: #4D5AA6; color: #F9F6F2; font-size: 18px; }

/* Stats Bar */
.stats-bar {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.stat-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 6px;
    padding: 6px 4px;
    text-align: center;
}

.stat-title {
    font-size: 10px;
    font-weight: 700;
    color: var(--sub-color);
}

.stat-val {
    font-size: 14px;
    font-weight: 800;
    color: var(--title-color);
    margin-top: 2px;
}

/* Footer Controls & Legend */
.footer-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 2px;
}

.footer-btn-group {
    display: flex;
    gap: 8px;
}

.footer-btn {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    color: var(--sub-color);
    border-radius: 5px;
    font-size: 11px;
    font-weight: 700;
    padding: 6px 12px;
    cursor: pointer;
}

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

.run-mode-tag {
    font-size: 11px;
    font-weight: 700;
    color: var(--sub-color);
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    padding: 6px 10px;
    border-radius: 5px;
}

.controls-legend-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 9px 12px;
    margin-top: 2px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.controls-legend-title {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.5px;
    color: var(--title-color);
}

.controls-legend-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px 10px;
}

.control-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(0, 0, 0, 0.04);
    border-radius: 5px;
    padding: 4px 8px;
    font-size: 11px;
}

body.dark-theme .control-item {
    background: rgba(255, 255, 255, 0.04);
}

.control-action {
    font-weight: 700;
    color: var(--title-color);
}

.control-keys {
    font-weight: 600;
    color: var(--sub-color);
    display: flex;
    align-items: center;
    gap: 2px;
}

.control-keys kbd {
    background: var(--grid-bg);
    border: 1px solid var(--card-border);
    border-radius: 3px;
    padding: 1px 4px;
    font-family: inherit;
    font-size: 10px;
    font-weight: 800;
    color: var(--title-color);
}

/* Non-Interrupting High Score Celebration Animations */
.score-box {
    transition: box-shadow 0.25s ease, transform 0.25s ease;
}

.score-box.record-active-glow {
    box-shadow: 0 0 0 2px #EDC22E, 0 0 14px rgba(237, 194, 46, 0.55);
}

.score-record-pulse {
    animation: score-celebrate 650ms cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.lb-record-pulse {
    animation: lb-celebrate 800ms ease-out;
}

@keyframes score-celebrate {
    0% { transform: scale(1); }
    35% { transform: scale(1.16); box-shadow: 0 0 0 3px #EDC22E, 0 0 20px rgba(237, 194, 46, 0.85); }
    70% { transform: scale(0.97); }
    100% { transform: scale(1); }
}

@keyframes lb-celebrate {
    0% { background: rgba(237, 194, 46, 0.55); transform: scale(1.03); }
    100% { transform: scale(1); }
}

/* Milestone & High Score Popup Banner */
.milestone-banner {
    position: absolute;
    top: 14px;
    left: 14px;
    right: 14px;
    background: var(--banner-bg);
    color: var(--banner-text);
    font-size: 14px;
    font-weight: 800;
    text-align: center;
    padding: 10px 14px;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    opacity: 0;
    pointer-events: none;
    transform: translateY(-8px) scale(0.96);
    transition: opacity 0.25s ease, transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 15;
}

.milestone-banner.record-banner {
    background: linear-gradient(135deg, #EDC22E 0%, #ED8E46 100%);
    color: #FFFFFF;
    box-shadow: 0 6px 20px rgba(237, 142, 70, 0.45);
}

.milestone-banner.active {
    opacity: 1;
    transform: translateY(0) scale(1);
}

/* Floating Bar when Inspecting Final Board after Game Over */
.inspect-board-bar {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    background: rgba(26, 28, 36, 0.90);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    color: #FFFFFF;
    padding: 7px 12px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
    opacity: 0;
    pointer-events: none;
    transform: translateY(-6px);
    transition: opacity 0.2s ease, transform 0.2s ease;
    z-index: 25;
}

.inspect-board-bar.active {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.inspect-board-lbl {
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.2px;
}

.inspect-board-actions {
    display: flex;
    gap: 6px;
}

.btn-inspect-action {
    background: rgba(255, 255, 255, 0.16);
    color: #FFFFFF;
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 4px;
    padding: 4px 9px;
    font-size: 11px;
    font-weight: 700;
    cursor: pointer;
}

.btn-inspect-action.primary {
    background: var(--btn-bot-active);
    border-color: transparent;
}

/* Game Over Overlay */
.game-over-overlay {
    position: absolute;
    inset: 0;
    background: var(--overlay-bg);
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 20;
}

.game-over-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.game-over-title {
    font-size: 38px;
    font-weight: 800;
    color: var(--title-color);
}

.game-over-desc {
    font-size: 14px;
    font-weight: 600;
    color: var(--sub-color);
}

.game-over-actions {
    display: flex;
    gap: 10px;
    margin-top: 4px;
}

.btn-view-board {
    background: var(--header-box);
    padding: 10px 16px;
    font-size: 14px;
}

.btn-try-again {
    background: var(--btn-bot-active);
    padding: 10px 18px;
    font-size: 14px;
}

/* New All-Time Record Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 100;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    background: var(--bg);
    border: 2px solid var(--banner-bg);
    border-radius: 12px;
    padding: 24px;
    width: 90%;
    max-width: 380px;
    text-align: center;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
    transform: scale(0.9);
    transition: transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.modal-overlay.active .modal-content {
    transform: scale(1);
}

.modal-icon {
    font-size: 44px;
    line-height: 1;
}

.modal-title {
    font-size: 22px;
    font-weight: 800;
    color: var(--title-color);
    letter-spacing: -0.5px;
}

.modal-category {
    font-size: 13px;
    font-weight: 700;
    color: var(--sub-color);
}

.modal-score {
    font-size: 34px;
    font-weight: 900;
    color: var(--btn-bot-active);
    line-height: 1;
}

.modal-instruction {
    font-size: 13px;
    font-weight: 600;
    color: var(--title-color);
}

.modal-input-group {
    display: flex;
    gap: 8px;
}

.modal-input {
    flex: 1;
    padding: 10px 12px;
    border-radius: 6px;
    border: 2px solid var(--card-border);
    background: var(--grid-bg);
    color: var(--title-color);
    font-size: 15px;
    font-weight: 700;
    outline: none;
    transition: border-color 0.15s ease;
}

.modal-input:focus {
    border-color: var(--btn-bot-active);
}

.btn-save-record {
    background: var(--btn-bot-active);
    color: #FFFFFF;
    font-size: 14px;
    padding: 10px 16px;
    border-radius: 6px;
    border: none;
    font-weight: 800;
    cursor: pointer;
}

.btn-skip-record {
    background: transparent;
    color: var(--sub-color);
    border: none;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
    align-self: center;
}

.modal-security-note {
    font-size: 11px;
    font-weight: 600;
    color: var(--sub-color);
    opacity: 0.8;
}
