/* ===== Variables ===== */
:root {
    --bg-primary: #f0f2f5;
    --bg-secondary: #ffffff;
    --bg-hover: #e8ebef;
    --bg-active: #dce0e6;
    --text-primary: #1a1d23;
    --text-secondary: #5f6672;
    --text-muted: #9ca3af;
    --border: #e2e5ea;
    --border-light: #f0f2f5;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --accent-light: #eff6ff;
    --accent-border: #bfdbfe;
    --green: #22c55e;
    --green-light: #f0fdf4;
    --green-border: #bbf7d0;
    --red: #ef4444;
    --red-light: #fef2f2;
    --red-border: #fecaca;
    --orange: #f59e0b;
    --orange-light: #fffbeb;
    --purple: #8b5cf6;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.04);
    --shadow-lg: 0 10px 15px rgba(0,0,0,0.06), 0 4px 6px rgba(0,0,0,0.04);
    --radius: 10px;
    --radius-sm: 6px;
    --radius-lg: 14px;
    --transition: 0.15s ease;
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}
button { cursor: pointer; font-family: inherit; }
input { font-family: inherit; }
.hidden { display: none !important; }

/* ===== Connection Bar ===== */
#connection-bar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}
.connection-left {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}
.connection-left h1 {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
}
.connection-left h1 i { color: var(--accent); }
#connection-status {
    font-size: 12px;
    font-weight: 500;
    padding: 4px 10px;
    border-radius: 20px;
    white-space: nowrap;
}
.status-disconnected {
    color: var(--text-muted);
    background: var(--bg-primary);
}
.status-connected {
    color: var(--green);
    background: var(--green-light);
}
.status-connecting {
    color: var(--orange);
    background: var(--orange-light);
}
.connection-form {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    justify-content: flex-end;
}
.connection-form input {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border var(--transition);
    width: 160px;
}
.connection-form input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}
.connection-form input::placeholder { color: var(--text-muted); }

/* ===== Buttons ===== */
.btn-primary {
    padding: 8px 16px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 500;
    transition: all var(--transition);
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 6px;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary.connected {
    background: var(--red);
}
.btn-primary.connected:hover { background: #dc2626; }

.btn-secondary {
    padding: 10px 20px;
    background: var(--bg-secondary);
    color: var(--accent);
    border: 1px solid var(--accent-border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    transition: all var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 8px;
}
.btn-secondary:hover { background: var(--accent-light); }

.btn-icon {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 16px;
    transition: all var(--transition);
    flex-shrink: 0;
}
.btn-icon:hover { background: var(--bg-hover); color: var(--accent); }

.btn-icon-sm {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 13px;
    transition: all var(--transition);
}
.btn-icon-sm:hover { background: var(--bg-hover); color: var(--accent); }

/* ===== Main Content ===== */
#main-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

/* ===== Center Message (disconnected) ===== */
.center-message {
    text-align: center;
    padding: 80px 20px;
}
.center-message i {
    font-size: 48px;
    color: var(--accent);
    opacity: 0.6;
    margin-bottom: 16px;
}
.center-message h2 {
    font-size: 22px;
    font-weight: 600;
    margin-bottom: 8px;
}
.center-message p {
    color: var(--text-secondary);
    font-size: 14px;
    margin-bottom: 24px;
    max-width: 400px;
    margin-left: auto;
    margin-right: auto;
}

/* ===== Panels ===== */
#connected-view {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
.panel {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow);
    transition: box-shadow var(--transition);
}
.panel:hover { box-shadow: var(--shadow-md); }
.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-light);
}
.panel-header h2 {
    font-size: 14px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
}
.panel-header h2 i { color: var(--accent); font-size: 14px; }
.panel-subtitle {
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 400;
}

#stream-controls { grid-column: 1 / -1; }
#canvas-panel { grid-column: 1 / -1; }
#screenshot-panel { grid-column: 1 / -1; }

/* ===== Canvas Preview ===== */
.canvas-controls {
    display: flex;
    align-items: center;
    gap: 6px;
}
.fps-label select {
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 12px;
    font-family: inherit;
}
#canvas-wrapper {
    padding: 12px;
}
#canvas-container {
    position: relative;
    width: 100%;
    aspect-ratio: 16/9;
    background: #1a1d23;
    border-radius: var(--radius);
    overflow: hidden;
    cursor: default;
}
#canvas-preview {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}
#canvas-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
}
.source-box {
    position: absolute;
    border: 2px solid transparent;
    pointer-events: all;
    cursor: move;
    transition: border-color 0.1s;
}
.source-box:hover {
    border-color: var(--accent);
}
.source-box.selected {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent);
}
.source-box-label {
    position: absolute;
    top: -20px;
    left: 0;
    background: var(--accent);
    color: white;
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 3px;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.1s;
    pointer-events: none;
}
.source-box:hover .source-box-label,
.source-box.selected .source-box-label {
    opacity: 1;
}
.resize-handle {
    position: absolute;
    width: 10px;
    height: 10px;
    background: var(--accent);
    border: 1px solid white;
    border-radius: 2px;
    pointer-events: all;
    display: none;
}
.source-box.selected .resize-handle { display: block; }
.resize-handle.se { bottom: -5px; right: -5px; cursor: se-resize; }
.resize-handle.sw { bottom: -5px; left: -5px; cursor: sw-resize; }
.resize-handle.ne { top: -5px; right: -5px; cursor: ne-resize; }
.resize-handle.nw { top: -5px; left: -5px; cursor: nw-resize; }

#canvas-placeholder {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
}

/* Transform Bar */
#source-transform-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    border-top: 1px solid var(--border-light);
    font-size: 13px;
    flex-wrap: wrap;
}
#transform-source-name {
    font-weight: 600;
    color: var(--accent);
    min-width: 80px;
}
.transform-inputs {
    display: flex;
    gap: 8px;
    flex: 1;
}
.transform-inputs label {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    color: var(--text-secondary);
}
.transform-inputs input {
    width: 65px;
    padding: 4px 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 12px;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: inherit;
}
.btn-sm { padding: 5px 12px; font-size: 12px; }

/* ===== Stream Controls ===== */
.stream-buttons {
    display: flex;
    gap: 10px;
    padding: 16px 18px;
    flex-wrap: wrap;
}
.stream-buttons button {
    flex: 1;
    min-width: 140px;
    padding: 12px 16px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all var(--transition);
}
.stream-buttons button:hover:not(:disabled) {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-light);
}
.stream-buttons button:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-stream-on {
    border-color: var(--red) !important;
    background: var(--red-light) !important;
    color: var(--red) !important;
}
.btn-record-on {
    border-color: var(--red) !important;
    background: var(--red-light) !important;
    color: var(--red) !important;
}
.btn-record-on i { animation: pulse-red 1.5s infinite; }
.btn-vcam-on {
    border-color: var(--green) !important;
    background: var(--green-light) !important;
    color: var(--green) !important;
}

@keyframes pulse-red {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.stream-stats {
    display: flex;
    gap: 20px;
    padding: 0 18px 14px;
    flex-wrap: wrap;
}
.stat {
    font-size: 12px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 6px;
}
.stat i { color: var(--accent); font-size: 11px; }

/* ===== Scenes Grid ===== */
.scenes-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 8px;
    padding: 14px 18px;
}
.scene-btn {
    padding: 14px 10px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 12px;
    font-weight: 500;
    text-align: center;
    transition: all var(--transition);
    word-break: break-word;
}
.scene-btn:hover {
    border-color: var(--accent);
    background: var(--accent-light);
}
.scene-btn.active {
    border-color: var(--accent);
    background: var(--accent);
    color: white;
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}
.scene-btn i { display: block; font-size: 18px; margin-bottom: 6px; }

/* ===== Sources List ===== */
.sources-list {
    padding: 8px 18px 14px;
    max-height: 350px;
    overflow-y: auto;
}
.source-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    transition: background var(--transition);
}
.source-item:hover { background: var(--bg-primary); }
.source-info {
    display: flex;
    align-items: center;
    gap: 10px;
}
.source-icon {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    background: var(--accent-light);
    color: var(--accent);
    font-size: 12px;
}
.source-name { font-size: 13px; font-weight: 500; }
.source-type { font-size: 11px; color: var(--text-muted); }

/* Toggle Switch */
.toggle {
    width: 40px;
    height: 22px;
    border: none;
    border-radius: 11px;
    background: var(--bg-active);
    position: relative;
    transition: all var(--transition);
    flex-shrink: 0;
}
.toggle::after {
    content: '';
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: white;
    position: absolute;
    top: 3px;
    left: 3px;
    transition: all var(--transition);
    box-shadow: var(--shadow-sm);
}
.toggle.on { background: var(--accent); }
.toggle.on::after { left: 21px; }

/* ===== Audio List ===== */
.audio-list {
    padding: 8px 18px 14px;
    max-height: 350px;
    overflow-y: auto;
}
.audio-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid var(--border-light);
}
.audio-item:last-child { border-bottom: none; }
.audio-mute {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    font-size: 14px;
    transition: all var(--transition);
    flex-shrink: 0;
}
.audio-mute:hover { background: var(--bg-hover); }
.audio-mute.muted { color: var(--red); }
.audio-info { flex: 1; min-width: 0; }
.audio-name {
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.audio-slider-wrap { flex: 1; display: flex; align-items: center; gap: 8px; }
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 4px;
    border-radius: 2px;
    background: var(--bg-active);
    outline: none;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    box-shadow: var(--shadow-sm);
}
input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}
.audio-db {
    font-size: 11px;
    color: var(--text-muted);
    min-width: 42px;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* ===== Actions Grid ===== */
.actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 8px;
    padding: 14px 18px;
}
.action-btn {
    padding: 14px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-secondary);
    color: var(--text-secondary);
    font-size: 11px;
    font-weight: 500;
    text-align: center;
    transition: all var(--transition);
}
.action-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-light);
}
.action-btn.active {
    border-color: var(--purple);
    color: var(--purple);
    background: #f5f3ff;
}
.action-btn i { display: block; font-size: 18px; margin-bottom: 6px; }

/* ===== Screenshot ===== */
#screenshot-container {
    padding: 16px;
    text-align: center;
}
#screenshot-img {
    max-width: 100%;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
}

/* ===== Placeholder ===== */
.placeholder-text {
    color: var(--text-muted);
    font-size: 13px;
    text-align: center;
    padding: 20px;
}

/* ===== Modal ===== */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    backdrop-filter: blur(4px);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.modal-content {
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 680px;
    max-height: 85vh;
    overflow: hidden;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
}
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h2 {
    font-size: 16px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
}
.modal-header h2 i { color: var(--accent); }
.modal-body {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
}

/* ===== Guide Tabs ===== */
.guide-tabs {
    display: flex;
    gap: 4px;
    background: var(--bg-primary);
    border-radius: var(--radius);
    padding: 4px;
    margin-bottom: 20px;
}
.guide-tab {
    flex: 1;
    padding: 8px 12px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 500;
    transition: all var(--transition);
}
.guide-tab:hover { color: var(--text-primary); }
.guide-tab.active {
    background: var(--bg-secondary);
    color: var(--text-primary);
    box-shadow: var(--shadow-sm);
}

/* ===== Guide Content ===== */
.guide-content h3 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-primary);
}
.guide-step {
    display: flex;
    gap: 14px;
    padding: 14px 0;
    border-bottom: 1px solid var(--border-light);
}
.guide-step:last-child { border-bottom: none; }
.step-number {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: 50%;
    font-size: 13px;
    font-weight: 600;
    flex-shrink: 0;
}
.step-content { flex: 1; }
.step-content p { font-size: 14px; line-height: 1.5; margin-bottom: 4px; }
.step-content h4 { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.note {
    font-size: 12px !important;
    color: var(--text-muted) !important;
}
.note.warning { color: var(--red) !important; }
code {
    background: var(--bg-primary);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12px;
    font-family: 'SF Mono', 'Fira Code', monospace;
}
.code-block {
    display: block;
    padding: 10px 14px;
    margin-top: 8px;
    border-radius: var(--radius-sm);
    background: var(--bg-primary);
    font-size: 12px;
}

/* Feature list in guide */
.feature-list { display: flex; flex-direction: column; gap: 4px; }
.feature-item {
    display: flex;
    gap: 14px;
    padding: 12px;
    border-radius: var(--radius);
    transition: background var(--transition);
}
.feature-item:hover { background: var(--bg-primary); }
.feature-item > i {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: var(--radius-sm);
    font-size: 14px;
    flex-shrink: 0;
}
.feature-item h4 { font-size: 14px; font-weight: 600; margin-bottom: 2px; }
.feature-item p { font-size: 13px; color: var(--text-secondary); }

/* ===== Toast ===== */
#toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 12px 16px;
    border-radius: var(--radius);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
    animation: slideIn 0.2s ease;
    max-width: 320px;
}
.toast.success { border-color: var(--green-border); }
.toast.success i { color: var(--green); }
.toast.error { border-color: var(--red-border); }
.toast.error i { color: var(--red); }
.toast.info i { color: var(--accent); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    #connection-bar {
        flex-wrap: wrap;
        gap: 10px;
    }
    .connection-form {
        order: 3;
        width: 100%;
        flex-wrap: wrap;
    }
    .connection-form input { flex: 1; min-width: 100px; }
    #connected-view {
        grid-template-columns: 1fr;
    }
    .stream-buttons { flex-direction: column; }
    .scenes-grid {
        grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    }
}

@media (max-width: 480px) {
    #main-content { padding: 12px; }
    .panel-header { padding: 12px 14px; }
    .scenes-grid { padding: 10px 14px; }
    .sources-list { padding: 6px 14px 10px; }
    .audio-list { padding: 6px 14px 10px; }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-active); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
