:root {
    --bg: #FFFFFF;
    --bg-secondary: #F7F7F5;
    --bg-hover: #F1F1EF;
    --bg-active: #EBEBEA;
    --border: #E9E9E7;
    --border-light: #F0F0EE;
    --text-primary: #37352F;
    --text-secondary: #787774;
    --text-tertiary: #ACACAA;
    --text-placeholder: #C4C4C2;
    --accent: #2383E2;
    --accent-hover: #1A6FC4;
    --success: #0F7B6C;
    --error: #EB5757;
    --error-bg: #FFF5F5;
    --error-border: #FECACA;
    --radius: 6px;
    --radius-sm: 4px;
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', ui-monospace, monospace;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow: 0 2px 8px rgba(0,0,0,0.06);
    --transition: 0.12s ease;
}

[data-theme="dark"] {
    --bg: #191919;
    --bg-secondary: #202020;
    --bg-hover: #2A2A2A;
    --bg-active: #303030;
    --border: #333333;
    --border-light: #2B2B2B;
    --text-primary: #E2DDD5;
    --text-secondary: #868585;
    --text-tertiary: #555555;
    --text-placeholder: #444444;
    --error-bg: #2D1B1B;
    --error-border: #5C2B2B;
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    transition: background var(--transition), color var(--transition);
}

/* ─── Header ─────────────────────────────────────────── */

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 52px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    background: var(--bg);
    position: relative;
    z-index: 10;
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 220px;
}

.header-right {
    justify-content: flex-end;
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    user-select: none;
}

.logo-icon {
    color: var(--text-primary);
    flex-shrink: 0;
}

.logo-text {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: -0.02em;
    white-space: nowrap;
}

.diagram-title {
    border: none;
    outline: none;
    font-family: var(--font-sans);
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
    background: transparent;
    text-align: center;
    padding: 5px 10px;
    border-radius: var(--radius);
    min-width: 160px;
    max-width: 320px;
    transition: background var(--transition);
}

.diagram-title::placeholder { color: var(--text-placeholder); }
.diagram-title:hover { background: var(--bg-hover); }
.diagram-title:focus { background: var(--bg-secondary); }

/* ─── Buttons ─────────────────────────────────────────── */

.btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 5px 12px;
    border-radius: var(--radius);
    font-family: var(--font-sans);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition), color var(--transition);
    border: none;
    white-space: nowrap;
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.btn-primary {
    background: var(--text-primary);
    color: var(--bg);
}

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

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-size: 15px;
    line-height: 1;
    transition: background var(--transition), color var(--transition);
    user-select: none;
}

.icon-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.divider-v {
    width: 1px;
    height: 18px;
    background: var(--border);
    margin: 0 4px;
}

.divider-v.small {
    height: 14px;
    margin: 0 2px;
}

/* ─── Template Select ─────────────────────────────────── */

.template-select {
    appearance: none;
    border: none;
    background: transparent;
    font-family: var(--font-sans);
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 3px 6px;
    border-radius: var(--radius-sm);
    transition: background var(--transition);
    outline: none;
}

.template-select:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.template-select option {
    background: var(--bg);
    color: var(--text-primary);
}

/* ─── Main Layout ─────────────────────────────────────── */

.main {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ─── Panels ──────────────────────────────────────────── */

.panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background var(--transition);
}

.panel-editor {
    width: 42%;
    min-width: 260px;
    border-right: 1px solid var(--border);
    background: var(--bg-secondary);
}

.panel-preview {
    flex: 1;
    background: var(--bg);
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 14px;
    height: 38px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    background: var(--bg);
}

.panel-editor .panel-header {
    background: var(--bg-secondary);
}

.panel-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-tertiary);
}

.panel-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

/* ─── Editor (textarea) ───────────────────────────────── */

.editor {
    flex: 1;
    width: 100%;
    padding: 18px 20px;
    background: transparent;
    border: none;
    outline: none;
    resize: none;
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.75;
    color: var(--text-primary);
    tab-size: 2;
    overflow-y: auto;
}

.editor::placeholder { color: var(--text-placeholder); }

/* ─── dead CodeMirror rules (kept for reference) ─────── */
.cm-UNUSED {
    background: rgba(35, 131, 226, 0.15) !important;
}

.editor-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 14px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    min-height: 32px;
}

.status-indicator {
    font-size: 12px;
    font-weight: 500;
    transition: color var(--transition);
}

.status-indicator.valid { color: var(--success); }
.status-indicator.error { color: var(--error); }
.status-indicator.loading { color: var(--text-tertiary); }

.char-count {
    font-size: 12px;
    color: var(--text-tertiary);
    font-variant-numeric: tabular-nums;
}

/* ─── Resize Handle ───────────────────────────────────── */

.resize-handle {
    width: 5px;
    background: transparent;
    cursor: col-resize;
    flex-shrink: 0;
    position: relative;
    z-index: 5;
    transition: background 0.2s;
}

.resize-handle::after {
    content: '';
    position: absolute;
    inset: 0 -2px;
}

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

/* ─── Preview Canvas ──────────────────────────────────── */

.preview-canvas {
    flex: 1;
    overflow: auto;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 48px 40px;
}

.preview-diagram {
    transform-origin: top center;
    transition: transform 0.15s ease, opacity 0.1s;
    min-width: 100px;
}

.preview-diagram.updating {
    opacity: 0.6;
}

.zoom-level {
    font-size: 12px;
    color: var(--text-secondary);
    min-width: 38px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    user-select: none;
}

/* ─── Error Message ───────────────────────────────────── */

.error-box {
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    border-radius: var(--radius);
    padding: 14px 16px;
    color: var(--error);
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.65;
    max-width: 520px;
    word-break: break-word;
}

.error-box .error-label {
    font-weight: 600;
    margin-bottom: 6px;
    display: block;
    font-family: var(--font-sans);
}

/* ─── Empty State ─────────────────────────────────────── */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    color: var(--text-placeholder);
    padding: 60px 40px;
    text-align: center;
    user-select: none;
}

.empty-state svg {
    opacity: 0.25;
}

.empty-state-title {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-tertiary);
}

.empty-state-sub {
    font-size: 13px;
    color: var(--text-placeholder);
    line-height: 1.6;
}

/* ─── Scrollbar ───────────────────────────────────────── */

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-placeholder); }

/* ─── Diagram SVG tweaks ──────────────────────────────── */

.preview-diagram svg {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius);
}

/* ─── Toast ───────────────────────────────────────────── */

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(12px);
    background: var(--text-primary);
    color: var(--bg);
    font-size: 13px;
    font-weight: 500;
    padding: 8px 16px;
    border-radius: 20px;
    opacity: 0;
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
    z-index: 100;
    white-space: nowrap;
    box-shadow: var(--shadow);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}
