/* =========================
   1. CORE VARIABLES & THEMES
   ========================= */
:root {
    --bg: #ffffff;
    --topbar-bg: #ffffff;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-muted: #64748b;
    --accent: #0070f3; 
    --air-blue: #f8fafc;
    --run-btn: #0070f3;
    --tab-active-bg: #f1f5f9;
    --io-pane-size: 33%;
    --stdin-section-size: 35%;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

[data-theme="dark"] {
    --bg: #0f172a;
    --topbar-bg: #1e293b;
    --border: #334155;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --air-blue: #1e293b;
    --tab-active-bg: #334155;
}

[data-theme="light"] .theme-icon-dark { display: none; }
[data-theme="dark"] .theme-icon-light { display: none; }

* { box-sizing: border-box; }
body { 
    margin: 0; 
    font-family: 'Inter', sans-serif; 
    background: var(--bg); 
    color: var(--text); 
    height: 100vh; 
    height: 100dvh;
    overflow: hidden; 
}

#app {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 100%;
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

/* =========================
   2. HEADER & NAVIGATION
   ========================= */
#topbar {
    display: flex;
    flex-direction: column;
    background: var(--topbar-bg);
    border-bottom: 1px solid var(--border);
}

.header-main-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 56px;
    padding: 0 16px;
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.logo { font-weight: 800; font-size: 20px; letter-spacing: -1px; }
.logo span { color: var(--accent); }

.header-tabs-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 42px;
    padding: 0 12px;
    background: var(--air-blue);
    border-top: 1px solid var(--border);
}

#tabs-container {
    display: flex;
    align-items: center;
    height: 100%;
    flex: 1;
    overflow-x: auto;
    overflow-y: hidden;
    min-width: 0;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#tabs-container::-webkit-scrollbar {
    display: none;
}

#tabs { display: flex; height: 100%; gap: 4px; min-width: max-content; }

.tab {
    display: flex;
    align-items: center;
    padding: 0 12px;
    height: 100%;
    font-size: 13px;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 6px 6px 0 0;
    transition: all 0.2s;
    gap: 8px;
    white-space: nowrap;
}

.tab.dragging {
    cursor: no-drop !important;
    opacity: 0.85;
}

.tab.drag-over-left, .tab.drag-over-right {
    background: rgba(0, 112, 243, 0.08);
}

.tab.drag-over-left {
    box-shadow: inset 4px 0 0 0 var(--accent);
}

.tab.drag-over-right {
    box-shadow: inset -4px 0 0 0 var(--accent);
}

.tab.active {
    color: var(--accent);
    background: var(--tab-active-bg);
    font-weight: 600;
    border-bottom: 2px solid var(--accent);
}

.tab-rename {
    width: 14px;
    height: 14px;
    opacity: 0;
    transition: opacity 0.2s;
    cursor: pointer;
}
.tab:hover .tab-rename { opacity: 0.5; }
.tab-rename:hover { opacity: 1 !important; color: var(--accent); }

.tab-close { 
    width: 14px; 
    height: 14px; 
    opacity: 0.5; 
    transition: opacity 0.2s;
}
.tab-close:hover { opacity: 1; color: #ef4444; }

.add-tab-btn { 
    background: none; border: none; color: var(--text-muted); 
    cursor: pointer; padding: 8px; display: flex;
    min-height: 44px; /* Touch target fix */
    align-items: center;
}
.add-tab-btn.disabled,
.add-tab-btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* =========================
   3. CONTROLS & BUTTONS
   ========================= */
.action-controls { display: flex; align-items: center; gap: 10px; }

#language {
    padding: 6px 12px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    min-height: 38px; /* Touch target fix */
}

.run-btn {
    background: var(--run-btn);
    color: white;
    border: none;
    padding: 8px 18px;
    border-radius: 6px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 38px; /* Touch target fix */
    transition: filter 0.2s;
}
.run-btn:hover { filter: brightness(1.1); }

.icon-group { display: flex; gap: 4px; padding-left: 8px; border-left: 1px solid var(--border); }
.icon-btn { 
    background: none; border: none; color: var(--text-muted); 
    padding: 8px; cursor: pointer; border-radius: 6px; display: flex;
    min-height: 44px; /* Touch target fix */
    align-items: center;
    justify-content: center;
}
.icon-btn:hover { background: var(--tab-active-bg); color: var(--accent); }

/* =========================
   4. EDITOR & LAYOUT
   ========================= */
#editor-layout { display: flex; flex: 1 1 auto; min-height: 0; }
#editor-pane {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    display: flex;
    background: var(--bg);
    touch-action: manipulation;
}

#editor {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
}

#editor .CodeMirror {
    height: 100%;
    touch-action: pan-y;
}
#io-pane {
    flex: 0 0 var(--io-pane-size);
    display: flex;
    flex-direction: column;
    min-width: 260px;
    min-height: 0;
    background: var(--bg);
}

.pane-resizer {
    position: relative;
    flex: 0 0 auto;
    touch-action: none;
    user-select: none;
}

.pane-resizer::before {
    content: "";
    position: absolute;
    inset: 0;
    transition: background-color 0.2s ease;
}

.pane-resizer:hover::before,
.pane-resizer.dragging::before {
    background-color: rgba(0, 112, 243, 0.12);
}

.pane-resizer-vertical {
    width: 10px;
    cursor: col-resize;
    background: linear-gradient(
        90deg,
        transparent 0,
        transparent 4px,
        var(--border) 4px,
        var(--border) 6px,
        transparent 6px,
        transparent 100%
    );
}

.pane-resizer-horizontal {
    height: 10px;
    cursor: row-resize;
    background: linear-gradient(
        180deg,
        transparent 0,
        transparent 4px,
        var(--border) 4px,
        var(--border) 6px,
        transparent 6px,
        transparent 100%
    );
}

/* Keep the editor/IO divider visually identical when #app enters fullscreen. */
#app:fullscreen,
#app:-webkit-full-screen {
    background: var(--bg);
}

#app:fullscreen .pane-resizer-vertical,
#app:-webkit-full-screen .pane-resizer-vertical {
    background: linear-gradient(
        90deg,
        transparent 0,
        transparent 4px,
        var(--border) 4px,
        var(--border) 6px,
        transparent 6px,
        transparent 100%
    );
}

.io-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

#stdin-section {
    flex: 0 0 var(--stdin-section-size);
    min-height: 120px;
    max-height: none;
}

#stdout-section {
    flex: 1 1 auto;
    min-height: 120px;
}

#io-pane.html-preview-active #stdin-section,
#io-pane.html-preview-active #stdin-stdout-resizer {
    display: none;
}

#io-pane.html-preview-active #stdout-section {
    flex: 1 1 auto;
    min-height: 0;
}

.io-header { 
    padding: 8px 16px; 
    font-size: 11px; 
    font-weight: 800; 
    text-transform: uppercase; 
    color: var(--accent); 
    background: var(--air-blue);
    letter-spacing: 0.5px;
}

#stdin-section form {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
    margin: 0;
}

#stdin, #output { 
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    padding: 16px; font-family: 'Fira Code', monospace; 
    font-size: 13px; border: none; outline: none; 
    background: var(--bg); color: var(--text); resize: none; overflow-y: auto;
}

#stdin {
    display: block;
    width: 100%;
    height: 100%;
}

body.is-resizing,
body.is-resizing * {
    user-select: none;
}

[data-theme="dark"] .CodeMirror { background: #0f172a !important; color: #f8fafc !important; }
[data-theme="dark"] .CodeMirror-gutters { background: #1e293b !important; border-right: 1px solid #334155; }

/* CodeMirror touch scrolling support */
.CodeMirror {
    touch-action: pan-y;
}

.CodeMirror-scroll {
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
}

/* =========================
   5. JOBE STATUS BAR & OUTPUT
   ========================= */
.status-bar {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 12px; background: var(--air-blue);
    border-left: 4px solid #ccc; margin-bottom: 8px; border-radius: 4px;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; }
.status-text { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.05em; }
.output-content { margin: 0; padding: 0 4px; line-height: 1.6; font-size: 13px; white-space: pre-wrap; word-break: break-all; }

/* =========================
   6. MOBILE & RESPONSIVE
   ========================= */
.mobile-toggle-wrapper { display: none; }
.segmented-control { display: flex; background: var(--air-blue); padding: 3px; border-radius: 8px; }
.segmented-control button {
    border: none; background: none; padding: 5px 12px; font-size: 12px;
    font-weight: 600; border-radius: 6px; cursor: pointer; color: var(--text-muted);
    min-height: 38px; /* Touch target fix */
}
.segmented-control button.active { background: var(--accent); color: white; }

.mobile-only { display: none !important; }
.desktop-only { display: flex; }

@media (max-width: 1024px) {
    .header-main-row { height: 52px; padding: 0 12px; }
    .header-tabs-row { height: 44px; }
    .mobile-toggle-wrapper { display: block; }

    #editor-io-resizer { display: none; }
    .view-editor #io-pane { display: none; }
    .view-editor #editor-pane { flex: 1; display: block; }
    .view-io #editor-pane { display: none; }
    .view-io #io-pane { flex: 1; min-width: 0; display: flex; }
}

@media (min-width: 769px) and (max-width: 1024px) {
    .header-left, .header-right { gap: 12px; }
    .logo { display: none; }
    .action-controls { gap: 8px; }
}

@media (max-width: 768px) {
    .mobile-only { display: flex !important; }
    .desktop-only { display: none !important; }
    
    .theme-btn-mobile { margin-left: auto; padding: 8px; min-width: 44px; }

    .header-main-row { height: 50px; padding: 0 10px; }
    .logo { display: none; }

    .run-btn span { display: none; }
    #language { font-size: 12px; padding: 5px; }
    
    /* Mobile scrolling fix: allow scroll on editor pane without needing focus */
    .view-editor #editor-pane {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .view-editor #editor {
        min-height: auto;
    }
}
