:root {
    color-scheme: dark;
    --bg: #0a0a0a;
    --panel: #171717;
    --surface: #121212;
    --fg: #fafafa;
    --muted: #a3a3a3;
    --border: #2e2e2e;
    --accent: #6d8dff;
    --green: #34d399;
    --html: #e34c26;
    --css: #2965f1;
    --js: #f7df1e;
    --button: #225c45;
    --button-text: #cecece;
}

:root[data-theme="light"] {
    color-scheme: light;
    --bg: #ffffff;
    --panel: #ffffff;
    --surface: #f4f4f5;
    --muted: #666666;
    --fg: #0a0a0a;
    --border: #e5e5e5;
    --accent: #e8590c;
    --green: #0f766e;
    --button: #22ae45;
    --button-text: #242424;
}

* {
    box-sizing: border-box;
}
[hidden] {
    display: none !important;
}
html,
body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font:
        15px/1.4 system-ui,
        sans-serif;

    &.resizing {
        user-select: none;
        cursor: col-resize;

        iframe,
        .CodeMirror {
            pointer-events: none;
        }
    }
}

.v-divider {
    flex: 0 0 6px;
    background: var(--border);
    cursor: row-resize;
    position: relative;
    touch-action: none;
    transition: background 0.15s;

    &::before {
        content: "";
        position: absolute;
        inset: -3px 0; /* widen the grab strip vertically */
    }
    &:hover,
    &:active {
        background: var(--accent);
    }
}

.app {
    display: grid;
    grid-template-rows: auto 1fr;
    height: 100dvh;

    @media (max-width: 900px) {
        height: auto;
        min-height: 100dvh;
    }
}

/* Top bar */
.bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--panel);

    a {
        display: flex;
        align-items: center;
        text-decoration: none;
        font-size: clamp(1.1rem, 1.2vw, 2rem);
        font-weight: 700;
        transform: scaleY(1.3);

        min-height: 36px;
        padding: 0 12px;
        background: transparent;
        color: var(--fg);
        font: inherit;
        border: 1px solid var(--border);
        border-radius: 8px;
        cursor: pointer;
        text-transform: capitalize;

        &:hover {
            border-color: var(--muted);
        }
    }

    .brand {
        display: flex;
        align-items: center;
        gap: 8px;
        font-weight: 700;
        letter-spacing: -0.01em;

        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: var(--accent);
            box-shadow: 0 0 0 4px
                color-mix(in srgb, var(--accent) 25%, transparent);
        }
    }

    .actions {
        display: flex;
        gap: 8px;

        button {
            min-height: 36px;
            padding: 0 12px;
            background: transparent;
            color: var(--fg);
            font: inherit;
            border: 1px solid var(--border);
            border-radius: 8px;
            cursor: pointer;

            &:hover {
                border-color: var(--muted);
            }
        }
    }
}

/* Split: editors left | divider | preview right */
.split {
    display: grid;
    grid-template-columns: var(--split, 44%) 6px 1fr;
    min-height: 0;

    @media (max-width: 900px) {
        grid-template-columns: 1fr;
    }
}

.divider {
    width: 6px;
    background: var(--border);
    cursor: col-resize;
    position: relative;
    touch-action: none;
    transition: background 0.15s;

    &::before {
        content: "";
        position: absolute;
        inset: 0 -3px; /* widen the grab strip beyond 6px */
    }
    &:hover,
    &:active {
        background: var(--accent);
    }

    @media (max-width: 900px) {
        display: none;
    }
}

.editors {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--panel);

    @media (max-width: 900px) {
        border-bottom: 1px solid var(--border);
    }
}

.editor {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    min-height: 0;
    border-bottom: 0;

    &:last-child {
        border-bottom: 0;
    }

    @media (max-width: 900px) {
        min-height: 200px;
    }
}

.editor-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);

    .lang {
        &::before {
            content: "";
            display: inline-block;
            width: 9px;
            height: 9px;
            margin-right: 6px;
            border-radius: 2px;
            vertical-align: middle;
        }

        &.html {
            color: var(--html);
            &::before {
                background: var(--html);
            }
        }
        &.css {
            color: var(--css);
            &::before {
                background: var(--css);
            }
        }
        &.js {
            color: var(--js);
            &::before {
                background: var(--js);
            }
        }
    }

    .save {
        min-height: 26px;
        padding: 0 10px;
        font: inherit;
        font-size: 0.72rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        color: var(--fg);
        background: transparent;
        border: 1px solid var(--border);
        border-radius: 6px;
        cursor: pointer;

        &:hover {
            border-color: var(--muted);
        }

        &.saved {
            border-color: var(--green);
            color: var(--green);
        }
    }
}

.editor-body {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;

    .CodeMirror,
    textarea {
        position: absolute;
        inset: 0;
        font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
        font-size: 13px;
        line-height: 1.5;
    }

    .CodeMirror {
        height: 100%;
    }
    textarea {
        width: 100%;
        height: 100%;
        padding: 8px 12px;
        border: 0;
        resize: none;
        background: var(--panel);
        color: var(--fg);
    }
}

/* Preview */
.preview {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--surface);

    @media (max-width: 900px) {
        min-height: 60vh;
    }

    .preview-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 6px 12px;
        border-bottom: 1px solid var(--border);
        font-size: 0.75rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--muted);

        .status {
            max-width: 60%;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            color: var(--muted);
            text-transform: none;
            letter-spacing: 0;

            &.error {
                color: #ff5c5c;
            }
        }
    }

    #preview {
        flex: 1;
        width: 100%;
        border: 0;
        background: #fff;
    }
}

/* CodeMirror theme (no separate CM theme file) */
.CodeMirror {
    border: 0;
    background: var(--panel) !important;
    color: var(--fg) !important;

    .CodeMirror-gutters {
        border: 0;
        border-right: 1px solid var(--border) !important;
        background: var(--panel) !important;
        color: var(--fg) !important;
    }
    .CodeMirror-linenumber {
        color: var(--muted) !important;
    }
    .CodeMirror-cursor {
        border-left-color: var(--fg) !important;
    }
    .CodeMirror-selected {
        background: color-mix(
            in srgb,
            var(--accent) 30%,
            transparent
        ) !important;
    }

    .cm-keyword,
    .cm-tag {
        color: var(--accent);
    }
    .cm-attribute,
    .cm-property {
        color: var(--green);
    }
    .cm-string,
    .cm-variable-2 {
        color: #e6b422;
    }
    .cm-comment {
        color: var(--muted);
        font-style: italic;
    }
}
