/* ═══════════════════════════════════════════════════════════════════════════
   EVO - Editorial Design System (2025)
   
   Aesthetic: Refined editorial — exceptional typography meets functional density
   Inspired by: iA Writer, Linear, Stripe's documentation
   
   Technical features:
   - CSS Nesting, Container Queries, :has(), oklch colors
   - Native <dialog>, Popover API, text-box
   - No animations — snappy, professional
   ═══════════════════════════════════════════════════════════════════════════ */

/* Typography: Geist (Vercel) - distinctive, geometric, excellent for UI */
@import url("https://fonts.googleapis.com/css2?family=Geist:wght@400;500;600;700&family=Geist+Mono:wght@400;500&display=swap");

/* Fallback if Geist isn't available - use system fonts that are distinctive */
/* @import url("https://api.fontshare.com/v2/css?f[]=satoshi@400,500,700&display=swap"); */

/* ═══════════════════════════════════════════════════════════════════════════
   CASCADE LAYERS
   ═══════════════════════════════════════════════════════════════════════════ */
@layer reset, base, components, utilities;

/* ═══════════════════════════════════════════════════════════════════════════
   RESET LAYER
   ═══════════════════════════════════════════════════════════════════════════ */
@layer reset {
  *, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  html {
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    tab-size: 4;
    -webkit-tap-highlight-color: transparent;
    hanging-punctuation: first last;
  }

  body {
    line-height: inherit;
  }

  hr {
    height: 0;
    color: inherit;
    border-top-width: 1px;
  }

  abbr:where([title]) {
    text-decoration: underline dotted;
  }

  h1, h2, h3, h4, h5, h6 {
    font-size: inherit;
    font-weight: inherit;
    text-wrap: balance;
  }

  a {
    color: inherit;
    text-decoration: inherit;
  }

  b, strong {
    font-weight: 600;
  }

  code, kbd, samp, pre {
    font-family: var(--font-mono);
    font-size: 0.9em;
  }

  small {
    font-size: 80%;
  }

  sub, sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
  }

  sub { bottom: -0.25em; }
  sup { top: -0.5em; }

  table {
    text-indent: 0;
    border-color: inherit;
    border-collapse: collapse;
  }

  button, input, select, optgroup, textarea {
    font: inherit;
    color: inherit;
    background: transparent;
    border-radius: 0;
  }

  button {
    cursor: pointer;
  }

  :-moz-focusring {
    outline: auto;
  }

  progress {
    vertical-align: baseline;
  }

  summary {
    display: list-item;
  }

  ol, ul, menu {
    list-style: none;
  }

  img, svg, video, canvas, audio, iframe, embed, object {
    display: block;
    vertical-align: middle;
  }

  img, video {
    max-width: 100%;
    height: auto;
  }

  [hidden]:not([hidden="until-found"]) {
    display: none !important;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   BASE LAYER - Design Tokens
   ═══════════════════════════════════════════════════════════════════════════ */
@layer base {
  :root {
    /* ─── Typography ─────────────────────────────────────────────────────── */
    --font-sans: "Geist", "SF Pro Display", -apple-system, system-ui, sans-serif;
    --font-mono: "Geist Mono", "SF Mono", ui-monospace, monospace;
    
    /* Refined type scale */
    --text-xs: 0.75rem;    /* 12px */
    --text-sm: 0.8125rem;  /* 13px */
    --text-base: 1.0625rem; /* 17px - comfortable default reading */
    --text-lg: 1.125rem;   /* 18px */
    --text-xl: 1.375rem;   /* 22px */
    --text-2xl: 1.75rem;   /* 28px */
    --text-3xl: 2.25rem;   /* 36px */

    /* ─── Responsive Line Height (gwern-inspired) ───────────────────────────── */
    --line-height-prose: 1.5; /* Default, overridden by media queries below */

    /* ─── Light Mode Colors ──────────────────────────────────────────────── */
    /* Warm paper tones instead of pure white */
    --color-bg: oklch(99% 0.002 80);
    --color-surface: oklch(97.5% 0.003 80);
    --color-surface-raised: oklch(100% 0 0);
    
    /* Warm gray borders */
    --color-border: oklch(92% 0.004 80);
    --color-border-strong: oklch(85% 0.006 80);

    /* Ink-like text hierarchy */
    --color-text: oklch(18% 0.01 80);
    --color-text-secondary: oklch(40% 0.01 80);
    --color-text-tertiary: oklch(55% 0.008 80);

    /* Accent: Warm amber/copper - distinctive, not generic blue/teal */
    --color-accent: oklch(55% 0.16 55);
    --color-accent-hover: oklch(48% 0.18 55);
    --color-accent-subtle: oklch(96% 0.03 55);
    --color-accent-text: oklch(45% 0.14 55);

    /* Selection: Soft lavender */
    --color-selected-bg: oklch(95% 0.03 280);
    --color-selected-border: oklch(80% 0.08 280);
    
    /* Focus: Amber ring */
    --color-focus-ring: oklch(70% 0.15 55);
    --color-focused-bg: oklch(97% 0.02 55);

    /* Semantic */
    --color-danger: oklch(55% 0.2 25);
    --color-danger-subtle: oklch(96% 0.04 25);
    --color-success: oklch(50% 0.14 145);

    /* ─── Sidebar Dimensions (shared for layout) ─────────────────────────── */
    --sidebar-width: 240px;
    --sidebar-min-width: 180px;
    --sidebar-max-width: 400px;

    /* ─── Spacing ────────────────────────────────────────────────────────── */
    --space-px: 1px;
    --space-0: 0;
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;
    --space-12: 48px;

    /* ─── Block Layout (threading-aware) ─────────────────────────────────── */
    --block-line-height: 1.5;
    /* Responsive indent — tight on narrow viewports, roomier on wide screens
       so deep nesting stays legible without eating the whole measure. */
    --block-indent: clamp(14px, 1.6vw, 22px);
    /* Bullet scales with surrounding text (Logseq pattern). Headings get a
       proportionally larger bullet which reads as deliberate hierarchy. */
    --block-bullet-size: 0.32em;
    --block-bullet-padding: 0.5em;  /* Click target padding around bullet */
    --block-bullet-left: 5px;       /* Bullet position from block left edge */
    --block-thread-width: 1px;
    --block-thread-color: var(--color-border);
    --block-thread-color-active: var(--color-accent);
    --block-thread-radius: 8px;            /* Curve radius for threading */

    /* ─── Radius ─────────────────────────────────────────────────────────── */
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 8px;
    --radius-xl: 12px;

    /* ─── Shadows (subtle, warm) ─────────────────────────────────────────── */
    --shadow-sm: 0 1px 2px oklch(20% 0.02 80 / 8%);
    --shadow-md: 0 2px 8px oklch(20% 0.02 80 / 10%), 0 1px 2px oklch(20% 0.02 80 / 6%);
    --shadow-lg: 0 8px 24px oklch(20% 0.02 80 / 12%), 0 2px 6px oklch(20% 0.02 80 / 8%);
    --shadow-dialog: 0 16px 48px oklch(20% 0.02 80 / 20%), 0 4px 12px oklch(20% 0.02 80 / 10%);
  }

  html {
    color-scheme: light;
  }

  /* Responsive line-height scaling (gwern-inspired progressive typography) */
  @media (max-width: 649px) {
    :root { --line-height-prose: 1.45; }
  }
  @media (min-width: 650px) and (max-width: 999px) {
    :root { --line-height-prose: 1.5; }
  }
  @media (min-width: 1000px) and (max-width: 1199px) {
    :root { --line-height-prose: 1.55; }
  }
  @media (min-width: 1200px) {
    :root { --line-height-prose: 1.6; }
  }

  body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.6;
    color: var(--color-text);
    background: var(--color-bg);
    min-height: 100dvh;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    font-kerning: normal;
    font-optical-sizing: auto;
    font-synthesis: style;
    font-feature-settings:
      "kern" 1,
      "liga" 1,
      "calt" 1,
      "ss01",
      "ss02",
      "cv01";
  }

  /* Selection: soft amber wash, preserves text color for long runs */
  ::selection {
    background: oklch(90% 0.05 55 / 0.9);
    color: var(--color-text);
  }

  ::placeholder {
    color: var(--color-text-tertiary);
  }

  /* Rounded focus ring via box-shadow so radii are respected */
  :focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--color-bg), 0 0 0 4px var(--color-focus-ring);
    border-radius: var(--radius-sm);
  }

  /* Scrollbar styling */
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }

  ::-webkit-scrollbar-track {
    background: transparent;
  }

  ::-webkit-scrollbar-thumb {
    background: var(--color-border-strong);
    border-radius: 4px;
  }

  ::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-tertiary);
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   COMPONENTS LAYER
   ═══════════════════════════════════════════════════════════════════════════ */
@layer components {

  /* ─────────────────────────────────────────────────────────────────────────
     Typography
     ───────────────────────────────────────────────────────────────────────── */
  h1, h2, h3 {
    font-family: var(--font-sans);
    font-weight: 600;
    letter-spacing: -0.025em;
    color: var(--color-text);
  }

  h1 { 
    font-size: var(--text-2xl); 
    font-weight: 700;
    letter-spacing: -0.03em;
  }
  h2 { font-size: var(--text-lg); }
  h3 { font-size: var(--text-base); }

  p {
    color: var(--color-text-secondary);
  }

  code {
    font-family: var(--font-mono);
    font-size: 0.875em;
    background: var(--color-surface);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    color: var(--color-accent-text);
    /* Mono + lining/tabular digits — code reads as data, not prose */
    font-variant-numeric: tabular-nums lining-nums;
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Layout
     ───────────────────────────────────────────────────────────────────────── */
  .app {
    display: flex;
    min-height: 100vh;
  }

  /* Wrapper takes remaining space after sidebar */
  .main-wrapper {
    flex: 1;
    min-width: 0;
    display: flex;
    justify-content: center;
    overflow-x: hidden;
  }

  .main-content {
    width: 100%;
    /* Editorial measure — ~72ch keeps line length in the 60–80ch reading zone
       while still giving wide screens room for deeply nested blocks. */
    max-width: clamp(640px, 72ch, 920px);
    padding: 1rem 1.5rem; /* Vertical spacing + horizontal breathing room */
    container-type: inline-size;
    container-name: main;
  }

  .app--embed {
    display: block;
    min-height: 0;
    background: transparent;
  }

  .main-wrapper--embed {
    justify-content: stretch;
    overflow: visible;
  }

  .main-content--embed {
    max-width: none;
    padding: 0;
  }

  .embed-shell {
    padding: 12px 14px 10px;
    background: white;
    border: 1px solid oklch(92% 0.01 250);
    border-radius: 18px;
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.06);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Reading mode — slightly larger type, optimized measure, sidebar hidden.
     Intentionally close to the default scale so the transition is even,
     not jarring. Tighter line-height keeps the rhythm balanced with bigger type.
     ───────────────────────────────────────────────────────────────────────── */
  .app.reading-mode .main-content {
    max-width: clamp(560px, 66ch, 780px);
    font-size: 1.15rem;
    line-height: 1.6;
    padding-top: 2rem;
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Floating controls (bottom-right)
     ───────────────────────────────────────────────────────────────────────── */
  .floating-controls {
    position: fixed;
    bottom: 16px;
    right: 16px;
    display: flex;
    gap: 8px;
    z-index: 50;
  }

  .floating-btn {
    width: 26px;
    height: 26px;
    border-radius: 999px;
    border: 1px solid oklch(92% 0.005 250);
    background: oklch(99% 0.002 250);
    color: oklch(60% 0.02 260);
    font-size: 11.5px;
    font-weight: 500;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 2px oklch(0% 0 0 / 0.04);
    transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
  }

  .floating-btn:hover {
    background: var(--color-selected-bg, oklch(95% 0.03 280));
    border-color: var(--color-selected-border, oklch(80% 0.08 280));
    color: oklch(45% 0.1 280);
  }

  .floating-btn.is-active {
    background: var(--color-selected-bg, oklch(95% 0.03 280));
    border-color: var(--color-selected-border, oklch(80% 0.08 280));
    color: oklch(42% 0.12 280);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Hotkeys panel — right-side companion sidebar. Matches left sidebar
     styling tokens so it reads as part of the app chrome, not a modal.
     ───────────────────────────────────────────────────────────────────────── */
  .hotkeys-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 280px;
    z-index: 45;

    background: oklch(98.5% 0.002 250);
    border-left: 1px solid oklch(92% 0.005 250);
    box-shadow: inset 8px 0 16px -12px oklch(0% 0 0 / 0.04);

    display: flex;
    flex-direction: column;
    padding: 10px 16px 12px;
    gap: 8px;
    overflow-y: auto;
    overscroll-behavior: contain;

    font-family: var(--font-body);
    font-size: 12px;
    color: oklch(35% 0.02 250);
  }

  .hotkeys-head {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: oklch(55% 0.015 250);
    padding-bottom: 6px;
    border-bottom: 1px solid oklch(93% 0.005 250);
  }

  .hotkeys-group { display: flex; flex-direction: column; gap: 1px; }

  .hotkeys-group h5 {
    margin: 0 0 2px 0;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: oklch(70% 0.01 250);
  }

  .hotkeys-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 1px 0;
    line-height: 1.3;
  }

  .hotkeys-desc { color: oklch(40% 0.02 250); }

  .hotkeys-keys { display: inline-flex; align-items: center; gap: 2px; flex-shrink: 0; }

  .hotkeys-plus { color: oklch(70% 0.01 250); font-size: 9px; padding: 0 1px; }

  .hotkeys-panel kbd {
    display: inline-block;
    min-width: 20px;
    padding: 2px 7px;
    font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
    font-size: 11.5px;
    font-weight: 500;
    line-height: 1.35;
    text-align: center;
    color: oklch(28% 0.02 250);
    background: oklch(94% 0.005 250);
    border: 1px solid oklch(80% 0.01 250);
    border-radius: 4px;
    box-shadow: 0 1px 0 oklch(85% 0.01 250);
  }

  /* Floating controls stay put even when the hotkeys panel is open —
     they render above it (z-index 50 > 45) so the toggle target doesn't
     move under the user's cursor. */

  /* ─────────────────────────────────────────────────────────────────────────
     Sidebar - Editorial refinement with subtle depth
     ───────────────────────────────────────────────────────────────────────── */
  .sidebar {
    /* Dimensions inherited from :root for layout coordination */
    --sidebar-bg: oklch(98.5% 0.002 250);
    --sidebar-border: oklch(92% 0.005 250);
    --sidebar-text: oklch(35% 0.02 250);
    --sidebar-text-muted: oklch(55% 0.015 250);
    --sidebar-text-faint: oklch(70% 0.01 250);
    --sidebar-hover: oklch(96% 0.005 250);
    --sidebar-active: oklch(93% 0.02 250);
    --sidebar-accent: oklch(55% 0.15 250);

    /* Flex item - doesn't shrink, width controlled by CSS variable */
    flex-shrink: 0;
    width: var(--sidebar-width);
    min-width: var(--sidebar-min-width);
    max-width: var(--sidebar-max-width);
    
    /* Layout */
    height: 100vh;
    position: sticky;
    top: 0;
    overflow-y: auto;
    overflow-x: hidden;
    /* Reserve space for the scrollbar even when absent — prevents horizontal
       layout shift when the page list grows past the fold. */
    scrollbar-gutter: stable;
    /* Don't propagate scroll to the main editor (or bounce the page on
       macOS trackpads) when the sidebar hits its end. */
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    
    /* Styling */
    background: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    font-family: var(--font-body);

    /* Subtle inner shadow for depth */
    box-shadow: inset -8px 0 16px -12px oklch(0% 0 0 / 0.04);
  }

  /* Sidebar resize handle */
  .sidebar-resize-handle {
    position: absolute;
    top: 0;
    right: -3px;
    width: 6px;
    height: 100%;
    cursor: col-resize;
    z-index: 10;
    background: transparent;

    /* Visual indicator on hover */
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 2px;
      width: 2px;
      height: 100%;
      background: var(--color-accent);
      opacity: 0;
      transition: opacity 0.15s ease;
    }

    &:hover::after,
    &.dragging::after {
      opacity: 0.5;
    }

    &.dragging::after {
      opacity: 1;
    }
  }

  /* Prevent text selection during resize */
  body.sidebar-resizing {
    cursor: col-resize !important;
    user-select: none;
  }

  /* ── Storage Section ────────────────────────────────────────────────────── */
  .sidebar-storage {
    padding: var(--space-3);
    border-bottom: 1px solid var(--sidebar-border);
  }

  .storage-connected {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--sidebar-hover);
    border-radius: var(--radius-md);
    font-size: var(--text-xs);
  }

  .storage-icon {
    color: var(--sidebar-text-muted);
    display: flex;
  }

  .storage-name {
    flex: 1;
    color: var(--sidebar-text);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .storage-disconnect {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--sidebar-text-faint);
    cursor: pointer;
    opacity: 0;
    transition: background-color 0.15s ease, color 0.15s ease, opacity 0.15s ease;
  }

  .storage-connected:hover .storage-disconnect {
    opacity: 1;
  }

  .storage-disconnect:hover {
    background: oklch(90% 0.05 25);
    color: oklch(55% 0.2 25);
  }

  .storage-picker {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-3);
    background: transparent;
    border: 1.5px dashed var(--sidebar-border);
    border-radius: var(--radius-md);
    color: var(--sidebar-text-muted);
    font-size: var(--text-sm);
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
  }

  .storage-picker:hover {
    border-color: var(--sidebar-accent);
    color: var(--sidebar-accent);
    background: oklch(from var(--sidebar-accent) l c h / 0.05);
  }

  .storage-picker:disabled {
    cursor: wait;
    opacity: 0.6;
  }

  /* ── Navigation Section (Logseq-style nav links) ─────────────────────────── */
  .sidebar-nav {
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--sidebar-border);
  }

  .sidebar-nav-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background 0.12s ease;
  }

  .sidebar-nav-item:hover {
    background: var(--sidebar-hover);
  }

  .sidebar-nav-item.active {
    background: oklch(95% 0.02 250);
  }

  .nav-label {
    flex: 1;
    font-size: 13px;
    font-weight: 500;
    color: var(--sidebar-text);
  }

  .nav-count {
    font-size: 10px;
    font-weight: 500;
    color: var(--sidebar-text-faint);
    background: var(--sidebar-hover);
    padding: 1px 6px;
    border-radius: 10px;
    min-width: 18px;
    text-align: center;
    margin-left: var(--space-2);
    font-variant-numeric: tabular-nums lining-nums;
    font-feature-settings: "tnum" 1, "lnum" 1;
  }

  /* ── Collapsible Section ───────────────────────────────────────────────── */
  .sidebar-section {
    border-bottom: 1px solid var(--sidebar-border);
  }

  .sidebar-section.collapsed .sidebar-section-content {
    display: none;
  }

  .sidebar-section-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    cursor: pointer;
    user-select: none;
    transition: background 0.1s ease;
  }

  .sidebar-section-header:hover {
    background: var(--sidebar-hover);
  }

  .section-chevron {
    flex-shrink: 0;
    color: var(--sidebar-text-faint);
    display: flex;
    transition: transform 0.15s ease;
  }

  .section-icon {
    flex-shrink: 0;
    color: var(--sidebar-text-muted);
    display: flex;
  }

  /* Favorites section gets gold star */
  .sidebar-section:has(.section-icon svg polygon) .section-icon {
    color: oklch(75% 0.15 85); /* Warm gold */
  }

  .section-title {
    flex: 1;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--sidebar-text-muted);
  }

  .section-count {
    font-size: 10px;
    font-weight: 500;
    color: var(--sidebar-text-faint);
    background: var(--sidebar-hover);
    padding: 1px 6px;
    border-radius: 10px;
    min-width: 18px;
    text-align: center;
    font-variant-numeric: tabular-nums lining-nums;
    font-feature-settings: "tnum" 1, "lnum" 1;
  }

  .section-action {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--sidebar-text-faint);
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease, opacity 0.15s ease;
    opacity: 0;
  }

  .sidebar-section-header:hover .section-action {
    opacity: 1;
  }

  .section-action:hover {
    background: var(--sidebar-hover);
    color: var(--sidebar-accent);
  }

  .sidebar-section-content {
    padding: 0 var(--space-2) var(--space-2);
  }

  /* ── Page List ──────────────────────────────────────────────────────────── */
  .page-list {
    display: flex;
    flex-direction: column;
  }

  .sidebar-page-item {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 5px 8px;
    padding-right: 60px; /* Space for actions */
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background 0.12s ease;
  }

  .sidebar-page-item:hover {
    background: var(--sidebar-hover);
  }

  .sidebar-page-item:hover .page-actions {
    opacity: 1;
  }

  .sidebar-page-item.active {
    background: var(--sidebar-active);
  }

  .sidebar-page-item.active .page-title {
    font-weight: 600;
    color: var(--sidebar-text);
  }

  /* Page icon hidden - cleaner UI */
  .page-icon {
    display: none;
  }

  .page-title {
    flex: 1;
    font-size: 13px;
    font-weight: 450;
    color: var(--sidebar-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: color 0.1s ease;
  }

  .sidebar-page-item:hover .page-title {
    color: var(--sidebar-text);
  }

  /* ── Page Actions (hover-reveal) ─────────────────────────────────────────── */
  .page-actions {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 2px;
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .star-button,
  .delete-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--sidebar-text-faint);
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  .star-button:hover {
    color: oklch(75% 0.18 85); /* Gold */
    background: oklch(95% 0.05 85);
  }

  .star-button.active {
    color: oklch(70% 0.2 85); /* Filled gold star */
    opacity: 1;
  }

  /* Always show active star */
  .sidebar-page-item .star-button.active {
    opacity: 1;
  }

  .delete-button:hover {
    color: oklch(55% 0.2 25); /* Red */
    background: oklch(95% 0.05 25);
  }

  /* ── Empty State ────────────────────────────────────────────────────────── */
  .sidebar-empty {
    padding: var(--space-4) var(--space-3);
    text-align: center;
  }

  .sidebar-empty p {
    margin: 0;
    font-size: var(--text-sm);
    color: var(--sidebar-text-muted);
  }

  .sidebar-empty .hint {
    margin-top: var(--space-1);
    font-size: var(--text-xs);
    color: var(--sidebar-text-faint);
  }

  /* ── Trash Section ───────────────────────────────────────────────────────── */
  .trash-hint {
    padding: var(--space-1) var(--space-2);
    font-size: var(--text-xs);
    color: var(--sidebar-text-faint);
    font-style: italic;
  }

  .trash-list {
    display: flex;
    flex-direction: column;
  }

  .sidebar-trash-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 5px 8px;
    border-radius: var(--radius-md);
    transition: background 0.12s ease;
  }

  .sidebar-trash-item:hover {
    background: var(--sidebar-hover);
  }

  .trash-title {
    flex: 1;
    font-size: 13px;
    font-weight: 450;
    color: var(--sidebar-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-decoration: line-through;
    opacity: 0.7;
  }

  .trash-date {
    font-size: var(--text-xs);
    color: var(--sidebar-text-faint);
    white-space: nowrap;
  }

  .trash-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .sidebar-trash-item:hover .trash-actions {
    opacity: 1;
  }

  .restore-button,
  .permanent-delete-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--sidebar-text-faint);
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  .restore-button:hover {
    color: oklch(60% 0.15 145); /* Green for restore */
    background: oklch(95% 0.05 145);
  }

  .permanent-delete-button:hover {
    color: oklch(55% 0.2 25); /* Red for delete */
    background: oklch(95% 0.05 25);
  }

  /* ── Responsive ─────────────────────────────────────────────────────────── */
  @container sidebar (max-width: 200px) {
    .sidebar-page-item {
      padding: 4px 6px;
      padding-right: 50px;
    }

    .page-title {
      font-size: 12px;
    }

    .section-count {
      display: none;
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Outline container
     ───────────────────────────────────────────────────────────────────────── */
  .outline {
    margin-top: var(--space-4);
    outline: none;
    container-type: inline-size;
    container-name: outline;

    &--empty {
      padding: var(--space-12) var(--space-6);
      text-align: center;
      color: var(--color-text-tertiary);
      border: 1px dashed var(--color-border);
      border-radius: var(--radius-lg);
      cursor: text;

      &:hover {
        border-color: var(--color-text-tertiary);
        background: var(--color-surface);
      }
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Blocks - The star of the show
     ───────────────────────────────────────────────────────────────────────── */
  .block {
    position: relative;
    padding: 0 0 0 var(--block-indent);
    /* Subtle vertical rhythm — enough air to separate sibling blocks
       without breaking the outliner's dense feel. */
    margin-block: 0.15em;
    border-radius: var(--radius-sm);
    /* Hairline left bar — only present when focused/selected. Avoids the
       2px-of-transparent gutter that shifted text rightward on every block. */
    border-left: 1px solid transparent;

    &.selected {
      background: var(--color-selected-bg);
      border-left-color: var(--color-selected-border);
    }

    &.focused {
      background: var(--color-focused-bg);
      /* Desaturated accent — present but doesn't compete with text */
      border-left-color: oklch(70% 0.06 55);
    }

    &.editing {
      /* Subtle warm tint to lift the edited block off the page —
         shallower than `--color-focused-bg`, still readable under prose.
         Editing affordance: tint + warm-amber left bar + bold caret. */
      background: oklch(98.5% 0.012 55);
      border-left-color: oklch(65% 0.12 55);
    }

    &.dragging {
      opacity: 0.5;
    }

    /* Bullet accent when focused/selected — softened.
       Solid dot: gentle desaturated amber, not full chroma.
       Hollow ring (bullet-expanded): keep the existing 1px border color
       set globally on .bullet-expanded::before; just tint, don't thicken. */
    &.focused:not(.editing),
    &.selected:not(.editing) {
      & .block-bullet::before {
        background: oklch(65% 0.09 55);
      }

      & .block-bullet.bullet-expanded::before {
        background: transparent;
        border-color: oklch(65% 0.09 55);
      }
    }

    &.editing .block-bullet.bullet-dot::before {
      background: var(--color-border-strong);
    }
  }

  .block-content {
    font-family: var(--font-sans);
    font-size: inherit;
    line-height: var(--block-line-height);
    color: var(--color-text);
    outline: none;
    min-height: calc(var(--block-line-height) * 1em);
    display: block;
    width: 100%;
    /* Thin bar, warmer than prose. Enough chroma to catch the eye
       without the block-shape's traffic-cone quality. */
    caret-color: oklch(52% 0.16 55);

    /* Editorial typography (gwern-inspired) */
    font-variant-numeric: oldstyle-nums proportional-nums; /* Elegant prose numerals */
    hanging-punctuation: first last;
    /* Better line breaking — avoids orphans/rivers without the re-balance cost
       of `text-wrap: balance` on long content. Safe fallback to `wrap`. */
    text-wrap: pretty;
    /* Logseq pattern — preserve user-typed whitespace (tabs, runs of spaces)
       and break long unbroken tokens (URLs, hashes) so they don't blow the
       measure. `anywhere` is needed for soft-break inside long words; plain
       `break-word` only breaks at hyphens. */
    white-space: pre-wrap;
    overflow-wrap: anywhere;

    /* Suppress the global focus ring on contenteditable — the .editing
       background + bullet accent already signal active state. The ring
       is for buttons, links, and other discrete controls. */
    &:focus,
    &:focus-visible {
      outline: none;
      box-shadow: none;
    }

    &:empty::before {
      content: "\00a0";
      color: transparent;
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Block Quotes
     ───────────────────────────────────────────────────────────────────────── */
  blockquote.block-content {
    padding: var(--space-3) var(--space-4);
    border-left: 3px solid var(--color-accent);
    background: var(--color-accent-subtle);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    font-style: italic;
    color: var(--color-text-secondary);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Headings in blocks
     ───────────────────────────────────────────────────────────────────────── */
  h1.block-content {
    font-size: var(--text-2xl);
    font-weight: 700;
    line-height: 1.3;
    letter-spacing: -0.03em;
    margin-top: var(--space-3);
    margin-bottom: var(--space-2);
    padding-bottom: var(--space-2);
    /* Editorial section break (Logseq pattern, softened) */
    border-bottom: 1px solid var(--color-border);
    color: var(--color-text);
  }

  h2.block-content {
    font-size: var(--text-xl);
    font-weight: 600;
    line-height: 1.35;
    letter-spacing: -0.02em;
    margin-top: var(--space-3);
    margin-bottom: var(--space-1);
    padding-bottom: var(--space-1);
    border-bottom: 1px solid var(--color-border);
    color: var(--color-text);
  }

  h3.block-content {
    font-size: var(--text-lg);
    font-weight: 600;
    line-height: 1.4;
    letter-spacing: -0.015em;
    color: var(--color-text);
  }

  h4.block-content {
    font-size: var(--text-base);
    font-weight: 600;
    line-height: 1.5;
    color: var(--color-text);
  }

  h5.block-content {
    font-size: var(--text-sm);
    font-weight: 600;
    line-height: 1.5;
    color: var(--color-text-secondary);
  }

  h6.block-content {
    font-size: var(--text-xs);
    font-weight: 600;
    line-height: 1.5;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-text-tertiary);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Inline Images
     ───────────────────────────────────────────────────────────────────────── */
  .inline-image {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    margin: var(--space-2) 0;
    display: block;

    /* Subtle shadow for depth */
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

    /* Smooth loading transition */
    opacity: 1;
    transition: opacity 0.2s ease;

    /* Constrain large images */
    max-height: 400px;
    object-fit: contain;
  }

  /* Loading state - hidden until src is set */
  .inline-image:not([src]) {
    opacity: 0;
    height: 0;
    margin: 0;
  }

  /* When used inline with text */
  .block-content .inline-image {
    display: inline-block;
    vertical-align: middle;
    max-height: 1.5em;
    margin: 0 var(--space-1);
    border-radius: var(--radius-sm);
    box-shadow: none;
  }

  /* Standalone image (whole block is just an image) */
  .block-content:has(> .inline-image:only-child) .inline-image {
    display: block;
    max-height: 400px;
    margin: var(--space-2) 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }

  /* Multiple images - gallery layout */
  .block-content:has(.inline-image + .inline-image) {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    align-items: flex-start;
  }

  .block-content:has(.inline-image + .inline-image) .inline-image {
    display: block;
    max-width: calc(50% - var(--space-1));
    max-height: 200px;
    object-fit: cover;
    margin: 0;
    border-radius: var(--radius-md);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }

  /* 3+ images - smaller thumbnails */
  .block-content:has(.inline-image + .inline-image + .inline-image) .inline-image {
    max-width: calc(33.333% - var(--space-2));
    max-height: 150px;
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Block-Level Images (Image Blocks)
     Single image per block - replaces inline image pattern
     ───────────────────────────────────────────────────────────────────────── */

  /* Image block container */
  .block.image-block {
    padding: var(--space-1) 0;
  }

  .image-block-content {
    display: flex;
    align-items: flex-start;
    padding: var(--space-1) 0;
  }

  .image-block-content.focused {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: var(--radius-md);
  }

  .block-image {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    display: block;

    /* Subtle shadow for depth */
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

    /* Smooth loading transition */
    opacity: 1;
    transition: opacity 0.2s ease;

    /* Constrain large images */
    max-height: 500px;
    object-fit: contain;
  }

  /* Loading state - hidden until src is set */
  .block-image:not([src]) {
    opacity: 0;
    height: 0;
    margin: 0;
  }

  /* Selected image block */
  .block.image-block.selected .block-image {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  /* Image resize handle */
  .image-block-content {
    position: relative;
    display: inline-block;
  }

  .image-resize-handle {
    position: absolute;
    right: -4px;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 40px;
    background: var(--accent);
    border-radius: 4px;
    cursor: ew-resize;
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .image-block-content:hover .image-resize-handle,
  .image-block-content.focused .image-resize-handle {
    opacity: 0.7;
  }

  .image-resize-handle:hover {
    opacity: 1 !important;
  }

  /* During resize - show cursor globally */
  body.resizing-image {
    cursor: ew-resize !important;
  }

  body.resizing-image * {
    cursor: ew-resize !important;
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Lightbox (Fullscreen Image View)
     ───────────────────────────────────────────────────────────────────────── */

  /* Prevent body scroll when lightbox open */
  body.lightbox-open {
    overflow: hidden;
  }

  .lightbox-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    cursor: zoom-out;
    animation: lightbox-fade-in 0.2s ease;
  }

  @keyframes lightbox-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  .lightbox-content {
    position: relative;
    max-width: 95vw;
    max-height: 95vh;
    cursor: default;
  }

  .lightbox-image {
    max-width: 95vw;
    max-height: 95vh;
    object-fit: contain;
    border-radius: var(--radius-md);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  }

  .lightbox-close {
    position: absolute;
    top: -40px;
    right: 0;
    width: 36px;
    height: 36px;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s ease;
  }

  .lightbox-close:hover {
    background: rgba(255, 255, 255, 0.2);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Tweet Embed
     ───────────────────────────────────────────────────────────────────────── */
  .tweet-embed {
    display: inline-flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-3) var(--space-4);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    font-size: var(--text-sm);
    text-decoration: none;
    color: inherit;
    max-width: 340px;

    &:hover {
      border-color: var(--color-accent);
      background: var(--color-accent-subtle);
    }
  }

  .tweet-embed-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .tweet-icon {
    font-weight: 700;
    font-size: var(--text-base);
  }

  .tweet-author {
    font-weight: 600;
    color: var(--color-text);
  }

  .tweet-embed-body {
    color: var(--color-text-secondary);
    font-size: var(--text-sm);
    line-height: 1.45;
  }

  .tweet-embed-footer {
    font-size: var(--text-xs);
    color: var(--color-accent-text);
    margin-top: var(--space-1);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Video Embed
     ───────────────────────────────────────────────────────────────────────── */
  .video-embed {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
  }

  .video-embed-header {
    display: flex;
    align-items: center;
    gap: var(--space-1);
  }

  .video-icon {
    font-size: var(--text-base);
  }

  .video-link {
    color: var(--color-accent-text);
    text-decoration: none;
    font-size: var(--text-xs);
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    &:hover {
      text-decoration: underline;
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Block Children
     ───────────────────────────────────────────────────────────────────────── */
  .block-children {
    position: relative;
    margin-left: 0;
  }

  @container outline (max-width: 400px) {
    .block-children {
      padding-left: calc(var(--block-bullet-left) + 2px);
    }

    .block {
      padding-left: calc(var(--block-indent) - 4px);
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Page Title (editable, matches journal-title style)
     ───────────────────────────────────────────────────────────────────────── */
  .page-title-header {
    margin-bottom: var(--space-2);
  }

  h1.page-title-display {
    font-size: var(--text-xl);
    font-weight: 600;
    color: oklch(45% 0.15 250);
    margin: 0;
    cursor: text;
    padding: var(--space-1) 0;
    border-radius: var(--radius-sm);
    transition: background 0.1s ease;

    &:hover {
      background: var(--color-surface);
    }
  }

  .page-title-input {
    font-size: var(--text-xl);
    font-weight: 600;
    color: oklch(45% 0.15 250);
    width: 100%;
    padding: var(--space-1) var(--space-2);
    margin: 0;
    border: 2px solid var(--color-accent);
    border-radius: var(--radius-sm);
    background: var(--color-surface-raised);
    outline: none;
    box-shadow: 0 0 0 3px var(--color-accent-subtle);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Block bullets - Refined, minimal

     Positioned to align with threading lines via shared variables.
     ───────────────────────────────────────────────────────────────────────── */
  .block-bullet {
    position: absolute;
    left: var(--block-bullet-left);
    /* Center the wrapper (bullet + padding) in the line height */
    top: calc((var(--block-line-height) * 1em - var(--block-bullet-size) - var(--block-bullet-padding)) / 2);
    width: calc(var(--block-bullet-size) + var(--block-bullet-padding));
    height: calc(var(--block-bullet-size) + var(--block-bullet-padding));
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;

    &:hover::before {
      transform: scale(1.3);
    }

    &::before {
      content: "";
      width: var(--block-bullet-size);
      height: var(--block-bullet-size);
      border-radius: 50%;
      background: var(--color-text-tertiary);
      transition: transform 0.1s ease;
    }

    &.bullet-collapsed::before {
      width: calc(var(--block-bullet-size) + 0.2em);
      height: calc(var(--block-bullet-size) + 0.2em);
      /* Distinct from leaf dots (warm gray) and from focused/selected accents —
         pulls from the lavender 280-hue selection family but saturated enough
         to read as its own signal: "collapsed parent has hidden children". */
      background: oklch(55% 0.14 280);
    }

    &.bullet-expanded::before {
      width: calc(var(--block-bullet-size) + 0.125em);
      height: calc(var(--block-bullet-size) + 0.125em);
      background: transparent;
      /* Hairline ring — 1px reads as outlined dot, 1.5px reads as button */
      border: 1px solid var(--color-text-tertiary);
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Page & Block refs
     ───────────────────────────────────────────────────────────────────────── */
  .page-ref {
    color: var(--color-accent-text);
    font-weight: 500;
    cursor: pointer;

    /* Lining numerals for links (avoid descender clash with underline) */
    font-variant-numeric: lining-nums;

    /* Animated underline (gwern-style) */
    text-decoration: none;
    background-image: linear-gradient(to right, currentColor 0%, currentColor 100%);
    background-size: 0% 1px;
    background-position: 0 100%;
    background-repeat: no-repeat;
    transition: background-size 0.25s ease, color 0.15s ease;

    &:hover {
      background-size: 100% 1px;
    }
  }

  .markdown-link,
  .evo-link {
    color: var(--color-accent-text);
    text-decoration: underline;
    text-decoration-color: color-mix(in oklch, var(--color-accent-text) 45%, transparent);
    text-underline-offset: 0.14em;

    &:hover {
      text-decoration-color: currentColor;
    }
  }

  /* Dead-link styling: evo://page/ to a page that doesn't exist yet.
     Click still navigates (the :navigate-to-page intent creates the page),
     but the muted color signals the missing target. */
  .evo-page-link--missing {
    color: var(--color-text-tertiary);
    text-decoration-style: dashed;
    text-decoration-color: color-mix(in oklch, var(--color-text-tertiary) 55%, transparent);
  }

  .evo-page-card {
    margin: var(--space-1) 0;
  }

  .evo-page-card-link {
    display: block;
    padding: 12px 14px;
    border: 1px solid color-mix(in oklch, var(--color-accent-text) 18%, var(--color-border));
    border-radius: var(--radius-lg);
    background:
      linear-gradient(180deg,
                      color-mix(in oklch, var(--color-accent-subtle) 70%, white) 0%,
                      color-mix(in oklch, var(--color-surface-raised) 88%, var(--color-accent-subtle)) 100%);
    box-shadow: var(--shadow-sm);
    text-decoration: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;

    &:hover {
      border-color: color-mix(in oklch, var(--color-accent-text) 36%, var(--color-border-strong));
      box-shadow: var(--shadow-md);
      transform: translateY(-1px);
    }
  }

  .evo-page-card-label {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-text-tertiary);
    margin-bottom: 4px;
  }

  .evo-page-card-title {
    color: var(--color-text);
    font-size: var(--text-lg);
    font-weight: 600;
    line-height: 1.2;
  }

  .evo-page-card-meta {
    margin-top: 6px;
    color: var(--color-accent-text);
    font-size: var(--text-sm);
    font-weight: 500;
  }

  .evo-page-card-excerpt {
    margin-top: 8px;
    color: var(--color-text-secondary);
    font-size: var(--text-sm);
    line-height: 1.45;
  }

  .block-ref {
    background: var(--color-accent-subtle);
    color: var(--color-accent-text);
    padding: 1px 5px;
    border-radius: var(--radius-sm);
    font-size: 0.95em;
    cursor: pointer;
    font-variant-numeric: lining-nums;
    transition: background 0.15s ease, box-shadow 0.15s ease;

    &:hover {
      background: oklch(from var(--color-accent-subtle) calc(l - 0.03) c h);
      box-shadow: 0 1px 2px oklch(0% 0 0 / 0.08);
    }

    &-missing {
      background: var(--color-danger-subtle);
      color: var(--color-danger);
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Buttons
     ───────────────────────────────────────────────────────────────────────── */
  button {
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    font-weight: 500;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface-raised);
    color: var(--color-text);
    text-box: trim-both cap alphabetic;

    &:hover:not(:disabled) {
      background: var(--color-surface);
      border-color: var(--color-border-strong);
    }

    &:disabled {
      opacity: 0.4;
      cursor: not-allowed;
    }

    &.primary {
      background: var(--color-accent);
      color: white;
      border-color: var(--color-accent);

      &:hover:not(:disabled) {
        background: var(--color-accent-hover);
      }
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Keyboard shortcuts
     ───────────────────────────────────────────────────────────────────────── */
  kbd {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    font-weight: 500;
    padding: 2px 6px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text-secondary);
    box-shadow: var(--shadow-sm);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Forms
     ───────────────────────────────────────────────────────────────────────── */
  input, textarea, select {
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-surface-raised);
    color: var(--color-text);

    &:focus {
      outline: none;
      border-color: var(--color-accent);
      box-shadow: 0 0 0 3px var(--color-accent-subtle);
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Hotkeys reference
     ───────────────────────────────────────────────────────────────────────── */
  .hotkeys-reference {
    margin-top: var(--space-10);
    padding-top: var(--space-6);
    border-top: 1px solid var(--color-border);

    & h4 {
      font-size: var(--text-xs);
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.1em;
      color: var(--color-text-tertiary);
      margin-bottom: var(--space-4);
    }
  }

  .hotkeys-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: var(--space-2);
  }

  .hotkey-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-2) 0;
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Devtools panel
     ───────────────────────────────────────────────────────────────────────── */
  .devtools-panel {
    margin-top: var(--space-10);
    padding: var(--space-4);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Drop indicators
     ───────────────────────────────────────────────────────────────────────── */
  .drop-indicator {
    position: absolute;
    left: var(--space-6);
    right: 0;
    height: 2px;
    background: var(--color-accent);
    pointer-events: none;
    border-radius: 1px;

    &--above { top: 0; }
    &--below { bottom: 0; }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Autocomplete popup
     ───────────────────────────────────────────────────────────────────────── */
  .autocomplete-popup {
    margin: 0;
    padding: 0;
    border: none;
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    min-width: 220px;
    max-width: 340px;
    max-height: 320px;
    overflow: visible;
  }

  .autocomplete-items {
    overflow-y: auto;
    max-height: 320px;
    padding: var(--space-1);
  }

  .autocomplete-item {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    color: var(--color-text);
    cursor: pointer;
    border-radius: var(--radius-md);

    &:hover {
      background: var(--color-surface);
    }

    &.selected {
      background: var(--color-accent-subtle);
      color: var(--color-accent-text);
    }

    & mark {
      background: transparent;
      color: var(--color-accent);
      font-weight: 600;
    }

    &.create-new {
      border-top: 1px solid var(--color-border);
      margin-top: var(--space-1);
      padding-top: var(--space-3);
      color: var(--color-text-secondary);

      &.selected {
        background: var(--color-accent-subtle);
        color: var(--color-accent-text);
      }
    }
  }

  .autocomplete-empty {
    padding: var(--space-4) var(--space-5);
    font-size: var(--text-sm);
    color: var(--color-text-tertiary);
    text-align: center;
  }

  .create-new-label {
    display: flex;
    align-items: center;
    gap: var(--space-1);
  }

  .create-icon {
    font-weight: bold;
    font-size: var(--text-base);
    color: var(--color-accent);
  }

  /* ── Slash Command Items ─────────────────────────────────────────────────── */
  .autocomplete-item.command-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
  }

  .command-icon {
    font-size: var(--text-base);
    width: 24px;
    text-align: center;
    flex-shrink: 0;
  }

  .command-content {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
  }

  .command-name {
    font-weight: 500;
    color: var(--color-text);
  }

  .command-description {
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .autocomplete-item.command-item.selected {
    .command-name {
      color: var(--color-accent-text);
    }
    .command-description {
      color: var(--color-text-secondary);
    }
  }

  /* ─────────────────────────────────────────────────────────────────────────
     Quick Switcher - Native <dialog>
     ───────────────────────────────────────────────────────────────────────── */
  dialog.quick-switcher {
    max-width: 560px;
    width: 100%;
    max-height: 70vh;
    margin-top: 15vh;
    padding: 0;
    border: none;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-dialog);
    overflow: hidden;
    background: var(--color-surface-raised);

    &::backdrop {
      background: oklch(0% 0 0 / 50%);
      backdrop-filter: blur(4px);
    }
  }

  .quick-switcher-content {
    display: flex;
    flex-direction: column;
    max-height: 70vh;
  }

  .quick-switcher-input-wrapper {
    padding: var(--space-4);
    border-bottom: 1px solid var(--color-border);
  }

  .quick-switcher-input {
    width: 100%;
    font-size: var(--text-base);
    padding: var(--space-3) var(--space-4);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-bg);

    &:focus {
      border-color: var(--color-accent);
      box-shadow: 0 0 0 4px var(--color-accent-subtle);
    }
  }

  .quick-switcher-results {
    padding: var(--space-2);
    overflow-y: auto;
    flex: 1;
  }

  .quick-switcher-empty {
    padding: var(--space-6);
    text-align: center;
    color: var(--color-text-tertiary);
  }

  .quick-switcher-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    cursor: pointer;

    &:hover {
      background: var(--color-surface);
    }

    &.selected {
      background: var(--color-accent-subtle);
    }
  }

  .quick-switcher-icon {
    font-size: var(--text-base);
    flex-shrink: 0;
  }

  .quick-switcher-title {
    font-size: var(--text-sm);
    color: var(--color-text);

    & mark {
      background: transparent;
      color: var(--color-accent);
      font-weight: 600;
    }
  }

  /* ── Toast Notification ─────────────────────────────────────────────────── */
  /* Modern toast using Popover API with CSS animations */

  .notification {
    position: fixed;
    bottom: var(--space-6);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-dialog);
    font-size: var(--text-sm);
    font-weight: 500;
    z-index: 2147483647;
    border: none;
    margin: 0;
    animation: notification-slide-up 0.3s cubic-bezier(0.16, 1, 0.3, 1);

    /* Popover API: rendered in top layer automatically */
    &:popover-open {
      display: flex;
    }
  }

  @keyframes notification-slide-up {
    from {
      opacity: 0;
      transform: translateX(-50%) translateY(100%);
    }
    to {
      opacity: 1;
      transform: translateX(-50%) translateY(0);
    }
  }

  /* Type variants */
  .notification--success {
    background: oklch(95% 0.05 145);
    color: oklch(35% 0.15 145);
    border: 1px solid oklch(85% 0.1 145);
  }

  .notification--error {
    background: oklch(95% 0.05 25);
    color: oklch(40% 0.15 25);
    border: 1px solid oklch(85% 0.1 25);
  }

  .notification--warning {
    background: oklch(95% 0.05 85);
    color: oklch(40% 0.12 85);
    border: 1px solid oklch(85% 0.1 85);
  }

  .notification--info {
    background: oklch(95% 0.03 250);
    color: oklch(35% 0.1 250);
    border: 1px solid oklch(85% 0.06 250);
  }

  .notification__message {
    flex: 1;
  }

  .notification__action {
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-md);
    font-size: var(--text-xs);
    font-weight: 600;
    cursor: pointer;
    background: transparent;
    border: 1px solid currentColor;
    color: inherit;
    opacity: 0.8;
    transition: opacity 0.15s;

    &:hover {
      opacity: 1;
    }
  }

  .notification__dismiss {
    padding: var(--space-1);
    line-height: 1;
    font-size: var(--text-base);
    cursor: pointer;
    background: transparent;
    border: none;
    color: inherit;
    opacity: 0.5;
    transition: opacity 0.15s;

    &:hover {
      opacity: 1;
    }
  }

  /* ── Journals View ────────────────────────────────────────────────────────── */
  .journals-view {
    max-width: 800px;
    margin: 0 auto;
    padding: 0;
  }

  .journals-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: 1px solid oklch(90% 0.01 250);
  }

  .journals-back {
    padding: var(--space-2) var(--space-3);
    background: transparent;
    border: 1px solid oklch(85% 0.02 250);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    color: var(--text-muted);
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;

    &:hover {
      background: oklch(95% 0.02 250);
      color: var(--text-body);
    }
  }

  .journals-title {
    flex: 1;
    font-size: var(--text-xl);
    font-weight: 600;
    color: var(--text-heading);
    margin: 0;
  }

  .journals-count {
    font-size: var(--text-sm);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums lining-nums;
    font-feature-settings: "tnum" 1, "lnum" 1;
  }

  .journals-list {
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .journal-item {
    padding: var(--space-2) var(--space-2);
    border-bottom: 1px solid oklch(92% 0.01 250);
    border-radius: var(--radius-lg);
    margin: 0 calc(-1 * var(--space-2));
    transition: background 0.15s ease, box-shadow 0.15s ease;

    &:last-child,
    &.journal-last {
      border-bottom: none;
    }

    /* Clickable journal item with hover affordance */
    &.journal-clickable {
      cursor: pointer;

      &:hover {
        background: oklch(97% 0.02 250);
        box-shadow: 0 2px 8px oklch(0% 0 0 / 0.04);

        .journal-title {
          color: oklch(35% 0.18 250);
        }
      }

      &:active {
        background: oklch(95% 0.03 250);
      }
    }
  }

  .journal-title {
    font-size: var(--text-lg);
    font-weight: 600;
    color: oklch(45% 0.15 250);
    margin: 0 0 var(--space-1) 0;
    transition: color 0.15s ease;
  }

  .journal-blocks {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding-left: var(--space-2);
  }

  .journal-block {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    line-height: 1.6;

    /* Journal list uses simpler inline bullets */
    & .journal-bullet {
      color: var(--text-muted);
      flex-shrink: 0;
      margin-top: 2px;
    }
  }

  .block-text {
    color: var(--text-body);
  }

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

  .journals-empty {
    text-align: center;
    padding: var(--space-12) var(--space-6);
    color: var(--text-muted);

    p {
      margin: 0;
    }

    .hint {
      font-size: var(--text-sm);
      margin-top: var(--space-2);
    }
  }

  /* ── Backlinks Panel (Editorial style from publishing/Acknowledgements) ─── */
  .backlinks-panel {
    margin-top: var(--space-10);
    margin-bottom: 1rem; /* Bottom breathing room */
    padding: var(--space-4) var(--space-5);
    border: 1px solid oklch(88% 0.03 279);
    border-left: 3px solid oklch(68% 0.08 279);
    border-radius: var(--radius-sm);
    background: oklch(98% 0.008 279);
  }

  .backlinks-header {
    display: flex;
    align-items: center;
    gap: 0.5em;
    margin-bottom: var(--space-3);
  }

  .backlinks-ornament {
    font-size: 1.1rem;
    color: oklch(62% 0.08 279);
    line-height: 1;
  }

  .backlinks-title {
    font-size: var(--text-sm);
    font-weight: 700;
    color: oklch(38% 0.06 279);
    margin: 0;
    letter-spacing: 0.02em;
  }

  .backlinks-count {
    background: oklch(92% 0.04 279);
    padding: 2px 8px;
    border-radius: 10px;
    font-size: var(--text-xs);
    font-weight: 600;
    color: oklch(50% 0.08 279);
    font-variant-numeric: tabular-nums lining-nums;
    font-feature-settings: "tnum" 1, "lnum" 1;
  }

  .backlinks-content {
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
    line-height: 1.6;
  }

  .backlink-group {
    margin-bottom: var(--space-4);

    &:last-child {
      margin-bottom: 0;
    }
  }

  .backlink-page-header {
    font-weight: 600;
    font-size: var(--text-sm);
    color: oklch(45% 0.1 279);
    margin-bottom: var(--space-2);
    cursor: pointer;

    &:hover {
      color: oklch(52% 0.12 279);
      text-decoration: underline;
    }
  }

  .backlink-block {
    padding: var(--space-2) var(--space-3);
    margin: var(--space-1) 0;
    background: oklch(96% 0.01 279);
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    line-height: 1.5;
  }

  /* ── Pages Index (Compact table layout) ────────────────────────────────── */
  .pages-index {
    --pages-grid: 1fr 65px 65px 45px 25px;
    --pages-gap: var(--space-3);
    --pages-padding: var(--space-1) var(--space-2);
    max-width: 720px;
    margin: 0 auto;
    padding: var(--space-2) 0;
  }

  /* Summary header */
  .pages-summary {
    display: flex;
    align-items: baseline;
    gap: var(--space-1);
    margin-bottom: var(--space-4);
    padding-bottom: var(--space-2);
    border-bottom: 1px solid var(--color-border);
  }

  .pages-stat {
    font-size: var(--text-lg);
    font-weight: 600;
    color: var(--color-text);
    font-variant-numeric: tabular-nums;
  }

  .pages-stat-label {
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
    margin-right: var(--space-2);
  }

  .pages-sep {
    color: var(--color-border-strong);
    margin: 0 var(--space-1);
  }

  /* Column header row */
  .pages-header-row {
    display: grid;
    grid-template-columns: var(--pages-grid);
    gap: var(--pages-gap);
    padding: var(--pages-padding);
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-text-tertiary);
    border-bottom: 1px solid var(--color-border);
    margin-bottom: var(--space-2);
  }

  .pages-header--created,
  .pages-header--modified,
  .pages-header--words,
  .pages-header--blocks {
    text-align: right;
  }

  /* Section styling */
  .pages-section {
    margin-bottom: var(--space-4);

    &:last-child {
      margin-bottom: 0;
    }
  }

  .pages-section-label {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-text-tertiary);
    margin: var(--space-3) 0 var(--space-1) var(--space-2);
  }

  .pages-section-count {
    font-size: 10px;
    font-weight: 500;
    color: var(--color-text-tertiary);
    opacity: 0.7;
    font-variant-numeric: tabular-nums lining-nums;
    font-feature-settings: "tnum" 1, "lnum" 1;
  }

  /* Section color accents */
  .pages-section--favorites .pages-section-label {
    color: oklch(55% 0.12 85);
  }

  .pages-section--journals .pages-section-label {
    color: oklch(50% 0.08 279);
  }

  /* List container */
  .pages-list {
    display: flex;
    flex-direction: column;
  }

  /* Row */
  .pages-row {
    display: grid;
    grid-template-columns: var(--pages-grid);
    gap: var(--pages-gap);
    align-items: center;
    padding: var(--pages-padding);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.1s ease;
  }

  .pages-row:hover {
    background: var(--color-surface);
  }

  .pages-section--favorites .pages-row:hover {
    background: oklch(97% 0.02 85);
  }

  .pages-section--journals .pages-row:hover {
    background: oklch(97.5% 0.01 279);
  }

  /* Table cells */
  .pages-cell {
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
    font-variant-numeric: tabular-nums;
  }

  .pages-cell--title {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    color: var(--color-text);
    font-weight: 450;
    overflow: hidden;
  }

  .pages-title-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .pages-star {
    color: oklch(70% 0.18 85);
    flex-shrink: 0;
  }

  .pages-cell--created {
    text-align: right;
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
  }

  .pages-cell--modified {
    text-align: right;
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
  }

  .pages-cell--words {
    text-align: right;
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
  }

  .pages-cell--blocks {
    text-align: right;
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
  }

  /* Empty state */
  .pages-empty {
    text-align: center;
    padding: var(--space-10) var(--space-6);
    color: var(--color-text-tertiary);

    p {
      margin: 0;
    }
  }

  .pages-empty-hint {
    font-size: var(--text-sm);
    margin-top: var(--space-2);
  }

}

/* ═══════════════════════════════════════════════════════════════════════════
   UTILITIES LAYER
   ═══════════════════════════════════════════════════════════════════════════ */
@layer utilities {
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  [inert] {
    pointer-events: none;
    user-select: none;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   PRINT STYLESHEET
   Clean layout for printing/exporting notes
   ═══════════════════════════════════════════════════════════════════════════ */
@media print {
  /* Hide non-essential elements */
  .sidebar,
  .quick-switcher,
  .autocomplete-popup,
  .notification,
  .devtools-panel,
  .hotkeys-reference,
  button:not(.print-include) {
    display: none !important;
  }

  /* Reset colors for print */
  :root {
    --color-text: black;
    --color-text-secondary: oklch(30% 0 0);
    --color-text-tertiary: oklch(50% 0 0);
    --color-bg: white;
    --color-surface: white;
    --color-accent-text: black;
  }

  /* Full-width main content */
  .app {
    display: block;
  }

  .main-wrapper,
  .main-content {
    max-width: 100%;
    padding: 0;
  }

  /* Optimize typography for print */
  body {
    font-size: 11pt;
    line-height: 1.5;
    color: black;
    background: white;
  }

  .block-content {
    font-size: 11pt;
  }

  /* Links show URL */
  .page-ref::after {
    content: ' [' attr(data-page-name) ']';
    font-size: 0.85em;
    color: oklch(50% 0 0);
  }

  /* Remove decorative elements */
  .block-bullet {
    &::before {
      background: black !important;
      border-color: black !important;
    }
  }

  /* Prevent orphans and widows */
  .block-content {
    orphans: 3;
    widows: 3;
  }

  /* Avoid breaks inside blocks */
  .block {
    break-inside: avoid;
  }

  /* Page margins */
  @page {
    margin: 2cm;
  }

  @page :first {
    margin-top: 3cm;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   HIDDEN MARKER SPANS
   Visually-hidden spans carrying markdown markers (`**`, `_`, `==`, `~~`)
   around formatted segments so native partial-text copy produces
   round-trippable markdown. aria-hidden on the DOM side keeps screen
   readers from announcing the literal markers.
   ═══════════════════════════════════════════════════════════════════════════ */

.block-content .marker {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  user-select: text; /* ensure selection traversal still sees the span */
}
