/* ============================================================
   Theme palettes
   ============================================================ */

/* Catppuccin Macchiato (dark) — default */
:root,
html[data-theme="dark"] {
    --bg: #24273a;
    --surface: #1e2030;
    --border: #363a4f;
    --text: #cad3f5;
    --text-muted: #a5adcb;
    --accent: #8aadf4;
    --error: #ed8796;
    --success: #a6da95;
    --warning: #f5a97f;
    --active-line-bg: rgba(138, 173, 244, 0.06);
    --selection-bg: rgba(138, 173, 244, 0.15);
    --hover-bg: rgba(138, 173, 244, 0.08);
    --header-height: 48px;
    --panel-header-height: 32px;
    /* Graph */
    --graph-bg: #1e2030;
    --graph-concept-fill: #1a2447;
    --graph-concept-stroke: #8aadf4;
    --graph-concept-text: #cad3f5;
    --graph-property-fill: #142828;
    --graph-property-stroke: #8bd5ca;
    --graph-property-text: #cad3f5;
    --graph-edge-subclass: #8aadf4;
    --graph-edge-has: #a6da95;
    --graph-edge-range: #f5a97f;
    --graph-label-text: #a5adcb;
    --graph-field-text: #cad3f5;
}

/* Catppuccin Latte (light) */
html[data-theme="light"] {
    --bg: #eff1f5;
    --surface: #e6e9ef;
    --border: #ccd0da;
    --text: #4c4f69;
    --text-muted: #6c6f85;
    --accent: #1e66f5;
    --error: #d20f39;
    --success: #40a02b;
    --warning: #fe640b;
    --active-line-bg: rgba(30, 102, 245, 0.06);
    --selection-bg: rgba(30, 102, 245, 0.15);
    --hover-bg: rgba(30, 102, 245, 0.08);
    /* Graph */
    --graph-bg: #e6e9ef;
    --graph-concept-fill: #dce6fc;
    --graph-concept-stroke: #1e66f5;
    --graph-concept-text: #4c4f69;
    --graph-property-fill: #d0eeee;
    --graph-property-stroke: #179299;
    --graph-property-text: #4c4f69;
    --graph-edge-subclass: #1e66f5;
    --graph-edge-has: #40a02b;
    --graph-edge-range: #fe640b;
    --graph-label-text: #6c6f85;
    --graph-field-text: #4c4f69;
}

/* System preference: light */
@media (prefers-color-scheme: light) {
    html[data-theme="system"] {
        --bg: #eff1f5;
        --surface: #e6e9ef;
        --border: #ccd0da;
        --text: #4c4f69;
        --text-muted: #6c6f85;
        --accent: #1e66f5;
        --error: #d20f39;
        --success: #40a02b;
        --warning: #fe640b;
        --active-line-bg: rgba(30, 102, 245, 0.06);
        --selection-bg: rgba(30, 102, 245, 0.15);
        --hover-bg: rgba(30, 102, 245, 0.08);
        /* Graph */
        --graph-bg: #e6e9ef;
        --graph-concept-fill: #dce6fc;
        --graph-concept-stroke: #1e66f5;
        --graph-concept-text: #4c4f69;
        --graph-property-fill: #d0eeee;
        --graph-property-stroke: #179299;
        --graph-property-text: #4c4f69;
        --graph-edge-subclass: #1e66f5;
        --graph-edge-has: #40a02b;
        --graph-edge-range: #fe640b;
        --graph-label-text: #6c6f85;
        --graph-field-text: #4c4f69;
    }
}

/* ============================================================
   Reset
   ============================================================ */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
}

span[hidden] {
    display: none !important;
}

/* ---- Header ---- */

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

header h1 {
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
}

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

.toolbar select,
.toolbar button {
    font-family: inherit;
    font-size: 0.8rem;
    padding: 4px 10px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
}

.toolbar button {
    background: var(--accent);
    color: var(--bg);
    font-weight: 600;
    border: none;
}

.toolbar button:hover {
    opacity: 0.9;
}

/* Package load / save + example rename buttons */
#pkg-load-btn,
#pkg-save-btn,
#example-rename-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border) !important;
    font-weight: 400;
    min-width: unset;
    padding: 4px 8px;
}

#pkg-load-btn:hover,
#pkg-save-btn:hover,
#example-rename-btn:hover {
    color: var(--text);
    background: var(--hover-bg);
    opacity: 1;
}

/* Theme toggle button */
#theme-btn {
    background: var(--surface);
    color: var(--text-muted);
    border: 1px solid var(--border) !important;
    border: none;
    font-weight: 400;
    min-width: 80px;
}

#theme-btn:hover {
    color: var(--text);
    opacity: 1;
}

.status {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.status.ok {
    color: var(--success);
}

.status.err {
    color: var(--error);
}

/* ---- Main layout ---- */

main {
    display: flex;
    height: calc(100% - var(--header-height));
}

.panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.panel-header {
    height: var(--panel-header-height);
    line-height: var(--panel-header-height);
    padding: 0 12px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

/* ---- Editor panel header with file tree selector ---- */

.editor-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 8px 0 12px;
    line-height: normal;
}

.panel-label {
    flex-shrink: 0;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.file-selector {
    flex: 1;
    min-width: 0;
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.75rem;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 3px;
    padding: 2px 4px;
    cursor: pointer;
    outline: none;
}

.file-selector:focus {
    border-color: var(--accent);
}

/* ---- File action buttons (Add / Delete / Rename) ---- */

.file-action-btn {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    padding: 0;
    font-size: 0.85rem;
    line-height: 1;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid transparent;
    border-radius: 3px;
    cursor: pointer;
    transition: color 0.15s, background 0.15s, border-color 0.15s;
}

.file-action-btn:hover {
    color: var(--text);
    background: var(--hover-bg);
    border-color: var(--border);
}

/* ---- File name modal ---- */

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal-backdrop[hidden] {
    display: none;
}

.modal-dialog {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 20px;
    min-width: 320px;
    max-width: 480px;
    width: 90%;
    display: flex;
    flex-direction: column;
    gap: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.modal-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text);
}

.modal-input {
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.8rem;
    padding: 6px 8px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    outline: none;
    width: 100%;
}

.modal-input:focus {
    border-color: var(--accent);
}

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

.modal-btn {
    font-family: inherit;
    font-size: 0.8rem;
    padding: 5px 14px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
    transition: opacity 0.15s;
}

.modal-btn:hover {
    opacity: 0.85;
}

.modal-btn-primary {
    background: var(--accent);
    color: var(--bg);
    border-color: transparent;
    font-weight: 600;
}

/* ---- Gutter (resizer) ---- */

.gutter {
    width: 6px;
    cursor: col-resize;
    background: var(--border);
    flex-shrink: 0;
}

.gutter:hover,
.gutter.dragging {
    background: var(--accent);
}

/* ---- Editor panel ---- */

#editor {
    flex: 1;
    overflow: hidden;
}

#editor .cm-editor {
    height: 100%;
}

#editor .cm-editor .cm-scroller {
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 14px;
    line-height: 1.6;
}

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

#editor .cm-editor {
    background: var(--bg);
}

#editor .cm-gutters {
    background: var(--surface);
    border-right: 1px solid var(--border);
    color: var(--text-muted);
}

#editor .cm-activeLineGutter,
#editor .cm-activeLine {
    background: var(--active-line-bg);
}

#editor .cm-selectionBackground {
    background: var(--selection-bg) !important;
}

#editor .cm-cursor {
    border-left-color: var(--accent);
}

/* ---- Output tabs ---- */

.output-tabs {
    display: flex;
    align-items: center;
    gap: 0;
    padding: 0;
    height: var(--panel-header-height);
}

.tab-btn {
    height: 100%;
    padding: 0 14px;
    font-family: inherit;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    background: transparent;
    border: none;
    border-right: 1px solid var(--border);
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
}

.tab-btn:hover {
    color: var(--text);
    background: var(--active-line-bg);
}

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

.graph-reset-btn {
    margin-left: auto;
    height: 100%;
    padding: 0 12px;
    font-family: inherit;
    font-size: 0.7rem;
    color: var(--text-muted);
    background: transparent;
    border: none;
    border-left: 1px solid var(--border);
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
}

.graph-reset-btn:hover {
    color: var(--text);
    background: var(--hover-bg);
}

.tab-content {
    display: none;
    flex: 1;
    overflow: auto;
}

.tab-content.active {
    display: flex;
    flex-direction: column;
}

.svg-container {
    align-items: flex-start;
    justify-content: flex-start;
    padding: 0;
    background: var(--bg);
    overflow: hidden;
    position: relative;
    cursor: grab;
    user-select: none;
}

.svg-pan-inner {
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    line-height: 0;
}

.svg-pan-inner svg {
    display: block;
}

/* ---- Output panel ---- */

#output,
#turtle-output {
    flex: 1;
    overflow: auto;
    padding: 12px;
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 13px;
    line-height: 1.6;
    background: var(--bg);
    color: var(--text);
    tab-size: 2;
    white-space: pre-wrap;
    word-wrap: break-word;
}

/* ---- Right panels (output + problems) ---- */

.right-panels {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.right-panels > #output-panel {
    flex: 3;
    overflow: hidden;
    min-height: 80px;
}

.hgutter {
    height: 6px;
    cursor: row-resize;
    background: var(--border);
    flex-shrink: 0;
}

.hgutter:hover,
.hgutter.dragging {
    background: var(--accent);
}

.right-panels > #problems-panel {
    flex: 1;
    overflow: hidden;
    border-top: none;
    min-height: 48px;
}

/* ---- Problems panel ---- */

#problems-panel .panel-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Container for the two problem-count badges */
#problems-count {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 18px;
    padding: 0 6px;
    border-radius: 9px;
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--bg);
}

.badge-errors {
    background: var(--error);
}

.badge-warnings {
    background: var(--warning);
}

.badge-clean {
    background: var(--success);
}

.problems-list {
    overflow-y: auto;
    padding: 4px 0;
    max-height: calc(40vh - var(--panel-header-height));
}

.problems-list:empty::after {
    content: "No problems detected.";
    display: block;
    padding: 12px;
    color: var(--text-muted);
    font-size: 0.8rem;
    text-align: center;
}

/* ---- Problem card ---- */

.problem-card {
    display: flex;
    gap: 10px;
    padding: 8px 12px;
    margin: 2px 6px;
    border-radius: 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    font-size: 0.8rem;
    line-height: 1.4;
    cursor: pointer;
    transition: background 0.15s;
}

.problem-card:hover {
    background: var(--hover-bg);
    border-color: var(--accent);
}

.problem-card .problem-icon {
    font-size: 1rem;
    flex-shrink: 0;
    line-height: 1.4;
}

.problem-card .problem-body {
    flex: 1;
    min-width: 0;
}

.problem-card .problem-message {
    color: var(--text);
    word-break: break-word;
}

.problem-card .problem-location {
    margin-top: 2px;
    font-size: 0.7rem;
    color: var(--text-muted);
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
}

.problem-card .problem-expected {
    margin-top: 4px;
    font-size: 0.7rem;
    color: var(--text-muted);
    font-style: italic;
}

.problem-card.error .problem-message {
    color: var(--error);
}

.problem-card.warning .problem-message {
    color: var(--warning);
}

.problem-card.hint .problem-message {
    color: var(--accent);
}

/* ---- SVG ontology highlight / dim ---- */

.ontology-embed {
    transition: opacity 0.2s;
}

.ontology-dimmed {
    opacity: 0.15;
}

.ontology-highlighted .ontology-highlight-ring {
    display: block;
}

.ontology-highlight-ring {
    display: none;
    fill: none;
    stroke: var(--accent);
    stroke-width: 2.5;
    rx: 12;
    ry: 12;
    pointer-events: none;
}

/* ---- Graph tab ---- */

.graph-container {
    background: var(--graph-bg);
    overflow: hidden;
    position: relative;
    cursor: grab;
    user-select: none;
}

.graph-placeholder {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-muted);
    font-size: 0.85rem;
    pointer-events: none;
}

.graph-placeholder--error {
    color: var(--error);
}

.graph-controls {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    z-index: 10;
}

.graph-zoom-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    line-height: 1;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    user-select: none;
    transition: background 0.15s, color 0.15s;
}

.graph-zoom-btn:hover {
    background: var(--hover-bg);
    color: var(--accent);
    border-color: var(--accent);
}

/* ============================================================
   Semantic token highlight classes
   Indices match the LSP token-type legend in dolfin-lsp-core/src/semantic.rs
   ============================================================ */

/* Dark theme (Catppuccin Macchiato) */
html[data-theme="dark"],
:root {
    --dlf-tt-namespace: #eed49f;   /* yellow   — ontology name    */
    --dlf-tt-type:      #8aadf4;   /* blue     — type references  */
    --dlf-tt-class:     #ed8796;   /* red      — concept names    */
    --dlf-tt-enum:      #c6a0f6;   /* purple   — enum types       */
    --dlf-tt-enumMember:#f5a97f;   /* orange   — enum variants    */
    --dlf-tt-function:  #8bd5ca;   /* teal     — property names   */
    --dlf-tt-macro:     #a6da95;   /* green    — built-in macros  */
    --dlf-tt-variable:  #cad3f5;   /* text     — variables        */
    --dlf-tt-keyword:   #c6a0f6;   /* purple   — keywords         */
}

/* Light theme (Catppuccin Latte) */
html[data-theme="light"] {
    --dlf-tt-namespace: #df8e1d;
    --dlf-tt-type:      #1e66f5;
    --dlf-tt-class:     #d20f39;
    --dlf-tt-enum:      #8839ef;
    --dlf-tt-enumMember:#fe640b;
    --dlf-tt-function:  #179299;
    --dlf-tt-macro:     #40a02b;
    --dlf-tt-variable:  #4c4f69;
    --dlf-tt-keyword:   #8839ef;
}

@media (prefers-color-scheme: light) {
    html[data-theme="system"] {
        --dlf-tt-namespace: #df8e1d;
        --dlf-tt-type:      #1e66f5;
        --dlf-tt-class:     #d20f39;
        --dlf-tt-enum:      #8839ef;
        --dlf-tt-enumMember:#fe640b;
        --dlf-tt-function:  #179299;
        --dlf-tt-macro:     #40a02b;
        --dlf-tt-variable:  #4c4f69;
        --dlf-tt-keyword:   #8839ef;
    }
}

/* token-type index → CSS var (must match semantic.rs TOKEN_TYPES order) */
.dlf-tt-0 { color: var(--dlf-tt-namespace);  }   /* namespace  */
.dlf-tt-1 { color: var(--dlf-tt-type);       }   /* type       */
.dlf-tt-2 { color: var(--dlf-tt-class);      }   /* class      */
.dlf-tt-3 { color: var(--dlf-tt-enum);       }   /* enum       */
.dlf-tt-4 { color: var(--dlf-tt-enumMember); }   /* enumMember */
.dlf-tt-5 { color: var(--dlf-tt-function);   }   /* function   */
.dlf-tt-6 { color: var(--dlf-tt-macro);      }   /* macro      */
.dlf-tt-7 { color: var(--dlf-tt-variable);   }   /* variable   */
.dlf-tt-8 { color: var(--dlf-tt-keyword);    }   /* keyword    */

/* Declaration modifier: bold the declaration site */
.dlf-tt-decl { font-weight: 600; }

/* ============================================================
   LSP hover tooltip bubble
   ============================================================ */

.dlf-hover {
    padding: 5px 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.82em;
    color: var(--text);
    max-width: 520px;
    white-space: pre-wrap;
    line-height: 1.5;
}

/* ============================================================
   Mobile layout (≤768px)
   ============================================================ */
@media (max-width: 768px) {
    /* Allow the page to scroll vertically when panels are stacked */
    html {
        overflow-x: hidden;
        overflow-y: auto;
        height: auto;
    }

    body {
        overflow-x: hidden;
        overflow-y: auto;
        height: auto;
        min-height: 100vh;
    }

    /* Header: wrap toolbar items instead of overflowing */
    header {
        height: auto;
        min-height: var(--header-height);
        flex-wrap: wrap;
        align-items: center;
        padding: 8px 12px;
        gap: 8px;
    }

    header h1 {
        flex-shrink: 0;
    }

    .toolbar {
        flex-wrap: wrap;
        gap: 8px;
    }

    /* Stack editor + output vertically */
    main {
        flex-direction: column;
        height: auto;
        overflow: visible;
    }

    /* Hide drag gutters — irrelevant in stacked layout */
    .gutter, .hgutter {
        display: none;
    }

    /* Override any inline flex set by the gutter drag JS */
    #editor-panel {
        flex: none !important;
        width: 100%;
        height: 45vh;
        min-height: 220px;
    }

    .right-panels {
        flex: none !important;
        width: 100%;
        height: 50vh;
        min-height: 280px;
        overflow: hidden;
    }

    .right-panels > #problems-panel {
        border-top: 1px solid var(--border);
    }

    /* Smaller mono font on narrow screens */
    #editor .cm-editor .cm-scroller {
        font-size: 12px;
    }

    #output,
    #turtle-output {
        font-size: 12px;
    }

    /* Tab buttons: allow scroll if they overflow */
    .output-tabs {
        overflow-x: auto;
        flex-shrink: 0;
    }
}
