/* ==========================================================================
   NoteForge Stylesheet (Dark & Light Themes, Side-by-Side Split Workspace)
   ========================================================================== */

:root {
    --bg-app: #0b1120;
    --bg-surface: #111827;
    --bg-elevated: #1e293b;
    --bg-hover: #243247;
    --bg-input: #0f172a;
    --border: #263244;
    --border-strong: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --accent: #38bdf8;
    --accent-strong: #0ea5e9;
    --accent-text: #082f49;
    --success: #34d399;
    --danger: #f87171;
    --warning: #fbbf24;
    --code-bg: #0a0f1d;
    --shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
    --font-sans: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', Consolas, 'Courier New', monospace;
}

[data-theme="light"] {
    --bg-app: #f1f5f9;
    --bg-surface: #ffffff;
    --bg-elevated: #f8fafc;
    --bg-hover: #e2e8f0;
    --bg-input: #ffffff;
    --border: #cbd5e1;
    --border-strong: #94a3b8;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #64748b;
    --accent: #0284c7;
    --accent-strong: #0369a1;
    --accent-text: #ffffff;
    --success: #059669;
    --danger: #dc2626;
    --warning: #d97706;
    --code-bg: #f8fafc;
    --shadow: 0 18px 40px rgba(15, 23, 42, 0.14);
}

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

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: var(--font-sans);
    background: var(--bg-app);
    color: var(--text-primary);
    overflow: hidden;
}

button, input, textarea {
    font: inherit;
    color: inherit;
}

[hidden] {
    display: none !important;
}

body.is-resizing {
    cursor: col-resize;
    user-select: none;
}

body.is-resizing iframe {
    pointer-events: none;
}

/* --------------------------------------------------------------------------
   App Shell Layout
   -------------------------------------------------------------------------- */
.app-shell {
    display: flex;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 290px;
    min-width: 260px;
    background: var(--bg-surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 20;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.9rem 1rem;
    border-bottom: 1px solid var(--border);
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

.brand-logo {
    width: 32px;
    height: 32px;
    border-radius: 9px;
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, var(--accent), #6366f1);
    color: #fff;
    font-weight: 700;
}

.brand-name {
    display: block;
    font-weight: 700;
    font-size: 0.98rem;
    letter-spacing: -0.01em;
}

.brand-sub {
    display: block;
    font-size: 0.72rem;
    color: var(--text-muted);
}

.sidebar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    padding: 0.75rem 0.9rem 0.5rem;
}

.search-box {
    padding: 0.25rem 0.9rem 0.6rem;
}

.search-box input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg-input);
    font-size: 0.85rem;
    outline: none;
}

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

.filter-pills {
    display: flex;
    gap: 0.35rem;
    padding: 0 0.9rem 0.65rem;
    border-bottom: 1px solid var(--border);
}

.pill {
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text-secondary);
    padding: 0.25rem 0.65rem;
    border-radius: 999px;
    font-size: 0.75rem;
    cursor: pointer;
}

.pill.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
    font-weight: 600;
}

.notes-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.note-item {
    text-align: left;
    border: 1px solid transparent;
    background: transparent;
    padding: 0.65rem 0.75rem;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.note-item:hover {
    background: var(--bg-elevated);
}

.note-item.active {
    background: var(--bg-elevated);
    border-color: var(--accent);
}

.note-item-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.4rem;
}

.note-item-title {
    font-weight: 600;
    font-size: 0.88rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.pin-dot {
    color: var(--warning);
    margin-right: 0.25rem;
}

.mode-pill {
    font-size: 0.68rem;
    font-weight: 600;
    padding: 0.12rem 0.42rem;
    border-radius: 5px;
    flex-shrink: 0;
}

.mode-markdown {
    background: rgba(56, 189, 248, 0.15);
    color: var(--accent);
}

.mode-html {
    background: rgba(52, 211, 153, 0.15);
    color: var(--success);
}

.note-item-snippet {
    font-size: 0.78rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-item-meta {
    display: flex;
    justify-content: space-between;
    font-size: 0.71rem;
    color: var(--text-muted);
}

.sidebar-footer {
    padding: 0.6rem 0.9rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    font-size: 0.74rem;
    color: var(--text-muted);
}

/* --------------------------------------------------------------------------
   Workspace & Topbar
   -------------------------------------------------------------------------- */
.workspace {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.65rem 1rem;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    flex: 1;
    min-width: 220px;
}

.title-input {
    font-size: 1rem;
    font-weight: 600;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 7px;
    padding: 0.35rem 0.5rem;
    min-width: 150px;
    flex: 1;
    outline: none;
}

.title-input:focus {
    background: var(--bg-input);
    border-color: var(--accent);
}

.tags-input {
    width: 170px;
    font-size: 0.8rem;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 0.35rem 0.55rem;
    color: var(--text-secondary);
    outline: none;
}

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

.topbar-right {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    flex-wrap: wrap;
}

/* Buttons & Controls */
.btn {
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 0.42rem 0.78rem;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.btn-xs {
    padding: 0.25rem 0.5rem;
    font-size: 0.74rem;
    border-radius: 6px;
}

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

.btn-primary:hover {
    background: var(--accent-strong);
}

.btn-accent {
    background: #6366f1;
    color: #ffffff;
}

.btn-accent:hover {
    background: #4f46e5;
}

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

.btn-secondary:hover {
    background: var(--bg-hover);
}

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

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

.btn-ghost.is-pinned {
    color: var(--warning);
    border-color: rgba(251, 191, 36, 0.45);
}

.btn-danger-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--danger);
}

.btn-danger-ghost:hover {
    background: rgba(248, 113, 113, 0.12);
}

.full-width {
    width: 100%;
}

.icon-btn {
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    border-radius: 8px;
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    cursor: pointer;
    font-size: 1rem;
}

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

.mobile-only {
    display: none;
}

.segmented {
    display: inline-flex;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 2px;
}

.seg-btn {
    border: none;
    background: transparent;
    color: var(--text-secondary);
    padding: 0.3rem 0.6rem;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
}

.seg-btn.active {
    background: var(--bg-elevated);
    color: var(--text-primary);
    font-weight: 600;
}

.badge {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    padding: 0.05rem 0.4rem;
    border-radius: 999px;
    font-size: 0.72rem;
}

/* --------------------------------------------------------------------------
   Side-by-Side Split Workspace
   -------------------------------------------------------------------------- */
.split-container {
    --editor-width: 50%;
    flex: 1;
    display: flex;
    min-height: 0;
    overflow: hidden;
}

.editor-pane {
    width: var(--editor-width);
    min-width: 240px;
    display: flex;
    flex-direction: column;
    background: var(--bg-app);
}

.preview-pane {
    flex: 1;
    min-width: 240px;
    display: flex;
    flex-direction: column;
    background: var(--bg-surface);
}

.split-container[data-layout="editor"] .editor-pane {
    width: 100%;
}
.split-container[data-layout="editor"] .splitter,
.split-container[data-layout="editor"] .preview-pane {
    display: none;
}

.split-container[data-layout="preview"] .editor-pane,
.split-container[data-layout="preview"] .splitter {
    display: none;
}

.splitter {
    width: 6px;
    background: var(--border);
    cursor: col-resize;
    flex-shrink: 0;
    transition: background 0.15s ease;
}

.splitter:hover {
    background: var(--accent);
}

.pane-bar {
    height: 40px;
    padding: 0 0.85rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-surface);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}

.pane-bar-left, .pane-bar-right {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.pane-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.md-toolbar, .code-tabs {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.tool-btn {
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text-secondary);
    border-radius: 6px;
    padding: 0.2rem 0.5rem;
    font-size: 0.76rem;
    cursor: pointer;
}

.tool-btn:hover {
    color: var(--text-primary);
    border-color: var(--border-strong);
}

.code-tab {
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-secondary);
    padding: 0.25rem 0.65rem;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
}

.code-tab.active {
    background: var(--bg-elevated);
    border-color: var(--accent);
    color: var(--accent);
}

.save-status {
    font-size: 0.76rem;
    color: var(--success);
}

.save-status.is-dirty {
    color: var(--warning);
}

.editor-wrap {
    flex: 1;
    min-height: 0;
    display: flex;
}

.code-editor {
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    resize: none;
    padding: 1rem 1.15rem;
    background: var(--code-bg);
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 0.9rem;
    line-height: 1.6;
    tab-size: 2;
}

.pane-footer {
    height: 28px;
    padding: 0 0.85rem;
    border-top: 1px solid var(--border);
    background: var(--bg-surface);
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 0.73rem;
    color: var(--text-muted);
}

/* Preview Area */
.preview-body {
    flex: 1;
    min-height: 0;
    overflow: auto;
    position: relative;
}

.markdown-preview {
    padding: 1.4rem 1.8rem;
    line-height: 1.7;
    max-width: 820px;
}

.markdown-preview h1,
.markdown-preview h2,
.markdown-preview h3 {
    margin-top: 1.35em;
    margin-bottom: 0.5em;
    line-height: 1.25;
}

.markdown-preview h1:first-child {
    margin-top: 0;
}

.markdown-preview h1 {
    font-size: 1.75rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.35rem;
}

.markdown-preview code {
    font-family: var(--font-mono);
    background: var(--bg-elevated);
    padding: 0.14rem 0.35rem;
    border-radius: 5px;
    font-size: 0.86em;
}

.markdown-preview .code-block {
    position: relative;
    margin: 1rem 0;
    background: var(--code-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
}

.markdown-preview .code-lang {
    position: absolute;
    top: 0.4rem;
    right: 0.6rem;
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.markdown-preview pre {
    margin: 0;
    padding: 1rem;
    overflow-x: auto;
}

.markdown-preview pre code {
    background: transparent;
    padding: 0;
}

.markdown-preview blockquote {
    margin: 1rem 0;
    padding: 0.6rem 1rem;
    border-left: 3px solid var(--accent);
    background: var(--bg-elevated);
    border-radius: 0 8px 8px 0;
    color: var(--text-secondary);
}

.markdown-preview table {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
    font-size: 0.9rem;
}

.markdown-preview th,
.markdown-preview td {
    border: 1px solid var(--border);
    padding: 0.5rem 0.75rem;
    text-align: left;
}

.markdown-preview th {
    background: var(--bg-elevated);
}

.markdown-preview .task-item {
    list-style: none;
    margin-left: -1.1rem;
    display: flex;
    align-items: center;
    gap: 0.45rem;
}

.markdown-preview a {
    color: var(--accent);
}

.markdown-preview hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1.5rem 0;
}

.empty-preview-hint {
    color: var(--text-muted);
    font-style: italic;
}

.html-preview {
    width: 100%;
    height: 100%;
    border: none;
    background: #ffffff;
}

/* Sandbox Console */
.sandbox-console {
    height: 120px;
    border-top: 1px solid var(--border);
    background: var(--code-bg);
    display: flex;
    flex-direction: column;
}

.console-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.3rem 0.75rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.74rem;
    color: var(--text-secondary);
}

.console-logs {
    flex: 1;
    overflow-y: auto;
    padding: 0.4rem 0.75rem;
    font-family: var(--font-mono);
    font-size: 0.78rem;
}

.console-entry {
    padding: 0.15rem 0;
}
.console-error { color: var(--danger); }
.console-warn { color: var(--warning); }
.console-log, .console-info { color: var(--text-secondary); }

/* --------------------------------------------------------------------------
   Version History Drawer
   -------------------------------------------------------------------------- */
.version-drawer {
    width: 320px;
    background: var(--bg-surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 15;
}

.drawer-header {
    padding: 0.9rem 1rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.drawer-header h3 {
    margin: 0;
    font-size: 0.98rem;
}

.drawer-sub {
    margin: 0.15rem 0 0;
    font-size: 0.76rem;
    color: var(--text-muted);
}

.drawer-actions {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}

.versions-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.version-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.version-card.is-current {
    border-color: var(--accent);
}

.version-card-head {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.version-tag {
    font-family: var(--font-mono);
    font-weight: 700;
    font-size: 0.82rem;
}

.current-pill {
    margin-left: auto;
    font-size: 0.68rem;
    font-weight: 600;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    background: rgba(52, 211, 153, 0.15);
    color: var(--success);
}

.version-label {
    font-size: 0.85rem;
    font-weight: 500;
}

.version-time {
    font-size: 0.72rem;
    color: var(--text-muted);
}

.version-actions {
    display: flex;
    gap: 0.35rem;
    margin-top: 0.25rem;
}

/* --------------------------------------------------------------------------
   Modals, Diff Viewer, Empty State, & Toast
   -------------------------------------------------------------------------- */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.72);
    display: grid;
    place-items: center;
    padding: 1rem;
    z-index: 50;
}

.modal {
    width: min(460px, 95vw);
    background: var(--bg-surface);
    border: 1px solid var(--border-strong);
    border-radius: 14px;
    box-shadow: var(--shadow);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-wide {
    width: min(920px, 95vw);
    height: min(82vh, 760px);
}

.modal-header {
    padding: 0.9rem 1.15rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h3 {
    margin: 0;
    font-size: 1rem;
}

.modal-body {
    padding: 1.15rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.modal-body label {
    font-size: 0.85rem;
    font-weight: 600;
}

.modal-body input[type="text"] {
    padding: 0.55rem 0.75rem;
    border-radius: 8px;
    border: 1px solid var(--border-strong);
    background: var(--bg-input);
    outline: none;
}

.modal-body input[type="text"]:focus {
    border-color: var(--accent);
}

.modal-help {
    margin: 0;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.modal-footer {
    padding: 0.85rem 1.15rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
}

.compare-body {
    flex: 1;
    min-height: 0;
}

.compare-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.compare-file-pills {
    display: flex;
    gap: 0.35rem;
}

.diff-output {
    flex: 1;
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--code-bg);
    font-family: var(--font-mono);
    font-size: 0.8rem;
}

.diff-identical {
    padding: 1.5rem;
    text-align: center;
    color: var(--text-secondary);
}

.diff-row {
    display: grid;
    grid-template-columns: 44px 44px 20px 1fr;
    padding: 0.12rem 0.5rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
}

.diff-ln {
    color: var(--text-muted);
    user-select: none;
    text-align: right;
    padding-right: 0.5rem;
}

.diff-sign {
    font-weight: 700;
    text-align: center;
}

.diff-add {
    background: rgba(52, 211, 153, 0.14);
}
.diff-add .diff-sign { color: var(--success); }

.diff-remove {
    background: rgba(248, 113, 113, 0.14);
}
.diff-remove .diff-sign { color: var(--danger); }

.snapshot-preview-wrap {
    flex: 1;
    min-height: 0;
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: auto;
    background: var(--bg-surface);
}

.empty-workspace {
    flex: 1;
    display: grid;
    place-items: center;
    padding: 2rem;
}

.empty-card {
    text-align: center;
    max-width: 440px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 2rem;
}

.empty-actions {
    display: flex;
    gap: 0.65rem;
    justify-content: center;
    margin-top: 1.25rem;
}

.empty-list {
    padding: 1.25rem;
    text-align: center;
    font-size: 0.82rem;
    color: var(--text-muted);
}

.toast {
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    padding: 0.65rem 1rem;
    border-radius: 10px;
    font-size: 0.84rem;
    font-weight: 600;
    box-shadow: var(--shadow);
    z-index: 80;
}

.toast-success {
    background: var(--bg-elevated);
    border: 1px solid var(--success);
    color: var(--text-primary);
}

.toast-error {
    background: var(--bg-elevated);
    border: 1px solid var(--danger);
    color: var(--danger);
}

.fatal-banner {
    max-width: 600px;
    margin: 4rem auto;
    padding: 2rem;
    border-radius: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--danger);
}

/* --------------------------------------------------------------------------
   Responsive Breakpoints
   -------------------------------------------------------------------------- */
@media (max-width: 960px) {
    .mobile-only {
        display: inline-grid;
    }

    .sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
    }

    .sidebar.is-open {
        transform: translateX(0);
    }

    .version-drawer {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        box-shadow: var(--shadow);
    }

    .split-container[data-layout="split"] {
        flex-direction: column;
    }

    .split-container[data-layout="split"] .editor-pane {
        width: 100% !important;
        height: 50%;
    }

    .splitter {
        width: 100%;
        height: 6px;
        cursor: row-resize;
    }
}
