/* =========================================
   0. CORE VARIABLES & TOKENS
   ========================================= */
:root {
    /* SYSTEM IDENTITY */
    --hue-sys: 210; 
    --sat-sys: 100%; 
    --lum-sys: 50%;
    
    /* DYNAMIC PALETTE GENERATION */
    --color-primary: hsl(var(--hue-sys), var(--sat-sys), var(--lum-sys));
    --color-primary-dim: hsla(var(--hue-sys), var(--sat-sys), var(--lum-sys), 0.15);
    --color-primary-glow: hsla(var(--hue-sys), var(--sat-sys), var(--lum-sys), 0.6);
    --color-bg: #020304;
    --color-bg-transparent: rgba(2, 3, 4, 0.6);
    
    /* UI COMPONENT COLORS */
    --color-panel: rgba(10, 14, 18, 0.85);
    --color-panel-header: rgba(255, 255, 255, 0.05);
    --color-border: hsla(var(--hue-sys), var(--sat-sys), var(--lum-sys), 0.4);
    --color-border-faint: hsla(var(--hue-sys), var(--sat-sys), var(--lum-sys), 0.1);
    
    /* TEXT COLORS */
    --color-text: #e0e6ed;
    --color-text-dim: #6b7d8c;
    --color-text-mut: #4a5b6c;
    
    /* SEMANTIC COLORS */
    --color-error: #ff3333;
    --color-success: #00ff66;
    --color-warning: #ffcc00;
    --color-info: #00ccff;

    /* FONTS */
    --font-ui: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --font-mono: 'Consolas', 'Courier New', monospace;

    /* LAYOUT & SPACING */
    --taskbar-height: 48px;
    --header-height: 32px;
    
    /* Z-LAYERS (Strict Hierarchy) */
    --z-bg: 0;
    --z-scanline: 10;
    --z-ui: 100;
    --z-win: 200;
    --z-win-focused: 300;
    --z-modal: 500;
    --z-ctx: 800;
    --z-overlay: 900;
    --z-boot: 1000;
    --z-cursor: 2000;
}

/* =========================================
   1. GLOBAL RESET & BASE
   ========================================= */
*, *::before, *::after { 
    box-sizing: border-box; 
    user-select: none; 
    -webkit-user-drag: none;
    -webkit-tap-highlight-color: transparent;
}

body, html {
    width: 100%; height: 100%;
    margin: 0; padding: 0;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-ui);
    overflow: hidden;
    cursor: default;
}

/* CUSTOM SCROLLBARS (Webkit) */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: rgba(0,0,0,0.3); }
::-webkit-scrollbar-corner { background: transparent; }
::-webkit-scrollbar-thumb { 
    background: var(--color-border); 
    border-radius: 0px; 
    border: 1px solid transparent;
    background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--color-primary); }

/* =========================================
   2. CRT & POST-PROCESSING EFFECTS
   ========================================= */
#crt-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    z-index: var(--z-overlay); pointer-events: none;
    background: 
        linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.1) 50%), 
        linear-gradient(90deg, rgba(255, 0, 0, 0.03), rgba(0, 255, 0, 0.01), rgba(0, 0, 255, 0.03));
    background-size: 100% 3px, 3px 100%;
    mix-blend-mode: overlay;
}

#scanline {
    position: fixed; top: 0; left: 0; width: 100%; height: 12px;
    z-index: var(--z-scanline); pointer-events: none;
    background: linear-gradient(to bottom, transparent, var(--color-primary-glow), transparent);
    animation: scan 8s linear infinite;
    opacity: 0.1;
}
@keyframes scan { 0% { top: -10%; } 100% { top: 110%; } }

/* RGB SHIFT ANIMATION FOR SHAKE */
.shake-anim { animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both; }
@keyframes shake {
    10%, 90% { transform: translate3d(-2px, 0, 0); filter: drop-shadow(-2px 0 2px var(--color-error)); }
    20%, 80% { transform: translate3d(3px, 0, 0); }
    30%, 50%, 70% { transform: translate3d(-5px, 0, 0); }
    40%, 60% { transform: translate3d(5px, 0, 0); }
}

/* =========================================
   3. BACKGROUND VISUALS
   ========================================= */
#layer-bg {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    z-index: var(--z-bg);
    transition: opacity 2s ease-in-out;
    opacity: 0; /* Managed by Boot Sequence */
}

/* =========================================
   4. WINDOW SYSTEM
   ========================================= */
#layer-ui {
    position: absolute; top: 0; left: 0; 
    width: 100%; height: calc(100% - var(--taskbar-height));
    z-index: var(--z-ui); pointer-events: none;
}

.window {
    position: absolute;
    background: var(--color-panel);
    border: 1px solid var(--color-border);
    border-top: 2px solid var(--color-primary);
    box-shadow: 
        0 15px 40px rgba(0,0,0,0.9), 
        0 0 0 1px rgba(255,255,255,0.02),
        inset 0 1px 0 rgba(255,255,255,0.1);
    backdrop-filter: blur(16px) saturate(120%);
    -webkit-backdrop-filter: blur(16px) saturate(120%);
    border-radius: 2px;
    display: flex; flex-direction: column;
    pointer-events: auto;
    overflow: hidden;
    min-width: 240px; min-height: 150px;
    transition: opacity 0.25s, transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.2s;
    will-change: transform, opacity;
}

/* Open/Close Animations */
.window.opening { animation: win-open 0.4s cubic-bezier(0.23, 1, 0.32, 1) forwards; }
@keyframes win-open {
    from { opacity: 0; transform: scale(0.94) translateY(20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.window.focused {
    z-index: var(--z-win-focused);
    box-shadow: 
        0 30px 70px rgba(0,0,0,1), 
        0 0 20px var(--color-primary-dim);
    border-color: var(--color-primary);
}

.window.maximized {
    top: 0 !important; left: 0 !important;
    width: 100% !important; height: 100% !important;
    border-radius: 0; border: none; border-top: 2px solid var(--color-primary);
}

.window.minimized {
    opacity: 0; transform: scale(0.8) translateY(200px);
    pointer-events: none;
}

/* WINDOW HEADER */
.win-header {
    height: var(--header-height);
    background: linear-gradient(90deg, var(--color-panel-header), transparent);
    border-bottom: 1px solid var(--color-border-faint);
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 12px;
    cursor: grab;
    font-family: var(--font-mono); font-size: 0.75rem; letter-spacing: 1.5px;
    color: var(--color-primary);
    text-shadow: 0 0 10px var(--color-primary-dim);
}
.win-header:active { cursor: grabbing; background: rgba(255,255,255,0.08); }

.win-title { display: flex; align-items: center; gap: 8px; font-weight: 700; text-transform: uppercase; }
.win-icon { font-size: 14px; filter: drop-shadow(0 0 5px var(--color-primary)); }

.win-controls { display: flex; gap: 8px; height: 100%; align-items: center; }
.win-btn {
    width: 10px; height: 10px; border-radius: 50%;
    position: relative; cursor: pointer;
    transition: transform 0.2s, filter 0.2s;
}
.win-btn:hover { transform: scale(1.3); filter: brightness(1.4); }
.win-btn.min { background: #ffd43b; box-shadow: 0 0 6px rgba(255, 212, 59, 0.4); }
.win-btn.max { background: #51cf66; box-shadow: 0 0 6px rgba(81, 207, 102, 0.4); }
.win-btn.close { background: #ff6b6b; box-shadow: 0 0 6px rgba(255, 107, 107, 0.4); }

/* WINDOW CONTENT */
.win-content {
    flex: 1; position: relative;
    display: flex; flex-direction: column;
    overflow: hidden;
    background: rgba(0,0,0,0.2);
}

.win-resize {
    position: absolute; bottom: 0; right: 0;
    width: 20px; height: 20px;
    cursor: nwse-resize;
    background: linear-gradient(135deg, transparent 50%, var(--color-border) 50%);
    opacity: 0.5;
    z-index: 10;
    clip-path: polygon(100% 0, 0 100%, 100% 100%);
}
.win-resize:hover { opacity: 1; background: linear-gradient(135deg, transparent 50%, var(--color-primary) 50%); }

/* =========================================
   5. APP SPECIFIC STYLES
   ========================================= */

/* TERMINAL APP */
.term-wrapper {
    flex: 1; display: flex; flex-direction: column;
    padding: 12px; font-family: var(--font-mono); font-size: 0.85rem;
    color: #fff;
}
.term-output {
    flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px;
    padding-bottom: 12px; scroll-behavior: smooth;
}
.term-line { 
    display: flex; gap: 12px; line-height: 1.5; 
    word-break: break-all; animation: fade-in 0.1s; 
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.t-ts { color: var(--color-text-mut); font-size: 0.75rem; min-width: 60px; user-select: none; }
.t-sys { color: var(--color-text-dim); }
.t-cmd { color: #fff; font-weight: bold; }
.t-err { color: var(--color-error); text-shadow: 0 0 8px rgba(255,51,51,0.4); }
.t-ok { color: var(--color-primary); text-shadow: 0 0 8px var(--color-primary-dim); }
.t-warn { color: var(--color-warning); }
.t-highlight { color: #fff; background: var(--color-primary-dim); padding: 0 4px; border-radius: 2px; }

.term-input-row { 
    display: flex; gap: 8px; align-items: center; 
    border-top: 1px solid rgba(255,255,255,0.1); 
    padding-top: 12px; margin-top: 4px;
}
.t-prompt { color: var(--color-primary); font-weight: bold; text-shadow: 0 0 5px var(--color-primary-dim); }
#term-cmd { 
    flex: 1; background: transparent; border: none; outline: none; 
    color: #fff; font-family: var(--font-mono); font-size: 0.9rem; 
    caret-color: var(--color-primary); 
}

/* GLOBE / STATUS APP */
.webgl-container { 
    width: 100%; height: 100%; position: relative; 
    background: radial-gradient(circle at center, #111 0%, #000 100%); 
    overflow: hidden; 
}
.status-overlay {
    position: absolute; bottom: 12px; left: 12px;
    font-family: var(--font-mono); font-size: 0.7rem; color: var(--color-primary);
    pointer-events: none;
    text-shadow: 0 0 5px #000;
    background: rgba(0,0,0,0.7); 
    padding: 8px; 
    border: 1px solid var(--color-primary-dim);
    border-left: 2px solid var(--color-primary);
    line-height: 1.6;
}
.status-header { font-weight: bold; border-bottom: 1px solid rgba(255,255,255,0.1); margin-bottom: 4px; display: block; }

/* SETTINGS APP */
.cfg-container { padding: 20px; display: flex; flex-direction: column; gap: 20px; }
.cfg-row { 
    display: flex; flex-direction: column; gap: 8px;
    font-size: 0.8rem; font-weight: 600; color: #ccc; 
}
.cfg-label-row { display: flex; justify-content: space-between; }
.cfg-label-row label { letter-spacing: 1px; font-family: var(--font-mono); color: var(--color-primary); }
.cfg-val { color: var(--color-text-dim); }

input[type="range"] {
    -webkit-appearance: none; background: transparent; width: 100%;
}
input[type="range"]::-webkit-slider-runnable-track {
    height: 4px; background: rgba(255,255,255,0.1); border-radius: 2px;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; margin-top: -6px;
    width: 16px; height: 16px; background: var(--color-bg); 
    border: 2px solid var(--color-primary);
    border-radius: 50%;
    box-shadow: 0 0 10px var(--color-primary); cursor: pointer;
    transition: transform 0.1s;
}
input[type="range"]::-webkit-slider-thumb:hover { transform: scale(1.2); background: var(--color-primary); }

.btn-full {
    width: 100%; padding: 14px;
    background: rgba(255,255,255,0.02); border: 1px solid var(--color-primary-dim);
    color: var(--color-primary); font-family: var(--font-mono);
    font-weight: bold; cursor: pointer; transition: all 0.2s;
    text-transform: uppercase; letter-spacing: 2px; font-size: 0.75rem;
    position: relative; overflow: hidden;
}
.btn-full::before {
    content: ''; position: absolute; top: 0; left: -100%; width: 100%; height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
    transition: 0.4s;
}
.btn-full:hover { 
    background: var(--color-primary); color: #000; 
    box-shadow: 0 0 25px var(--color-primary-dim); 
}
.btn-full:hover::before { left: 100%; }

/* =========================================
   6. TASKBAR
   ========================================= */
#taskbar {
    position: fixed; bottom: 0; left: 0; width: 100%; height: var(--taskbar-height);
    background: rgba(5, 8, 12, 0.95);
    border-top: 1px solid var(--color-border);
    display: flex; align-items: center; padding: 0 8px; gap: 8px;
    z-index: var(--z-overlay);
    box-shadow: 0 -5px 20px rgba(0,0,0,0.7);
    backdrop-filter: blur(20px);
}

#start-btn {
    padding: 0 24px; height: 36px;
    display: flex; align-items: center; justify-content: center; gap: 8px;
    border: 1px solid var(--color-primary-dim);
    color: var(--color-primary); font-weight: 900; letter-spacing: 2px;
    background: rgba(255,255,255,0.02);
    font-family: var(--font-mono); font-size: 0.9rem;
    cursor: pointer; transition: 0.2s;
    text-shadow: 0 0 8px var(--color-primary-dim);
}
#start-btn:hover, #start-btn.active { 
    background: var(--color-primary); color: #000; 
    box-shadow: 0 0 20px var(--color-primary);
    text-shadow: none;
}
.start-icon { font-size: 1.1em; }

.task-sep { width: 1px; height: 24px; background: rgba(255,255,255,0.1); margin: 0 8px; }

#task-list { flex: 1; display: flex; gap: 4px; overflow-x: auto; height: 100%; align-items: center; padding-right: 10px; }

.task-item {
    padding: 0 16px; height: 36px; min-width: 140px; max-width: 220px;
    background: rgba(255,255,255,0.03);
    border-bottom: 2px solid transparent;
    display: flex; align-items: center; gap: 10px;
    font-size: 0.8rem; color: var(--color-text-dim);
    cursor: pointer; transition: all 0.2s;
    border-radius: 2px 2px 0 0;
    white-space: nowrap; overflow: hidden;
    font-family: var(--font-ui);
    user-select: none;
}
.task-item:hover { background: rgba(255,255,255,0.08); color: #fff; }
.task-item.active {
    background: linear-gradient(to top, rgba(255,255,255,0.08), transparent);
    border-bottom-color: var(--color-primary);
    color: var(--color-primary);
    text-shadow: 0 0 8px var(--color-primary-dim);
}
.task-icon { font-size: 1.1em; filter: drop-shadow(0 0 2px var(--color-primary)); }

#sys-tray { 
    margin-left: auto; display: flex; align-items: center; gap: 18px; 
    padding-right: 12px; font-family: var(--font-mono); font-size: 0.75rem; 
    color: var(--color-text-dim);
}
.tray-icon { cursor: pointer; transition: color 0.2s; font-size: 1.1rem; }
.tray-icon:hover { color: #fff; text-shadow: 0 0 5px #fff; }
.net-stat { display: flex; align-items: center; gap: 8px; cursor: help; }
.ping-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--color-success); box-shadow: 0 0 6px var(--color-success); animation: pulse 2s infinite; }
@keyframes pulse { 0% { opacity: 0.4; transform: scale(0.8); } 50% { opacity: 1; transform: scale(1.2); } 100% { opacity: 0.4; transform: scale(0.8); } }

/* =========================================
   7. CONTEXT MENU & TOASTS
   ========================================= */
#ctx-menu {
    position: fixed; display: none; flex-direction: column;
    background: rgba(15, 20, 25, 0.98);
    border: 1px solid var(--color-border);
    min-width: 200px; padding: 6px 0;
    z-index: var(--z-ctx);
    box-shadow: 0 15px 40px rgba(0,0,0,0.8);
    backdrop-filter: blur(12px);
    border-radius: 2px;
    animation: ctx-fade 0.1s ease-out;
}
@keyframes ctx-fade { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }

.ctx-item {
    padding: 10px 16px; font-size: 0.85rem; cursor: pointer;
    color: #ccc; transition: 0.1s; display: flex; justify-content: space-between; align-items: center;
    font-family: var(--font-ui);
}
.ctx-item:hover { background: var(--color-primary); color: #000; }
.ctx-item span { pointer-events: none; }
.ctx-shortcut { font-size: 0.7rem; opacity: 0.6; }
.ctx-div { height: 1px; background: rgba(255,255,255,0.1); margin: 4px 0; }

#toast-area {
    position: fixed; top: 20px; right: 20px;
    display: flex; flex-direction: column; gap: 12px;
    z-index: var(--z-boot); pointer-events: none;
    width: 320px;
}
.toast {
    pointer-events: auto;
    background: rgba(12, 18, 24, 0.95);
    border: 1px solid var(--color-border);
    border-left: 4px solid var(--color-primary);
    padding: 16px 20px;
    font-family: var(--font-mono); font-size: 0.8rem;
    color: #fff;
    box-shadow: 0 10px 30px rgba(0,0,0,0.6);
    animation: slide-in 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
    backdrop-filter: blur(8px);
    display: flex; flex-direction: column; gap: 4px;
}
.toast-title { font-weight: bold; color: var(--color-primary); text-transform: uppercase; font-size: 0.75rem; letter-spacing: 1px; }
.toast-body { line-height: 1.4; color: #ddd; }

.toast.err { border-left-color: var(--color-error); }
.toast.err .toast-title { color: var(--color-error); }

.toast.warn { border-left-color: var(--color-warning); }
.toast.warn .toast-title { color: var(--color-warning); }

@keyframes slide-in { from { opacity:0; transform: translateX(50px); } to { opacity:1; transform: translateX(0); } }
.toast-out { animation: slide-out 0.4s cubic-bezier(0.6, -0.28, 0.735, 0.045) forwards; }
@keyframes slide-out { to { opacity:0; transform: translateX(50px); } }

/* =========================================
   8. BOOT SCREEN
   ========================================= */
#boot-screen {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: #000; color: var(--color-primary);
    font-family: var(--font-mono); padding: 40px;
    z-index: var(--z-boot);
    display: flex; flex-direction: column; justify-content: flex-end;
    text-shadow: 0 0 5px var(--color-primary-dim);
    cursor: pointer;
    user-select: none;
}
.boot-row { margin-bottom: 6px; opacity: 0; animation: type-line 0.05s forwards; font-size: 0.9rem; }
@keyframes type-line { to { opacity: 1; } }

.boot-hint { 
    position: absolute; bottom: 30px; right: 30px; 
    font-size: 0.75rem; color: #555; 
    animation: blink 1s infinite; 
    border: 1px solid #333; padding: 8px 16px;
}
@keyframes blink { 50% { opacity: 0; } }