:root {
  --bg: #f7f6f2;
  --panel: #ffffff;
  --sidebar: #1f2a27;
  --sidebar-text: #d9e2de;
  --sidebar-muted: #8fa29b;
  --sidebar-active: #2f5d50;
  --text: #1d2220;
  --muted: #6b7470;
  --line: #e4e2dc;
  --accent: #2f7d64;
  --accent-text: #ffffff;
  --selected: #e7f1ec;
  --danger: #b3261e;
  --radius: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161a19;
    --panel: #1d2221;
    --sidebar: #111514;
    --sidebar-text: #d4ddd9;
    --sidebar-muted: #7f908a;
    --sidebar-active: #24463c;
    --text: #e6ebe9;
    --muted: #96a19c;
    --line: #2d3432;
    --accent: #4fae8d;
    --accent-text: #0d1513;
    --selected: #23332e;
    --danger: #f2827a;
    color-scheme: dark;
  }
  .ql-snow .ql-stroke { stroke: var(--text); }
  .ql-snow .ql-fill { fill: var(--text); }
  .ql-snow .ql-picker { color: var(--text); }
  .ql-snow .ql-picker-options { background: var(--panel); }
  .ql-snow .ql-tooltip { background: var(--panel); color: var(--text); border-color: var(--line); box-shadow: none; }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, select { font: inherit; color: inherit; }
button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 6px 12px;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
button.danger { color: var(--danger); }
button.small { padding: 3px 10px; font-size: 13px; }
button.link { border: 0; background: none; color: var(--accent); padding: 4px; }
button.icon { border: 0; background: none; padding: 4px 8px; font-size: 18px; line-height: 1; }
input, select {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 7px 10px;
  min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.muted { color: var(--muted); }
.small-text { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }
.spacer { flex: 1; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--selected); color: var(--muted); }

.boot { display: grid; place-items: center; height: 100%; color: var(--muted); }

/* Sign in */
.auth { display: grid; place-items: center; min-height: 100%; padding: 24px 16px; }
.auth-card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
  display: grid;
  gap: 12px;
}
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card p { margin: 0; }
.auth-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-card input { color: var(--text); font-size: 15px; }

/* Layout */
.app {
  display: grid;
  grid-template-columns: 240px minmax(260px, 340px) 1fr;
  height: 100dvh;
  padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px);
}
.mobile-only { display: none; }

.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-top { padding: 14px; }
.new-note { width: 100%; padding: 9px; }
.nav { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.nav h3 {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--sidebar-muted);
  margin: 16px 8px 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.nav h3 button { border: 0; background: none; color: var(--sidebar-muted); padding: 0 4px; font-size: 16px; }
.nav .stack { font-size: 12px; color: var(--sidebar-muted); margin: 8px 8px 2px 10px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  border: 0;
  background: none;
  color: inherit;
  text-align: left;
  padding: 6px 10px;
  border-radius: 6px;
}
.nav-item:hover { background: rgb(255 255 255 / 0.06); }
.nav-item[aria-current="true"] { background: var(--sidebar-active); color: #fff; }
.nav-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .count { font-size: 12px; color: var(--sidebar-muted); }
.nav-item[aria-current="true"] .count { color: #cfe3da; }
.sidebar-foot {
  border-top: 1px solid rgb(255 255 255 / 0.08);
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.foot-user {
  flex: 1;
  border: 0;
  background: none;
  color: inherit;
  text-align: left;
  padding: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sync-status { font-size: 12px; color: var(--sidebar-muted); white-space: nowrap; }
.sync-status[data-status="offline"], .sync-status[data-status="error"] { color: #f0b37e; }

.list-pane {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.list-head { padding: 14px 14px 10px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.list-title-row { display: flex; align-items: baseline; gap: 8px; }
.list-title-row h2 { margin: 0; font-size: 19px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.list-actions:empty { display: none; }
.list-actions .break { flex-basis: 100%; height: 0; }
.note-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.note-list li {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  display: grid;
  gap: 3px;
}
.note-list li:hover { background: color-mix(in srgb, var(--selected) 50%, transparent); }
.note-list li[aria-selected="true"] { background: var(--selected); box-shadow: inset 3px 0 0 var(--accent); }
.note-list .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-list .snippet {
  color: var(--muted);
  font-size: 13px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.note-list .meta { font-size: 12px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; }
.note-list .empty { cursor: default; color: var(--muted); text-align: center; padding: 40px 16px; }

.editor-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--panel); }
.empty-editor { margin: auto; padding: 24px; text-align: center; }
.editor-wrap { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.editor-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.editor-head select { max-width: 45%; }
.note-title {
  border: 0;
  font-size: 26px;
  font-weight: 700;
  padding: 16px 18px 4px;
  background: transparent;
  border-radius: 0;
}
.note-title:focus-visible { outline: none; }
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 4px 18px 8px; }
.tag-row input { border: 0; padding: 3px 4px; width: 120px; background: transparent; }
.tags { display: contents; }
.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  background: var(--selected);
  border-radius: 99px;
  padding: 2px 4px 2px 10px;
}
.tag button { border: 0; background: none; padding: 0 4px; font-size: 13px; color: var(--muted); }
.source-link { padding: 0 18px 6px; font-size: 13px; color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.editor .ql-toolbar.ql-snow { border: 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 6px 12px; }
.editor .ql-container.ql-snow { border: 0; flex: 1; min-height: 0; overflow-y: auto; font-size: 16px; font-family: inherit; }
.editor .ql-editor { padding: 16px 18px 80px; color: var(--text); }
.editor .ql-editor.ql-blank::before { color: var(--muted); font-style: normal; left: 18px; }
.editor .ql-editor img { max-width: 100%; max-height: 420px; height: auto; object-fit: contain; border-radius: 6px; }
.editor .ql-editor li[data-list="checked"] { color: var(--muted); text-decoration: line-through; }
.attachments { list-style: none; margin: 0; padding: 0 14px; display: flex; flex-wrap: wrap; gap: 8px; }
.attachments:not(:empty) { padding: 10px 14px; border-top: 1px solid var(--line); }
.attachments li {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 4px 4px 10px;
  font-size: 13px;
  max-width: 100%;
}
.attachments a { color: inherit; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Items: photos, scans, sketches and files */
button.active { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
textarea {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 7px 10px;
  resize: vertical;
}
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.add-button { font-weight: 600; }
.add-menu {
  position: fixed;
  z-index: 20;
  display: grid;
  min-width: 200px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px;
  box-shadow: 0 8px 28px rgb(0 0 0 / 0.18);
}
.add-menu button { border: 0; text-align: left; padding: 10px 12px; font-size: 15px; }
.add-menu button:hover, .add-menu button:focus-visible { background: var(--selected); }
.attachments li { padding: 0; border: 0; }
.item-card {
  display: flex;
  gap: 10px;
  align-items: center;
  text-align: left;
  padding: 6px 10px 6px 6px;
  max-width: min(340px, 100%);
}
.thumb {
  width: 44px;
  height: 44px;
  flex: none;
  border-radius: 6px;
  object-fit: cover;
  background: var(--selected);
}
.thumb.icon { display: grid; place-items: center; font-size: 22px; }
.item-text { display: grid; gap: 1px; min-width: 0; }
.item-text strong, .item-text .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-list li.item-row { grid-template-columns: 56px 1fr; align-items: center; column-gap: 12px; }
.note-list li.item-row .thumb { width: 56px; height: 56px; }
.item-preview { max-height: 40vh; width: 100%; object-fit: contain; border-radius: 8px; background: var(--bg); }
.item-form { padding: 0 !important; }

.pending-box {
  display: grid;
  gap: 6px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: color-mix(in srgb, var(--selected) 60%, transparent);
}
.nowrap { white-space: nowrap; text-align: right; }

/* PDFs shown inside a note */
.ql-editor .ql-pdf {
  margin: 10px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  overflow: hidden;
  max-width: 720px;
}
.pdf-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 13px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.pdf-bar .pdf-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pdf-bar button { padding: 2px 8px; font-size: 13px; }
.pdf-page { display: block; width: 100%; height: auto; background: #fff; }
.pdf-message { padding: 24px; text-align: center; color: var(--muted); font-size: 13px; }

/* Camera, scanner and sketch pad */
.dialog.capture { width: min(960px, calc(100vw - 16px)); }
.camera-view video { width: 100%; max-height: 65vh; background: #000; border-radius: 8px; }
.crop-stage { position: relative; display: grid; justify-items: center; touch-action: none; }
.crop-canvas { max-width: 100%; border-radius: 4px; }
.crop-handle {
  position: absolute;
  width: 28px;
  height: 28px;
  margin: -14px 0 0 -14px;
  border-radius: 50%;
  background: rgb(79 174 141 / 0.35);
  border: 3px solid #4fae8d;
  cursor: grab;
  touch-action: none;
}
.scan-pages { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 4px; }
.scan-pages figure { margin: 0; flex: none; display: grid; gap: 6px; justify-items: center; font-size: 13px; }
.scan-pages figcaption { display: grid; gap: 4px; justify-items: center; }
.page-tools { display: flex; gap: 4px; }
.page-tools button { padding: 2px 8px; }
.pdf-option { display: flex !important; align-items: center; gap: 8px; color: var(--text) !important; font-size: 14px !important; }
.pdf-option input { width: 18px; height: 18px; }
.scan-thumb { height: 220px; width: auto; max-width: 70vw; border: 1px solid var(--line); border-radius: 4px; background: #fff; }
.sketch-tools { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.swatch { width: 28px; height: 28px; padding: 0; border-radius: 50%; background: var(--swatch); border: 2px solid var(--line); }
.swatch.active { outline: 2px solid var(--accent); outline-offset: 2px; }
.sketch-canvas {
  width: 100%;
  height: min(60vh, 560px);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  touch-action: none;
  cursor: crosshair;
}

/* Dialogs */
.dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0;
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  background: var(--panel);
  color: var(--text);
}
.dialog::backdrop { background: rgb(0 0 0 / 0.4); }
.dialog form, .dialog .body { padding: 20px; display: grid; gap: 12px; }
.dialog h2 { margin: 0; font-size: 18px; }
.dialog h3 { margin: 8px 0 0; font-size: 14px; }
.dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.dialog .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.dialog .row > input { flex: 1; }
.dialog .actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dialog table { width: 100%; border-collapse: collapse; font-size: 14px; }
.dialog td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.dialog code {
  display: block;
  word-break: break-all;
  background: var(--bg);
  padding: 8px;
  border-radius: 6px;
  user-select: all;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  background: #22302b;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
  z-index: 10;
}

/* Phones and small tablets: one pane at a time. */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .mobile-only { display: inline-block; }
  .app > * { display: none; }
  .app[data-pane="nav"] .sidebar,
  .app[data-pane="list"] .list-pane,
  .app[data-pane="editor"] .editor-pane { display: flex; }
  #note-saved { display: none; }
  .scan-thumb { height: 150px; max-width: 38vw; object-fit: contain; }
  .editor-head { flex-wrap: nowrap; }
  .note-title { font-size: 22px; padding: 12px 14px 4px; }
  .tag-row, .source-link { padding-left: 14px; padding-right: 14px; }
  .editor .ql-editor { padding: 12px 14px 80px; }
  .editor .ql-toolbar.ql-snow { padding: 4px 8px; }
}
