/**
 * ColdCode - A Simple Code Editor
 * Main Stylesheet
 * Updated for CodeMirror 6
 */

/* ============================================
   CSS Variables - Light Theme (Default)
   ============================================ */
:root {
    --background: oklch(1 0 0);
    --foreground: oklch(0.145 0 0);
    --card: oklch(1 0 0);
    --card-foreground: oklch(0.145 0 0);
    --popover: oklch(1 0 0);
    --popover-foreground: oklch(0.145 0 0);
    --primary: oklch(0.205 0 0);
    --primary-foreground: oklch(0.985 0 0);
    --secondary: oklch(0.97 0 0);
    --secondary-foreground: oklch(0.205 0 0);
    --muted: oklch(0.97 0 0);
    --muted-foreground: oklch(0.556 0 0);
    --accent: oklch(0.97 0 0);
    --accent-foreground: oklch(0.205 0 0);
    --destructive: oklch(0.704 0.191 22.216);
    --destructive-foreground: oklch(0.985 0 0);
    --border: oklch(0.922 0 0);
    --input: oklch(0.922 0 0);
    --ring: oklch(0.708 0 0);
    --radius: 0.5rem;
}

/* ============================================
   CSS Variables - Dark Theme
   ============================================ */
.dark {
    --background: oklch(0.145 0 0);
    --foreground: oklch(0.985 0 0);
    --card: oklch(0.205 0 0);
    --card-foreground: oklch(0.985 0 0);
    --popover: oklch(0.269 0 0);
    --popover-foreground: oklch(0.985 0 0);
    --primary: oklch(0.922 0 0);
    --primary-foreground: oklch(0.205 0 0);
    --secondary: oklch(0.269 0 0);
    --secondary-foreground: oklch(0.985 0 0);
    --muted: oklch(0.269 0 0);
    --muted-foreground: oklch(0.708 0 0);
    --accent: oklch(0.371 0 0);
    --accent-foreground: oklch(0.985 0 0);
    --destructive: oklch(0.704 0.191 22.216);
    --destructive-foreground: oklch(0.985 0 0);
    --border: oklch(1 0 0 / 10%);
    --input: oklch(1 0 0 / 15%);
    --ring: oklch(0.556 0 0);
}

/* ============================================
   Base Styles
   ============================================ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    border-color: var(--border);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--background);
    color: var(--foreground);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    transition: background-color 0.2s, color 0.2s;
}

/* ============================================
   Header
   ============================================ */
.header {
    background-color: var(--card);
    padding: 12px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
    gap: 16px;
    position: relative;
    z-index: 20;
}

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

.logo-icon {
    width: 28px;
    height: 28px;
}

.header h1 {
    font-family: 'Nunito', sans-serif;
    font-size: 22px;
    font-weight: 800;
    letter-spacing: -0.5px;
}

.controls {
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}

/* ============================================
   Buttons
   ============================================ */
button {
    background-color: var(--primary);
    color: var(--primary-foreground);
    border: 1px solid transparent;
    padding: 8px 16px;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background-color 0.2s, opacity 0.2s, transform 0.1s, box-shadow 0.1s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

button:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}

button:active {
    transform: translateY(0);
    box-shadow: none;
}

button.icon-btn {
    padding: 8px;
}

button.secondary {
    background-color: var(--secondary);
    color: var(--secondary-foreground);
    border-color: var(--border);
}

button.destructive {
    background-color: var(--destructive);
    color: var(--destructive-foreground);
    border-color: transparent;
}

/* ============================================
   Icons
   ============================================ */
.icon {
    width: 1.125em;
    height: 1.125em;
    stroke-width: 2;
    vertical-align: middle;
}

button > .icon {
    margin-right: 0.5em;
}

button.icon-btn > .icon {
    margin-right: 0;
}

.editor-header > .icon {
    width: 1.25rem;
    height: 1.25rem;
}

/* Language-specific icon colors */
.html-icon { color: #E44D26; }
.css-icon { color: #264DE4; }
.js-icon { color: #F0DB4F; }

/* ============================================
   Tooltips
   ============================================ */
[data-tooltip] {
    position: relative;
}

[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: 8px;
    background-color: oklch(0.1 0 0);
    color: oklch(0.95 0 0);
    padding: 6px 12px;
    border-radius: var(--radius);
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    /* display:none (not opacity) so hidden tooltips can't widen the document */
    display: none;
    opacity: 0;
    pointer-events: none;
    z-index: 1000;
}

[data-tooltip]:hover::after {
    display: block;
    /* fade in after a 0.5s delay; invisible (opacity 0) until then */
    animation: tooltip-in 0.2s ease-in-out 0.5s forwards;
}

@keyframes tooltip-in {
    to {
        opacity: 1;
    }
}

[data-tooltip-placement="left"]::after {
    top: 50%;
    left: auto;
    right: 100%;
    transform: translateY(-50%);
    margin-top: 0;
    margin-right: 8px;
}

/* Below, right-aligned: for controls at the right edge of the viewport */
[data-tooltip-placement="bottom-end"]::after {
    left: auto;
    right: 0;
    transform: none;
}

/* Never show a dropdown's tooltip while its menu is open (the tooltip anchors
   exactly where the menu appears) */
.custom-dropdown:has(.custom-dropdown-menu.show)::after {
    display: none;
}

/* ============================================
   Main Container & Layout
   ============================================ */
.container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.editors {
    display: flex;
    background-color: var(--background);
    min-width: 0;
    min-height: 0;
}

.editor-container {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

/* ============================================
   Editor Header
   ============================================ */
.editor-header {
    background-color: var(--card);
    padding: 8px 15px;
    height: 41px;
    font-size: 13px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
}

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

.editor-header .header-actions {
    margin-left: auto;
}

/* ============================================
   Editor Area (CodeMirror 6)
   ============================================ */
.editor-area {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* CodeMirror 6 Styles */
.cm-editor {
    height: 100%;
    width: 100%;
    font-size: 14px;
    line-height: 1.6;
}

.cm-editor .cm-scroller {
    overflow: auto;
    font-family: 'JetBrains Mono', monospace;
}

.cm-editor .cm-content {
    font-family: 'JetBrains Mono', monospace;
}

.cm-editor.cm-focused {
    outline: none;
}

/* Light theme editor background */
.cm-editor {
    background-color: #fff;
}

/* Dark theme editor background (CM6 One Dark handles this but we ensure container matches) */
.dark .cm-editor {
    background-color: #282c34;
}

/* Gutter styling */
.cm-editor .cm-gutters {
    border-right: 1px solid var(--border);
}

.dark .cm-editor .cm-gutters {
    background-color: #21252b;
    border-right-color: #181a1f;
}

/* Active line highlighting */
.cm-editor .cm-activeLine {
    background-color: rgba(0, 0, 0, 0.04);
}

.dark .cm-editor .cm-activeLine {
    background-color: rgba(255, 255, 255, 0.04);
}

/* Selection */
.cm-editor .cm-selectionBackground {
    background-color: rgba(0, 100, 200, 0.2) !important;
}

.dark .cm-editor .cm-selectionBackground {
    background-color: rgba(100, 150, 255, 0.2) !important;
}

/* ============================================
   Preview Container
   ============================================ */
.preview-container {
    display: flex;
    flex-direction: column;
    background-color: white;
    min-width: 0;
    min-height: 0;
}

.preview-header {
    background-color: var(--card);
    padding: 8px 15px;
    font-size: 13px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    height: 41px;
    display: flex;
    align-items: center;
}

#preview {
    flex: 1;
    width: 100%;
    border: none;
    background-color: white;
}

/* ============================================
   Preview Panel (for Split with Console)
   ============================================ */
.preview-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.preview-panel #preview {
    flex: 1;
    min-height: 0;
}

/* ============================================
   Console Panel
   ============================================ */
.console-panel {
    display: flex;
    flex-direction: column;
    background-color: var(--background);
    min-height: 0;
    border-top: 1px solid var(--border);
}

.console-header {
    background-color: var(--card);
    padding: 8px 15px;
    height: 41px;
    font-size: 13px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

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

.console-header .header-actions {
    margin-left: auto;
}

.console-output {
    flex: 1;
    overflow-y: auto;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    padding: 8px 12px;
    min-height: 0;
}

.console-output:empty::before {
    content: 'Console output will appear here...';
    color: var(--muted-foreground);
    font-style: italic;
}

.console-log {
    padding: 2px 0;
    border-bottom: 1px solid var(--border);
    word-wrap: break-word;
    white-space: pre-wrap;
}

.console-log:last-child {
    border-bottom: none;
}

.console-log.log {
    color: var(--foreground);
}

/* Text colors keep at least 4.5:1 contrast (WCAG AA) on their backgrounds;
   the light theme needs darker shades than the dark one */
.console-log.error {
    color: #b91c1c;
    background-color: rgba(239, 68, 68, 0.1);
    padding: 4px 8px;
    margin: 2px -8px;
    border-radius: 4px;
}

.console-log.warn {
    color: #b45309;
    background-color: rgba(245, 158, 11, 0.1);
    padding: 4px 8px;
    margin: 2px -8px;
    border-radius: 4px;
}

.console-log.info {
    color: #2563eb;
}

.dark .console-log.error {
    color: #ef4444;
    background-color: rgba(239, 68, 68, 0.15);
}

.dark .console-log.warn {
    color: #f59e0b;
    background-color: rgba(245, 158, 11, 0.15);
}

.dark .console-log.info {
    color: #3b82f6;
}

/* ============================================
   Status Toast
   ============================================ */
.status {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background-color: var(--popover);
    color: var(--popover-foreground);
    padding: 10px 16px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    font-size: 14px;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 100;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    display: flex;
    align-items: center;
    gap: 8px;
    transform: translateY(10px);
    /* Faded out, the toast stays in place (it is a live region, so it must not
       be display:none); it must never catch clicks meant for what's below */
    pointer-events: none;
}

.status.show {
    opacity: 1;
    transform: translateY(0);
}

/* ============================================
   Utility Classes
   ============================================ */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.hidden {
    display: none;
}

/* ============================================
   Split.js Gutters
   ============================================ */
.gutter {
    background-color: var(--border);
    background-repeat: no-repeat;
    background-position: 50%;
    transition: background-color 0.2s;
}

.gutter:hover {
    background-color: var(--ring);
}

.gutter.gutter-horizontal {
    cursor: col-resize;
}

.gutter.gutter-vertical {
    cursor: row-resize;
}

.dark .gutter {
    background-color: oklch(1 0 0 / 15%);
}

.dark .gutter:hover {
    background-color: oklch(1 0 0 / 25%);
}

/* ============================================
   Layout Variants - Split Editors
   ============================================ */
.container.layout-left {
    flex-direction: row;
}
.container.layout-left .editors {
    flex-direction: column;
}
.container.layout-left .editor-container {
    border-bottom: 1px solid var(--border);
}
.container.layout-left .editor-container:last-child {
    border-bottom: none;
}

/* Pane order for right/bottom layouts is handled by reordering the DOM in
   setLayout() (Split.js needs DOM order == visual order for its gutters),
   so no reversed flex directions here. */
.container.layout-right {
    flex-direction: row;
}
.container.layout-right .editors {
    flex-direction: column;
}
.container.layout-right .editor-container {
    border-bottom: 1px solid var(--border);
}
.container.layout-right .editor-container:last-child {
    border-bottom: none;
}

.container.layout-top {
    flex-direction: column;
}
.container.layout-top .editors {
    flex-direction: row;
    border-bottom: 1px solid var(--border);
}
.container.layout-top .editor-container {
    border-right: 1px solid var(--border);
}
.container.layout-top .editor-container:last-child {
    border-right: none;
}

.container.layout-bottom {
    flex-direction: column;
}
.container.layout-bottom .editors {
    flex-direction: row;
    border-top: 1px solid var(--border);
}
.container.layout-bottom .editor-container {
    border-right: 1px solid var(--border);
}
.container.layout-bottom .editor-container:last-child {
    border-right: none;
}

/* ============================================
   Layout Variants - Full Code (Single Editor)
   ============================================ */
.container.layout-full-left {
    flex-direction: row;
}
.container.layout-full-left .editors {
    flex-direction: column;
}
.container.layout-full-left .editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.container.layout-full-right {
    flex-direction: row;
}
.container.layout-full-right .editors {
    flex-direction: column;
}
.container.layout-full-right .editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.container.layout-full-top {
    flex-direction: column;
}
.container.layout-full-top .editors {
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.container.layout-full-top .editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.container.layout-full-top .editor-area {
    flex: 1;
    min-height: 0;
}

.container.layout-full-bottom {
    flex-direction: column;
}
.container.layout-full-bottom .editors {
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.container.layout-full-bottom .editor-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.container.layout-full-bottom .editor-area {
    flex: 1;
    min-height: 0;
}

/* ============================================
   Modal Overlay & Content
   ============================================ */
.modal-overlay {
    position: fixed;
    inset: 0;
    background-color: oklch(0 0 0 / 50%);
    /* display:none (not opacity) keeps closed dialogs out of the
       accessibility tree and tab order */
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.modal-overlay.show {
    display: flex;
    animation: overlay-in 0.2s ease-out;
}

@keyframes overlay-in {
    from {
        opacity: 0;
    }
}

.modal-content {
    background-color: var(--card);
    color: var(--card-foreground);
    padding: 24px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    width: 90%;
    max-width: 500px;
}

.modal-overlay.show .modal-content {
    animation: modal-in 0.2s ease-out;
}

@keyframes modal-in {
    from {
        transform: scale(0.95);
    }
}

.modal-content h2 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 8px;
}

.modal-content p {
    font-size: 14px;
    color: var(--muted-foreground);
    margin-bottom: 24px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* Import Modal Options */
.modal-content .import-option {
    margin-bottom: 20px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border);
}

.modal-content .import-option:last-child {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
}

.modal-content .import-option h3 {
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 4px;
}

.modal-content .import-option .single-file-buttons {
    display: flex;
    gap: 10px;
}

/* File Input in Modal */
.modal-content .file-input {
    width: 100%;
    padding: 8px 12px;
    font-size: 14px;
    background-color: var(--input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--foreground);
    margin-bottom: 24px;
}

.modal-content .file-input:focus {
    outline: none;
    border-color: var(--ring);
    box-shadow: 0 0 0 2px var(--ring);
}

/* ============================================
   Custom Dropdown
   ============================================ */
.custom-dropdown {
    position: relative;
}

.custom-dropdown-toggle {
    background-color: var(--secondary);
    color: var(--secondary-foreground);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 7px 12px;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
    justify-content: space-between;
}

#layout-dropdown-toggle {
    width: 175px;
}

#download-dropdown-toggle {
    padding: 8px 16px;
}

/* Layout icons everywhere (toggle button AND menu items) — without an explicit
   size the menu SVGs render at their intrinsic ~100px default */
.layout-icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    stroke: var(--secondary-foreground);
    stroke-width: 1.5;
}

.custom-dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    background-color: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
    margin-top: 4px;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    z-index: 1001; /* above tooltips (1000) */
    width: 100%;
    /* display:none (not opacity) so closed menus don't create scrollbars and
       stay out of the accessibility tree and tab order */
    display: none;
}

.custom-dropdown-menu.show {
    display: block;
    animation: dropdown-in 0.15s ease-out;
}

@keyframes dropdown-in {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
}

.custom-dropdown-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    border-radius: calc(var(--radius) - 2px);
    cursor: pointer;
    font-size: 14px;
}

.custom-dropdown-item:hover {
    background-color: var(--accent);
}

#download-dropdown-menu {
    width: 200px;
    right: 0;
    left: auto;
}

/* ============================================
   Responsive Design
   ============================================ */
@media (max-width: 768px) {
    .container,
    .container.layout-left,
    .container.layout-right,
    .container.layout-top,
    .container.layout-bottom {
        flex-direction: column !important;
    }

    .editors,
    .container.layout-top .editors,
    .container.layout-bottom .editors {
        flex-direction: column !important;
        height: 60%;
        border: none !important;
    }

    .editor-container,
    .container.layout-top .editor-container,
    .container.layout-bottom .editor-container {
        flex: 1 1 0 !important;
        min-height: 100px;
        border-right: none !important;
        border-bottom: 1px solid var(--border) !important;
    }

    .editor-container:last-child {
        border-bottom: none !important;
    }

    .preview-container {
        height: 40%;
    }

    .gutter {
        display: none !important;
    }
}
