:root {
  --bg: #ffffff;
  --bg-alt: #f6f7f7;
  --bg-hover: #f0f2f5;
  --bg-selected: #e3ebfb;
  --text: #2c3338;
  --text-soft: #646970;
  --text-faint: #8c8f94;
  --border: #dcdcde;
  --accent: #3361cc;
  --accent-text: #ffffff;
  --danger: #d63638;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --scale: 1;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d2125;
    --bg-alt: #23282d;
    --bg-hover: #2a3036;
    --bg-selected: #25345a;
    --text: #e6e8ea;
    --text-soft: #a7aaad;
    --text-faint: #7d8187;
    --border: #353b41;
    --accent: #84a4f0;
    --accent-text: #10141a;
    --danger: #f2686a;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  --bg: #1d2125;
  --bg-alt: #23282d;
  --bg-hover: #2a3036;
  --bg-selected: #25345a;
  --text: #e6e8ea;
  --text-soft: #a7aaad;
  --text-faint: #7d8187;
  --border: #353b41;
  --accent: #84a4f0;
  --accent-text: #10141a;
  --danger: #f2686a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: calc(16px * var(--scale))/1.5 var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-columns: 0 minmax(260px, 340px) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
.app.drawer-open { grid-template-columns: 220px minmax(260px, 340px) minmax(0, 1fr); }
.drawer, .list-pane, .editor-pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.drawer { background: var(--bg-alt); border-right: 1px solid var(--border); overflow: hidden; }
.app:not(.drawer-open) .drawer { border-right: 0; visibility: hidden; }
.list-pane { border-right: 1px solid var(--border); }
.scrim { display: none; }

/* ---------- Drawer ---------- */
.drawer-scroll { flex: 1; overflow-y: auto; padding: 10px 8px; }
.nav-item {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 9px 12px; border-radius: 6px; text-align: left; color: var(--text); white-space: nowrap;
}
.nav-item:hover { background: var(--bg-hover); }
.nav-item.active { background: var(--bg-selected); color: var(--accent); font-weight: 600; }
.nav-item span { overflow: hidden; text-overflow: ellipsis; }
.nav-heading { margin: 16px 12px 6px; font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-faint); }
.tag-list .nav-item { padding: 7px 12px; }
.tag-list .empty { padding: 4px 12px; color: var(--text-faint); font-size: 14px; }
.nav-settings { border-top: 1px solid var(--border); border-radius: 0; padding: 12px 20px; }

/* ---------- List ---------- */
.list-head, .editor-head {
  display: flex; align-items: center; gap: 6px; flex: none;
  height: 56px; padding: 0 10px; border-bottom: 1px solid var(--border);
}
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 8px; color: var(--text-soft); flex: none; text-decoration: none;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.accent { color: var(--accent); }
.icon-btn[aria-pressed="true"] { color: var(--accent); background: var(--bg-selected); }
.icon-btn[aria-pressed="true"] svg { fill: currentColor; }
.search-wrap { flex: 1; min-width: 0; }
#search {
  width: 100%; height: 36px; padding: 0 12px; border: 1px solid var(--border); border-radius: 18px;
  background: var(--bg-alt); outline: none; -webkit-appearance: none; appearance: none;
}
#search:focus { border-color: var(--accent); background: var(--bg); }
.view-label { padding: 8px 16px; font-size: 13px; font-weight: 600; color: var(--text-soft); background: var(--bg-alt); border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.view-label button { color: var(--accent); font-weight: 600; font-size: 13px; }
.view-label button.danger { color: var(--danger); }
.banner { padding: 10px 16px; font-size: 14px; background: #fcf3cf; color: #5c4400; border-bottom: 1px solid var(--border); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .banner { background: #4a3d10; color: #f6e7a8; } }
:root[data-theme="dark"] .banner { background: #4a3d10; color: #f6e7a8; }
.note-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.note-item {
  display: block; width: 100%; text-align: left; padding: 12px 16px 12px 18px;
  border-bottom: 1px solid var(--border); position: relative; min-height: 64px;
}
.note-item:hover { background: var(--bg-hover); }
.note-item.selected { background: var(--bg-selected); }
.note-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; }
.note-title .t { overflow: hidden; text-overflow: ellipsis; }
.note-title.untitled .t { color: var(--text-faint); font-weight: 400; }
.note-title svg { width: 15px; height: 15px; color: var(--text-faint); }
.note-title svg.pin { color: var(--accent); fill: currentColor; }
.note-preview {
  color: var(--text-soft); font-size: calc(14px * var(--scale)); line-height: 1.4; margin-top: 2px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; word-break: break-word;
}
.note-item .unsynced { position: absolute; left: 7px; top: 21px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.list-empty { padding: 40px 24px; text-align: center; color: var(--text-faint); }
.list-foot {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 16px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom));
  font-size: 12px; color: var(--text-faint); border-top: 1px solid var(--border); background: var(--bg-alt);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); flex: none; }
.dot.ok { background: #2ea043; }
.dot.busy { background: #d4a72c; }
.dot.off { background: var(--text-faint); }
.dot.err { background: var(--danger); }

/* ---------- Editor ---------- */
.editor-pane { position: relative; }
.spacer { flex: 1; }
.actions { display: flex; align-items: center; gap: 2px; }
.text-btn { padding: 8px 12px; border-radius: 6px; color: var(--accent); font-weight: 600; }
.text-btn:hover { background: var(--bg-hover); }
.text-btn.danger { color: var(--danger); }

.fmt-bar {
  flex: none; display: flex; align-items: center; gap: 2px; padding: 4px 8px;
  border-bottom: 1px solid var(--border); background: var(--bg-alt);
  overflow-x: auto; white-space: nowrap; scrollbar-width: none; font-size: 15px;
}
.fmt-bar::-webkit-scrollbar { display: none; }
.fmt-bar select { height: 34px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); padding: 0 6px; margin-right: 4px; flex: none; font-size: 14px; }
.fmt {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  min-width: 34px; height: 34px; padding: 0 6px; border-radius: 6px; color: var(--text-soft);
}
.fmt svg { width: 20px; height: 20px; }
.fmt:hover { background: var(--bg-hover); color: var(--text); }
.fmt[aria-pressed="true"] { background: var(--bg-selected); color: var(--accent); }
.fmt.wide { padding: 0 10px; font-size: 13px; font-weight: 600; }
.fmt.danger { color: var(--danger); }
.fmt-sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; flex: none; }
.table-tools { display: inline-flex; align-items: center; gap: 2px; }

.editor-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; cursor: text; }
.editor {
  max-width: 780px; margin: 0 auto; min-height: 100%;
  padding: 24px 28px 40vh; outline: none;
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
  font-size: calc(17px * var(--scale)); line-height: 1.6; caret-color: var(--accent);
}
.editor > p:first-child, .editor > h1:first-child, .editor > h2:first-child, .editor > h3:first-child { font-weight: 600; font-size: 1.15em; }
.editor.readonly { color: var(--text-soft); }
.editor p { margin: 0; min-height: 1.6em; }
.editor h1, .editor h2, .editor h3 { margin: 0.7em 0 0.2em; line-height: 1.3; font-weight: 700; }
.editor h1 { font-size: 1.5em; }
.editor h2 { font-size: 1.28em; }
.editor h3 { font-size: 1.1em; }
.editor > h1:first-child, .editor > h2:first-child, .editor > h3:first-child { margin-top: 0; }
.editor span, .editor font { font: inherit !important; color: inherit !important; background: none !important; }
.editor ul, .editor ol { margin: 0.15em 0; padding-left: 1.7em; }
.editor li { margin: 0.05em 0; }
.editor li.check { list-style: none; position: relative; }
.editor li.check::before {
  content: ""; position: absolute; left: -1.45em; top: 0.3em; width: 1em; height: 1em;
  border: 1.5px solid var(--text-faint); border-radius: 4px; cursor: pointer; background: var(--bg);
}
.editor li.check.done::before {
  background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.5 8.5l3 3 6-7' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/100% no-repeat;
  border-color: var(--accent);
}
.editor li.check.done { color: var(--text-faint); text-decoration: line-through; }
.editor li.check.done li { text-decoration: none; }
.editor blockquote { margin: 0.3em 0; padding: 0.1em 0 0.1em 0.9em; border-left: 3px solid var(--border); color: var(--text-soft); }
.editor hr { border: 0; border-top: 1px solid var(--border); margin: 0.8em 0; }
.editor a { color: var(--accent); text-decoration: underline; cursor: pointer; }
.editor code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em; background: var(--bg-hover); padding: 0.1em 0.3em; border-radius: 4px; }
.editor table {
  display: block; overflow-x: auto; max-width: 100%; border-collapse: collapse; margin: 0.5em 0;
  white-space: normal; font-size: 0.94em; line-height: 1.45;
}
.editor th, .editor td {
  border: 1px solid var(--border); padding: 6px 10px; min-width: 70px; vertical-align: top; text-align: left;
  white-space: pre-wrap; overflow-wrap: normal; word-break: normal;
}
.editor th { background: var(--bg-alt); font-weight: 600; }
.editor .tg { margin: 0.25em 0; }
.editor .tg-h { position: relative; padding-left: 1.4em; font-weight: 600; min-height: 1.6em; }
.editor .tg-h::before {
  content: ""; position: absolute; left: 0.25em; top: 0.5em; cursor: pointer;
  border-style: solid; border-width: 0.32em 0 0.32em 0.52em; border-color: transparent transparent transparent var(--text-soft);
  transition: transform 0.12s ease;
}
.editor .tg.open > .tg-h::before { transform: rotate(90deg); }
.editor .tg-b { padding-left: 1.4em; }
.editor .tg:not(.open) > .tg-b { display: none; }
.editor img.shot {
  display: inline-block; vertical-align: bottom; max-width: 100%; max-height: 80vh; height: auto;
  border: 1px solid var(--border); border-radius: 6px; margin: 4px 0; cursor: zoom-in; background: var(--bg-alt);
  min-width: 48px; min-height: 48px;
}
.editor img.shot.missing { min-width: 220px; min-height: 90px; border-style: dashed; cursor: default; }
.tag-bar {
  flex: none; display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 8px 16px; padding-bottom: calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); min-height: 48px;
}
.chips { display: contents; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 10px; border-radius: 14px; background: var(--bg-hover); font-size: 14px; max-width: 100%; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { width: 22px; height: 22px; border-radius: 50%; color: var(--text-soft); line-height: 1; font-size: 16px; }
.chip button:hover { background: var(--border); color: var(--text); }
#tagInput { flex: 1; min-width: 110px; border: 0; outline: none; background: transparent; padding: 6px 2px; font-size: 14px; }
.empty-state {
  position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; color: var(--text-faint); background: var(--bg);
}
.empty-state svg { width: 56px; height: 56px; stroke-width: 1; }
.editor-pane.empty .empty-state { display: flex; }
.only-narrow { display: none; }

/* ---------- Narrow screens (phone, folded) ---------- */
@media (max-width: 759px) {
  .app, .app.drawer-open { grid-template-columns: minmax(0, 1fr); }
  .only-narrow { display: inline-flex; }
  .list-pane { border-right: 0; grid-column: 1; grid-row: 1; }
  .editor-pane { grid-column: 1; grid-row: 1; background: var(--bg); display: none; }
  .app.show-editor .editor-pane { display: flex; }
  .app.show-editor .list-pane { display: none; }
  .drawer {
    position: fixed; z-index: 30; top: 0; bottom: 0; left: 0; width: min(280px, 82vw);
    transform: translateX(-102%); transition: transform 0.18s ease; visibility: visible !important;
    border-right: 1px solid var(--border) !important;
    padding-top: env(safe-area-inset-top);
  }
  .app.drawer-open .drawer { transform: none; box-shadow: var(--shadow); }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.35); }
  .editor { padding: 18px 18px 45vh; }
  .editor-pane.empty .empty-state { display: none; }
}

/* ---------- Sign-in ---------- */
.auth { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; background: var(--bg-alt); overflow-y: auto; }
.auth-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; padding: 28px; background: var(--bg); border: 1px solid var(--border); border-radius: 12px; }
.auth-card img { align-self: center; border-radius: 12px; }
.auth-card h1 { margin: 0; font-size: 22px; text-align: center; }
.auth-hint { margin: 0; color: var(--text-soft); font-size: 14px; text-align: center; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.auth-card input { height: 44px; padding: 0 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); font-weight: 400; font-size: 16px; }
.auth-card input:focus { border-color: var(--accent); outline: none; }
.auth-error { margin: 0; min-height: 20px; color: var(--danger); font-size: 14px; }
.primary { height: 46px; border-radius: 8px; background: var(--accent); color: var(--accent-text); font-weight: 600; padding: 0 16px; }
.primary:disabled { opacity: 0.6; cursor: default; }

/* ---------- Dialogs, lightbox, toast ---------- */
.dialog { width: min(480px, calc(100vw - 32px)); padding: 22px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg); color: var(--text); box-shadow: var(--shadow); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog h2 { margin: 0 0 6px; font-size: 19px; }
.dialog input[type="url"], .dialog input[type="text"] { width: 100%; height: 42px; padding: 0 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); margin: 8px 0; font-size: 16px; }
.muted { color: var(--text-soft); font-size: 14px; margin: 4px 0; min-height: 0; }
.size-row { display: flex; flex-direction: column; gap: 6px; margin: 12px 0 4px; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; padding: 8px 4px; font-size: 14px; border-right: 1px solid var(--border); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--bg-selected); color: var(--accent); font-weight: 600; }
.dialog-list { display: flex; flex-direction: column; margin: 12px -8px 4px; }
.row-btn { display: flex; flex-direction: column; gap: 2px; text-align: left; padding: 10px 8px; border-radius: 8px; }
.row-btn:hover { background: var(--bg-hover); }
.row-btn span { color: var(--text-soft); font-size: 14px; }
.row-btn:disabled { opacity: 0.5; cursor: default; }
.dialog-foot { display: flex; justify-content: flex-end; margin-top: 8px; gap: 4px; }
.info { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 12px 0; font-size: 15px; }
.info dt { color: var(--text-soft); }
.info dd { margin: 0; text-align: right; }
.link-pop {
  position: fixed; z-index: 40; display: flex; gap: 2px; padding: 4px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); box-shadow: var(--shadow); font-size: 14px;
}
.link-pop button { padding: 6px 10px; border-radius: 6px; color: var(--accent); font-weight: 600; }
.link-pop button:hover { background: var(--bg-hover); }
.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, 0.88); display: flex; align-items: flex-start; justify-content: center; overflow: auto; cursor: zoom-out; padding: 12px; }
.lightbox img { margin: auto; max-width: 100%; height: auto; display: block; }
.lightbox.zoomed img { max-width: none; }
.toast {
  position: fixed; z-index: 70; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 8px; background: #2c3338; color: #fff; font-size: 14px; box-shadow: var(--shadow);
}

/* ---------- Settings rows, connected AIs, history, approval page ---------- */
.set-row { display: flex; flex-direction: column; gap: 6px; margin: 12px 0 4px; }
.set-row select { height: 40px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); font-size: 15px; }
.dialog.wide { width: min(560px, calc(100vw - 32px)); }
.dialog { max-height: calc(100dvh - 32px); overflow-y: auto; }
.dialog h3 { margin: 18px 0 2px; font-size: 16px; }
.copy-row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
.copy-row code, .copy-row input { flex: 1; min-width: 0; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-alt); font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow-x: auto; white-space: nowrap; }
.ai-list { display: flex; flex-direction: column; margin: 6px 0; border: 1px solid var(--border); border-radius: 8px; }
.ai-list:empty { display: none; }
.ai-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.ai-row:last-child { border-bottom: 0; }
.ai-row .grow { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.ai-row .grow span { color: var(--text-soft); font-size: 13px; }
.ai-chip { display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 9px; background: var(--bg-selected); color: var(--accent); font-size: 11px; font-weight: 700; line-height: 18px; vertical-align: middle; flex: none; }
.hist-row { padding: 12px 0; border-bottom: 1px solid var(--border); }
.hist-row:last-child { border-bottom: 0; }
.hist-row .hist-meta { color: var(--text-soft); font-size: 13px; }
.hist-row .hist-prev { margin: 6px 0; font-size: 14px; color: var(--text-soft); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 4.5em; overflow: hidden; }
.hist-row.open .hist-prev { max-height: 40vh; overflow-y: auto; color: var(--text); padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-alt); }
.hist-row .hist-acts { display: flex; gap: 4px; margin-left: -12px; }
.pick { flex-direction: row !important; align-items: flex-start; gap: 10px !important; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; }
.pick input { height: auto !important; margin-top: 4px; flex: none; }
.pick span { display: flex; flex-direction: column; gap: 2px; }
.pick small { font-weight: 400; color: var(--text-soft); font-size: 13px; }
.pick:has(input:checked) { border-color: var(--accent); background: var(--bg-selected); }
.access-pick { display: flex; flex-direction: column; gap: 8px; }
.warn-box { margin: 0; padding: 10px 12px; border-radius: 8px; background: #fcf3cf; color: #5c4a00; font-size: 14px; }
