:root {
    --bg-base:      #f4f6fb;
    --bg-elev-1:    #ffffff;
    --bg-elev-2:    #f8fafc;
    --bg-pane:      #ffffff;
    --bg-editor:    #ffffff;
    --bg-output:    #fafbfd;
    --bg-stdin:     #fafbfd;

    --border:       #e2e8f0;
    --border-soft:  #eef2f7;
    --border-strong:#cbd5e1;

    --text:         #0f172a;
    --text-soft:    #475569;
    --text-mute:    #94a3b8;

    --accent:       #6366f1;
    --accent-2:     #06b6d4;
    --success:      #10b981;
    --warning:      #f59e0b;
    --danger:       #ef4444;

    --grad-brand:   linear-gradient(135deg, #6366f1 0%, #06b6d4 100%);
    --grad-run:     linear-gradient(135deg, #10b981 0%, #059669 100%);
    --grad-header:  linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);

    --shadow-pane:  0 1px 3px rgba(15,23,42,0.04), 0 4px 16px rgba(15,23,42,0.06);
    --shadow-btn:   0 6px 16px rgba(99,102,241,0.25);
    --shadow-run:   0 6px 18px rgba(16,185,129,0.30);

    --radius:       12px;
    --radius-sm:    9px;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    background:
        radial-gradient(1200px 600px at 10% -10%, rgba(99,102,241,0.08), transparent 60%),
        radial-gradient(900px 500px at 100% 0%, rgba(6,182,212,0.06), transparent 55%),
        var(--bg-base);
    color: var(--text);
    font-family: 'Inter', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    overflow: hidden;
}

/* ========== Header ========== */
.app-header {
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 22px;
    background: var(--grad-header);
    border-bottom: 1px solid var(--border);
    position: relative;
    z-index: 10;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    text-decoration: none;
    color: inherit;
    min-width: 0;
}
.brand:hover { text-decoration: none; }

.brand-mark {
    width: 44px; height: 44px;
    border-radius: 11px;
    display: grid; place-items: center;
    background: #ffffff;
    box-shadow: 0 1px 3px rgba(15,23,42,0.06), 0 0 0 1px var(--border);
    overflow: hidden;
    flex-shrink: 0;
}
.brand-mark img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    padding: 4px;
}

.brand-name {
    font-weight: 700;
    font-size: 15px;
    letter-spacing: -0.01em;
    background: var(--grad-brand);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 240px;
}

.brand-tag {
    font-size: 10.5px;
    color: var(--text-mute);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    margin-top: 2px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.lang-picker {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px 6px 12px;
    background: var(--bg-elev-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-soft);
    transition: border-color .15s ease;
}
.lang-picker:hover { border-color: var(--border-strong); }
.lang-picker .bi { font-size: 14px; color: var(--accent); }

.lang-picker select.form-select {
    background: transparent !important;
    border: none !important;
    color: var(--text) !important;
    box-shadow: none !important;
    padding: 2px 26px 2px 4px;
    font-weight: 500;
    min-width: 150px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%2394a3b8'%3E%3Cpath d='M3.5 6l4.5 4.5L12.5 6z'/%3E%3C/svg%3E") !important;
    background-repeat: no-repeat !important;
    background-position: right 4px center !important;
    background-size: 14px !important;
}
.lang-picker select.form-select option {
    background: #fff;
    color: var(--text);
    font-weight: 500;
}
.lang-picker select.form-select optgroup {
    background: #fff;
    color: var(--text-mute);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.btn-ghost {
    background: #fff;
    border: 1px solid var(--border);
    color: var(--text-soft);
    border-radius: var(--radius-sm);
    padding: 7px 14px;
    font-weight: 500;
    display: inline-flex; align-items: center; gap: 6px;
    transition: all .15s ease;
}
.btn-ghost:hover {
    background: var(--bg-elev-2);
    color: var(--text);
    border-color: var(--accent);
}

.btn-run {
    background: var(--grad-run);
    border: none;
    color: #fff;
    font-weight: 600;
    padding: 8px 18px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-run);
    display: inline-flex; align-items: center; gap: 8px;
    transition: transform .12s ease, box-shadow .12s ease, filter .12s ease;
}
.btn-run:hover { transform: translateY(-1px); filter: brightness(1.08); color: #fff; }
.btn-run:active { transform: translateY(0); }
.btn-run:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
.btn-run .bi { font-size: 16px; }

.run-kbd {
    background: rgba(255,255,255,0.25);
    color: #ffffff;
    padding: 1px 7px;
    border-radius: 6px;
    font-size: 11px;
    font-family: 'JetBrains Mono', monospace;
    border: 1px solid rgba(255,255,255,0.35);
}

/* ========== Workspace ========== */
.workspace {
    height: calc(100vh - 64px);
    display: grid;
    grid-template-columns: 1fr 6px 1fr;
    gap: 0;
    padding: 16px;
}

.splitter { background: transparent; }

.pane {
    background: var(--bg-pane);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-pane);
}

.pane-head {
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 14px;
    background: var(--bg-elev-2);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.pane-title {
    display: flex;
    align-items: center;
    gap: 8px;
}

.pane-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-pane {
    background: #fff;
    border: 1px solid var(--border);
    color: var(--text-soft);
    border-radius: 7px;
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 500;
    display: inline-flex; align-items: center; gap: 5px;
    cursor: pointer;
    transition: all .15s ease;
    line-height: 1.4;
}
.btn-pane:hover {
    background: var(--bg-elev-2);
    color: var(--accent);
    border-color: var(--accent);
}
.btn-pane:active { transform: translateY(1px); }
.btn-pane .bi { font-size: 13px; }

.btn-pane-run {
    background: var(--grad-run);
    border-color: transparent;
    color: #fff;
    box-shadow: 0 2px 8px rgba(16,185,129,0.30);
    padding: 4px 12px;
}
.btn-pane-run:hover {
    background: var(--grad-run);
    color: #fff;
    border-color: transparent;
    filter: brightness(1.08);
    box-shadow: 0 4px 12px rgba(16,185,129,0.40);
}
.btn-pane-run:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    filter: none;
}
.btn-pane-run .bi { font-size: 14px; }

.pane-label {
    font-weight: 500;
    color: var(--text);
    display: inline-flex; align-items: center; gap: 6px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
}
.pane-label .bi { color: var(--accent); }

.dot {
    width: 11px; height: 11px;
    border-radius: 50%;
    display: inline-block;
}
.dot-red   { background: #ff5f57; }
.dot-amber { background: #febc2e; }
.dot-green { background: #28c840; }
.pane-title .dot + .dot { margin-left: 4px; }
.pane-title .pane-label { margin-left: 12px; }

.pane-body {
    flex: 1;
    min-height: 0;
    position: relative;
}

/* ========== Editor ========== */
.CodeMirror {
    height: 100%;
    font-family: 'JetBrains Mono', Menlo, Consolas, monospace;
    font-size: 14px;
    line-height: 1.65;
    background: var(--bg-editor) !important;
    color: var(--text) !important;
}
.CodeMirror-gutters {
    background: var(--bg-editor) !important;
    border-right: 1px solid var(--border-soft) !important;
}
.CodeMirror-linenumber { color: var(--text-mute) !important; }
.CodeMirror-cursor { border-left-color: var(--accent) !important; }
.CodeMirror-selected { background: rgba(99,102,241,0.15) !important; }
.CodeMirror-focused .CodeMirror-selected { background: rgba(99,102,241,0.2) !important; }
.CodeMirror-matchingbracket {
    color: var(--accent) !important;
    background: rgba(99,102,241,0.12) !important;
    border-bottom: 1px solid var(--accent);
}

/* ========== Right column (STDIN on top, Output below) ========== */
.right-stack {
    display: grid;
    grid-template-rows: auto 1fr;
    gap: 12px;
    min-height: 0;
}

.pane-head-sm { height: 38px; }

.stdin-hint {
    color: var(--text-mute);
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    font-weight: 400;
    margin-left: 8px;
    letter-spacing: 0.01em;
}
.stdin-hint .text-muted { color: var(--text-mute) !important; }

.btn-icon {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-mute);
    width: 26px; height: 26px;
    border-radius: 6px;
    display: grid; place-items: center;
    cursor: pointer;
    transition: all .15s ease;
}
.btn-icon:hover {
    background: var(--bg-elev-2);
    border-color: var(--border);
    color: var(--text);
}
.btn-icon .bi { transition: transform .2s ease; }

.pane-stdin {
    flex-shrink: 0;
    transition: max-height .25s ease;
    overflow: hidden;
}
.pane-stdin.collapsed { max-height: 38px; }
.pane-stdin.collapsed .btn-icon .bi { transform: rotate(-90deg); }

.stdin-wrap {
    background: var(--bg-stdin);
    height: 120px;
}

.stdin-input {
    width: 100%; height: 100%;
    background: var(--bg-stdin);
    color: var(--text);
    border: none;
    outline: none;
    resize: none;
    padding: 12px 16px;
    font-family: 'JetBrains Mono', Menlo, Consolas, monospace;
    font-size: 13px;
    line-height: 1.6;
}
.stdin-input::placeholder { color: var(--text-mute); }

/* ========== Output ========== */
.output-wrap {
    overflow: hidden;
    background: var(--bg-output);
}

.output-pane {
    margin: 0;
    height: 100%;
    padding: 16px 18px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'JetBrains Mono', Menlo, Consolas, monospace;
    font-size: 13px;
    line-height: 1.7;
    color: var(--text);
    background: var(--bg-output);
}

.output-pane.ok    { color: #065f46; }
.output-pane.error { color: #991b1b; }
.output-pane.warn  { color: #92400e; }
.output-pane strong { color: var(--accent); font-weight: 600; }

.output-frame {
    display: none;
    width: 100%;
    height: 100%;
    border: none;
    background: #ffffff;
}

/* When HTML mode is active, swap pre → iframe and show file tabs */
.workspace.html-mode .output-pane  { display: none; }
.workspace.html-mode .output-frame { display: block; }
.workspace.html-mode .pane-stdin   { display: none; }
.workspace.html-mode .right-stack  { grid-template-rows: 1fr; }
.workspace.html-mode .pane-label   { display: none; }
.workspace.html-mode .file-tabs    { display: inline-flex; }

/* ========== HTML file tabs ========== */
.file-tabs {
    display: none;
    gap: 4px;
    margin-left: 12px;
    flex-wrap: wrap;
}

.file-tab {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-mute);
    padding: 5px 12px;
    border-radius: 7px;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-family: 'JetBrains Mono', monospace;
    transition: all .15s ease;
    line-height: 1;
}
.file-tab:hover {
    background: var(--bg-elev-2);
    color: var(--text);
}
.file-tab.is-active {
    background: #fff;
    border-color: var(--border);
    color: var(--accent);
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.file-tab .bi { font-size: 13px; }
.file-tab[data-file="html"].is-active { color: #e34c26; }
.file-tab[data-file="css"].is-active  { color: #2965f1; }
.file-tab[data-file="js"].is-active   { color: #f0b400; }

/* scrollbars */
.output-pane::-webkit-scrollbar,
.stdin-input::-webkit-scrollbar,
.CodeMirror-vscrollbar::-webkit-scrollbar { width: 10px; height: 10px; }
.output-pane::-webkit-scrollbar-thumb,
.stdin-input::-webkit-scrollbar-thumb,
.CodeMirror-vscrollbar::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 8px;
    border: 2px solid transparent;
    background-clip: padding-box;
}
.output-pane::-webkit-scrollbar-thumb:hover,
.stdin-input::-webkit-scrollbar-thumb:hover { background: #94a3b8; background-clip: padding-box; }

/* ========== Status badge ========== */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 4px 11px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
    background: var(--bg-elev-2);
    border: 1px solid var(--border);
    color: var(--text-soft);
    text-transform: lowercase;
    letter-spacing: 0.02em;
}

.status-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--text-mute);
}

.status-idle .status-dot     { background: var(--text-mute); }
.status-running              { color: var(--warning); border-color: #fde68a; background: #fffbeb; }
.status-running .status-dot  { background: var(--warning); animation: pulse 1s ease-in-out infinite; }
.status-ok                   { color: #047857; border-color: #a7f3d0; background: #ecfdf5; }
.status-ok .status-dot       { background: var(--success); }
.status-error                { color: #b91c1c; border-color: #fecaca; background: #fef2f2; }
.status-error .status-dot    { background: var(--danger); }
.status-timeout              { color: #b45309; border-color: #fde68a; background: #fffbeb; }
.status-timeout .status-dot  { background: var(--warning); }

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(245,158,11,0.5); }
    50%      { box-shadow: 0 0 0 6px rgba(245,158,11,0); }
}

/* File-tab short labels — desktop hides them */
.ft-short { display: none; }
.file-tab .ft-full,
.file-tab .ft-short { margin-left: 4px; }

/* ========== Responsive ========== */

/* Use dynamic viewport units where available (handles iOS Safari address-bar). */
@supports (height: 100dvh) {
    html, body { height: 100dvh; }
    .workspace { height: calc(100dvh - 64px); }
}

/* Tablets — stack panes vertically */
@media (max-width: 992px) {
    html, body { overflow: auto; height: auto; }

    .workspace {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto;   /* 2 rows: editor, right-stack */
        gap: 12px;
        height: auto;
        min-height: calc(100vh - 64px);
        padding: 12px;
    }
    @supports (height: 100dvh) {
        .workspace { min-height: calc(100dvh - 64px); }
    }
    .splitter { display: none; }

    .pane-editor { min-height: 55vh; }

    /* Right column: stop using a 1fr row that collapses inside auto parent */
    .right-stack {
        grid-template-rows: auto auto;
        min-height: 0;
    }
    .workspace.html-mode .right-stack { grid-template-rows: auto; }

    .output-wrap { min-height: 300px; }
    .stdin-wrap  { height: 110px; }

    .brand-tag { display: none; }
}

/* Phones */
@media (max-width: 600px) {
    .app-header {
        height: auto;
        padding: 10px 14px;
        flex-wrap: wrap;
        gap: 10px;
        /* Notched devices */
        padding-left: max(14px, env(safe-area-inset-left));
        padding-right: max(14px, env(safe-area-inset-right));
    }
    .brand-mark { width: 36px; height: 36px; border-radius: 9px; }
    .brand-mark img { padding: 3px; }
    .brand-name { font-size: 13px; max-width: 180px; }
    .brand-tag  { font-size: 9.5px; }

    .header-actions {
        width: 100%;
        justify-content: flex-end;
    }
    .lang-picker {
        flex: 1;
        padding: 5px 8px 5px 10px;
    }
    .lang-picker select.form-select {
        min-width: 0;
        width: 100%;
        font-size: 13px;
    }

    .workspace {
        padding: 10px;
        gap: 10px;
        padding-left: max(10px, env(safe-area-inset-left));
        padding-right: max(10px, env(safe-area-inset-right));
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }
    .pane-editor { min-height: 50vh; }
    .output-wrap { min-height: 240px; }

    .pane-head, .pane-head-sm {
        height: auto;
        min-height: 42px;
        padding: 8px 10px;
        gap: 6px;
        flex-wrap: wrap;
    }
    .pane-title {
        gap: 6px;
        flex-wrap: wrap;
        flex: 1 1 auto;
        min-width: 0;
    }
    .pane-title .dot { display: none; }
    .pane-title .pane-label { margin-left: 0; font-size: 12px; }

    .pane-actions {
        gap: 5px;
        margin-left: auto;
        flex-wrap: wrap;
        justify-content: flex-end;
    }
    .pane-actions #editorMeta { display: none; }

    /* Touch-friendly pane buttons */
    .btn-pane {
        padding: 6px 10px;
        font-size: 12px;
        min-height: 32px;
    }
    .btn-pane .bi { font-size: 13px; }

    /* File tabs: short labels on phones */
    .file-tabs { margin-left: 4px; gap: 3px; }
    .file-tab  { padding: 5px 9px; min-height: 30px; }
    .file-tab .ft-full  { display: none; }
    .file-tab .ft-short { display: inline; margin-left: 4px; }

    .CodeMirror { font-size: 13px; line-height: 1.55; }
    .CodeMirror-gutter-elt { padding: 0 4px !important; }

    /* SQL ASCII tables can be wider than the viewport — allow horizontal scroll */
    .output-pane {
        font-size: 12.5px;
        padding: 12px 14px;
        overflow-x: auto;
        white-space: pre;
        word-break: normal;
    }
    .stdin-input {
        font-size: 12.5px;
        padding: 12px 14px;
    }

    .stdin-hint { display: none; }
    .stdin-wrap { height: 90px; }

    .status-badge {
        padding: 3px 8px;
        font-size: 11px;
    }
}

/* Very small phones (≤ 380px, e.g. iPhone SE 1st gen) */
@media (max-width: 380px) {
    .btn-pane span      { display: none; }   /* Icon-only action buttons */
    .btn-pane           { padding: 6px 9px; }
    .btn-pane .bi       { font-size: 14px; }
    .status-badge       { font-size: 10.5px; padding: 3px 7px; }
    .file-tab           { padding: 5px 7px; }
}

/* Landscape phones — short height, give output less min-height */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    .pane-editor { min-height: 60vh; }
    .output-wrap { min-height: 180px; }
    .stdin-wrap  { height: 70px; }
}
