/* ==========================================================================
   Meridian — knowledge base viewer
   Light: Notion warm-minimal · Dark: Linear precision
   Self-contained. No external assets.
   ========================================================================== */

/* ---------- Design tokens ---------- */
:root {
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto,
    "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "Cascadia Code", "Segoe UI Mono",
    Menlo, Consolas, monospace;

  --bg: #f7f7f6;
  --surface: #ffffff;
  --surface-2: #f0f0ee;
  --surface-3: #e9e9e7;
  --text: #1b1b1e;
  --text-2: #5d5d63;
  --text-3: #94949c;
  --border: rgba(22, 22, 28, 0.08);
  --border-strong: rgba(22, 22, 28, 0.16);
  --accent: #5a63d8;
  --accent-strong: #4a52c0;
  --accent-soft: rgba(90, 99, 216, 0.11);
  --ring: rgba(90, 99, 216, 0.38);
  --mark-bg: rgba(90, 99, 216, 0.16);
  --topbar-bg: rgba(247, 247, 246, 0.82);
  --shadow-card: 0 1px 2px rgba(20, 20, 26, 0.04),
    0 4px 12px rgba(20, 20, 26, 0.05), 0 14px 34px rgba(20, 20, 26, 0.05);
  --shadow-modal: 0 2px 6px rgba(10, 10, 16, 0.06),
    0 14px 44px rgba(10, 10, 16, 0.14);
  --notice-fg: #7a4b00;
  --notice-bg: #fdf3e0;
  --notice-border: rgba(190, 120, 10, 0.28);

  /* category accents (light) */
  --cat-persona: #6e56cf, #f0ebfc;
  --cat-business: #2f6fdb, #e9f0fd;
  --cat-call-rules: #0e8a7c, #e3f6f2;
  --cat-processes: #a15c00, #fcf0dc;
  --cat-operations: #c0395f, #fbe9ef;
  --cat-general: #6b6b74, #efefef;
}

[data-theme="dark"] {
  --bg: #0b0c0e;
  --surface: #141519;
  --surface-2: #1c1d22;
  --surface-3: #232429;
  --text: #ececf1;
  --text-2: #a2a4ad;
  --text-3: #6d6f7a;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.15);
  --accent: #7b82f2;
  --accent-strong: #a5aaf6;
  --accent-soft: rgba(123, 130, 242, 0.15);
  --ring: rgba(123, 130, 242, 0.45);
  --mark-bg: rgba(123, 130, 242, 0.30);
  --topbar-bg: rgba(11, 12, 14, 0.80);
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  --shadow-modal: 0 4px 12px rgba(0, 0, 0, 0.4), 0 24px 64px rgba(0, 0, 0, 0.5);
  --notice-fg: #e8bd74;
  --notice-bg: rgba(240, 180, 80, 0.08);
  --notice-border: rgba(240, 180, 80, 0.25);

  --cat-persona: #a78bfa, rgba(139, 106, 250, 0.14);
  --cat-business: #7aa2f7, rgba(90, 140, 255, 0.14);
  --cat-call-rules: #4fd1c0, rgba(52, 211, 153, 0.12);
  --cat-processes: #f2b45c, rgba(242, 180, 92, 0.13);
  --cat-operations: #f27fa6, rgba(240, 110, 160, 0.13);
  --cat-general: #9a9aa5, rgba(255, 255, 255, 0.08);
}

/* Category color plumbing: .cat-<slug> exposes --cat-fg / --cat-bg */
.cat-persona { --cat-fg: var(--cat-persona); }
.cat-business { --cat-fg: var(--cat-business); }
.cat-call-rules { --cat-fg: var(--cat-call-rules); }
.cat-processes { --cat-fg: var(--cat-processes); }
.cat-operations { --cat-fg: var(--cat-operations); }
.cat-general { --cat-fg: var(--cat-general); }

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background 0.2s ease, color 0.2s ease;
}
h1, h2, h3, p { margin: 0; }
button { font: inherit; color: inherit; background: none; border: none; padding: 0; cursor: pointer; }
input { font: inherit; }
[hidden] { display: none !important; }
code {
  font-family: var(--font-mono);
  font-size: 0.92em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
::selection { background: var(--mark-bg); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

/* Subtle scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-track { background: transparent; }

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: 264px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  transition: transform 0.22s ease;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 16px 14px;
}
.brand-mark { width: 28px; height: 28px; flex: none; border-radius: 8px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-name { font-size: 14px; font-weight: 650; letter-spacing: -0.01em; }
.brand-sub { font-size: 11px; color: var(--text-3); margin-top: 2px; }

.nav { flex: 1; padding: 4px 10px 16px; }
.nav-section { margin-bottom: 18px; }
.nav-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-3);
  padding: 8px 10px 6px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 7px 10px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  text-align: left;
  transition: background 0.12s ease, color 0.12s ease;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-strong); }
.nav-icon { width: 15px; height: 15px; flex: none; opacity: 0.75; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--cat-fg, var(--text-3)); flex: none; }
.nav-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-count { font-size: 11px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.nav-item.active .nav-count { color: inherit; }

.file-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-2);
}
.file-item svg { width: 14px; height: 14px; flex: none; color: var(--text-3); }
.file-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--font-mono); font-size: 11.5px; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-3);
}
.stats { font-variant-numeric: tabular-nums; }

/* ---------- Backdrop (mobile) ---------- */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 14, 0.45);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.22s ease;
}

/* ---------- Topbar ---------- */
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
  padding: 0 24px;
  flex: none;
  background: var(--topbar-bg);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}
.menu-btn { display: none; }

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  max-width: 540px;
  height: 34px;
  padding: 0 12px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 8px;
  transition: border-color 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}
.search-wrap:focus-within {
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}
.search-icon { width: 15px; height: 15px; flex: none; color: var(--text-3); }
.search-wrap input {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  outline: none;
  font-size: 13.5px;
  color: var(--text);
}
.search-wrap input::placeholder { color: var(--text-3); }
.search-wrap input::-webkit-search-cancel-button { -webkit-appearance: none; }
.search-kbd {
  flex: none;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-3);
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 2px 6px;
  background: var(--surface);
}
.result-count {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-3);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--text-2);
  transition: background 0.12s ease, color 0.12s ease;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn svg { width: 16px; height: 16px; }
.icon-sun, .icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: block; }

/* ---------- Content ---------- */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 28px 32px 64px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}
.content-head { margin-bottom: 18px; }
.content-head h1 {
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.015em;
  color: var(--text);
}
.content-sub { font-size: 13px; color: var(--text-3); margin-top: 4px; }

/* ---------- Notice (file:// fallback banner) ---------- */
.notice {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 18px;
  padding: 10px 14px;
  border: 1px solid var(--notice-border);
  border-radius: 10px;
  background: var(--notice-bg);
  color: var(--notice-fg);
  font-size: 12.5px;
  line-height: 1.6;
}
.notice strong { font-weight: 600; }

/* ---------- Tag filter bar ---------- */
.tagbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 18px;
}
.tag-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 11px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-2);
  transition: border-color 0.12s ease, color 0.12s ease, background 0.12s ease;
}
.tag-btn:hover { border-color: var(--accent); color: var(--accent-strong); }
.tag-btn.active {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent-strong);
}
.tag-count { font-size: 10.5px; opacity: 0.7; font-variant-numeric: tabular-nums; }

/* ---------- Entry grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 14px;
}
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}
.card:hover {
  border-color: var(--border-strong);
  transform: translateY(-1px);
  box-shadow: var(--shadow-card);
}
.card-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-date { font-size: 11px; color: var(--text-3); white-space: nowrap; }
.chip {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  background: var(--cat-bg, var(--surface-2));
  color: var(--cat-fg, var(--text-2));
  white-space: nowrap;
}
.card-title {
  font-size: 14.5px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.4;
  color: var(--text);
}
.card-excerpt {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-2);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-tags { display: flex; flex-wrap: wrap; gap: 5px; min-height: 20px; }
.tag {
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 10.5px;
  font-weight: 500;
}
.card-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}
.card-source {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.card-open {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--accent);
  opacity: 0;
  transform: translateX(-3px);
  transition: opacity 0.15s ease, transform 0.15s ease;
  flex: none;
}
.card-open svg { width: 12px; height: 12px; }
.card:hover .card-open, .card:focus-visible .card-open { opacity: 1; transform: none; }

mark {
  background: var(--mark-bg);
  color: inherit;
  border-radius: 3px;
  padding: 0 1px;
}

/* ---------- Skeleton ---------- */
.card.skel { pointer-events: none; min-height: 150px; }
.skel-line {
  height: 11px;
  border-radius: 6px;
  background: var(--surface-2);
  background-image: linear-gradient(90deg, transparent, var(--surface-3), transparent);
  background-size: 200px 100%;
  animation: shimmer 1.3s infinite linear;
}
.skel-line.w30 { width: 30%; }
.skel-line.w50 { width: 50%; }
.skel-line.w70 { width: 70%; }
.skel-line.w90 { width: 90%; }
@keyframes shimmer {
  from { background-position: -200px 0; }
  to { background-position: 200px 0; }
}

/* ---------- Empty state ---------- */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 72px 24px;
  gap: 6px;
}
.empty-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-3);
  margin-bottom: 10px;
}
.empty-icon svg { width: 20px; height: 20px; }
.empty h3 { font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.empty p { font-size: 13px; color: var(--text-2); margin-bottom: 14px; }
.btn {
  padding: 7px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-2);
  transition: border-color 0.12s ease, color 0.12s ease;
}
.btn:hover { border-color: var(--accent); color: var(--accent-strong); }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; }
.modal[hidden] { display: none; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 14, 0.5);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.16s ease;
}
.modal-panel {
  position: relative;
  width: min(680px, calc(100vw - 32px));
  max-height: 82vh;
  overflow-y: auto;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  box-shadow: var(--shadow-modal);
  animation: modalIn 0.18s cubic-bezier(0.2, 0.8, 0.3, 1);
}
@keyframes fadeIn { from { opacity: 0; } }
@keyframes modalIn {
  from { opacity: 0; transform: translateY(10px) scale(0.985); }
}
.modal-head { display: flex; align-items: center; gap: 10px; }
.modal-source {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-3);
  margin-right: auto;
}
.modal-panel h2 {
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.015em;
  line-height: 1.3;
  margin: 16px 0 6px;
  padding-right: 8px;
}
.modal-meta { font-size: 12px; color: var(--text-3); margin-bottom: 18px; }
.modal-body { font-size: 14.5px; line-height: 1.7; color: var(--text); }
.modal-body p { margin-bottom: 12px; }
.modal-body p:last-child { margin-bottom: 0; }
.modal-body strong { font-weight: 600; }
.modal-body a { color: var(--accent-strong); text-decoration: none; }
.modal-body a:hover { text-decoration: underline; text-underline-offset: 2px; }
.modal-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--border); }
body.modal-open { overflow: hidden; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 264px;
    z-index: 50;
    transform: translateX(-100%);
    box-shadow: var(--shadow-modal);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  body.sidebar-open .backdrop { opacity: 1; }
  .menu-btn { display: inline-flex; }
  .content { padding: 20px 20px 48px; }
}

@media (max-width: 560px) {
  .topbar { padding: 0 12px; gap: 8px; }
  .result-count { display: none; }
  .grid { grid-template-columns: 1fr; }
  .modal-panel { padding: 20px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
