:root {
  color-scheme: light dark;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #4f46e5;
  --accent-soft: rgba(79, 70, 229, 0.11);
  --danger: #dc2626;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.08);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Noto Sans SC", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1e293b;
    --border: #263246;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --accent: #818cf8;
    --accent-soft: rgba(129, 140, 248, 0.16);
    --danger: #f87171;
    --warn-bg: rgba(245, 158, 11, 0.14);
    --warn-text: #fcd34d;
    --shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
  }
}

:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #263246;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #818cf8;
  --accent-soft: rgba(129, 140, 248, 0.16);
  --danger: #f87171;
  --warn-bg: rgba(245, 158, 11, 0.14);
  --warn-text: #fcd34d;
  --shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
code, pre { font-family: var(--mono); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.stack { display: flex; flex-direction: column; gap: 14px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.divider { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

/* Buttons & inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); background: var(--accent); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.06); background: var(--danger); }
.btn-ghost { background: transparent; }
.btn-sm { padding: 4px 9px; font-size: 12px; }
.btn-icon { padding: 7px 10px; }
.btn-block { width: 100%; padding: 10px 14px; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.field > span { font-weight: 500; color: var(--text); }
.field input, .field textarea, .field select {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 11px;
  font: inherit;
  outline: none;
}
.field textarea { resize: vertical; font-family: var(--mono); font-size: 13px; line-height: 1.5; }
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.pill-muted { background: var(--surface-2); color: var(--muted); }
.pill-accent { background: var(--accent-soft); color: var(--accent); }
.pill-warn { background: var(--warn-bg); color: var(--warn-text); }

.alert {
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
}
.alert p { margin: 4px 0 0; }
.alert-warn { background: var(--warn-bg); color: var(--warn-text); }

.form-error { color: var(--danger); font-size: 13px; margin: 0; }
.form-info { color: var(--accent); font-size: 13px; margin: 0; }

/* Login */
.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.brand-lockup { display: flex; align-items: center; gap: 12px; }
.brand-lockup h1 { margin: 0; font-size: 22px; letter-spacing: -0.02em; }
.brand-lockup p { margin: 2px 0 0; font-size: 13px; }
.brand-icon { width: 38px; height: 38px; flex-shrink: 0; }
.login-hint { font-size: 12px; margin: 0; }

/* App shell */
.app-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand-inline { display: flex; align-items: center; gap: 8px; }
.brand-icon-sm { width: 24px; height: 24px; }
.brand-name { font-size: 16px; font-weight: 700; letter-spacing: -0.02em; }

.search-wrap {
  flex: 1;
  max-width: 560px;
  position: relative;
  display: flex;
  align-items: center;
}
.search-wrap input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 9px;
  padding: 8px 34px 8px 12px;
  font-size: 13.5px;
  outline: none;
}
.search-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.icon-btn {
  position: absolute;
  right: 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 18px;
  overflow-y: auto;
}
.sidebar-heading {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 0 8px 6px;
}
.nav-list { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  width: 100%;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-count { font-size: 11px; color: var(--muted); font-family: var(--mono); }
.nav-item.active .nav-count { color: var(--accent); }
.sidebar-footer {
  margin-top: auto;
  padding: 10px 8px 0;
  border-top: 1px solid var(--border);
  font-size: 11.5px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  word-break: break-all;
}

/* List pane */
.content-pane {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  padding: 16px 20px 40px;
}
.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  min-height: 24px;
}
.list-summary { font-size: 12.5px; }
.memory-list { display: flex; flex-direction: column; gap: 10px; }

.memory-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 0.14s, box-shadow 0.14s;
}
.memory-card:hover { border-color: var(--accent); }
.memory-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.memory-card.archived { opacity: 0.72; }

.card-top { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.mono-id { font-family: var(--mono); font-size: 12px; color: var(--accent); font-weight: 600; flex-shrink: 0; }
.card-title { font-size: 14.5px; font-weight: 600; margin: 0; color: var(--text); word-break: break-word; }
.card-snippet { margin: 0 0 10px; color: var(--muted); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 11.5px; color: var(--muted); }
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { background: var(--surface-2); color: var(--muted); padding: 2px 7px; border-radius: 6px; font-size: 11.5px; }

.empty-state {
  text-align: center;
  padding: 64px 24px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty-state p { margin: 6px 0; }
.load-more { display: flex; justify-content: center; margin-top: 16px; }

/* Detail pane */
.detail-pane {
  width: 48%;
  max-width: 680px;
  min-width: 340px;
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.detail-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.detail-id-group { display: flex; align-items: center; gap: 8px; }
.detail-actions { display: flex; align-items: center; gap: 6px; }
.detail-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.detail-body h2 { margin: 0; font-size: 18px; line-height: 1.35; }
.detail-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 8px 14px;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: 8px;
  font-size: 12px;
}
.meta-item { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.meta-label { color: var(--muted); font-size: 11px; }
.meta-val { font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.memory-content {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.65;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

.history-section {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
}
.history-section summary { cursor: pointer; font-weight: 600; font-size: 13px; }
.history-list { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.history-item {
  border-left: 2px solid var(--accent);
  padding: 4px 0 4px 12px;
  font-size: 12.5px;
}
.history-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 4px; }
.history-content {
  margin: 6px 0 0;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 6px;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--mono);
  font-size: 12px;
  max-height: 180px;
  overflow-y: auto;
}

/* Modals */
dialog.modal {
  border: 0;
  padding: 0;
  background: transparent;
  max-width: 620px;
  width: calc(100% - 28px);
  color: var(--text);
}
dialog.modal-wide { max-width: 720px; }
dialog.modal::backdrop { background: rgba(15, 23, 42, 0.52); backdrop-filter: blur(2px); }
.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 22px;
  box-shadow: var(--shadow);
}
.modal-header { display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.connect-tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.tab-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 11px;
  font-size: 12.5px;
  cursor: pointer;
}
.tab-btn.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); font-weight: 600; }
.code-box-wrap { position: relative; }
.code-box {
  margin: 0;
  padding: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
  font-size: 12.5px;
  line-height: 1.55;
}
.code-box-sm { font-size: 12px; padding: 10px 12px; }
.copy-floating { position: absolute; top: 8px; right: 8px; }
.io-block h4 { margin: 0 0 4px; font-size: 14px; }
.io-block p { margin: 0 0 8px; font-size: 12.5px; }

/* Toast */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow);
  z-index: 999;
}

@media (max-width: 960px) {
  .sidebar { display: none; }
  .detail-pane {
    position: fixed;
    inset: 54px 0 0 0;
    width: 100%;
    max-width: none;
    min-width: 0;
    z-index: 40;
  }
  .topbar { flex-wrap: wrap; }
  .search-wrap { order: 3; max-width: none; width: 100%; flex-basis: 100%; }
  .grid-2 { grid-template-columns: 1fr; }
}
