/* * ==========================================================================
 * 1. CORE SYSTEM VARIABLES & RESET
 * ==========================================================================
 */
:root {
    --h-primary: #00ffcc;       /* Cyan Main */
    --h-secondary: #ff0055;     /* Error/Alert Red */
    --h-warning: #ffcc00;       /* Warning Yellow */
    --h-dim: rgba(0, 255, 204, 0.08);
    --h-dark: #050a0f;          /* Deep Void */
    --h-panel: rgba(10, 16, 22, 0.95);
    --h-border: 1px solid rgba(0, 255, 204, 0.3);
    --h-glow: 0 0 10px rgba(0, 255, 204, 0.5);
    
    --font-ui: 'Rajdhani', sans-serif;
    --font-code: 'JetBrains Mono', monospace;
    
    --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
    
    /* Kích thước header/footer mặc định */
    --header-h: 50px;
    --footer-h: 90px;
}

* { margin: 0; padding: 0; box-sizing: border-box; outline: none; user-select: none; -webkit-tap-highlight-color: transparent; }

body {
    background-color: var(--h-dark);
    color: var(--h-primary);
    font-family: var(--font-code);
    /* Sử dụng dvh để fix lỗi trên mobile browsers */
    height: 100vh;
    height: 100dvh; 
    overflow: hidden; /* Mặc định ẩn scroll body trên desktop */
    display: flex;
    flex-direction: column;
    position: relative;
    font-size: 16px; /* Base font size */
}

/* * ==========================================================================
 * 2. CUSTOM SCROLLBARS (Cross-platform)
 * ==========================================================================
 */
::-webkit-scrollbar { width: 6px; height: 6px; background: #000; }
::-webkit-scrollbar-track { background: rgba(0, 255, 204, 0.05); }
::-webkit-scrollbar-thumb { 
    background: var(--h-primary); border: 1px solid #000; 
    box-shadow: inset 0 0 5px rgba(0,0,0,0.5);
}
::-webkit-scrollbar-corner { background: #000; }

/* * ==========================================================================
 * 3. VISUAL EFFECTS LAYER
 * ==========================================================================
 */
.scanlines {
    position: fixed; inset: 0; pointer-events: none; z-index: 999;
    background: linear-gradient(to bottom, rgba(255,255,255,0), rgba(255,255,255,0) 50%, rgba(0,0,0,0.2) 50%, rgba(0,0,0,0.2));
    background-size: 100% 4px; opacity: 0.6;
}
.crt-flicker { animation: flicker 0.15s infinite; opacity: 0.98; }
@keyframes flicker { 0% { opacity: 0.98; } 50% { opacity: 0.95; } 100% { opacity: 0.98; } }

/* * ==========================================================================
 * 4. LAYOUT COMPONENTS (Desktop Default)
 * ==========================================================================
 */
header {
    height: var(--header-h);
    flex-shrink: 0;
    border-bottom: var(--h-border);
    display: flex; justify-content: space-between; align-items: center;
    padding: 0 20px; background: rgba(0,0,0,0.8);
    font-family: var(--font-ui); text-transform: uppercase; letter-spacing: 2px;
    z-index: 10;
    /* An toàn cho màn hình tai thỏ */
    padding-top: env(safe-area-inset-top); 
}

.main-workspace {
    flex: 1; /* Chiếm toàn bộ không gian còn lại */
    display: grid;
    /* Grid thông minh: Cột giữa co giãn, 2 cột bên cố định nhưng responsive */
    grid-template-columns: 300px minmax(0, 1fr) 300px;
    gap: 15px; padding: 15px;
    overflow: hidden; 
    position: relative;
}

.panel {
    background: var(--h-panel);
    border: var(--h-border);
    display: flex; flex-direction: column;
    position: relative;
    backdrop-filter: blur(5px);
    transition: border-color 0.3s;
    overflow: hidden;
}
.panel:hover { border-color: var(--h-primary); }

.panel-head {
    background: var(--h-dim); padding: 8px 12px;
    font-size: 0.75rem; font-weight: bold;
    border-bottom: var(--h-border);
    display: flex; justify-content: space-between; align-items: center;
    color: #fff; flex-shrink: 0;
}

/* --- LEFT PANEL: DATABASE --- */
.playlist-container { overflow-y: auto; flex: 1; }
.track-item {
    padding: 12px 10px; border-bottom: 1px solid rgba(255,255,255,0.05);
    cursor: pointer; transition: 0.2s; font-size: 0.8rem;
    display: flex; justify-content: space-between; align-items: center;
}
.track-item:hover { background: rgba(0, 255, 204, 0.1); padding-left: 15px; }
.track-item.active { 
    background: var(--h-primary); color: #000; font-weight: bold; 
    border-left: 4px solid #fff;
}
.track-remove { 
    opacity: 0; color: var(--h-secondary); padding: 2px 6px; 
    border: 1px solid var(--h-secondary); font-size: 0.7em; cursor: pointer;
}
.track-item:hover .track-remove { opacity: 1; }
.track-remove:hover { background: var(--h-secondary); color: #fff; }

.add-zone {
    padding: 10px; border-top: var(--h-border);
    background: rgba(0,0,0,0.3); flex-shrink: 0;
}
input[type="text"] {
    background: rgba(0,0,0,0.5); border: 1px solid #333; color: #fff;
    padding: 8px; font-family: var(--font-code); font-size: 0.8rem;
    width: 100%; transition: 0.3s;
}
input[type="text"]:focus { border-color: var(--h-primary); }
#file-input-hidden { display: none; }

/* --- CENTER PANEL: VISUALIZER --- */
#viz-wrapper { 
    position: relative; width: 100%; height: 100%; 
    background: radial-gradient(circle at center, #111 0%, #000 100%);
    overflow: hidden;
}
canvas { display: block; width: 100%; height: 100%; }

.drag-overlay {
    position: absolute; inset: 10px; background: rgba(0, 255, 204, 0.1);
    border: 2px dashed var(--h-primary); z-index: 50;
    display: none; align-items: center; justify-content: center; flex-direction: column;
    pointer-events: none; backdrop-filter: blur(4px);
}
body.is-dragging .drag-overlay { display: flex; }

/* --- RIGHT PANEL: LOGS & EQ --- */
.log-terminal {
    flex: 1; overflow-y: auto; background: #000; padding: 10px;
    font-size: 0.7rem; color: #aaa; border-bottom: 1px solid #333;
    min-height: 150px;
}
.log-line { margin-bottom: 5px; border-left: 2px solid transparent; padding-left: 5px; word-break: break-all; }
.log-info { color: var(--h-primary); border-color: var(--h-primary); }
.log-warn { color: var(--h-warning); border-color: var(--h-warning); }
.log-err { color: var(--h-secondary); border-color: var(--h-secondary); background: rgba(255,0,85,0.1); }

.eq-container {
    height: 150px; padding: 10px; flex-shrink: 0;
    display: flex; justify-content: space-around; align-items: flex-end;
    background: linear-gradient(to top, rgba(0,255,204,0.05), transparent);
}
.slider-wrap { display: flex; flex-direction: column; align-items: center; height: 100%; gap: 5px; }
.slider-wrap span { font-size: 0.6rem; color: #aaa; }
input[type=range].eq-slider {
    writing-mode: bt-lr; -webkit-appearance: slider-vertical;
    width: 20px; /* Rộng hơn để dễ chạm trên mobile */
    height: 100%; background: #222; cursor: pointer;
}

/* * ==========================================================================
 * 5. FOOTER CONTROLS
 * ==========================================================================
 */
footer {
    height: var(--footer-h);
    flex-shrink: 0;
    border-top: var(--h-border); background: var(--h-panel);
    display: grid; grid-template-columns: 280px 1fr 280px;
    align-items: center; padding: 0 25px; gap: 20px;
    z-index: 20; position: relative;
    /* An toàn cho thanh vuốt iPhone */
    padding-bottom: calc(5px + env(safe-area-inset-bottom)); 
}

.track-info { overflow: hidden; white-space: nowrap; }
.track-title { font-size: 1rem; font-weight: bold; color: #fff; text-overflow: ellipsis; overflow: hidden; }
.track-meta { font-size: 0.7rem; color: var(--h-primary); opacity: 0.8; }

.transport-controls { display: flex; justify-content: center; gap: 15px; align-items: center; }
.btn {
    background: rgba(0,0,0,0.5); border: 1px solid var(--h-primary); color: var(--h-primary);
    padding: 8px 20px; font-family: var(--font-ui); cursor: pointer; text-transform: uppercase;
    font-weight: 700; transition: all 0.2s; position: relative; overflow: hidden;
    min-width: 44px; min-height: 44px; /* Chuẩn touch target tối thiểu */
    display: flex; align-items: center; justify-content: center;
}
.btn:hover { background: var(--h-primary); color: #000; box-shadow: 0 0 10px var(--h-primary); }
.btn:active { transform: scale(0.96); }
.btn-sm { padding: 4px 10px; font-size: 0.7rem; min-width: auto; min-height: 30px;}

.progress-bar-wrapper {
    position: absolute; top: -5px; left: 0; width: 100%; height: 5px;
    background: #111; cursor: pointer; z-index: 30; transition: height 0.2s;
}
.progress-bar-wrapper:hover { height: 10px; top: -10px; }
.progress-fill { 
    height: 100%; background: var(--h-primary); width: 0%; 
    box-shadow: 0 0 10px var(--h-primary); position: relative;
}

.volume-control {
    display: flex; align-items: center; gap: 10px; justify-content: flex-end;
}
input[type=range].vol-slider {
    -webkit-appearance: none; width: 100px; height: 4px; background: #333;
}
input[type=range].vol-slider::-webkit-slider-thumb {
    -webkit-appearance: none; width: 15px; height: 15px; 
    background: var(--h-primary); border-radius: 50%; cursor: pointer;
}

/* * ==========================================================================
 * 6. RESPONSIVE LOGIC (MEDIA QUERIES)
 * ==========================================================================
 */

/* --- TABLET (iPad Portrait) & SMALL LAPTOPS (Max width 1024px) --- */
@media (max-width: 1024px) {
    .main-workspace {
        /* Chuyển sang 2 cột: Playlist bên trái, Viz + EQ bên phải */
        grid-template-columns: 250px 1fr;
        grid-template-rows: 1fr 200px; /* Viz trên, EQ dưới */
        grid-template-areas: 
            "playlist viz"
            "playlist logs";
    }
    
    /* Gán khu vực */
    .panel:nth-child(1) { grid-area: playlist; } /* Playlist Panel */
    .panel:nth-child(2) { grid-area: viz; }      /* Viz Panel */
    .panel:nth-child(3) { grid-area: logs; }     /* Log Panel */

    footer {
        padding: 10px 15px;
        grid-template-columns: 200px 1fr 200px;
    }
}

/* --- MOBILE DEVICES (Max width 768px) --- */
@media (max-width: 768px) {
    body {
        /* Cho phép cuộn trang trên mobile vì nội dung sẽ dài ra */
        overflow: auto; 
        height: auto; 
        min-height: 100dvh;
        display: block; /* Bỏ flex column để scroll tự nhiên */
    }

    header {
        position: sticky; top: 0; width: 100%;
        background: rgba(5, 10, 15, 0.95); /* Đậm hơn để che nội dung khi scroll */
    }

    .main-workspace {
        display: flex; flex-direction: column;
        height: auto; overflow: visible; padding: 10px;
    }

    /* Tái cấu trúc thứ tự hiển thị trên Mobile */
    .panel { margin-bottom: 15px; width: 100%; overflow: visible; min-height: 0; }
    
    /* 1. Visualizer lên đầu (quan trọng nhất) */
    .panel:nth-child(2) { order: 1; height: 35vh; min-height: 250px; }
    
    /* 2. Playlist ở giữa */
    .panel:nth-child(1) { order: 2; height: 40vh; min-height: 300px; }
    
    /* 3. Log/EQ ở cuối */
    .panel:nth-child(3) { order: 3; height: auto; }

    .log-terminal { height: 150px; } /* Giới hạn chiều cao log */
    
    /* Footer Mobile: Stack dọc */
    footer {
        position: sticky; bottom: 0;
        display: flex; flex-direction: column;
        height: auto; padding: 15px; gap: 15px;
        background: rgba(10, 16, 22, 0.98);
        border-top: 2px solid var(--h-primary);
    }

    .track-info { width: 100%; text-align: center; margin-bottom: 5px; }
    .transport-controls { width: 100%; justify-content: space-between; }
    .volume-control { width: 100%; justify-content: center; }
    input[type=range].vol-slider { width: 80%; }
    
    /* Touch optimization */
    .track-remove { opacity: 1; padding: 5px 10px; } /* Luôn hiện nút xóa */
}

/* --- LARGE SCREENS / TV (4K - Min width 1920px) --- */
@media (min-width: 2000px) {
    :root {
        --header-h: 80px;
        --footer-h: 140px;
    }
    body { font-size: 24px; } /* Tăng base font size */
    
    .panel-head { font-size: 1.2rem; padding: 15px 20px; }
    .track-item { padding: 20px; font-size: 1.2rem; }
    
    .btn { font-size: 1.2rem; padding: 15px 40px; }
    .track-title { font-size: 2rem; }
    
    /* Scanlines dày hơn cho màn hình to */
    .scanlines { background-size: 100% 8px; }
}