/* ── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

body {
    margin: 0;
    overflow: hidden;
    background-color: #1a1a1a;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

/* ── Start / Blocker ─────────────────────────────────────────────────────── */
#blocker {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

#instructions {
    text-align: center;
    color: white;
    cursor: pointer;
    line-height: 1.8;
}

/* ── HUD ─────────────────────────────────────────────────────────────────── */
#hud {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 10;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}


#room-name {
    color: white;
    font-size: 22px;
    font-weight: 700;
    text-shadow: 1px 1px 3px rgba(0,0,0,0.8);
    background: rgba(0, 0, 0, 0.35);
    padding: 10px 20px;
    border-radius: 10px;
    backdrop-filter: blur(6px);
    letter-spacing: 0.5px;
}

/* Mic status */
#mic-status {
    color: white;
    font-size: 18px;
    font-weight: 600;
    background: rgba(0, 0, 0, 0.35);
    padding: 10px 18px;
    border-radius: 10px;
    backdrop-filter: blur(6px);
    transition: color 0.3s ease;
    white-space: nowrap;
}

.status-idle      { color: #ccc !important; }
.status-activated { color: #ffd700 !important; }
.status-recording { color: #ff4444 !important; }
.status-speaking  { color: #44ff88 !important; }

.mic-container {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Voice tracker bars */
#voice-tracker {
    display: flex;
    gap: 4px;
    height: 36px;
    align-items: center;
    padding: 0 12px;
    background: rgba(0, 0, 0, 0.35);
    border-radius: 10px;
    backdrop-filter: blur(6px);
    transition: opacity 0.3s ease;
}

#voice-tracker.hidden {
    opacity: 0;
    pointer-events: none;
}

.bar {
    width: 5px;
    height: 4px;
    background: #888;
    border-radius: 3px;
    transition: height 0.05s ease, background 0.15s ease;
}

/* Bottom transcript */
.hud-bottom {
    padding: 20px;
    display: flex;
    justify-content: center;
}

#transcript {
    color: white;
    font-size: 20px;
    background: rgba(0, 0, 0, 0.55);
    padding: 14px 28px;
    border-radius: 12px;
    max-width: 78%;
    text-align: center;
    opacity: 0;
    transition: opacity 0.4s ease;
    backdrop-filter: blur(6px);
    line-height: 1.5;
}

/* ── Device Panel (modal) ──────────────────────────────────────────────────── */
#device-panel {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(880px, 92vw);
    max-height: 82vh;
    background: rgba(18, 22, 30, 0.96);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 18px;
    z-index: 50;
    backdrop-filter: blur(14px);
    color: #e8eaf0;
    padding: 0;
    display: flex;
    flex-direction: column;
    /* overflow: hidden removed — panel-body handles its own scroll */
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.7);
    overflow: hidden; /* clip rounded corners on children */
}

#device-panel.hidden {
    display: none;
    pointer-events: none;
}

#device-panel:not(.hidden) {
    pointer-events: auto;
}

/* Panel header — fixed, never scrolls */
.panel-header {
    padding: 18px 24px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;       /* <-- critical: header never shrinks */
    background: rgba(18, 22, 30, 0.98);
    border-radius: 18px 18px 0 0;
    z-index: 1;
}

.panel-header h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.5px;
    color: #ffffff;
}

/* Close button inside panel header */
#panel-close-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: #ccc;
    font-size: 13px;
    font-weight: 600;
    padding: 6px 14px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease;
    white-space: nowrap;
    flex-shrink: 0;
}

#panel-close-btn:hover {
    background: rgba(255, 80, 80, 0.25);
    color: #ff8888;
    border-color: rgba(255, 80, 80, 0.3);
}

/* Panel scrollable body — this is the only part that scrolls */
.panel-body {
    flex: 1 1 auto;       /* grow to fill remaining space */
    min-height: 0;        /* critical: allows flex child to shrink below content */
    overflow-y: auto;
    padding: 20px 24px 28px; /* extra bottom pad so last card isn't clipped */
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.15) transparent;
}

.panel-body::-webkit-scrollbar { width: 6px; }
.panel-body::-webkit-scrollbar-track { background: transparent; }
.panel-body::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }

/* Section divider */
.panel-section-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: #8899bb;
    margin: 24px 0 12px;
    padding-bottom: 6px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}

.panel-section-title:first-child { margin-top: 0; }

/* Device grid */
#device-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 14px;
}

/* Simulation controls grid */
#simulation-controls {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 14px;
}

/* ── Device / Sensor Cards ────────────────────────────────────────────────── */

.hud-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    width: 100%;
    pointer-events: none;
}

.hud-top > * {
    pointer-events: auto;
}

.hud-top-left {
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
}

.hud-top-right {
    display: flex;
}

.glass-btn {
    background: rgba(30, 40, 50, 0.7);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    backdrop-filter: blur(5px);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}

.glass-btn:hover {
    background: rgba(50, 70, 90, 0.8);
    transform: scale(1.05);
}

.glass-btn:active {
    transform: scale(0.95);
}

.device-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.07);
    padding: 16px;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: border-color 0.2s ease;
}

.device-card:hover {
    border-color: rgba(255, 255, 255, 0.15);
}

.device-card h3,
.device-card h4 {
    margin: 0;
    font-size: 13px;
    font-weight: 700;
    color: #aec6ef;
    text-transform: uppercase;
    letter-spacing: 0.6px;
}

.device-card p {
    margin: 0;
    font-size: 13px;
    color: #c0c8d8;
}

/* Device toggle button */
.device-card button {
    margin-top: 4px;
    padding: 7px 14px;
    background: rgba(74, 144, 226, 0.85);
    border: none;
    color: white;
    border-radius: 7px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    transition: background 0.2s ease, transform 0.1s ease;
    align-self: flex-start;
}

.device-card button:hover  { background: rgba(53, 122, 189, 0.95); }
.device-card button:active { transform: scale(0.97); }

/* ── Sensor Simulation Controls ──────────────────────────────────────────── */
.sensor-value {
    font-size: 22px;
    font-weight: 800;
    color: #ffffff;
    letter-spacing: 0.5px;
}

.sensor-unit {
    font-size: 13px;
    color: #7a90b0;
    margin-left: 2px;
}

/* Manual override toggle row */
.override-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 4px;
}

.override-row label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 13px;
    color: #9ab0cc;
    font-weight: 500;
}

/* Toggle switch */
.toggle-switch {
    position: relative;
    width: 36px;
    height: 20px;
    flex-shrink: 0;
}

.toggle-switch input { opacity: 0; width: 0; height: 0; }

.toggle-track {
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,0.12);
    border-radius: 20px;
    transition: background 0.25s ease;
    cursor: pointer;
}

.toggle-track::before {
    content: '';
    position: absolute;
    left: 3px;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    background: #aaa;
    border-radius: 50%;
    transition: left 0.25s ease, background 0.25s ease;
}

.toggle-switch input:checked + .toggle-track { background: rgba(74, 144, 226, 0.75); }
.toggle-switch input:checked + .toggle-track::before { left: 19px; background: #fff; }

/* Slider */
.slider-row {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 10px;
    margin-top: 6px;
}

.slider-row input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 5px;
    background: rgba(255,255,255,0.12);
    border-radius: 3px;
    outline: none;
    cursor: pointer;
    transition: background 0.2s;
}

.slider-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #4a90e2;
    cursor: pointer;
    box-shadow: 0 0 4px rgba(74, 144, 226, 0.6);
    transition: transform 0.15s ease;
}

.slider-row input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.2); }
.slider-row input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #4a90e2;
    border: none;
    cursor: pointer;
}

.slider-val {
    font-size: 13px;
    color: #c8d8f0;
    font-weight: 700;
    min-width: 42px;
    text-align: right;
}

/* ── Minimap ──────────────────────────────────────────────────────────────── */
#minimap {
    position: absolute;
    bottom: 20px;
    right: 20px;
    width: 140px;
    height: 140px;
    background: rgba(0, 0, 0, 0.45);
    border-radius: 10px;
    backdrop-filter: blur(6px);
    pointer-events: none;
    border: 1px solid rgba(255,255,255,0.08);
}

/* ── Responsive Breakpoints ───────────────────────────────────────────────── */
@media (max-width: 600px) {
    #device-panel {
        width: 96vw;
        max-height: 88vh;
        top: 50%;
        border-radius: 14px;
    }

    #device-grid,
    #simulation-controls {
        grid-template-columns: 1fr;
    }

    #room-name { font-size: 17px; }
    #mic-status { font-size: 15px; }
}

/* ── Error Popup ────────────────────────────────────────────────────────── */
#error-popup {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(40, 40, 45, 0.95);
    border: 1px solid #ff4444;
    border-radius: 12px;
    padding: 24px;
    color: #fff;
    z-index: 9999;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    max-width: 400px;
    backdrop-filter: blur(10px);
}
#error-popup.hidden {
    display: none;
}
#error-popup h3 {
    margin: 0 0 10px 0;
    color: #ff6666;
}
#error-popup p {
    margin: 0 0 20px 0;
    font-size: 14px;
    color: #ccc;
    line-height: 1.4;
}
#error-popup button {
    background: #ff4444;
    color: #fff;
    border: none;
    padding: 8px 24px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}
#error-popup button:hover {
    background: #ff6666;
}

/* ── History Panel ──────────────────────────────────────────────────────── */
#history-panel {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90vw;
    max-width: 600px;
    max-height: 80vh;
    background: rgba(26, 28, 35, 0.92);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 16px;
    backdrop-filter: blur(16px);
    box-shadow: 0 24px 60px rgba(0,0,0,0.4);
    display: flex;
    flex-direction: column;
    z-index: 50;
    overflow: hidden;
}

#history-panel.hidden {
    display: none;
    pointer-events: none;
}

.history-entry {
    background: rgba(0,0,0,0.2);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 12px;
    border: 1px solid rgba(255,255,255,0.05);
}

.history-header {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.history-time {
    font-size: 11px;
    color: #889;
    font-weight: 600;
}

.history-text {
    font-size: 14px;
    color: #eef;
    line-height: 1.4;
}

.history-actions-btn {
    margin-top: 10px;
    background: rgba(255,255,255,0.1);
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    color: #ccc;
    font-size: 12px;
    cursor: pointer;
    transition: background 0.2s;
}

.history-actions-btn:hover {
    background: rgba(255,255,255,0.15);
}

.history-actions-detail {
    margin-top: 10px;
    background: rgba(0,0,0,0.3);
    padding: 10px;
    border-radius: 6px;
    font-family: monospace;
    font-size: 12px;
    color: #aaddaa;
}

.history-actions-detail.hidden {
    display: none;
}

.action-row {
    margin-bottom: 4px;
}

/* ── Mobile Controls (Landscape) ────────────────────────────────────────── */
#mobile-controls {
    display: none;
}
#rotate-prompt {
    display: none;
}

@media (max-width: 900px) {
    /* Enforce Landscape Prompt */
    @media (orientation: portrait) {
        #rotate-prompt {
            display: flex;
            position: fixed;
            top: 0; left: 0; right: 0; bottom: 0;
            background: #111;
            color: #fff;
            z-index: 99999;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            font-size: 20px;
            text-align: center;
            padding: 20px;
        }
        #hud, #blocker, #simulation-controls, #history-panel, #device-panel {
            display: none !important;
        }
    }

    @media (orientation: landscape) {
        #blocker {
            display: none !important; /* Touch controls handle it */
        }
        
        #mobile-controls {
            display: flex;
            position: fixed;
            bottom: 20px;
            left: 20px;
            right: 20px;
            justify-content: space-between;
            align-items: flex-end;
            z-index: 40;
            pointer-events: none; /* Let clicks pass through empty areas */
        }
        
        .d-pad {
            display: grid;
            grid-template-columns: 60px 60px 60px;
            grid-template-rows: 60px 60px 60px;
            gap: 8px;
            pointer-events: auto;
        }
        .d-btn {
            background: rgba(30, 40, 50, 0.6);
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-radius: 50%;
            color: white;
            font-size: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            user-select: none;
            backdrop-filter: blur(5px);
            touch-action: none;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
        }
        .d-btn:active {
            background: rgba(255, 255, 255, 0.3);
            transform: scale(0.95);
        }
        .d-up { grid-column: 2; grid-row: 1; }
        .d-left { grid-column: 1; grid-row: 2; }
        .d-right { grid-column: 3; grid-row: 2; }
        .d-down { grid-column: 2; grid-row: 3; }

        .action-bar {
            display: flex;
            flex-direction: column;
            gap: 12px;
            pointer-events: auto;
            align-items: flex-end;
        }
        .action-btn {
            background: rgba(30, 40, 50, 0.7);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 14px 24px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            backdrop-filter: blur(5px);
            text-align: center;
            touch-action: none;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .action-btn:active {
            background: rgba(50, 70, 90, 0.8);
            transform: scale(0.95);
        }
    }
}
