/* ==================== CSS Variables ==================== */
:root {
    --bg-primary: #0a0e27;
    --bg-secondary: #141b2d;
    --bg-tertiary: #1a2238;
    --accent-primary: #00d4ff;
    --accent-secondary: #7b2cbf;
    --text-primary: #e0e6ed;
    --text-secondary: #9ba4b4;
    --border-color: #2a3550;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --danger-color: #ef4444;
    --shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    --transition: all 0.2s ease;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
}

.role-gate {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
    background: rgba(10, 14, 39, 0.88);
}

.role-gate-panel {
    width: min(560px, 100%);
    padding: 1.5rem;
    border-radius: 12px;
    background: var(--bg-secondary);
    box-shadow: var(--shadow);
    border: 1px solid var(--border-color);
}

.role-gate-panel h2 {
    color: var(--accent-primary);
    margin-bottom: 0.75rem;
}

.role-gate-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
    margin: 1rem 0;
}

.hidden {
    display: none !important;
}

/* ==================== Header ==================== */
.header {
    background: var(--bg-secondary);
    border-bottom: 2px solid var(--accent-primary);
    padding: 1rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow);
}

.header-left h1 {
    font-size: 1.8rem;
    margin-bottom: 0.3rem;
    background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.header-right {
    display: flex;
    gap: 2rem;
    align-items: center;
}

.system-stats {
    display: flex;
    gap: 1.5rem;
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 0.2rem;
}

.stat-value {
    font-size: 1.2rem;
    font-weight: bold;
    color: var(--accent-primary);
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: var(--bg-tertiary);
    border-radius: 20px;
}

.mode-switch {
    padding: 0.75rem 1.5rem 0;
    max-width: 1920px;
    margin: 0 auto;
}

.segmented-toggle {
    display: inline-grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.25rem;
    padding: 0.25rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
}

.segment-option {
    min-height: 44px;
    padding: 0.65rem 1rem;
    border: 0;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 6px;
    cursor: pointer;
    font-weight: 700;
    transition: var(--transition);
}

.segment-option:hover,
.segment-option:focus-visible {
    color: var(--text-primary);
    outline: none;
}

.segment-option.active {
    background: var(--accent-primary);
    color: #00111a;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--danger-color);
    animation: pulse 2s infinite;
}

.status-dot.connected {
    background: var(--success-color);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* ==================== Main Container ==================== */
.main-container {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 1.5rem;
    padding: 1.5rem;
    max-width: 1920px;
    margin: 0 auto;
}

/* ==================== Video Panel ==================== */
.video-panel {
    background: var(--bg-secondary);
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: var(--shadow);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.panel-header h2 {
    font-size: 1.5rem;
    color: var(--accent-primary);
}

.stream-info {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.video-container {
    position: relative;
    background: #000;
    border-radius: 8px;
    overflow: hidden;
    aspect-ratio: 16/9;
}

#video-player {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.webrtc-frame {
    width: 100%;
    height: 100%;
    border: 0;
    background: #000;
}

.source-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.situation-grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
}

.situation-grid.grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.situation-grid.grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.room-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
}

.room-selection {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.room-selection-label,
.room-lock {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.source-tile {
    min-height: 72px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    padding: 0.75rem;
    cursor: pointer;
    text-align: left;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 0.5rem;
}

.source-tile.monitored {
    border-color: var(--success-color);
    box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.4);
}

.source-tile.offline {
    opacity: 0.6;
}

.source-tile.risk {
    border-color: var(--danger-color);
    animation: flash 1s infinite;
}

.source-tile-header,
.source-tile-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
}

.source-header-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.source-tile-stream {
    position: relative;
    border-radius: 6px;
    overflow: hidden;
    background: #000;
    aspect-ratio: 16 / 9;
}

.source-tile-stream .webrtc-frame {
    display: block;
}

.source-tile-stream .source-video {
    display: block;
    width: 100%;
    height: 100%;
    background: #000;
    object-fit: cover;
    border: 0;
}

.source-status {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.source-status.online {
    color: var(--success-color);
}

.source-status.offline {
    color: var(--warning-color);
}

.source-select-button {
    align-self: flex-start;
    border: 1px solid var(--border-color);
    background: transparent;
    color: var(--text-primary);
    border-radius: 6px;
    padding: 0.45rem 0.7rem;
    cursor: pointer;
    font-weight: 700;
}

.source-monitor-button {
    border: 1px solid rgba(0, 212, 255, 0.35);
    background: rgba(0, 212, 255, 0.12);
    color: #d9fbff;
    border-radius: 6px;
    padding: 0.45rem 0.7rem;
    cursor: pointer;
    font-weight: 700;
}

.source-monitor-button.active {
    background: rgba(16, 185, 129, 0.2);
    border-color: rgba(16, 185, 129, 0.55);
    color: #d1fae5;
}

.source-monitor-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.source-tile-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.source-disconnect-button {
    border: 1px solid rgba(239, 68, 68, 0.45);
    background: rgba(239, 68, 68, 0.12);
    color: #fecaca;
    border-radius: 6px;
    padding: 0.45rem 0.7rem;
    cursor: pointer;
    font-weight: 700;
}

.source-disconnect-button:hover,
.source-disconnect-button:focus-visible {
    background: rgba(239, 68, 68, 0.2);
    outline: none;
}

.source-select-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.source-tile-title {
    font-weight: 700;
}

.source-tile-badge {
    align-self: flex-start;
    border-radius: 4px;
    background: rgba(0, 212, 255, 0.18);
    color: var(--accent-primary);
    font-size: 0.75rem;
    font-weight: 800;
    padding: 0.2rem 0.45rem;
}

.source-stream-overlay {
    position: absolute;
    top: 0.5rem;
    left: 0.5rem;
    max-width: min(80%, 520px);
    padding: 0.55rem 0.7rem;
    border-radius: 8px;
    background: rgba(127, 29, 29, 0.88);
    border: 1px solid rgba(248, 113, 113, 0.45);
    color: #fee2e2;
    font-size: 0.8rem;
    line-height: 1.35;
    white-space: pre-wrap;
    word-break: break-word;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease;
}

.source-stream-overlay.active {
    opacity: 1;
}

.source-stream-overlay-text {
    max-height: 40vh;
    overflow: auto;
}

.video-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(239, 68, 68, 0.3);
    display: none;
    pointer-events: none;
}

.video-overlay.active {
    display: flex;
    align-items: center;
    justify-content: center;
    animation: flash 1s infinite;
}

@keyframes flash {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

.risk-banner {
    background: var(--danger-color);
    color: white;
    padding: 1rem 2rem;
    font-size: 1.5rem;
    font-weight: bold;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.8);
}

.stream-urls {
    margin-top: 1rem;
    display: grid;
    gap: 0.5rem;
}

.camera-src-view {
    padding: 1.5rem;
    max-width: 1920px;
    margin: 0 auto;
}

.camera-src-panel {
    width: 100%;
}

.camera-src-layout {
    display: block;
}

.source-instructions {
    background: var(--bg-tertiary);
    max-width: 640px;
}

.helper-copy {
    color: var(--text-secondary);
    line-height: 1.5;
    margin-bottom: 1rem;
}

.url-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
}

.url-item label {
    color: var(--text-secondary);
    min-width: 60px;
}

.url-item code {
    flex: 1;
    background: var(--bg-tertiary);
    padding: 0.5rem;
    border-radius: 4px;
    color: var(--accent-primary);
    font-family: 'Courier New', monospace;
}

/* ==================== Control Panel ==================== */
.control-panel {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    overflow-y: auto;
    max-height: calc(100vh - 150px);
}

.section {
    background: var(--bg-secondary);
    border-radius: 12px;
    padding: 1.5rem;
    box-shadow: var(--shadow);
}

.section h2, .section h3 {
    color: var(--accent-primary);
    margin-bottom: 1rem;
}

/* ==================== Risk Status ==================== */
.status-display {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.status-indicator {
    text-align: center;
    padding: 2rem;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--success-color), #059669);
    transition: var(--transition);
}

.status-indicator.risk {
    background: linear-gradient(135deg, var(--danger-color), #dc2626);
    animation: flash 1s infinite;
}

.status-indicator.analyzing {
    background: linear-gradient(135deg, var(--warning-color), #d97706);
}

.status-text {
    font-size: 2rem;
    font-weight: bold;
    color: white;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.confidence-meter label {
    display: block;
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.meter-bar {
    height: 20px;
    background: var(--bg-tertiary);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 0.5rem;
}

.meter-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--success-color), var(--accent-primary));
    transition: width 0.5s ease;
    width: 0%;
}

.meter-value {
    font-size: 1.2rem;
    font-weight: bold;
    color: var(--accent-primary);
}

.explanation-box label {
    display: block;
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.explanation-box p {
    background: var(--bg-tertiary);
    padding: 1rem;
    border-radius: 8px;
    border-left: 4px solid var(--accent-primary);
    min-height: 60px;
    line-height: 1.5;
}

.inference-meta {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.75rem;
}

.inference-meta div {
    background: var(--bg-tertiary);
    border-radius: 8px;
    padding: 0.75rem;
    min-width: 0;
}

.inference-meta span {
    display: block;
    color: var(--text-secondary);
    font-size: 0.75rem;
    margin-bottom: 0.25rem;
}

.inference-meta strong {
    display: block;
    color: var(--text-primary);
    font-size: 0.9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ==================== Controls ==================== */
.control-group {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-primary), #0084ff);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 212, 255, 0.4);
}

.btn:disabled,
.btn.disabled {
    cursor: not-allowed;
    opacity: 0.55;
    transform: none;
    box-shadow: none;
}

.btn-success {
    background: linear-gradient(135deg, var(--success-color), #059669);
    color: white;
}

.btn-success:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(16, 185, 129, 0.4);
}

.btn-icon {
    font-size: 1.2rem;
}

.toggle-group {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 26px;
}

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

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--bg-tertiary);
    transition: var(--transition);
    border-radius: 26px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: var(--transition);
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--accent-primary);
}

input:checked + .slider:before {
    transform: translateX(24px);
}

.sound-info {
    background: var(--bg-tertiary);
    padding: 0.75rem;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.sound-meter-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
}

.sound-fps {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.sound-level-container {
    position: relative;
    height: 24px;
    display: flex;
    align-items: center;
}

.sound-level {
    position: absolute;
    left: 0;
    right: 0;
    height: 14px;
    background: var(--bg-primary);
    border-radius: 999px;
    overflow: hidden;
    pointer-events: none;
}

.sound-level-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--success-color), var(--warning-color), var(--danger-color));
    transition: width 0.12s linear;
}

.sound-threshold-marker {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 3px;
    background: #fff;
    left: 56%;
    box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
    z-index: 5;
}

.sound-slider {
    position: absolute;
    left: 0;
    right: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
    z-index: 10;
}

/* ==================== Accordion ==================== */
.settings-accordion {
    padding: 0;
}

.accordion-item {
    border-bottom: 1px solid var(--border-color);
}

.accordion-item:last-child {
    border-bottom: none;
}

.accordion-header {
    width: 100%;
    padding: 1rem 1.5rem;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: var(--transition);
}

.accordion-header:hover {
    background: var(--bg-tertiary);
}

.accordion-icon {
    transition: transform 0.3s ease;
}

.accordion-header.active .accordion-icon {
    transform: rotate(180deg);
}

.accordion-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    padding: 0 1.5rem;
}

.accordion-content.active {
    max-height: 1000px;
    padding: 1rem 1.5rem;
}

/* ==================== Form Elements ==================== */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.form-control {
    width: 100%;
    padding: 0.75rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 0.95rem;
    transition: var(--transition);
}

.form-control:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(0, 212, 255, 0.1);
}

textarea.form-control {
    resize: vertical;
    font-family: inherit;
}

.slider-input {
    width: calc(100% - 60px);
    margin-right: 0.5rem;
}

/* ==================== Toast Notification ==================== */
.toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 1rem 1.5rem;
    border-radius: 8px;
    border-left: 4px solid var(--accent-primary);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
    transform: translateX(400px);
    transition: transform 0.3s ease;
    z-index: 1000;
    max-width: 300px;
}

.toast.show {
    transform: translateX(0);
}

/* ==================== Responsive Design ==================== */
@media (max-width: 1200px) {
    .main-container {
        grid-template-columns: 1fr;
    }
    
    .header {
        flex-direction: column;
        gap: 1rem;
    }
    
    .system-stats {
        gap: 1rem;
    }
}

@media (max-width: 900px) {
    .camera-src-layout {
        grid-template-columns: 1fr;
    }

    .mode-switch {
        padding-inline: 1rem;
    }

    .segmented-toggle {
        width: 100%;
    }
}

/* ==================== Scrollbar ==================== */
.control-panel::-webkit-scrollbar {
    width: 8px;
}

.control-panel::-webkit-scrollbar-track {
    background: var(--bg-tertiary);
    border-radius: 4px;
}

.control-panel::-webkit-scrollbar-thumb {
    background: var(--accent-primary);
    border-radius: 4px;
}

.control-panel::-webkit-scrollbar-thumb:hover {
    background: var(--accent-secondary);
}
