* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #6366f1;
    --secondary-color: #64748b;
    --bg-color: #0f172a;
    --surface-color: #1e293b;
    --surface-color-strong: #0b1220;
    --text-color: #f1f5f9;
    --border-color: #334155;
    --danger-color: #ef4444;
    --success-color: #10b981;
    --overlay-color: rgba(15, 23, 42, 0.9);
}

.hidden {
    display: none !important;
}

body {
    min-height: 100vh;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
    background:
        radial-gradient(circle at top, rgba(99, 102, 241, 0.16), transparent 32%),
        linear-gradient(180deg, #020617 0%, var(--bg-color) 36%, #020617 100%);
    color: var(--text-color);
    overflow-x: hidden;
}

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

header {
    background: rgba(15, 23, 42, 0.86);
    backdrop-filter: blur(14px);
    padding: 1rem 2rem;
    border-bottom: 1px solid rgba(148, 163, 184, 0.18);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    position: sticky;
    top: 0;
    z-index: 20;
}

.header-expand-btn {
    position: fixed;
    top: 0.75rem;
    right: 0.75rem;
    z-index: 30;
    border: 1px solid rgba(148, 163, 184, 0.28);
    background: rgba(15, 23, 42, 0.88);
    color: var(--text-color);
    border-radius: 999px;
    padding: 0.5rem 0.9rem;
    font-size: 0.8rem;
    cursor: pointer;
}

body.header-collapsed header,
body.header-collapsed #focusPresetBar {
    display: none !important;
}

body.header-collapsed .header-expand-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
}

header h1 {
    font-size: 1.5rem;
    font-weight: 700;
}

.header-controls {
    display: flex;
    gap: 0.5rem;
}

.focus-preset-bar {
    display: flex;
    align-items: center;
    gap: 0.9rem;
    padding: 0.85rem 1rem;
    margin: 0 1rem;
    border-bottom: 1px solid rgba(148, 163, 184, 0.16);
    background: rgba(15, 23, 42, 0.58);
    backdrop-filter: blur(10px);
}

.focus-preset-label {
    font-size: 0.85rem;
    color: rgba(241, 245, 249, 0.88);
    white-space: nowrap;
}

.focus-preset-buttons {
    display: flex;
    gap: 0.5rem;
}

.focus-preset-btn {
    min-width: 40px;
    height: 36px;
    border: 1px solid rgba(148, 163, 184, 0.22);
    border-radius: 0.5rem;
    background: rgba(30, 41, 59, 0.72);
    color: var(--text-color);
    cursor: pointer;
    transition: transform 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

.focus-preset-btn:hover {
    transform: translateY(-1px);
    border-color: rgba(99, 102, 241, 0.65);
}

.focus-preset-btn.active {
    background: rgba(99, 102, 241, 0.28);
    border-color: rgba(99, 102, 241, 0.9);
}

.focus-preset-hint {
    font-size: 0.75rem;
    color: rgba(148, 163, 184, 0.9);
}

.btn-primary,
.btn-secondary {
    padding: 0.65rem 1rem;
    border: none;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, background 0.2s ease, opacity 0.2s ease;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: #4f46e5;
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--secondary-color);
    color: white;
}

.btn-secondary:hover {
    background: #475569;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.84);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.modal-content {
    background: var(--surface-color);
    padding: 2rem;
    border-radius: 0.75rem;
    max-width: 500px;
    width: 100%;
    border: 1px solid var(--border-color);
    box-shadow: 0 24px 80px rgba(2, 6, 23, 0.5);
}

.modal-content h2 {
    margin-bottom: 0.5rem;
}

.modal-content .hint {
    color: var(--secondary-color);
    font-size: 0.875rem;
    margin-bottom: 1rem;
}

.input-mode-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--border-color);
}

.tab-btn {
    padding: 0.5rem 1rem;
    background: transparent;
    border: none;
    color: var(--secondary-color);
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 600;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: color 0.2s ease, border-color 0.2s ease;
}

.tab-btn:hover {
    color: var(--text-color);
}

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.input-section {
    margin-bottom: 1rem;
}

.modal-content input,
.modal-content textarea {
    width: 100%;
    padding: 0.85rem 0.9rem;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    color: var(--text-color);
    font-size: 1rem;
    font-family: inherit;
}

.modal-content input:focus,
.modal-content textarea:focus {
    outline: none;
    border-color: var(--primary-color);
}

.modal-content textarea {
    resize: vertical;
    min-height: 120px;
}

.hint-small {
    font-size: 0.75rem;
    color: var(--secondary-color);
    margin-top: 0.5rem;
}

.modal-buttons {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
}

main {
    flex: 1;
    padding: 1rem;
    display: grid;
    gap: 1rem;
}

.stream-container {
    --grid-columns: 1;
    --grid-row-height: 240px;
    grid-template-columns: repeat(var(--grid-columns), minmax(0, 1fr));
    grid-auto-rows: var(--grid-row-height);
    grid-auto-flow: dense;
    align-content: start;
}

.stream-container.free-layout {
    position: relative;
    display: block;
    min-height: calc(100vh - 8rem);
    overflow: auto;
}

.stream-container.focus-mode {
    display: block;
}

.focus-layout {
    display: grid;
    gap: 0;
}

.focus-layout--top {
    grid-template-columns: 1fr;
}

.focus-layout--right {
    grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
    align-items: start;
}

.focus-layout--no-sidebar {
    grid-template-columns: 1fr;
}

.focus-featured-grid {
    display: grid;
    gap: 1rem;
}

.focus-featured-grid.columns-1 {
    grid-template-columns: 1fr;
}

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

.focus-layout--top .focus-featured-grid.columns-1 .stream-wrapper {
    min-height: min(72vh, 760px);
}

.focus-layout--top .focus-featured-grid.columns-2 .stream-wrapper {
    min-height: min(58vh, 620px);
}

.focus-layout--right .focus-featured-grid.columns-1 .stream-wrapper {
    min-height: min(78vh, 860px);
}

.focus-layout--right .focus-featured-grid.columns-2 .stream-wrapper {
    min-height: min(64vh, 700px);
}

.focus-featured-area {
    display: grid;
    min-height: 0;
}

.focus-featured-area.single {
    grid-template-columns: 1fr;
}

.focus-featured-area.dual {
    grid-template-columns: minmax(0, 1fr) 10px minmax(0, 1fr);
}

.focus-featured-area .stream-wrapper {
    min-height: min(64vh, 700px);
}

.thumbnail-row {
    display: grid;
    position: relative;
    grid-template-columns: repeat(auto-fill, minmax(var(--thumbnail-item-width, 180px), var(--thumbnail-item-width, 180px)));
    justify-content: start;
    gap: 0.75rem;
    padding: 1.1rem 0.75rem 0.75rem;
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(148, 163, 184, 0.14);
    border-radius: 0.75rem;
}

.thumbnail-row--vertical {
    display: flex;
    flex-direction: column;
    max-height: min(78vh, 860px);
    overflow-y: auto;
    padding-bottom: 1.25rem;
}

.thumbnail-row--vertical .stream-wrapper {
    min-height: var(--thumbnail-item-height, 160px);
    flex: 0 0 var(--thumbnail-item-height, 160px);
}

.focus-thumbnail-handle {
    position: absolute;
    z-index: 9;
    border-radius: 999px;
    background: rgba(99, 102, 241, 0.92);
    box-shadow: 0 10px 25px rgba(2, 6, 23, 0.32);
}

.focus-thumbnail-handle.horizontal {
    top: 0.3rem;
    right: 0.7rem;
    width: 54px;
    height: 6px;
    cursor: ew-resize;
}

.focus-thumbnail-handle.vertical {
    left: 50%;
    bottom: 0.45rem;
    transform: translateX(-50%);
    width: 6px;
    height: 54px;
    cursor: ns-resize;
}

.focus-resize-divider {
    position: relative;
    background: rgba(148, 163, 184, 0.18);
    z-index: 5;
}

.focus-resize-divider::after {
    content: '';
    position: absolute;
    inset: 50%;
    transform: translate(-50%, -50%);
    border-radius: 999px;
    background: rgba(241, 245, 249, 0.82);
}

.focus-resize-divider.horizontal {
    height: 10px;
    cursor: row-resize;
}

.focus-resize-divider.horizontal::after {
    width: 52px;
    height: 4px;
}

.focus-resize-divider.vertical {
    width: 10px;
    cursor: col-resize;
}

.focus-resize-divider.vertical::after {
    width: 4px;
    height: 52px;
}

.stream-wrapper {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    border-radius: 0.75rem;
    background: linear-gradient(180deg, rgba(30, 41, 59, 0.95), rgba(15, 23, 42, 0.98));
    border: 1px solid rgba(148, 163, 184, 0.18);
    box-shadow: 0 18px 45px rgba(2, 6, 23, 0.28);
    transition: transform 0.22s ease, border-color 0.22s ease, box-shadow 0.22s ease;
}

.stream-wrapper.free-item {
    position: absolute;
    width: 420px;
    height: 236px;
}

.stream-wrapper:hover {
    border-color: rgba(99, 102, 241, 0.7);
    box-shadow: 0 24px 55px rgba(2, 6, 23, 0.34);
}

.stream-wrapper.focused,
.stream-wrapper.focus-primary {
    border-color: rgba(16, 185, 129, 0.76);
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.14), 0 24px 55px rgba(2, 6, 23, 0.35);
}

.stream-wrapper.focus-secondary {
    border-color: rgba(99, 102, 241, 0.78);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.14), 0 24px 55px rgba(2, 6, 23, 0.35);
}

.stream-wrapper.thumbnail {
    min-height: 160px;
    cursor: pointer;
}

.stream-wrapper.dragging {
    opacity: 0.45;
    transform: scale(0.98);
}

.stream-wrapper.drag-over {
    border-color: rgba(99, 102, 241, 0.95);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.stream-wrapper.resizing {
    border-color: rgba(99, 102, 241, 0.95);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.18), 0 24px 55px rgba(2, 6, 23, 0.34);
    user-select: none;
}

.stream-content {
    position: relative;
    flex: 1;
    min-height: 0;
    background: var(--surface-color-strong);
}

.stream-content iframe {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: none;
    background: #000;
}

.stream-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.45rem;
    padding: 0.55rem 0.6rem 0.45rem;
    background: linear-gradient(180deg, rgba(2, 6, 23, 0.66), rgba(2, 6, 23, 0.2));
    border-bottom: 1px solid rgba(148, 163, 184, 0.16);
}

.drag-grip {
    position: relative;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.5rem;
    background: var(--overlay-color);
    color: white;
    cursor: grab;
    user-select: none;
    opacity: 0.85;
    transition: opacity 0.2s ease, transform 0.2s ease, background 0.2s ease;
}

.drag-grip:active {
    cursor: grabbing;
}

.drag-grip:hover {
    background: rgba(15, 23, 42, 1);
    transform: translateY(-1px);
}

.drag-grip.free-drag {
    cursor: grab;
}

.stream-controls {
    position: relative;
    display: flex;
    gap: 0.35rem;
    opacity: 0.9;
    transition: opacity 0.2s ease;
    flex-wrap: wrap;
    justify-content: flex-end;
    margin-left: auto;
}

.stream-wrapper:hover .stream-controls,
.stream-wrapper:hover .drag-grip,
.stream-wrapper:focus-within .stream-controls,
.stream-wrapper:focus-within .drag-grip,
.stream-wrapper.focused .stream-controls {
    opacity: 1;
}

.control-btn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 0.5rem;
    background: var(--overlay-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    transition: transform 0.2s ease, background 0.2s ease, opacity 0.2s ease;
}

.control-btn:hover {
    background: rgba(15, 23, 42, 1);
    transform: translateY(-1px);
}

.control-btn.active {
    background: rgba(99, 102, 241, 0.92);
}

.stream-wrapper.focus-primary .control-btn.focus-btn.active {
    background: rgba(16, 185, 129, 0.9);
}

.control-btn.delete {
    background: rgba(239, 68, 68, 0.9);
}

.control-btn.delete:hover {
    background: rgba(220, 38, 38, 1);
}

.control-btn.disabled {
    opacity: 0.5;
    cursor: default;
}

.resize-handle {
    position: absolute;
    right: 0.45rem;
    bottom: 0.45rem;
    z-index: 12;
    width: 18px;
    height: 18px;
    border-right: 3px solid rgba(255, 255, 255, 0.8);
    border-bottom: 3px solid rgba(255, 255, 255, 0.8);
    border-radius: 0 0 0.3rem 0;
    cursor: nwse-resize;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.stream-wrapper:hover .resize-handle,
.stream-wrapper.resizing .resize-handle {
    opacity: 1;
}

.stream-info {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 0.8rem 0.9rem;
    background: linear-gradient(180deg, transparent, rgba(2, 6, 23, 0.92));
    color: rgba(241, 245, 249, 0.92);
    font-size: 0.78rem;
    line-height: 1.3;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
}

.stream-wrapper:hover .stream-info,
.stream-wrapper.focused .stream-info,
.stream-wrapper.thumbnail .stream-info {
    opacity: 1;
}

.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
    text-align: center;
}

.empty-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
}

.empty-state h2 {
    margin-bottom: 0.5rem;
}

.empty-state p {
    color: var(--secondary-color);
}

.stream-wrapper:fullscreen {
    border-radius: 0;
    border-color: transparent;
    background: #000;
}

.stream-wrapper:fullscreen .stream-controls,
.stream-wrapper:fullscreen .stream-info {
    opacity: 1;
}

.stream-wrapper:fullscreen .stream-toolbar {
    opacity: 1;
}

@media (max-width: 1024px) {
    .focus-layout--right {
        grid-template-columns: 1fr;
    }

    .thumbnail-row--vertical {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        max-height: none;
        overflow: visible;
    }

    .thumbnail-row--vertical .stream-wrapper {
        min-height: 160px;
    }

    .focus-layout--right .focus-resize-divider.vertical {
        display: none;
    }
}

@media (max-width: 768px) {
    header {
        padding: 1rem;
    }

    header h1 {
        font-size: 1.25rem;
    }

    .header-controls {
        width: 100%;
    }

    .header-controls button {
        flex: 1;
    }

    .focus-preset-bar {
        flex-wrap: wrap;
        margin: 0;
        padding: 0.75rem 1rem;
    }

    .focus-preset-hint {
        width: 100%;
    }

    .stream-container {
        --grid-row-height: 220px;
        grid-template-columns: 1fr;
    }

    .focus-featured-grid.columns-2 {
        grid-template-columns: 1fr;
    }

    .focus-featured-area.dual {
        grid-template-columns: 1fr;
    }

    .thumbnail-row {
        grid-template-columns: 1fr;
    }

    .stream-wrapper:hover .stream-controls,
    .stream-wrapper:hover .drag-grip,
    .stream-wrapper .stream-controls,
    .stream-wrapper .drag-grip,
    .stream-wrapper .resize-handle,
    .focus-resize-divider,
    .stream-wrapper .stream-info {
        opacity: 1;
    }

    .drag-grip {
        width: 32px;
        height: 32px;
    }

    .focus-resize-divider {
        display: none;
    }
}
