:root {
  --font-size: 14px;
  --bg: #ffffff;
  --bg-elev: #f5f5f7;
  --card: #fbfbfd;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --border: rgba(0, 0, 0, 0.32);
  --btn-border: rgba(0, 0, 0, 0.45);
  --accent: #6352ec;
  --purple: #9a4ccf;
  --chip-bg: rgba(99, 82, 236, 0.14);
}

:root[data-theme="dark"],
:root[data-theme="system"] {
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #1c1c1e;
    --bg-elev: #2c2c2e;
    --card: #252527;
    --text: #f5f5f7;
    --muted: #9e9ea3;
    --border: rgba(255, 255, 255, 0.28);
    --btn-border: rgba(255, 255, 255, 0.5);
    --accent: #9a8cff;
    --purple: #c58cff;
    --chip-bg: rgba(154, 140, 255, 0.18);
  }
}

:root[data-theme="dark"] {
  --bg: #1c1c1e;
  --bg-elev: #2c2c2e;
  --card: #252527;
  --text: #f5f5f7;
  --muted: #9e9ea3;
  --border: rgba(255, 255, 255, 0.28);
  --btn-border: rgba(255, 255, 255, 0.5);
  --accent: #9a8cff;
  --purple: #c58cff;
  --chip-bg: rgba(154, 140, 255, 0.18);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-size: var(--font-size);
}

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.toolbar-group { display: flex; align-items: center; gap: 8px; }
.toolbar-spacer { flex: 1; }
.toolbar button, .toolbar select {
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 7px;
  border: 1px solid var(--btn-border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}
.toolbar button:hover { background: var(--chip-bg); }
.stepper { font-size: 13px; display: inline-flex; align-items: center; gap: 4px; }
.stepper input { width: 46px; padding: 3px 4px; border-radius: 6px; border: 1px solid var(--btn-border); background: var(--card); color: var(--text); }
#ai-status { font-size: 12px; padding: 2px 8px; border-radius: 10px; }
.ai-ok { color: #1b8f4d; background: rgba(27, 143, 77, 0.14); }
.ai-off { color: var(--muted); background: var(--chip-bg); }

/* Layout: sidebar + page */
.layout { display: flex; align-items: flex-start; }

.sidebar {
  width: 240px;
  flex: none;
  position: sticky;
  top: 49px; /* below toolbar */
  align-self: stretch;
  height: calc(100vh - 49px);
  overflow-y: auto;
  padding: 14px 12px;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
}
.sidebar.hidden { display: none; }
.side-section { margin-bottom: 18px; }
.side-title { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 4px 8px; }
.side-empty { font-size: 12px; color: var(--muted); padding: 0 4px; }
.side-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.side-tag { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 3px 9px; border: none; border-radius: 999px; cursor: pointer; background: var(--chip-bg); color: var(--accent); }
.side-tag.active { background: var(--accent); color: #fff; }
.side-count { font-size: 10px; opacity: 0.7; }
.side-item { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; border: none; background: none; color: var(--text); padding: 5px 8px; border-radius: 7px; cursor: pointer; font-size: 13px; }
.side-item:hover { background: var(--chip-bg); }
.side-item.active { background: var(--chip-bg); color: var(--accent); }
.side-item-icon { color: var(--muted); flex: none; width: 14px; text-align: center; }
.side-item-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Page */
.page { flex: 1; max-width: 880px; margin: 0 auto; padding: 20px 24px 120px; min-width: 0; }

.doc-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.doc-header h1 { margin: 0; font-size: calc(var(--font-size) + 14px); font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: text; }
.title-edit { font-size: calc(var(--font-size) + 14px); font-weight: 800; border: none; border-bottom: 2px solid var(--accent); background: transparent; color: var(--text); outline: none; width: 100%; padding: 0; }
.clock { text-align: right; flex: none; }
.clock-time { font-weight: 700; font-size: calc(var(--font-size) + 4px); font-variant-numeric: tabular-nums; }
.clock-date { font-size: 12px; color: var(--muted); }

/* Top bar: new + search */
.topbar { display: flex; align-items: center; gap: 12px; margin: 14px 0 6px; }
.new-note { display: flex; align-items: stretch; }
.new-note-main {
  font-size: 15px; font-weight: 600; padding: 6px 12px; cursor: pointer;
  border: 1px solid var(--btn-border); border-right: none; border-radius: 9px 0 0 9px;
  background: var(--card); color: var(--accent);
}
.new-note-menu { position: relative; }
.new-note-menu summary { list-style: none; cursor: pointer; padding: 8px 8px; border: 1px solid var(--btn-border); border-radius: 0 9px 9px 0; background: var(--card); color: var(--accent); }
.new-note-menu summary::-webkit-details-marker { display: none; }
.new-note-menu .menu { position: absolute; top: 110%; left: 0; background: var(--card); border: 1px solid var(--border); border-radius: 9px; padding: 4px; box-shadow: 0 8px 24px rgba(0,0,0,0.18); z-index: 5; }
.new-note-menu .menu button { display: block; width: 100%; text-align: left; white-space: nowrap; padding: 6px 12px; border: none; background: none; color: var(--text); border-radius: 6px; cursor: pointer; }
.new-note-menu .menu button:hover { background: var(--chip-bg); }

.search-box { display: flex; align-items: center; gap: 6px; padding: 5px 12px; border-radius: 999px; background: var(--bg-elev); flex: 1; max-width: 340px; }
.search-icon { color: var(--muted); }
.search-box input { border: none; background: none; outline: none; color: var(--text); font-size: 14px; width: 100%; }
.save-state { font-size: 12px; color: var(--muted); margin-left: auto; }

/* Notebook */
.notebook { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.empty { color: var(--muted); padding: 8px 2px; }

.card {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 12px;
}
.card.selected { border-color: var(--accent); border-width: 2px; }

.card-header { display: flex; align-items: center; gap: 8px; }
.type-badge { font-size: 12px; padding: 2px 4px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); }
.card-date { font-size: 11px; color: var(--muted); }
.analyzing { font-size: 12px; color: var(--muted); }
.card-actions { margin-left: auto; display: none; gap: 2px; }
.card:hover .card-actions, .card.selected .card-actions { display: flex; }
.icon-btn { border: none; background: none; cursor: pointer; color: var(--muted); font-size: 14px; padding: 2px 5px; border-radius: 6px; }
.icon-btn:hover:not([disabled]) { background: var(--chip-bg); color: var(--text); }
.icon-btn[disabled] { opacity: 0.3; cursor: default; }

.card-title { font-size: calc(var(--font-size) + 3px); font-weight: 600; margin: 6px 0 4px; }
.card-title.muted, .muted { color: var(--muted); }
.body { line-height: 1.5; }
.body.markdown p { margin: 6px 0; }
.body.markdown ul, .body.markdown ol { margin: 6px 0; padding-left: 22px; }
.body.markdown li { margin: 2px 0; }
.body.markdown code { background: var(--bg-elev); padding: 1px 5px; border-radius: 5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; }
.body.markdown pre { background: var(--bg-elev); padding: 10px; border-radius: 8px; overflow-x: auto; }
.body.markdown pre code { background: none; padding: 0; }
.body.markdown blockquote { margin: 6px 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.body.markdown a { color: var(--accent); }

/* Todo */
.todo { display: flex; flex-direction: column; gap: 5px; margin-top: 4px; }
.todo-progress { display: flex; align-items: center; gap: 8px; }
.todo-progress progress { width: 96px; height: 8px; }
.todo-item { display: flex; align-items: baseline; gap: 8px; }
.todo-text { cursor: pointer; }
.todo-item.done .todo-text { text-decoration: line-through; color: var(--muted); }
.todo-text a { color: var(--accent); }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { font-size: 12px; padding: 3px 9px; border-radius: 999px; border: none; cursor: pointer; }
.chip.keyword { background: var(--chip-bg); color: var(--accent); }
.chip.person { background: rgba(154, 76, 207, 0.16); color: var(--purple); }

/* Editor */
.editor-wrap { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.title-input { font-size: calc(var(--font-size) + 3px); font-weight: 600; padding: 6px 8px; border-radius: 7px; border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.fmt-toolbar { display: flex; align-items: center; gap: 2px; padding: 4px; background: var(--bg-elev); border-radius: 7px; }
.fmt-btn { font-size: 12px; padding: 3px 8px; border: none; background: none; color: var(--text); border-radius: 6px; cursor: pointer; }
.fmt-btn:hover { background: var(--chip-bg); }
.fmt-sep { width: 1px; height: 16px; background: var(--border); margin: 0 4px; }
.editor {
  width: 100%; min-height: 200px; resize: none; overflow: hidden;
  padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: var(--font-size); line-height: 1.5;
  caret-color: var(--accent);
}
.editor:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Insert divider */
.insert-divider { display: flex; align-items: center; gap: 6px; width: 100%; border: none; background: none; cursor: pointer; padding: 2px 0; opacity: 0.35; }
.insert-divider:hover { opacity: 1; }
.insert-line { flex: 1; height: 1px; background: var(--border); }
.insert-plus { color: var(--accent); font-weight: 700; }
