:root {
  color-scheme: dark;
  --bg: #080b11;
  --panel: #101722;
  --panel-2: #151f2d;
  --text: #edf4ff;
  --muted: #a9b8ca;
  --line: #2c3b50;
  --accent: #8b5cf6;
  --accent-2: #22d3ee;
  --danger: #fb7185;
  --danger-bg: rgba(251, 113, 133, .1);
  --card: rgba(255, 255, 255, .04);
  --input: rgba(8, 11, 17, .76);
  --shadow: 0 24px 80px rgba(0, 0, 0, .35);
  --paper-bg: #fdf6e3;
  --paper-line: #94b8d0;
  --paper-text: #1e1e1e;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-2: #edf1f7;
  --text: #172033;
  --muted: #526176;
  --line: #ccd5e2;
  --accent: #6d28d9;
  --accent-2: #087f8c;
  --danger: #be123c;
  --danger-bg: #fff1f2;
  --card: rgba(15, 23, 42, .035);
  --input: #ffffff;
  --shadow: 0 24px 80px rgba(40, 55, 80, .14);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: radial-gradient(circle at top left, color-mix(in srgb, var(--accent) 22%, transparent), transparent 35%), var(--bg);
  color: var(--text);
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .45; }
a { color: var(--accent-2); }

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}
.hidden { display: none !important; }
.skip-link {
  position: fixed;
  left: 12px; top: -80px;
  z-index: 100;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-weight: 800;
  transition: top .15s ease;
}
.skip-link:focus { top: 12px; }
:focus-visible { outline: 3px solid var(--accent-2); outline-offset: 3px; }

.offline-banner {
  position: fixed;
  z-index: 90;
  top: max(8px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  padding: 7px 12px;
  border-radius: 999px;
  background: #78350f;
  color: #fff7ed;
  font-size: 13px;
  font-weight: 700;
  box-shadow: var(--shadow);
}

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(320px, 370px) minmax(0, 1fr);
}
.sidebar {
  height: 100vh;
  height: 100dvh;
  position: sticky;
  top: 0;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 90%, transparent);
  backdrop-filter: blur(18px);
  padding: max(18px, env(safe-area-inset-top)) 18px max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
}
.topbar, .dialog-heading, .section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.top-actions, .button-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.eyebrow {
  color: var(--accent-2);
  margin: 0 0 2px;
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .16em;
}
h1 { margin: 0; font-size: 32px; letter-spacing: -.05em; }
h2, h3 { letter-spacing: -.025em; }

.icon-button, .button, .chip, .tag {
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--panel-2);
  color: var(--text);
  transition: transform .15s ease, border-color .15s ease, background .15s ease;
}
.icon-button:hover, .button:hover, .chip:hover, .tag:hover { transform: translateY(-1px); border-color: var(--accent); }
.icon-button { width: 42px; height: 42px; padding: 0; font-size: 23px; line-height: 1; }
.icon-button.small { width: 36px; height: 36px; }
.primary { color: white; background: linear-gradient(135deg, var(--accent), #5b21b6) !important; border-color: transparent !important; }
.button { padding: 9px 13px; font-weight: 750; }
.button.ghost { background: color-mix(in srgb, var(--panel-2) 55%, transparent); }
.button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); background: var(--danger-bg); }
.button.full { width: 100%; }

.search-wrap span { display: block; color: var(--muted); font-size: 13px; margin-bottom: 7px; }
.search-wrap input, .title-input, .tags-input, .body-input, .check-item input[type="text"], select {
  width: 100%;
  border: 1px solid var(--line);
  outline: none;
  background: var(--input);
  color: var(--text);
  border-radius: 14px;
}
.search-wrap input { padding: 12px 13px; }
.search-wrap input:focus, .title-input:focus, .tags-input:focus, .body-input:focus, .check-item input[type="text"]:focus, select:focus { border-color: var(--accent); }

.list-tools { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.filter-row { display: flex; gap: 7px; }
.chip { padding: 7px 10px; color: var(--muted); }
.chip.active { color: var(--text); border-color: var(--accent-2); background: color-mix(in srgb, var(--accent-2) 12%, transparent); }
.sort-control { min-width: 0; }
.sort-control select { max-width: 145px; padding: 7px 25px 7px 8px; font-size: 12px; }
.search-summary { min-height: 17px; color: var(--muted); font-size: 12px; }

.notes-list {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 2px 4px 2px 1px;
  scrollbar-gutter: stable;
}
.note-card {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  overflow: hidden;
}
.note-card.active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 15%, transparent); }
.note-card-main {
  width: 100%;
  display: block;
  border: 0;
  padding: 12px 13px 8px;
  text-align: left;
  color: var(--text);
  background: transparent;
}
.note-card-main h3 { margin: 0 0 6px; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-card-main p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.note-meta { margin-top: 8px; color: var(--muted); font-size: 11px; }
.tag-row { display: flex; flex-wrap: wrap; gap: 5px; padding: 0 13px 11px; }
.tag { padding: 2px 7px; font-size: 11px; color: var(--muted); }
.list-empty { margin: auto; color: var(--muted); text-align: center; padding: 24px; }

.pagination { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 11px; }
.pagination label, .page-controls { display: flex; align-items: center; gap: 6px; }
.pagination select { width: auto; padding: 6px 7px; }
.page-controls .button { min-width: 34px; padding: 5px 8px; }

.editor { min-width: 0; padding: clamp(18px, 4vw, 46px); display: flex; }
.editor-pane, .empty-state {
  width: min(900px, 100%);
  margin: auto;
  background: color-mix(in srgb, var(--panel) 72%, transparent);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
}
.editor-pane { padding: clamp(18px, 3vw, 34px); }
.empty-state { text-align: center; padding: 52px 26px; }
.empty-state .sigil { font-size: 50px; margin-bottom: 12px; }
.empty-state h2 { font-size: clamp(25px, 4vw, 44px); margin: 0 auto 12px; max-width: 640px; }
.empty-state p { color: var(--muted); margin: 0 auto 22px; max-width: 470px; line-height: 1.55; }
.mobile-editor-bar { display: none; }
.editor-actions { display: flex; justify-content: flex-end; gap: 8px; margin-bottom: 20px; flex-wrap: wrap; }
.title-input {
  min-height: 2.35em;
  max-height: 3.6em;
  overflow-y: auto;
  resize: none;
  field-sizing: content;
  font-size: clamp(28px, 4vw, 46px);
  font-weight: 820;
  line-height: 1.12;
  letter-spacing: -.045em;
  border: 0;
  background: transparent;
  padding: 4px 0;
  border-radius: 0;
}
.tags-input { margin: 14px 0; padding: 11px 13px; }
.body-input { min-height: 48vh; padding: 16px; resize: vertical; line-height: 1.65; overflow-wrap: anywhere; white-space: pre-wrap; }

/* Lined paper mode */
body.paper-mode .editor-pane {
  background: var(--paper-bg);
  border-color: color-mix(in srgb, var(--paper-line) 35%, transparent);
}
body.paper-mode .title-input,
body.paper-mode .tags-input {
  background: transparent;
  color: var(--paper-text);
  border-color: color-mix(in srgb, var(--paper-line) 45%, transparent);
}
body.paper-mode .title-input::placeholder,
body.paper-mode .tags-input::placeholder { color: color-mix(in srgb, var(--paper-text) 45%, transparent); }
body.paper-mode .editor-actions .button { background: color-mix(in srgb, var(--paper-bg) 80%, #fff); color: var(--paper-text); }
body.paper-mode .statusbar { color: color-mix(in srgb, var(--paper-text) 55%, transparent); }

/* Lined body textarea */
body.paper-mode .body-input {
  background:
    repeating-linear-gradient(
      transparent,
      transparent 30px,
      var(--paper-line) 30px,
      var(--paper-line) 31px
    );
  background-attachment: local;
  background-color: var(--paper-bg);
  color: var(--paper-text);
  border: 0;
  border-radius: 0;
  padding: 7px 14px;
  line-height: 31px;
  min-height: 48vh;
  font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
}
body.paper-mode .body-input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: 2px; }
body.paper-mode .body-input::placeholder { color: color-mix(in srgb, var(--paper-text) 40%, transparent); }

/* Lined checklist in paper mode */
body.paper-mode .check-item input[type="text"] {
  background: repeating-linear-gradient(transparent, transparent 30px, var(--paper-line) 30px, var(--paper-line) 31px);
  background-attachment: local;
  background-color: var(--paper-bg);
  color: var(--paper-text);
  border: 0;
  border-radius: 0;
  padding: 7px 0;
  line-height: 31px;
}
body.paper-mode .markdown-preview {
  background: var(--paper-bg);
  color: var(--paper-text);
  border-color: color-mix(in srgb, var(--paper-line) 35%, transparent);
}
.markdown-preview {
  min-height: 48vh;
  padding: 16px 20px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--input);
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.markdown-preview h1, .markdown-preview h2, .markdown-preview h3 { margin: 1.2em 0 .45em; }
.markdown-preview p { margin: .65em 0; }
.markdown-preview code { padding: 2px 5px; border-radius: 5px; background: var(--panel-2); }

.checklist-editor { display: flex; flex-direction: column; gap: 12px; }
.checklist-items { display: flex; flex-direction: column; gap: 9px; }
.check-item { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 9px; }
.check-item input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
.check-item input[type="text"] { padding: 11px 13px; }
.check-item input[type="checkbox"]:checked + input[type="text"] { text-decoration: line-through; color: var(--muted); }
.check-item button { width: 36px; height: 36px; border-radius: 11px; border: 1px solid var(--line); background: var(--danger-bg); color: var(--danger); }
.statusbar { margin-top: 16px; display: flex; justify-content: space-between; gap: 8px 14px; flex-wrap: wrap; color: var(--muted); font-size: 12px; }

.dialog {
  width: min(660px, calc(100vw - 28px));
  max-height: calc(100dvh - 28px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 22px;
  color: var(--text);
  background: var(--panel);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, .68); backdrop-filter: blur(4px); }
.dialog-card { padding: 24px; overflow: auto; }
.dialog-card.compact { text-align: center; }
.dialog-card section { padding: 18px 0; border-top: 1px solid var(--line); }
.dialog-card section h3 { margin: 0 0 7px; }
.dialog-heading h2 { margin: 0; }
.muted { color: var(--muted); }
.privacy-note { padding: 12px; border-radius: 12px; background: var(--panel-2); color: var(--muted); font-size: 12px; }
.trash-list { display: flex; flex-direction: column; gap: 8px; }
.trash-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 7px; padding: 8px; border: 1px solid var(--line); border-radius: 12px; }
.trash-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
progress { width: 100%; accent-color: var(--accent); }

.snackbar {
  position: fixed;
  z-index: 110;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(580px, calc(100vw - 28px));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 13px 15px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  box-shadow: var(--shadow);
}
.snackbar button { border: 0; background: transparent; color: var(--accent-2); font-weight: 800; }
.noscript { padding: 30px; text-align: center; background: #7f1d1d; color: white; }
.not-found { max-width: 720px; margin: 10vh auto; }
.icon-button.importing { animation: pulse-import .8s ease infinite; border-color: var(--accent-2); }
@keyframes pulse-import { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .55; transform: scale(.96); } }

@media (max-width: 800px) {
  .app-shell { display: block; min-height: 100dvh; }
  .sidebar {
    position: relative;
    width: 100%;
    height: 100dvh;
    border-right: 0;
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }
  .top-actions { gap: 6px; }
  .icon-button { width: 39px; height: 39px; }
  .list-tools { align-items: stretch; flex-direction: column; }
  .sort-control select { max-width: none; }
  .editor { display: none; min-height: 100dvh; padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left)); }
  body.mobile-editor-open .sidebar { display: none; }
  body.mobile-editor-open .editor { display: flex; }
  .editor-pane, .empty-state { margin: 0; min-height: calc(100dvh - 24px); border-radius: 18px; }
  .editor-pane { padding: 14px; }
  .mobile-editor-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
  .mobile-editor-bar span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 13px; }
  .editor-actions { justify-content: flex-start; margin-bottom: 12px; gap: 6px; }
  .editor-actions .button { padding: 7px 9px; font-size: 13px; }
  .title-input { font-size: clamp(27px, 8vw, 34px); }
  .body-input, .markdown-preview { min-height: calc(100dvh - 300px); }
  .statusbar { flex-direction: column; }
  .trash-row { grid-template-columns: 1fr 1fr; }
  .trash-row span { grid-column: 1 / -1; }
}

@media (max-width: 390px) {
  .topbar { align-items: flex-start; }
  h1 { font-size: 28px; }
  .top-actions { max-width: 190px; justify-content: flex-end; }
  .filter-row { width: 100%; }
  .chip { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: .01ms !important; }
}

/* Paper mode retains readable controls in either system theme. */
body.paper-mode .editor-actions .button.primary { color: white; }
body.paper-mode .markdown-preview code { background: #e8dec4; color: var(--paper-text); }
body.paper-mode .mobile-editor-bar span { color: #526176; }
