:root {
  --bg: #ffffff;
  --bg-sidebar: #f7f7f5;
  --text: #37352f;
  --text-soft: rgba(55, 53, 47, 0.65);
  --text-faint: rgba(55, 53, 47, 0.4);
  --hover: rgba(55, 53, 47, 0.06);
  --active: rgba(55, 53, 47, 0.1);
  --border: rgba(55, 53, 47, 0.12);
  --accent: #2eaadc;
  --callout-bg: rgba(241, 241, 239, 0.9);
  --code-bg: #f7f6f3;
  --shadow: 0 10px 30px rgba(15, 15, 15, 0.15), 0 0 0 1px rgba(15, 15, 15, 0.05);
}

[data-theme="dark"] {
  --bg: #191919;
  --bg-sidebar: #202020;
  --text: rgba(255, 255, 255, 0.88);
  --text-soft: rgba(255, 255, 255, 0.55);
  --text-faint: rgba(255, 255, 255, 0.32);
  --hover: rgba(255, 255, 255, 0.06);
  --active: rgba(255, 255, 255, 0.1);
  --border: rgba(255, 255, 255, 0.13);
  --callout-bg: rgba(255, 255, 255, 0.05);
  --code-bg: rgba(255, 255, 255, 0.05);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.08);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

body { display: flex; }

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
a { color: inherit; text-decoration: none; }

/* ---------------------------------------------------------------- sidebar */
#sidebar {
  width: 250px;
  min-width: 250px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 8px;
  gap: 2px;
  overflow-y: auto;
}

.ws-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  font-weight: 600;
  font-size: 14px;
}

.sidebar-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--text-soft);
  text-align: left;
}
.sidebar-btn:hover { background: var(--hover); }
.kbd {
  font-size: 11px;
  color: var(--text-faint);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}

.section-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
}
.icon-btn {
  border-radius: 4px;
  padding: 0 6px;
  color: var(--text-faint);
  font-size: 14px;
  line-height: 20px;
}
.icon-btn:hover { background: var(--active); color: var(--text); }

#page-tree { flex: 1; }

.tree-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--text-soft);
  cursor: pointer;
  user-select: none;
}
.tree-item:hover { background: var(--hover); }
.tree-item.active { background: var(--active); color: var(--text); font-weight: 500; }
.tree-item .twirl {
  width: 18px;
  min-width: 18px;
  text-align: center;
  font-size: 10px;
  color: var(--text-faint);
  border-radius: 4px;
  transition: transform 0.12s;
}
.tree-item .twirl.open { transform: rotate(90deg); }
.tree-item .twirl:hover { background: var(--active); }
.tree-item .t-icon { width: 20px; text-align: center; }
.tree-item .t-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tree-item .t-actions { display: none; gap: 2px; }
.tree-item:hover .t-actions { display: flex; }

.tree-children { margin-left: 14px; }
.tree-empty { padding: 4px 12px; font-size: 13px; color: var(--text-faint); font-style: italic; }

.sidebar-footer { border-top: 1px solid var(--border); padding-top: 6px; margin-top: 6px; }
.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 10px 2px;
  font-size: 13px;
  color: var(--text-faint);
}
.user-row .user-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-row .icon-btn { font-size: 12px; white-space: nowrap; }

/* ------------------------------------------------------------------ main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  font-size: 13px;
  color: var(--text-soft);
}
#breadcrumbs { display: flex; align-items: center; gap: 4px; overflow: hidden; white-space: nowrap; }
#breadcrumbs .crumb { padding: 2px 6px; border-radius: 4px; cursor: pointer; }
#breadcrumbs .crumb:hover { background: var(--hover); }
#breadcrumbs .crumb-sep { color: var(--text-faint); }
.topbar-btn { padding: 3px 8px; border-radius: 5px; cursor: pointer; color: var(--text-soft); }
.topbar-btn:hover { background: var(--hover); }

#editor-scroll { flex: 1; overflow-y: auto; }

/* ---------------------------------------------------------------- share */
#share-pop {
  position: absolute;
  top: 44px;
  right: 16px;
  z-index: 60;
  width: 340px;
  background: var(--bg);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 14px 16px 16px;
}
.share-head { font-size: 14px; font-weight: 600; margin-bottom: 8px; }
.share-actions { display: flex; gap: 8px; margin-top: 10px; }
.small-btn.danger { color: #d6453d; border-color: rgba(214, 69, 61, 0.4); }
.small-btn.danger:hover { background: rgba(214, 69, 61, 0.08); }
#main { position: relative; }

#editor {
  max-width: 720px;
  margin: 0 auto;
  padding: 40px 48px 30vh;
}

#page-icon {
  font-size: 64px;
  line-height: 1;
  padding: 6px;
  border-radius: 8px;
  margin-left: -6px;
}
#page-icon:hover { background: var(--hover); }

#page-title {
  font-size: 40px;
  font-weight: 700;
  margin: 8px 0 16px;
  outline: none;
  line-height: 1.2;
}
#page-title:empty::before,
.block-text:empty::before {
  content: attr(data-placeholder);
  color: var(--text-faint);
  pointer-events: none;
}

#block-tail { height: 120px; cursor: text; }

/* ---------------------------------------------------------------- blocks */
.block {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 4px;
  padding: 2px 0;
  border-radius: 4px;
}
.block.drop-above::before {
  content: "";
  position: absolute;
  top: -1px; left: 0; right: 0;
  height: 3px;
  background: var(--accent);
  border-radius: 2px;
}

.drag-handle {
  opacity: 0;
  width: 18px;
  min-width: 18px;
  margin-top: 4px;
  text-align: center;
  color: var(--text-faint);
  cursor: grab;
  border-radius: 4px;
  font-size: 14px;
  line-height: 18px;
  user-select: none;
}
.block:hover .drag-handle { opacity: 1; }
.drag-handle:hover { background: var(--hover); }

.block-body { flex: 1; min-width: 0; display: flex; align-items: flex-start; gap: 6px; }

.block-text {
  flex: 1;
  min-width: 0;
  outline: none;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
  font-size: 16px;
  padding: 1px 2px;
}

.block[data-type="h1"] .block-text { font-size: 30px; font-weight: 700; margin-top: 18px; }
.block[data-type="h2"] .block-text { font-size: 24px; font-weight: 700; margin-top: 12px; }
.block[data-type="h3"] .block-text { font-size: 20px; font-weight: 600; margin-top: 8px; }

.block .marker {
  min-width: 24px;
  text-align: center;
  line-height: 1.6;
  font-size: 16px;
  margin-top: 1px;
  color: var(--text);
  user-select: none;
}
.block[data-type="bullet"] .marker::before { content: "•"; font-size: 20px; }
.block[data-type="numbered"] .marker { color: var(--text); }

.block[data-type="todo"] .marker input {
  width: 16px;
  height: 16px;
  margin-top: 5px;
  accent-color: var(--accent);
  cursor: pointer;
}
.block[data-type="todo"].checked .block-text {
  text-decoration: line-through;
  color: var(--text-faint);
}

.block[data-type="toggle"] .marker {
  cursor: pointer;
  font-size: 11px;
  border-radius: 4px;
  transition: transform 0.12s;
  margin-top: 5px;
}
.block[data-type="toggle"]:not(.collapsed) .marker { transform: rotate(90deg); }
.block[data-type="toggle"] .marker:hover { background: var(--hover); }

.block[data-type="quote"] .block-body {
  border-left: 3px solid var(--text);
  padding-left: 12px;
}

.block[data-type="callout"] .block-body {
  background: var(--callout-bg);
  border-radius: 6px;
  padding: 12px 14px;
}
.block[data-type="callout"] .marker { margin-right: 2px; }

.block[data-type="code"] .block-body {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
}
.block[data-type="code"] .block-text {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13.5px;
  line-height: 1.5;
}

.block[data-type="prompt"] .block-body {
  position: relative;
  background: rgba(124, 92, 255, 0.06);
  border: 1px solid rgba(124, 92, 255, 0.3);
  border-radius: 8px;
  padding: 10px 28px 10px 12px;
  flex-wrap: wrap;
}
.prompt-remove {
  position: absolute;
  top: 7px;
  right: 7px;
  font-size: 11px;
  line-height: 1;
  padding: 4px 5px;
  border-radius: 4px;
  color: var(--text-faint);
  opacity: 0;
  transition: opacity 0.12s;
}
.block[data-type="prompt"]:hover .prompt-remove { opacity: 1; }
.prompt-remove:hover { background: rgba(214, 69, 61, 0.12); color: #d6453d; }
[data-theme="dark"] .block[data-type="prompt"] .block-body {
  background: rgba(124, 92, 255, 0.1);
  border-color: rgba(124, 92, 255, 0.4);
}
.block[data-type="prompt"] .marker { font-size: 14px; }
.block[data-type="prompt"] .block-text { font-size: 15px; }
.prompt-status {
  flex-basis: 100%;
  margin-top: 7px;
  padding-left: 30px;
  font-size: 11.5px;
  font-weight: 500;
  color: #7c5cff;
}
[data-theme="dark"] .prompt-status { color: #ab97f5; }
.prompt-status.answered { color: var(--text-faint); font-weight: 400; }
.prompt-status.err { color: #d6453d; }
.prompt-status.running { animation: prompt-pulse 1.4s ease-in-out infinite; }
@keyframes prompt-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

.block.agent-output {
  border-left: 2px solid rgba(124, 92, 255, 0.4);
  padding-left: 6px;
}

/* flashcards */
.fc-widget {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  background: var(--bg);
}
.fc-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.fc-title { font-size: 14px; font-weight: 600; }
.fc-count { font-size: 12px; font-weight: 400; color: var(--text-faint); margin-left: 4px; }
.fc-head-btns { display: flex; gap: 6px; }
.fc-row {
  display: grid;
  grid-template-columns: 22px 1fr 1fr 22px;
  gap: 8px;
  align-items: start;
  margin-bottom: 6px;
}
.fc-num { font-size: 12px; color: var(--text-faint); padding-top: 8px; text-align: center; }
.fc-q, .fc-a {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 14px;
  min-height: 34px;
  outline: none;
}
.fc-q:focus, .fc-a:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(46,170,220,0.18); }
.fc-q:empty::before, .fc-a:empty::before { content: attr(data-ph); color: var(--text-faint); }
.fc-row-del {
  font-size: 11px;
  color: var(--text-faint);
  padding-top: 8px;
  opacity: 0;
}
.fc-row:hover .fc-row-del { opacity: 1; }
.fc-row-del:hover { color: #d6453d; }
.fc-add {
  margin-top: 6px;
  font-size: 13px;
  color: var(--text-soft);
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px dashed var(--border);
  width: 100%;
}
.fc-add:hover { background: var(--hover); color: var(--text); }

.fc-study { text-align: center; }
.fc-bar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.fc-progress { font-size: 12.5px; color: var(--text-faint); font-variant-numeric: tabular-nums; }
.fc-card {
  position: relative;
  min-height: 150px;
  border-radius: 12px;
  cursor: pointer;
  perspective: 1200px;
}
.fc-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px 24px;
  border: 1px solid var(--border);
  border-radius: 12px;
  backface-visibility: hidden;
  transition: transform 0.45s;
  background: var(--bg);
}
.fc-front { transform: rotateY(0deg); }
.fc-back { transform: rotateY(180deg); background: rgba(46,170,220,0.06); }
.fc-card.flipped .fc-front { transform: rotateY(-180deg); }
.fc-card.flipped .fc-back { transform: rotateY(0deg); }
.fc-side { font-size: 10.5px; font-weight: 700; letter-spacing: 0.08em; color: var(--text-faint); }
.fc-content { font-size: 19px; font-weight: 500; line-height: 1.4; word-break: break-word; }
.fc-blank { color: var(--text-faint); font-weight: 400; }
.fc-nav { display: flex; justify-content: center; gap: 8px; margin-top: 12px; }
.fc-flip { min-width: 90px; }

/* stock widget */
.stock-widget {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  background: var(--bg);
  box-shadow: 0 1px 4px rgba(15, 15, 15, 0.04);
}
.stock-widget .sw-icon { font-size: 18px; }
.sw-input {
  flex: 1;
  font: inherit;
  font-size: 14.5px;
  color: var(--text);
  background: transparent;
  border: none;
  outline: none;
}
.sw-main { min-width: 0; }
.sw-head { display: flex; align-items: baseline; gap: 8px; }
.sw-sym {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.03em;
  cursor: pointer;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--code-bg);
  border: 1px solid var(--border);
}
.sw-sym:hover { background: var(--hover); }
.sw-name {
  font-size: 12px;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 220px;
}
.sw-price { font-size: 22px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.sw-change { font-size: 12.5px; font-weight: 600; font-variant-numeric: tabular-nums; }
.sw-change.up { color: #2f9e44; }
.sw-change.down { color: #d6453d; }
[data-theme="dark"] .sw-change.up { color: #69c97e; }
[data-theme="dark"] .sw-change.down { color: #ff8a80; }
.sw-spark { margin-left: auto; flex-shrink: 0; }
.sw-meta {
  position: absolute;
  right: 12px;
  bottom: 6px;
  font-size: 10px;
  color: var(--text-faint);
}
.stock-widget.sw-error .sw-meta { color: #d6453d; font-size: 11px; position: static; margin-left: auto; }
.sw-remove { top: 6px; right: 6px; }
.stock-widget:hover .sw-remove { opacity: 1; }

.block[data-type="image"] .block-body { position: relative; padding: 4px 0; }
.block[data-type="image"] img {
  display: block;
  max-width: 100%;
  border-radius: 6px;
}
.img-remove {
  position: absolute;
  top: 10px;
  right: 6px;
  background: rgba(15, 15, 15, 0.55);
  color: #fff;
  border-radius: 5px;
  padding: 2px 8px;
  font-size: 12px;
  opacity: 0;
  transition: opacity 0.12s;
}
.block[data-type="image"]:hover .img-remove { opacity: 1; }

.table-wrap { flex: 1; min-width: 0; position: relative; padding: 4px 0; }
.blk-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 14.5px;
  table-layout: fixed;
}
.blk-table th, .blk-table td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  outline: none;
  vertical-align: top;
  word-break: break-word;
  line-height: 1.5;
}
.blk-table th {
  background: var(--code-bg);
  font-weight: 600;
  text-align: left;
}
.blk-table th:focus, .blk-table td:focus {
  box-shadow: inset 0 0 0 2px rgba(46, 170, 220, 0.5);
}
.table-tools {
  display: none;
  gap: 4px;
  margin-top: 6px;
}
.block:hover .table-tools { display: flex; }
.table-tools button {
  font-size: 11.5px;
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text-faint);
}
.table-tools button:hover { background: var(--hover); color: var(--text); }
.table-tools button:last-child:hover { color: #d6453d; border-color: rgba(214, 69, 61, 0.4); }

.block[data-type="divider"] { padding: 8px 0; cursor: default; }
.block[data-type="divider"] .hr {
  flex: 1;
  height: 1px;
  background: var(--border);
  margin-top: 8px;
}

/* ------------------------------------------------------------ slash menu */
#slash-menu, #emoji-picker {
  position: absolute;
  z-index: 50;
  background: var(--bg);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow: hidden auto;
}
#slash-menu { width: 280px; max-height: 320px; padding: 6px; }

.slash-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.slash-item.selected { background: var(--hover); }
.slash-item .si-icon {
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 15px;
  background: var(--bg);
}
.slash-item .si-name { font-size: 14px; }
.slash-item .si-desc { font-size: 12px; color: var(--text-faint); }
.slash-empty { padding: 10px; font-size: 13px; color: var(--text-faint); }

/* ----------------------------------------------------------- emoji picker */
#emoji-picker {
  width: 264px;
  max-height: 240px;
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
}
#emoji-picker button {
  font-size: 19px;
  padding: 4px;
  border-radius: 5px;
  text-align: center;
}
#emoji-picker button:hover { background: var(--hover); }

/* -------------------------------------------------------------- calendar */
#calendar-view {
  max-width: 1100px;
  margin: 0 auto;
  padding: 28px 32px 60px;
}
.calv-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#calv-title { font-size: 30px; font-weight: 700; margin: 0; }
.cal-nav { display: flex; align-items: center; gap: 4px; }
.cal-nav .icon-btn { font-size: 18px; line-height: 26px; padding: 0 10px; }
.cal-today-btn {
  font-size: 13px;
  padding: 4px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-soft);
}
.cal-today-btn:hover { background: var(--hover); }
.calv-hint { font-size: 13px; color: var(--text-faint); margin: 6px 0 16px; }

.calv-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
}
.calv-dow {
  font-size: 11.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  text-align: center;
  padding: 6px 0;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
}
.calv-cell {
  min-height: 104px;
  padding: 5px 6px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.calv-cell.dim { background: var(--bg-sidebar); }
.calv-cell.dim .calv-num { color: var(--text-faint); }
.calv-daynum {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 22px;
}
.calv-num {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-soft);
  font-variant-numeric: tabular-nums;
  width: 22px; height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.calv-cell.today .calv-num { background: var(--accent); color: #fff; font-weight: 700; }
.calv-add {
  opacity: 0;
  font-size: 13px;
  color: var(--text-faint);
  border-radius: 4px;
  padding: 0 5px;
  line-height: 18px;
}
.calv-cell:hover .calv-add { opacity: 1; }
.calv-add:hover { background: var(--hover); color: var(--text); }

.calv-evt {
  position: relative;
  font-size: 11.5px;
  line-height: 1.35;
  background: rgba(46, 170, 220, 0.13);
  color: #1c7da6;
  border-radius: 4px;
  padding: 2px 16px 2px 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
[data-theme="dark"] .calv-evt { color: #6cc7ea; }
.calv-evt b { font-weight: 600; }
.calv-evt-del {
  position: absolute;
  right: 2px; top: 1px;
  font-size: 10px;
  color: inherit;
  opacity: 0;
  padding: 1px 3px;
}
.calv-evt:hover .calv-evt-del { opacity: 0.8; }
.calv-evt-del:hover { opacity: 1; }

.calv-mention {
  font-size: 11.5px;
  line-height: 1.35;
  background: rgba(124, 92, 255, 0.1);
  color: #6a4fd8;
  border-radius: 4px;
  padding: 2px 6px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
[data-theme="dark"] .calv-mention { color: #ab97f5; }
.calv-mention:hover { background: rgba(124, 92, 255, 0.2); }

#event-modal { position: fixed; inset: 0; z-index: 100; }
.event-card { padding: 22px 26px 24px; max-width: 430px; }
.event-lbl { display: block; font-size: 12px; font-weight: 600; color: var(--text-soft); margin: 10px 0 4px; }
.event-row { display: flex; gap: 10px; }
.event-col { flex: 1; }
#event-status { font-size: 13px; }
#event-status.err { color: #d6453d; }

/* ---------------------------------------------------------------- search */
#search-modal { position: fixed; inset: 0; z-index: 100; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 15, 15, 0.5); }
.modal-card {
  position: relative;
  max-width: 560px;
  margin: 12vh auto 0;
  background: var(--bg);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
#search-input {
  width: 100%;
  border: none;
  outline: none;
  padding: 16px 18px;
  font-size: 17px;
  background: transparent;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}
#search-results { max-height: 320px; overflow-y: auto; padding: 6px; }
.search-result {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
}
.search-result:hover, .search-result.selected { background: var(--hover); }
.search-result .sr-title { font-size: 14px; font-weight: 500; }
.search-result .sr-snippet {
  font-size: 12px;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.search-none { padding: 14px; font-size: 13px; color: var(--text-faint); text-align: center; }

/* --------------------------------------------------------------- settings */
#settings-modal { position: fixed; inset: 0; z-index: 100; }
.settings-card { padding: 24px 28px 28px; }
.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.settings-head h2 { margin: 0; font-size: 20px; }
.settings-section { padding: 14px 0; border-top: 1px solid var(--border); }
.settings-section:first-of-type { border-top: none; }
.settings-section h3, .settings-panel h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); }

.settings-tabs {
  display: flex;
  gap: 4px;
  margin: 14px 0 0;
  border-bottom: 1px solid var(--border);
}
.settings-tab {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-soft);
  padding: 8px 12px;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  cursor: pointer;
  white-space: nowrap;
}
.settings-tab:hover { color: var(--text); }
.settings-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.settings-panel { padding: 16px 0 2px; min-height: 200px; }
.settings-panel[hidden] { display: none; }
.settings-muted { font-size: 13.5px; color: var(--text-soft); margin: 0 0 10px; }
.settings-muted code { font-size: 12px; background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }

.token-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.token-row code {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.settings-input {
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  outline: none;
  margin-bottom: 8px;
}
.settings-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(46, 170, 220, 0.18); }
#openrouter-status { font-size: 13px; }
#openrouter-status.ok { color: #2f9e44; }
#openrouter-status.err { color: #d6453d; }

#webhook-input {
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  outline: none;
}
#webhook-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(46, 170, 220, 0.18); }

.settings-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
}
.primary-btn.small { padding: 6px 14px; font-size: 13.5px; }
.small-btn {
  font-size: 13.5px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-soft);
}
.small-btn:hover { background: var(--hover); }
#settings-status { font-size: 13px; color: var(--text-soft); }
#settings-status.ok { color: #2f9e44; }
#settings-status.err { color: #d6453d; }

/* ----------------------------------------------------------- API tokens */
.token-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.token-item-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.token-item-head .t-name { font-size: 14px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  white-space: nowrap;
}
.badge.full   { background: rgba(46, 170, 220, 0.15); color: #1c7da6; }
.badge.append { background: rgba(47, 158, 68, 0.15);  color: #2f7e44; }
.badge.read   { background: rgba(55, 53, 47, 0.1);    color: var(--text-soft); }
.badge.lock   { background: rgba(124, 92, 255, 0.13); color: #6a4fd8; }
[data-theme="dark"] .badge.full   { color: #6cc7ea; }
[data-theme="dark"] .badge.append { color: #69c97e; }
[data-theme="dark"] .badge.lock   { color: #ab97f5; }
.token-item .token-meta { font-size: 11.5px; color: var(--text-faint); margin-top: 5px; }
.token-revoke { color: var(--text-faint); font-size: 12px; }
.token-revoke:hover { color: #d6453d; }

.token-new { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.token-new input[type="text"] {
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  outline: none;
}
.token-new input[type="text"]:focus { border-color: var(--accent); }
.token-new-row { display: flex; gap: 8px; }
.token-new-row select {
  flex: 1;
  font: inherit;
  font-size: 13.5px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  outline: none;
  min-width: 0;
}
#token-status { font-size: 13px; }
#token-status.ok { color: #2f9e44; }
#token-status.err { color: #d6453d; }

/* ------------------------------------------------------------- audit log */
#audit-modal { position: fixed; inset: 0; z-index: 100; }
.audit-card {
  max-width: 760px;
  padding: 22px 26px 24px;
}
.audit-controls { display: flex; align-items: center; gap: 8px; }
.audit-controls select {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  outline: none;
}
#audit-list {
  max-height: 56vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
/* templates */
#template-modal, #history-modal, #trash-modal { position: fixed; inset: 0; z-index: 100; }
.template-card { max-width: 560px; padding: 22px 26px 24px; }
#template-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 8px;
  max-height: 56vh;
  overflow-y: auto;
}
.tpl-card {
  position: relative;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  cursor: pointer;
  transition: border-color 0.1s, background 0.1s;
}
.tpl-card:hover { border-color: var(--accent); background: var(--hover); }
.tpl-icon { font-size: 24px; }
.tpl-name { font-size: 14px; font-weight: 600; margin-top: 6px; }
.tpl-desc { font-size: 12px; color: var(--text-faint); margin-top: 2px; line-height: 1.4; }
.tpl-del {
  position: absolute; top: 8px; right: 8px;
  font-size: 11px; color: var(--text-faint);
  width: 18px; height: 18px; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  opacity: 0;
}
.tpl-card:hover .tpl-del { opacity: 1; }
.tpl-del:hover { background: rgba(214,69,61,0.12); color: #d6453d; }
.tpl-section { grid-column: 1 / -1; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); margin-top: 8px; }
.tpl-empty { grid-column: 1 / -1; padding: 20px; text-align: center; color: var(--text-faint); font-size: 13.5px; }
.tpl-footer { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 12px; }

/* history + trash */
.history-card, .trash-card { padding: 22px 26px 24px; max-width: 540px; }
.hist-row, .trash-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
}
.hist-row:last-child, .trash-row:last-child { border-bottom: none; }
.hist-main, .trash-main { flex: 1; min-width: 0; }
.hist-when { font-size: 14px; font-weight: 500; }
.hist-sub, .trash-sub { font-size: 12px; color: var(--text-faint); margin-top: 2px; }
.hist-actor {
  font-size: 11px;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 10px;
  white-space: nowrap;
}
.hist-actor.session { background: rgba(55,53,47,0.08); color: var(--text-soft); }
.hist-actor.token { background: rgba(46,170,220,0.14); color: #1c7da6; }
.hist-actor.assistant { background: rgba(124,92,255,0.13); color: #6a4fd8; }
.hist-actor.automation { background: rgba(255,171,0,0.16); color: #9a6700; }
[data-theme="dark"] .hist-actor.token { color: #6cc7ea; }
[data-theme="dark"] .hist-actor.assistant { color: #ab97f5; }
[data-theme="dark"] .hist-actor.automation { color: #e6b75a; }
.hist-row.current { background: rgba(46,170,220,0.05); border-radius: 6px; }
.hist-badge { font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--accent); }
.trash-empty, .hist-empty { padding: 20px; text-align: center; color: var(--text-faint); font-size: 13.5px; }
.trash-icon { font-size: 18px; }
.trash-title { font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#history-list, #trash-list { max-height: 56vh; overflow-y: auto; margin-top: 8px; }

.audit-row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.audit-row:last-child { border-bottom: none; }
.audit-row.denied { background: rgba(214, 69, 61, 0.07); }
.audit-time { color: var(--text-faint); font-size: 11.5px; white-space: nowrap; min-width: 96px; font-variant-numeric: tabular-nums; }
.audit-actor {
  font-size: 11px;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 10px;
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.audit-actor.session { background: rgba(55, 53, 47, 0.08); color: var(--text-soft); }
.audit-actor.token { background: rgba(46, 170, 220, 0.14); color: #1c7da6; }
.audit-actor.assistant { background: rgba(124, 92, 255, 0.13); color: #6a4fd8; }
.audit-actor.automation { background: rgba(255, 171, 0, 0.16); color: #9a6700; }
[data-theme="dark"] .audit-actor.automation { color: #e6b75a; }
[data-theme="dark"] .audit-actor.session { background: rgba(255, 255, 255, 0.08); }
[data-theme="dark"] .audit-actor.token { color: #6cc7ea; }
[data-theme="dark"] .audit-actor.assistant { color: #ab97f5; }
.audit-main { flex: 1; min-width: 0; }
.audit-action { font-weight: 600; margin-right: 6px; }
.audit-row.denied .audit-action { color: #d6453d; }
.audit-summary { color: var(--text-soft); overflow-wrap: break-word; }
.audit-ip { color: var(--text-faint); font-size: 11px; white-space: nowrap; font-family: ui-monospace, Menlo, monospace; }
.audit-empty { padding: 24px; text-align: center; color: var(--text-faint); font-size: 13.5px; }

/* ------------------------------------------------------------ automations */
#automation-list .auto-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--text-soft);
  cursor: pointer;
  user-select: none;
}
#automation-list .auto-item:hover { background: var(--hover); }
#automation-list .auto-item.active { background: var(--active); color: var(--text); font-weight: 500; }
#automation-list .auto-item .a-status {
  margin-left: auto;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 1px 6px;
  border-radius: 8px;
  text-transform: uppercase;
}
.a-status.draft { background: rgba(55, 53, 47, 0.1); color: var(--text-faint); }
.a-status.published { background: rgba(47, 158, 68, 0.16); color: #2f7e44; }
[data-theme="dark"] .a-status.published { color: #69c97e; }
.auto-empty { padding: 2px 12px 6px; font-size: 12.5px; color: var(--text-faint); font-style: italic; }

#automation-view { height: 100%; display: flex; flex-direction: column; }
.ab-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.ab-name {
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  background: transparent;
  border: none;
  outline: none;
  border-radius: 6px;
  padding: 4px 8px;
  width: 240px;
}
.ab-name:hover, .ab-name:focus { background: var(--hover); }
.ab-badge {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 8px;
  border-radius: 9px;
  background: rgba(55, 53, 47, 0.1);
  color: var(--text-faint);
}
.ab-badge.published { background: rgba(47, 158, 68, 0.16); color: #2f7e44; }
[data-theme="dark"] .ab-badge.published { color: #69c97e; }
.ab-actions { margin-left: auto; display: flex; gap: 8px; }

.ab-body { flex: 1; display: flex; min-height: 0; }
.ab-palette {
  width: 190px;
  min-width: 190px;
  border-right: 1px solid var(--border);
  padding: 12px 10px;
  overflow-y: auto;
}
.ab-pal-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  margin: 10px 4px 6px;
}
.ab-pal-label:first-child { margin-top: 0; }
.ab-pal-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 8px;
  border-radius: 7px;
  font-size: 13.5px;
  color: var(--text-soft);
  text-align: left;
}
.ab-pal-item:hover { background: var(--hover); color: var(--text); }
.ab-pal-icon { width: 20px; text-align: center; }
.ab-pal-hint { font-size: 11.5px; color: var(--text-faint); margin: 14px 4px 0; line-height: 1.5; }

.ab-canvas-wrap { flex: 1; overflow: auto; min-width: 0; }
.ab-canvas {
  position: relative;
  width: 3000px;
  height: 2000px;
  background-image: radial-gradient(circle, var(--border) 1px, transparent 1px);
  background-size: 22px 22px;
}
#ab-edges { position: absolute; inset: 0; pointer-events: none; }
#ab-edges path { pointer-events: stroke; cursor: pointer; fill: none; stroke: var(--text-faint); stroke-width: 2; }
#ab-edges path:hover { stroke: #d6453d; stroke-width: 3; }
#ab-edges path.temp { stroke: var(--accent); stroke-dasharray: 5 4; pointer-events: none; }

.auto-node {
  position: absolute;
  width: 180px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(15, 15, 15, 0.06);
  padding: 10px 12px;
  cursor: grab;
  user-select: none;
}
.auto-node.selected { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(46, 170, 220, 0.2); }
.auto-node.start { background: rgba(46, 170, 220, 0.07); }
.auto-node .an-title {
  font-size: 13.5px;
  font-weight: 600;
  display: flex;
  gap: 7px;
  align-items: center;
  pointer-events: none;
}
.auto-node .an-sub { font-size: 11px; color: var(--text-faint); margin-top: 2px; pointer-events: none; }
.port {
  position: absolute;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--bg);
  border: 2px solid var(--text-faint);
  z-index: 2;
}
.port:hover { border-color: var(--accent); background: var(--accent); }
.port.in { left: -7px; top: 22px; }
.port.out { right: -7px; top: 22px; cursor: crosshair; }
.port.out.true { top: 12px; border-color: #2f9e44; }
.port.out.false { top: 36px; border-color: #d6453d; }
.port-label {
  position: absolute;
  right: -24px;
  font-size: 9px;
  font-weight: 700;
  color: var(--text-faint);
  pointer-events: none;
}
.port-label.true { top: 10px; color: #2f9e44; }
.port-label.false { top: 34px; color: #d6453d; }

.ab-inspector {
  width: 270px;
  min-width: 270px;
  border-left: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
}
.ab-inspector h3 { margin: 0 0 2px; font-size: 17px; }
.ab-inspector .insp-desc { font-size: 12.5px; color: var(--text-soft); margin: 0 0 14px; line-height: 1.5; }
.insp-lbl { display: block; font-size: 12px; font-weight: 600; color: var(--text-soft); margin: 12px 0 4px; }
.ab-inspector textarea.settings-input { resize: vertical; min-height: 90px; font-size: 13px; line-height: 1.5; }
.ab-inspector select.settings-input { width: 100%; }
.insp-actions { display: flex; gap: 8px; margin-top: 18px; }
.insp-empty { color: var(--text-faint); font-size: 13px; margin-top: 20px; text-align: center; }

.run-card { padding: 22px 26px 24px; max-width: 560px; }
#run-log { max-height: 50vh; overflow-y: auto; }
.run-step {
  display: flex;
  gap: 10px;
  padding: 9px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}
.run-step:last-child { border-bottom: none; }
.run-step .rs-mark { font-weight: 700; }
.run-step.ok .rs-mark { color: #2f9e44; }
.run-step.fail .rs-mark { color: #d6453d; }
.run-step .rs-label { font-weight: 600; min-width: 110px; }
.run-step .rs-out { color: var(--text-soft); white-space: pre-wrap; word-break: break-word; }

/* ----------------------------------------------------------- empty state */
#empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-soft);
}
.empty-emoji { font-size: 56px; }
.primary-btn {
  background: var(--accent);
  color: #fff;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
}
.primary-btn:hover { filter: brightness(1.05); }

::selection { background: rgba(46, 170, 220, 0.25); }
