/* 
 * Commander Lab - Premium Tabletop UI
 * Professional Design with Dynamic Gameplay Animations
 */

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

:root {
    --app-bg: #050608;
    --app-surface: #0e111a;
    --app-surface-light: rgba(255, 255, 255, 0.05);
    --app-accent: #6366f1;
    --app-danger: #ff4757;
    --app-warning: #f59e0b;
    --app-success: #2ed573;
    --app-text: #ffffff;
    --app-text-dim: #94a3b8;
    --app-border: rgba(255, 255, 255, 0.1);
    
    /* Player Colors */
    --color-p1: #ef4444; --color-p1-rgb: 239, 68, 68;
    --color-p2: #3b82f6; --color-p2-rgb: 59, 130, 246;
    --color-p3: #10b981; --color-p3-rgb: 16, 185, 129;
    --color-p4: #f59e0b; --color-p4-rgb: 245, 158, 11;
    --color-p5: #8b5cf6; --color-p5-rgb: 139, 92, 246;
    --color-p6: #ec4899; --color-p6-rgb: 236, 72, 153;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; outline: none; }

body.mtg-app-active {
    display: block !important; opacity: 1 !important; visibility: visible !important;
    background-color: var(--app-bg) !important; margin: 0; padding: 0;
    font-family: 'Plus Jakarta Sans', sans-serif; color: var(--app-text);
}

body.mtg-game-running {
    overflow: hidden;
    height: 100vh; height: 100dvh; width: 100vw; position: fixed;
}

body.mtg-game-running #nav-placeholder,
body.mtg-game-running #footer-placeholder {
    display: none;
}

body.mtg-setup-running {
    min-height: 100vh;
    overflow-x: hidden;
    background:
        radial-gradient(circle at 15% 20%, rgba(99,102,241,0.22), transparent 32rem),
        radial-gradient(circle at 85% 12%, rgba(16,185,129,0.16), transparent 28rem),
        radial-gradient(circle at 70% 90%, rgba(245,158,11,0.16), transparent 30rem),
        var(--app-bg) !important;
}

#app-root {
    display: flex;
    min-height: 100vh;
    width: 100%;
    flex-direction: column;
}

body.mtg-game-running #app-root {
    height: 100vh;
    height: 100dvh;
    min-height: 0;
}

.app-container { height: 100%; width: 100%; display: flex; flex-direction: column; position: relative; }

/* Setup page inside the regular Scarekroow shell */
body.mtg-setup-running .app-container {
    min-height: calc(100vh - var(--nav-height, 72px));
    height: auto;
}

.setup-view {
    width: min(1200px, calc(100% - 32px));
    margin: 0 auto;
    padding: calc(var(--nav-height, 72px) + 40px) 0 80px;
    display: grid;
    grid-template-columns: 1fr 1.2fr;
    align-items: start;
    gap: 64px;
}

.setup-intro {
    padding-top: 18px;
    position: sticky;
    top: calc(var(--nav-height, 72px) + 24px);
}

.setup-kicker {
    margin: 0 0 10px;
    color: var(--app-text-dim);
    font-size: 0.78rem;
    font-weight: 900;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.setup-title {
    margin: 0;
    font-family: 'Outfit', sans-serif;
    font-size: clamp(3rem, 6vw, 5.8rem);
    line-height: 0.9;
    letter-spacing: -0.02em;
    text-shadow: 0 20px 70px rgba(99,102,241,0.38);
}

.setup-copy {
    max-width: 34rem;
    margin: 20px 0 0;
    color: var(--app-text-dim);
    font-size: 1.1rem;
    line-height: 1.6;
}

.setup-orbit {
    position: relative;
    width: min(340px, 80vw);
    aspect-ratio: 1;
    margin-top: 48px;
    border-radius: 50%;
    border: 1px solid rgba(255,255,255,0.12);
    background:
        radial-gradient(circle, rgba(255,255,255,0.08) 0 18%, transparent 19%),
        radial-gradient(circle, rgba(99,102,241,0.18), rgba(0,0,0,0.08) 54%, transparent 70%);
}

.setup-orbit::before,
.setup-orbit::after {
    content: '';
    position: absolute;
    inset: 14%;
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 50%;
}

.setup-orbit::after {
    inset: 32%;
    border-color: rgba(255,255,255,0.12);
}

.orbit-core {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 28%;
    aspect-ratio: 1;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: rgba(255,255,255,0.94);
    color: #050608;
    font-family: 'Outfit', sans-serif;
    font-size: clamp(2rem, 5vw, 3rem);
    font-weight: 900;
    box-shadow: 0 0 36px rgba(255,255,255,0.28);
}

.orbit-seat {
    position: absolute;
    width: 50px;
    height: 50px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: color-mix(in srgb, var(--seat-color) 78%, #000);
    border: 2px solid rgba(255,255,255,0.5);
    color: #fff;
    font-family: 'Outfit', sans-serif;
    font-weight: 900;
}

.orbit-seat.seat-1 { left: 50%; top: 0; transform: translate(-50%, -35%); }
.orbit-seat.seat-2 { right: 6%; top: 22%; transform: translate(35%, -35%); }
.orbit-seat.seat-3 { right: 6%; bottom: 22%; transform: translate(35%, 35%); }
.orbit-seat.seat-4 { left: 50%; bottom: 0; transform: translate(-50%, 35%); }
.orbit-seat.seat-5 { left: 6%; bottom: 22%; transform: translate(-35%, 35%); }
.orbit-seat.seat-6 { left: 6%; top: 22%; transform: translate(-35%, -35%); }

.setup-card {
    padding: clamp(20px, 4vw, 32px);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    background:
        linear-gradient(135deg, rgba(255,255,255,0.08), rgba(255,255,255,0.02)),
        rgba(14,17,26,0.85);
    box-shadow: 0 40px 100px rgba(0,0,0,0.4);
    backdrop-filter: blur(24px);
}

.setup-card-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.setup-card-head h2 {
    margin: 0;
    font-family: 'Outfit', sans-serif;
    font-size: 1.5rem;
}

.setup-settings-btn {
    min-height: 38px;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 8px;
    background: rgba(255,255,255,0.06);
    color: #fff;
    padding: 0 16px;
    font-weight: 700;
    cursor: pointer;
}

.setup-players-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.p-setup-row {
    display: grid;
    grid-template-columns: 6px 1fr auto;
    gap: 12px;
    align-items: stretch;
    min-height: 80px;
    padding: 12px;
    border-radius: 10px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.06);
    transition: all 0.2s ease;
}

.p-setup-row:hover {
    background: rgba(255,255,255,0.07);
    border-color: rgba(255,255,255,0.12);
}

.setup-color-mark {
    border-radius: 4px;
}

.p-inputs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.p-inputs input {
    width: 100%;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 8px;
    background: rgba(0,0,0,0.3);
    color: #fff;
    padding: 0 14px;
    font-size: 0.95rem;
}

.setup-p-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.high-roll-btn,
.remove-p-btn {
    height: 100%;
    min-height: 48px;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 8px;
    background: rgba(255,255,255,0.08);
    color: #fff;
    font-weight: 800;
    cursor: pointer;
}

.high-roll-btn { min-width: 80px; padding: 0 14px; }
.high-roll-btn.done { background: rgba(46,213,115,0.15); border-color: rgba(46,213,115,0.4); color: #bdf7d0; }
.remove-p-btn { width: 48px; }

.setup-actions {
    display: grid;
    grid-template-columns: 1fr 1.4fr;
    gap: 12px;
    margin-top: 24px;
}

/* --- RESPONSIVE ADAPTATIONS --- */
@media (max-width: 1024px) {
    .setup-view {
        grid-template-columns: 1fr;
        gap: 48px;
        padding-top: calc(var(--nav-height, 72px) + 24px);
        width: min(700px, calc(100% - 32px));
    }
    
    .setup-intro {
        position: static;
        text-align: center;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    
    .setup-orbit {
        margin: 32px auto 0;
        width: min(300px, 60vw);
    }
}

@media (max-width: 600px) {
    .setup-view {
        width: 100%;
        padding: calc(var(--nav-height, 72px) + 20px) 16px 60px;
        gap: 24px;
    }
    
    .setup-title {
        font-size: 3rem;
    }
    
    .setup-copy {
        font-size: 1rem;
    }

    .p-setup-row {
        grid-template-columns: 4px 1fr;
        padding: 10px;
        min-height: auto;
    }
    
    .setup-p-actions {
        grid-column: 2;
        margin-top: 8px;
        justify-content: flex-end;
    }
    
    .high-roll-btn, .remove-p-btn {
        min-height: 40px;
    }

    .setup-actions {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 400px) {
    .p-inputs {
        grid-template-columns: 1fr;
    }
    
    .setup-card {
        padding: 16px;
    }
}

/* --- GAMEPLAY GRID --- */
.game-view { 
    flex: 1; display: grid; gap: 1px; background: #000; 
    height: 100%; width: 100%; overflow: hidden; 
}

.player-panel {
    position: relative; 
    background-color: rgba(var(--panel-color-rgb), 0.15);
    display: flex; flex-direction: column; overflow: hidden; height: 100%;
    transition: background 0.4s cubic-bezier(0.23, 1, 0.32, 1);
}

.player-panel::after {
    content: ''; position: absolute; inset: 0;
    background: radial-gradient(circle at center, rgba(var(--panel-color-rgb), 0.1), transparent 70%);
    pointer-events: none; z-index: 0;
}

@keyframes hit-shake {
    0% { transform: translate(0, 0); }
    10% { transform: translate(-4px, -4px); }
    20% { transform: translate(4px, 4px); }
    30% { transform: translate(-4px, 4px); }
    40% { transform: translate(4px, -4px); }
    50% { transform: translate(-4px, 0); }
    100% { transform: translate(0, 0); }
}

@keyframes flash-red {
    0% { background-color: rgba(255, 71, 87, 0.6); }
    100% { background-color: rgba(var(--panel-color-rgb), 0.15); }
}

@keyframes flash-green {
    0% { background-color: rgba(46, 213, 115, 0.5); }
    100% { background-color: rgba(var(--panel-color-rgb), 0.15); }
}

.player-panel.taking-damage { animation: flash-red 0.5s ease-out; }
.player-panel.taking-damage .life-total { animation: hit-shake 0.4s ease-in-out; }
.player-panel.healing { animation: flash-green 0.5s ease-out; }

.dead-overlay, .winner-overlay {
    position: absolute; inset: 0; z-index: 200;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.9); backdrop-filter: blur(12px); animation: fadeIn 0.4s ease;
    padding: 1rem; text-align: center;
}

.dead-text, .winner-text {
    font-family: 'Outfit', sans-serif; font-size: clamp(2rem, 8vw, 4rem); font-weight: 900;
    text-transform: uppercase; letter-spacing: 6px; margin-bottom: 1.5rem;
}
.dead-text { color: var(--app-danger); transform: rotate(-10deg); text-shadow: 0 0 30px rgba(255, 71, 87, 0.6); }
.winner-text { color: var(--app-warning); text-shadow: 0 0 30px rgba(245, 158, 11, 0.8); }
.winner-crown { font-size: 3rem; margin-bottom: 0.5rem; animation: bounce 2s infinite; }

.revive-btn, .winner-btn {
    padding: 12px 28px; border-radius: 40px; border: 1px solid rgba(255,255,255,0.2);
    background: rgba(255,255,255,0.1); color: #fff; font-weight: 800; text-transform: uppercase;
    cursor: pointer; transition: all 0.2s; letter-spacing: 1px; font-size: 0.85rem;
}

/* Header */
.panel-header {
    padding: 4px 8px; display: flex; justify-content: space-between; align-items: center;
    background: rgba(0,0,0,0.5);
    z-index: 30; border-top: 4px solid var(--panel-color);
}
.p-meta { display: flex; flex-direction: column; overflow: hidden; }
.p-name { font-family: 'Outfit', sans-serif; font-weight: 800; text-transform: uppercase; font-size: 0.7rem; letter-spacing: 0.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-cmd { font-size: 0.5rem; color: #fff; opacity: 0.6; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100px; }
.status-icon { margin-left: 6px; font-size: 1rem; }
.high-roll-badge {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 44px; height: 20px; padding: 0 7px; border-radius: 6px;
    background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.16);
    color: #fff; font-size: 0.55rem; font-weight: 900; text-transform: uppercase;
}

/* Panel Content */
.panel-content { flex: 1; display: flex; flex-direction: column; position: relative; min-height: 0; z-index: 5; }
.view-container { flex: 1; display: flex; flex-direction: column; height: 100%; overflow: hidden; }

/* Life View */
.life-view { flex: 1; display: flex; flex-direction: column; height: 100%; }
.life-main { flex: 1; display: flex; align-items: center; justify-content: center; position: relative; }
.life-total {
    font-family: 'Outfit', sans-serif; font-size: clamp(3rem, 18vh, 35vw); font-weight: 900;
    z-index: 10; pointer-events: none; line-height: 0.8; transition: transform 0.1s;
    text-shadow: 0 5px 20px rgba(0,0,0,0.4);
}
.life-total.changing { transform: scale(1.1); }

.floating-change {
    position: absolute; top: 43%; left: 50%; transform: translate(-50%, -50%);
    font-family: 'Outfit', sans-serif; font-size: 2.2rem; font-weight: 900; pointer-events: none; z-index: 25; opacity: 0;
}
.floating-change.active { opacity: 1; transform: translate(-50%, -150%); transition: all 0.7s cubic-bezier(0.23, 1, 0.32, 1); }
.floating-change.neg { color: var(--app-danger); }
.floating-change.pos { color: var(--app-success); }

.hit-zones { position: absolute; inset: 0; display: flex; z-index: 5; }
.hit-zone { flex: 1; cursor: pointer; }

/* Quick Bar (Tax, Mon, Psn) */
.quick-bar {
    display: flex; justify-content: center; gap: 4px; padding: 4px;
    background: rgba(0,0,0,0.4); z-index: 40; border-top: 1px solid rgba(255,255,255,0.05);
}
.ui-chip {
    background: rgba(0,0,0,0.5); border: 1px solid rgba(255,255,255,0.1); border-radius: 10px;
    padding: 2px 4px; display: flex; flex-direction: column; align-items: center; min-width: 54px;
}
.ui-chip.active { border-color: #fff; background: rgba(255,255,255,0.2); }
.ui-chip .label { font-size: 0.45rem; font-weight: 800; color: rgba(255,255,255,0.6); text-transform: uppercase; margin-bottom: 1px; }
.chip-controls { display: flex; align-items: center; gap: 3px; }
.chip-btn {
    width: 20px; height: 20px; border-radius: 5px; border: 1px solid rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.1); color: #fff; font-weight: 900; cursor: pointer;
    display: flex; align-items: center; justify-content: center; font-size: 0.7rem;
}
.ui-chip .val { font-size: 0.75rem; font-weight: 900; min-width: 1.2ch; text-align: center; font-family: 'Outfit', sans-serif; }

/* --- CMD & Extra Views Styling Fixes --- */
.scroll-view {
    flex: 1; overflow-y: auto; padding: 12px 10px; display: flex; flex-direction: column; gap: 10px;
    scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.1) transparent;
}
.scroll-view::-webkit-scrollbar { width: 4px; }
.scroll-view::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 4px; }

.section-title {
    font-size: 0.6rem; font-weight: 900; text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--app-text-dim); margin-bottom: 4px; border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 2px;
}

/* Commander Grid */
.cmd-grid { display: flex; flex-direction: column; gap: 4px; }
.cmd-grid.compact .cmd-row { padding: 4px 8px; }
.cmd-row {
    background: rgba(255,255,255,0.05); border-radius: 8px; padding: 8px 12px;
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    border: 1px solid rgba(255,255,255,0.03);
}
.cmd-row-label { font-size: 0.75rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.cmd-row-controls { display: flex; align-items: center; gap: 10px; }
.cmd-btn {
    width: 34px; height: 34px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.12);
    background: rgba(255,255,255,0.08); color: #fff; font-weight: 900; cursor: pointer;
    display: flex; align-items: center; justify-content: center; font-size: 1rem;
    transition: background 0.2s, transform 0.1s;
}
.cmd-btn:active { background: rgba(255,255,255,0.2); transform: scale(0.92); }
.cmd-row-val { font-family: 'Outfit', sans-serif; font-weight: 900; font-size: 1.2rem; min-width: 1.8ch; text-align: center; }

.ui-row {
    background: rgba(255,255,255,0.05); border-radius: 8px; padding: 10px 14px;
    display: flex; align-items: center; justify-content: space-between;
}
.row-label { font-size: 0.8rem; font-weight: 700; }
.row-controls { display: flex; align-items: center; gap: 14px; }
.row-val { font-family: 'Outfit', sans-serif; font-weight: 900; font-size: 1.2rem; min-width: 1.8ch; text-align: center; }

/* Counters Grid */
.counters-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.counter-item {
    background: rgba(255,255,255,0.05); border-radius: 12px; padding: 10px 8px;
    display: flex; flex-direction: column; align-items: center; gap: 8px;
    border: 1px solid rgba(255,255,255,0.03);
}
.counter-icon { font-size: 0.65rem; font-weight: 900; text-transform: uppercase; color: var(--app-text-dim); }
.counter-controls { display: flex; align-items: center; justify-content: center; gap: 12px; width: 100%; }
.counter-val { font-family: 'Outfit', sans-serif; font-weight: 900; font-size: 1.2rem; min-width: 1.5ch; text-align: center; }

/* Ring Tracker */
.ring-tracker { background: rgba(0,0,0,0.2); padding: 10px; border-radius: 12px; }
.ring-selector { display: flex; gap: 6px; justify-content: center; margin-top: 6px; }
.ring-step {
    flex: 1; height: 8px; border-radius: 4px; background: rgba(255,255,255,0.1); border: none; cursor: pointer;
    transition: all 0.2s;
}
.ring-step.active { background: var(--app-warning); box-shadow: 0 0 10px var(--app-warning); }

/* Mechanics */
.mechanics-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.mechanic-btn {
    padding: 12px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.05); color: #fff; font-size: 0.75rem; font-weight: 800; cursor: pointer;
    transition: all 0.2s;
}
.mechanic-btn.active { border-color: #fff; background: rgba(255,255,255,0.25); font-weight: 900; }

.add-custom-btn {
    width: 100%; padding: 12px; margin-top: 6px; border-radius: 10px; border: 1px dashed rgba(255,255,255,0.2);
    background: transparent; color: var(--app-text-dim); font-size: 0.75rem; font-weight: 700; cursor: pointer;
}

/* Panel Navigation (Tabs) */
.panel-nav { display: flex; background: rgba(0,0,0,0.5); padding: 2px; gap: 2px; }
.panel-nav-btn {
    flex: 1; padding: 6px 2px; background: rgba(255,255,255,0.05); color: rgba(255,255,255,0.4); border: none;
    font-size: 0.55rem; font-weight: 800; text-transform: uppercase; border-radius: 5px; cursor: pointer; transition: all 0.2s;
}
.panel-nav-btn.active { background: #fff; color: #000; }

/* --- HUB NAVIGATION --- */
.app-navigation {
    position: absolute;
    left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    width: 72px; height: 72px;
    border-radius: 50%;
    z-index: 300;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: visible;
    background: rgba(0,0,0,0.9);
    border: 2px solid rgba(255,255,255,0.15);
    box-shadow: 0 0 20px rgba(0,0,0,0.5), 0 0 10px var(--app-accent);
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.nav-item {
    position: absolute;
    width: 80px; height: 80px;
    opacity: 0; pointer-events: none;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    background: rgba(0,0,0,0.94);
    border: 2px solid rgba(255,255,255,0.2);
    color: #fff;
    border-radius: 50%;
    font-size: 2.2rem;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    box-shadow: 0 8px 20px rgba(0,0,0,0.7);
    padding: 0;
}

.nav-item:hover { background: rgba(255,255,255,0.15); border-color: #fff; }

/* Central Hub Button */
.nav-item.hub-main-btn {
    position: relative;
    width: 100%; height: 100%;
    opacity: 1; pointer-events: auto;
    font-size: 2.2rem;
    background: transparent;
    border: none;
    z-index: 10;
    color: var(--app-accent);
    transition: transform 0.3s ease, color 0.3s ease;
    box-shadow: none;
}

.app-navigation.expanded {
    background: rgba(0,0,0,0.7);
    border-color: rgba(255,255,255,0.05);
}

.app-navigation.expanded .hub-main-btn {
    transform: rotate(45deg);
    color: var(--app-danger);
}

/* Expand items in a circle */
.app-navigation.expanded .nav-item:not(.hub-main-btn) {
    opacity: 1; pointer-events: auto;
}

/* Clockwise positions - Spaced out for larger buttons */
.app-navigation.expanded .nav-item:nth-child(1) { transform: translate(0, -120px); }    /* Undo */
.app-navigation.expanded .nav-item:nth-child(2) { transform: translate(115px, -30px); }  /* Log */
.app-navigation.expanded .nav-item:nth-child(3) { transform: translate(80px, 110px); }    /* Dice */
.app-navigation.expanded .nav-item:nth-child(4) { transform: translate(-80px, 110px); }   /* Settings */

/* Dynamic Player Grid - Balanced Layouts */
[data-players="1"] .game-view { grid-template-columns: 1fr; }
[data-players="2"] .game-view { grid-template-rows: 1fr 1fr; }

/* 3 Players: Top facing Center, bottom two facing Center */
[data-players="3"] .game-view {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}
[data-players="3"] .player-panel:nth-child(1) {
    grid-column: span 2;
}

[data-players="4"] .game-view { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }

/* 5 Players: 2 Top, 3 Bottom */
[data-players="5"] .game-view {
    grid-template-columns: repeat(6, 1fr);
    grid-template-rows: 1fr 1fr;
}
[data-players="5"] .player-panel:nth-child(1), [data-players="5"] .player-panel:nth-child(2) {
    grid-column: span 3;
}
[data-players="5"] .player-panel:nth-child(3), [data-players="5"] .player-panel:nth-child(4), [data-players="5"] .player-panel:nth-child(5) {
    grid-column: span 2;
}

/* 6 Players: 3 Top, 3 Bottom */
[data-players="6"] .game-view {
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 1fr 1fr;
}

/* Positioning fixes for asymmetric player counts to prevent overlapping life totals */
[data-players="1"] .app-navigation {
    left: auto; right: 24px;
    top: auto; bottom: 84px;
    transform: none;
}
[data-players="1"] .app-navigation.expanded .hub-main-btn { transform: rotate(45deg); }
[data-players="1"] .app-navigation.expanded .nav-item:nth-child(1) { transform: translate(0, -80px); }
[data-players="1"] .app-navigation.expanded .nav-item:nth-child(2) { transform: translate(-60px, -60px); }
[data-players="1"] .app-navigation.expanded .nav-item:nth-child(3) { transform: translate(-80px, 0); }
[data-players="1"] .app-navigation.expanded .nav-item:nth-child(4) { transform: translate(-60px, 60px); }

/* Landscape Optimizations & Boundary Shifting */
@media (min-aspect-ratio: 1.3/1) {
    [data-players="2"] .game-view { grid-template-rows: 1fr; grid-template-columns: 1fr 1fr; }
    [data-players="2"] .player-panel:nth-child(1) { transform: none; }
    
    [data-players="3"] .game-view { grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr; }
    [data-players="3"] .player-panel:nth-child(1) { grid-column: span 1; transform: none; }
    /* Shift hub to boundary between P1 and P2 in 3-player landscape */
    [data-players="3"] .app-navigation { left: 33.33%; }
    
    [data-players="4"] .game-view { grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: 1fr; }
    [data-players="4"] .player-panel:nth-child(1), [data-players="4"] .player-panel:nth-child(2) { transform: none; }
    
    [data-players="5"] .game-view { grid-template-columns: repeat(5, 1fr); grid-template-rows: 1fr; }
    [data-players="5"] .player-panel { grid-column: span 1 !important; transform: none; }
    /* Shift hub to boundary between P2 and P3 in 5-player landscape */
    [data-players="5"] .app-navigation { left: 40%; }
    
    [data-players="6"] .game-view { grid-template-columns: repeat(6, 1fr); grid-template-rows: 1fr; }
    [data-players="6"] .player-panel { transform: none; }
}

/* Portrait Rotations for "Around the Table" feel */
@media (max-aspect-ratio: 1/1) {
    [data-players="2"] .player-panel:nth-child(1) { transform: rotate(180deg); }
    [data-players="3"] .player-panel:nth-child(1) { transform: rotate(180deg); }
    [data-players="4"] .player-panel:nth-child(1), [data-players="4"] .player-panel:nth-child(2) { transform: rotate(180deg); }
    [data-players="5"] .player-panel:nth-child(1), [data-players="5"] .player-panel:nth-child(2) { transform: rotate(180deg); }
    [data-players="6"] .player-panel:nth-child(1), [data-players="6"] .player-panel:nth-child(2), [data-players="6"] .player-panel:nth-child(3) { transform: rotate(180deg); }
}

/* Screen Size Adaptations */
@media (max-width: 640px) {
    .life-total { font-size: clamp(2.2rem, 14vh, 24vw); }
    [data-players="5"] .life-total, [data-players="6"] .life-total { font-size: clamp(1.6rem, 10vh, 18vw); }

    [data-players="5"] .p-name, [data-players="6"] .p-name { font-size: 0.55rem; }
    [data-players="5"] .ui-chip, [data-players="6"] .ui-chip { min-width: 38px; padding: 1px; border-radius: 8px; }
    [data-players="5"] .chip-btn, [data-players="6"] .chip-btn { width: 14px; height: 14px; font-size: 0.6rem; }
    [data-players="5"] .panel-nav-btn, [data-players="6"] .panel-nav-btn { font-size: 0.5rem; padding: 3px 1px; }

    [data-players="5"] .header-icons, [data-players="6"] .header-icons { display: none; }

    /* Central hub space adjustment for mobile */
    .app-navigation { width: 64px; height: 64px; }
    .nav-item { width: 72px; height: 72px; font-size: 1.8rem; }
}

/* Modals & Overlays */
.app-overlay {
    position: fixed; inset: 0; z-index: 500;
    display: flex; align-items: center; justify-content: center;
    background: rgba(0,0,0,0.7); opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
    padding: 20px;
}
.app-overlay.active { opacity: 1; pointer-events: auto; }

.modal-card {
    background: var(--app-surface); border: 1px solid var(--app-border);
    border-radius: 16px; width: 100%; max-width: 400px; padding: 24px;
    max-height: 90vh; overflow-y: auto;
}

.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.modal-title { font-family: 'Outfit', sans-serif; font-size: 1.25rem; margin: 0; }
.modal-close { background: rgba(255,255,255,0.1); border: 0; color: #fff; width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 1.2rem; }

.ui-btn {
    width: 100%; min-height: 48px; border-radius: 10px; border: 0;
    background: #fff; color: #000; font-weight: 800; cursor: pointer;
    display: flex; align-items: center; justify-content: center; margin-bottom: 8px;
}
.ui-btn.secondary { background: rgba(255,255,255,0.1); color: #fff; }
.ui-btn.danger { background: rgba(255,71,87,0.2); color: #ffb3bb; border: 1px solid rgba(255,71,87,0.3); }

.setting-item { background: rgba(255,255,255,0.05); padding: 16px; border-radius: 12px; margin-bottom: 12px; }
.label-tiny { font-size: 0.7rem; font-weight: 900; color: var(--app-text-dim); text-transform: uppercase; margin-bottom: 8px; display: block; }
.life-presets { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.ui-pill { height: 40px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.1); background: rgba(255,255,255,0.05); color: #fff; font-weight: 800; cursor: pointer; }
.ui-pill.active { background: #fff; color: #000; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; font-size: 0.9rem; }

.dice-result-stage {
    height: 150px; display: flex; flex-direction: column; align-items: center; justify-content: center;
    background: rgba(255,255,255,0.03); border-radius: 12px; margin-bottom: 20px;
    perspective: 700px; position: relative; overflow: hidden;
    transition: background 0.3s ease, box-shadow 0.3s ease;
}
.dice-value {
    font-family: 'Outfit', sans-serif; font-size: clamp(2.2rem, 11vw, 4rem); font-weight: 900;
    line-height: 1; transform-style: preserve-3d; will-change: transform;
}
.dice-label { transition: color 0.3s ease; font-size: 0.8rem; color: var(--app-text-dim); margin-top: 10px; }
.dice-selection-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.dice-btn { height: 44px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.1); background: rgba(255,255,255,0.05); color: #fff; font-weight: 800; cursor: pointer; transition: all 0.2s ease; }
.dice-btn.active { border-color: var(--app-accent); background: rgba(99,102,241,0.1); box-shadow: 0 0 0 1px var(--app-accent) inset; }
.dice-btn.coin { grid-column: span 4; }

/* --- 3D Dice Styling --- */
.dice-cube {
    width: 60px; height: 60px; position: relative; transform-style: preserve-3d;
    transition: transform 0.6s cubic-bezier(0.2, 0, 0.2, 1);
}
.dice-face {
    position: absolute; width: 60px; height: 60px; background: #fff; color: #000;
    border: 2px solid #ddd; border-radius: 8px; display: flex; align-items: center;
    justify-content: center; font-size: 1.8rem; font-weight: 900; box-shadow: inset 0 0 15px rgba(0,0,0,0.1);
}
.face-1 { transform: translateZ(30px); }
.face-2 { transform: rotateY(180deg) translateZ(30px); }
.face-3 { transform: rotateY(90deg) translateZ(30px); }
.face-4 { transform: rotateY(-90deg) translateZ(30px); }
.face-5 { transform: rotateX(90deg) translateZ(30px); }
.face-6 { transform: rotateX(-90deg) translateZ(30px); }

/* Dice roll animation */
.dice-cube.rolling {
    animation: dice-cube-spin 0.4s linear infinite;
}
@keyframes dice-cube-spin {
    0% { transform: rotateX(0deg) rotateY(0deg); }
    100% { transform: rotateX(360deg) rotateY(360deg); }
}

/* --- Dice result states --- */
.dice-cube[data-face="1"] { transform: rotateX(0deg) rotateY(0deg); }
.dice-cube[data-face="2"] { transform: rotateX(0deg) rotateY(180deg); }
.dice-cube[data-face="3"] { transform: rotateX(0deg) rotateY(-90deg); }
.dice-cube[data-face="4"] { transform: rotateX(0deg) rotateY(90deg); }
.dice-cube[data-face="5"] { transform: rotateX(-90deg) rotateY(0deg); }
.dice-cube[data-face="6"] { transform: rotateX(90deg) rotateY(0deg); }

/* --- Dice roll / cycle animation for other dice --- */
.dice-value.rolling {
    animation: dice-tumble 0.42s cubic-bezier(0.36, 0.07, 0.19, 0.97) infinite;
}
@keyframes dice-tumble {
    0%   { transform: rotateX(0deg) rotateY(0deg) scale(1); }
    25%  { transform: rotateX(160deg) rotateY(70deg) scale(1.12); }
    50%  { transform: rotateX(320deg) rotateY(160deg) scale(0.92); }
    75%  { transform: rotateX(480deg) rotateY(250deg) scale(1.12); }
    100% { transform: rotateX(640deg) rotateY(340deg) scale(1); }
}
.dice-result-stage.rolling {
    animation: dice-stage-shake 0.3s ease-in-out infinite;
    box-shadow: 0 0 24px rgba(99,102,241,0.25) inset;
}
@keyframes dice-stage-shake {
    0%, 100% { transform: translate(0, 0) rotate(0deg); }
    25% { transform: translate(-3px, 2px) rotate(-1.2deg); }
    50% { transform: translate(3px, -2px) rotate(1.2deg); }
    75% { transform: translate(-2px, -2px) rotate(0.8deg); }
}

/* --- Coin flip variant --- */
.dice-value.coin-flipping { animation: coin-flip 0.28s linear infinite; }
@keyframes coin-flip {
    0%   { transform: rotateY(0deg) scale(1); }
    50%  { transform: rotateY(180deg) scale(0.85); }
    100% { transform: rotateY(360deg) scale(1); }
}

/* --- Random player picker variant --- */
.dice-value.name-cycling { animation: name-cycle 0.16s ease-in-out infinite alternate; }
@keyframes name-cycle {
    from { opacity: 0.55; transform: translateY(3px) scale(0.96); filter: blur(0.3px); }
    to   { opacity: 1; transform: translateY(-3px) scale(1.02); filter: blur(0px); }
}

/* --- Result reveal --- */
.dice-value.result-pop {
    animation: dice-result-pop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes dice-result-pop {
    0%   { transform: scale(0.3) rotate(-18deg); opacity: 0; }
    55%  { transform: scale(1.28) rotate(6deg); opacity: 1; }
    100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

/* --- Critical / fumble flavor for natural d20 rolls --- */
.dice-result-stage.roll-crit {
    background: radial-gradient(circle, rgba(245,158,11,0.28), rgba(255,255,255,0.03) 70%);
    box-shadow: 0 0 40px rgba(245,158,11,0.45) inset, 0 0 24px rgba(245,158,11,0.35);
}
.dice-value.roll-crit-text {
    color: var(--app-warning);
    text-shadow: 0 0 18px rgba(245,158,11,0.85);
    animation: dice-result-pop 0.5s cubic-bezier(0.34,1.56,0.64,1), crit-glow-pulse 1.3s ease-in-out infinite 0.5s;
}
@keyframes crit-glow-pulse {
    0%, 100% { text-shadow: 0 0 16px rgba(245,158,11,0.75); }
    50%      { text-shadow: 0 0 32px rgba(245,158,11,1); }
}
.dice-result-stage.roll-fumble {
    background: radial-gradient(circle, rgba(255,71,87,0.22), rgba(255,255,255,0.03) 70%);
}
.dice-value.roll-fumble-text {
    color: var(--app-danger);
    text-shadow: 0 0 16px rgba(255,71,87,0.6);
    animation: dice-result-pop 0.5s cubic-bezier(0.34,1.56,0.64,1), fumble-shake 0.4s ease-in-out 2 0.5s;
}
@keyframes fumble-shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-6px) rotate(-3deg); }
    75% { transform: translateX(6px) rotate(3deg); }
}

/* --- Battle Log --- */
.log-modal { max-width: 460px; }
.full-log {
    display: flex; flex-direction: column; gap: 8px;
    max-height: 58vh; overflow-y: auto; padding-right: 4px; margin-right: -4px;
}
.full-log::-webkit-scrollbar { width: 6px; }
.full-log::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 6px; }
.full-log::-webkit-scrollbar-track { background: transparent; }

.log-empty {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 8px; padding: 40px 16px; color: var(--app-text-dim); text-align: center; font-size: 0.9rem;
}
.log-empty-icon { font-size: 1.8rem; opacity: 0.6; }

.log-row {
    --log-accent: var(--app-accent);
    display: flex; align-items: flex-start; gap: 10px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.06);
    border-left: 3px solid var(--log-accent);
    border-radius: 10px;
    padding: 10px 12px;
    animation: log-in 0.28s ease;
}
.log-row.newest { background: rgba(255,255,255,0.07); }
@keyframes log-in {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.log-icon {
    flex-shrink: 0; width: 26px; height: 26px; border-radius: 7px;
    display: flex; align-items: center; justify-content: center;
    background: rgba(255,255,255,0.06); color: var(--log-accent);
    font-size: 0.85rem; margin-top: 1px;
}
.log-row.cat-elimination .log-icon { color: var(--app-danger); }
.log-row.cat-revive .log-icon { color: var(--app-success); }
.log-row.cat-poison .log-icon { color: #7dd87d; }
.log-row.cat-system .log-icon,
.log-row.cat-monarch .log-icon,
.log-row.cat-initiative .log-icon,
.log-row.cat-daynight .log-icon { color: var(--app-warning); }

.log-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.log-top-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.log-tag {
    font-size: 0.62rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em;
    color: var(--log-accent); opacity: 0.85;
}
.log-time { font-size: 0.65rem; color: var(--app-text-dim); white-space: nowrap; }
.log-msg { font-size: 0.85rem; line-height: 1.35; color: #fff; word-break: break-word; }

.log-delta {
    flex-shrink: 0; font-family: 'Outfit', sans-serif; font-weight: 800; font-size: 0.95rem;
    padding: 2px 8px; border-radius: 6px; align-self: center;
}
.log-delta.pos { color: var(--app-success); background: rgba(46,213,115,0.12); }
.log-delta.neg { color: var(--app-danger); background: rgba(255,71,87,0.12); }
