/* ── Reset & Variables ─────────────────────────────────── */

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #16161a;
    --bg-surface: #1c1c22;
    --text-primary: #d4d4dc;
    --text-secondary: #72728a;
    --text-heading: #fffffe;
    --accent: #3b6ff5;
    --accent-hover: #5a8aff;
    --border: #2a2a35;
}

.light {
    --bg-primary: #f8f8fa;
    --bg-surface: #ffffff;
    --text-primary: #2c2c34;
    --text-secondary: #8888a0;
    --text-heading: #111118;
    --accent: #002FA7;
    --accent-hover: #0040d4;
    --border: #e0e0e8;
}

html, body {
    height: 100%;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
}

/* ── Layout ───────────────────────────────────────────── */

.app {
    max-width: 720px;
    margin: 0 auto;
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* ── Editor Container ─────────────────────────────────── */

.editor-container {
    flex: 1;
    overflow-y: auto;
    padding: 32px;
    position: relative;
    scrollbar-width: none;
}

.editor-container::-webkit-scrollbar {
    width: 0;
}

.placeholder {
    position: absolute;
    top: 32px;
    left: 32px;
    color: var(--text-secondary);
    opacity: 0.5;
    font-size: 1rem;
    line-height: 1.8;
    pointer-events: none;
    user-select: none;
    transition: opacity 0.15s;
}

.placeholder.hidden {
    opacity: 0;
}

/* ── Scrollbar (opposite side of sidebar, edge-hover) ── */

.scrollbar-trigger {
    position: fixed;
    top: 0;
    right: 0;
    width: 10px;
    height: 100%;
    z-index: 50;
}

[data-sidebar-side="right"] .scrollbar-trigger {
    right: auto;
    left: 0;
}

.scrollbar-track {
    position: fixed;
    top: 0;
    right: 0;
    width: 6px;
    height: 100%;
    z-index: 51;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

[data-sidebar-side="right"] .scrollbar-track {
    right: auto;
    left: 0;
}

.scrollbar-track.visible {
    opacity: 1;
    pointer-events: auto;
}

.scrollbar-thumb {
    position: absolute;
    right: 0;
    width: 6px;
    border-radius: 3px;
    background: var(--border);
    min-height: 30px;
    transition: background 0.15s;
}

[data-sidebar-side="right"] .scrollbar-thumb {
    right: auto;
    left: 0;
}

.scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* ── Editor Core ──────────────────────────────────────── */

#editor {
    min-height: 100%;
    outline: none;
    font-size: 1rem;
    line-height: 1.8;
    color: var(--text-primary);
    caret-color: var(--accent);
}

/* ── Paragraphs ───────────────────────────────────────── */

#editor p {
    margin: 0 0 0.1em;
    min-height: 1.8em;
}

/* ── Headings ─────────────────────────────────────────── */

#editor h1 {
    font-size: 2em;
    font-weight: 700;
    color: var(--text-heading);
    margin: 0.6em 0 0.25em;
    line-height: 1.3;
    letter-spacing: -0.02em;
}

#editor h1:first-child {
    margin-top: 0;
}

#editor h2 {
    font-size: 1.4em;
    font-weight: 600;
    color: var(--text-heading);
    margin: 0.5em 0 0.2em;
    line-height: 1.4;
    letter-spacing: -0.01em;
}

#editor h2:first-child {
    margin-top: 0;
}

/* ── Bullet Lists ─────────────────────────────────────── */

#editor ul {
    margin: 0.3em 0;
    padding-left: 1.6em;
}

#editor ul li {
    margin: 0.05em 0;
    min-height: 1.8em;
}

#editor ul li::marker {
    color: var(--text-primary);
}

/* ── Task Lists ───────────────────────────────────────── */

#editor ul.task-list {
    list-style: none;
    padding-left: 0.2em;
}

#editor ul.task-list li {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    min-height: 1.8em;
}

#editor ul.task-list li input[type="checkbox"] {
    margin-top: 6px;
    cursor: pointer;
    accent-color: var(--accent);
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

#editor ul.task-list li > span {
    flex: 1;
    min-height: 1.8em;
    outline: none;
}

#editor ul.task-list li.checked > span {
    text-decoration: line-through;
    opacity: 0.4;
}

/* ── Inline Formatting ────────────────────────────────── */

#editor strong {
    font-weight: 700;
}

#editor em {
    font-style: italic;
}

/* ── Images ────────────────────────────────────────────── */

#editor img.editor-image {
    display: block;
    max-width: 100%;
    border-radius: 6px;
    margin: 0.5em 0;
}

/* ── Reference Links ──────────────────────────────────── */

#editor a.note-ref {
    color: var(--accent);
    text-decoration: underline;
    text-decoration-thickness: 1.5px;
    text-underline-offset: 3px;
    cursor: pointer;
    font-weight: 600;
    transition: color 0.15s;
}

#editor a.note-ref:hover {
    color: var(--accent-hover);
}

/* ── Link Autocomplete Popup ──────────────────────────── */

.link-popup {
    position: fixed;
    z-index: 500;
    width: 280px;
    max-height: 240px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
    overflow: hidden;
    display: none;
}

.link-popup.open {
    display: block;
}

.link-results {
    max-height: 240px;
    overflow-y: auto;
}

.link-option {
    padding: 8px 14px;
    font-size: 0.8125rem;
    color: var(--text-primary);
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: background 0.1s;
}

.link-option:hover,
.link-option.selected {
    background: rgba(128, 128, 128, 0.12);
    color: var(--text-heading);
}

.link-results:empty::before {
    content: 'No matching notes';
    display: block;
    padding: 10px 14px;
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-style: italic;
}

/* ── Search Highlight ─────────────────────────────────── */

#editor mark.search-highlight {
    background: rgba(59, 111, 245, 0.25);
    color: inherit;
    border-radius: 2px;
    padding: 1px 0;
}

.light #editor mark.search-highlight {
    background: rgba(0, 47, 167, 0.15);
}

/* ── Responsive ───────────────────────────────────────── */

@media (max-width: 600px) {
    .editor-container {
        padding: 16px;
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }
    .placeholder {
        top: 16px;
        left: max(16px, env(safe-area-inset-left));
    }
    .link-popup {
        width: calc(100vw - 32px);
        max-width: 280px;
    }
}

/* ════════════════════════════════════════════════════════
   SIDEBAR
   ════════════════════════════════════════════════════════ */

/* ── Trigger Zone (invisible hover strip) ─────────────── */

.sidebar-trigger {
    position: fixed;
    top: 0;
    left: 0;
    width: 10px;
    height: 100%;
    z-index: 200;
}

[data-sidebar-side="right"] .sidebar-trigger {
    left: auto;
    right: 0;
}

/* ── Sidebar Panel ────────────────────────────────────── */

.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 260px;
    height: 100%;
    background: var(--bg-surface);
    border-right: 1px solid var(--border);
    z-index: 199;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar.visible {
    transform: translateX(0);
}

[data-sidebar-side="right"] .sidebar {
    left: auto;
    right: 0;
    border-right: none;
    border-left: 1px solid var(--border);
    transform: translateX(100%);
}

[data-sidebar-side="right"] .sidebar.visible {
    transform: translateX(0);
}

/* ── Tree Container ───────────────────────────────────── */

.sidebar-tree {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
}

.sidebar-tree::-webkit-scrollbar { width: 4px; }
.sidebar-tree::-webkit-scrollbar-track { background: transparent; }
.sidebar-tree::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Tree Items ───────────────────────────────────────── */

.tree-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 3px 12px;
    cursor: pointer;
    font-size: 0.875rem;
    line-height: 1.6;
    color: var(--text-primary);
    border-radius: 0;
    user-select: none;
    transition: background 0.1s;
}

.tree-item:hover {
    background: rgba(128, 128, 128, 0.08);
}

.tree-item.active {
    background: rgba(128, 128, 128, 0.14);
    color: var(--text-heading);
}

/* ── Tree Arrow ───────────────────────────────────────── */

.tree-arrow {
    width: 14px;
    height: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 0;
    transition: transform 0.15s;
}

.tree-arrow::before {
    content: '';
    display: block;
    width: 0;
    height: 0;
    border-left: 4px solid var(--text-secondary);
    border-top: 3px solid transparent;
    border-bottom: 3px solid transparent;
}

.tree-arrow.open {
    transform: rotate(90deg);
}

/* ── Tree Names ───────────────────────────────────────── */

.tree-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.tree-name.untitled {
    color: var(--text-secondary);
    font-style: italic;
}

/* ── Inline Rename Input ──────────────────────────────── */

.tree-rename {
    width: 100%;
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--accent);
    border-radius: 3px;
    padding: 1px 4px;
    font-size: 0.875rem;
    font-family: inherit;
    outline: none;
}

/* ════════════════════════════════════════════════════════
   CONTEXT MENU
   ════════════════════════════════════════════════════════ */

.ctx-menu {
    position: fixed;
    z-index: 300;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 0;
    min-width: 140px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}

.ctx-item {
    padding: 6px 14px;
    font-size: 0.8125rem;
    color: var(--text-primary);
    cursor: pointer;
    user-select: none;
    transition: background 0.1s;
}

.ctx-item:hover {
    background: rgba(128, 128, 128, 0.12);
}

.ctx-menu hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 4px 0;
}

/* ════════════════════════════════════════════════════════
   SEARCH OVERLAY
   ════════════════════════════════════════════════════════ */

.search-overlay {
    position: fixed;
    inset: 0;
    z-index: 400;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    align-items: flex-start;
    justify-content: center;
    padding-top: 15vh;
}

.search-overlay.open {
    display: flex;
}

.search-box {
    width: 480px;
    max-width: 90vw;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.search-box input {
    width: 100%;
    padding: 14px 18px;
    background: transparent;
    border: none;
    color: var(--text-heading);
    font-size: 1rem;
    font-family: inherit;
    outline: none;
    border-bottom: 1px solid var(--border);
}

.search-box input::placeholder {
    color: var(--text-secondary);
}

.search-results {
    max-height: 320px;
    overflow-y: auto;
}

.search-results:empty {
    display: none;
}

.search-result {
    padding: 10px 18px;
    cursor: pointer;
    transition: background 0.1s;
}

.search-result:hover,
.search-result.selected {
    background: rgba(128, 128, 128, 0.1);
}

.search-result-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-heading);
}

.search-result-path {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

.search-result-snippet {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.search-result-snippet mark {
    background: transparent;
    color: var(--accent);
    font-weight: 600;
}
