:root,
:root[data-theme="dark"] {
  --bg: #1a1d23;
  --panel: #22262d;
  --panel-2: #1e2127;
  --panel-hover: #2a2f37;
  --border: #2e333d;
  --border-soft: #262b33;
  --text: #d2d6dd;
  --heading: #f0f2f5;
  --muted: #868d99;
  --accent: #61afef;
  --accent-soft: rgba(97, 175, 239, 0.16);
  --on-accent: #0b1f2e;
  --good: #98c379;
}

:root[data-theme="light"] {
  --bg: #f3f5f8;
  --panel: #ffffff;
  --panel-2: #eceef2;
  --panel-hover: #f4f6f9;
  --border: #dbe0e7;
  --border-soft: #e6e9ee;
  --text: #353a42;
  --heading: #171b22;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --on-accent: #ffffff;
  --good: #16a34a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--border); }
*::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 22px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
}

/* ---------- Section tabs (vertical, in sidebar) ---------- */
.tabs {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.tab {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 12px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, background 0.15s;
}
.tab:hover { color: var(--heading); background: var(--panel-hover); }
.tab:focus,
.tab:focus-visible { outline: none; }
.tab.active {
  color: var(--accent);
  background: var(--accent-soft);
}

.sidebar-divider {
  height: 2px;
  margin: 14px 0;
  background: var(--border);
}

#search {
  flex: 1;
  max-width: 420px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-family: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
  transition: border-color 0.15s;
}
#search::placeholder { color: var(--muted); }
#search:focus { border-color: var(--accent); }

.theme-toggle {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  font-size: 17px;
  line-height: 1;
  color: var(--text);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}
.theme-toggle:hover { color: var(--accent); background: var(--panel-hover); }

/* Flat, borderless icon button (sidebar toggle) */
.icon-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  font-size: 18px;
  line-height: 1;
  color: var(--muted);
  background: transparent;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}
.icon-btn:hover { color: var(--accent); background: var(--accent-soft); }
.icon-btn.active { color: var(--accent); background: var(--accent-soft); }

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

.sidebar {
  --sidebar-w: 248px;
  position: sticky;
  top: 61px;
  flex: 0 0 var(--sidebar-w);
  width: var(--sidebar-w);
  height: calc(100vh - 61px);
  display: flex;
  flex-direction: column;
  overflow: visible;
  padding: 16px 14px;
  border-right: 1px solid var(--border);
  background: var(--panel-2);
  transition: flex-basis 0.26s ease, width 0.26s ease, padding 0.26s ease,
              opacity 0.2s ease, border-right-width 0.26s ease;
}
/* While dragging the resizer, kill transitions so it tracks the cursor */
.sidebar.resizing { transition: none; }

/* Drag handle on the sidebar's right edge */
.sidebar-resizer {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 5;
}
.sidebar-resizer:hover,
.sidebar.resizing .sidebar-resizer { background: var(--accent-soft); }
.layout.sidebar-collapsed .sidebar-resizer { display: none; }

.sidebar .nav-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}
/* Wrap long template names instead of clipping them */
.sidebar .nav-link {
  white-space: normal;
  overflow-wrap: anywhere;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-soft);
}

.sidebar-footer .theme-toggle {
  margin-left: auto;
}

.layout.sidebar-collapsed .sidebar {
  flex-basis: 0;
  width: 0;
  padding-left: 0;
  padding-right: 0;
  border-right-width: 0;
  opacity: 0;
  pointer-events: none;
}

.content {
  flex: 1;
  min-width: 0;
  max-width: 920px;
  margin: 0 auto;
  padding: 32px 40px 140px;
}

/* ---------- Sidebar nav ---------- */
.nav-link {
  display: block;
  padding: 7px 12px;
  margin: 2px 0;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--muted);
  text-decoration: none;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.nav-link:hover {
  background: var(--accent-soft);
  color: var(--accent);
}
.nav-link:focus,
.nav-link:focus-visible { outline: none; }

/* Template items: leading dot marker */
.nav-item {
  position: relative;
  padding-left: 24px;
}
.nav-item::before {
  content: "";
  position: absolute;
  left: 10px;
  top: 15px; /* aligns to the first text line, even when the name wraps */
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 0.15s;
}
.nav-item:hover::before { background: var(--accent); }

/* Selected template */
.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  box-shadow: inset 2px 0 0 var(--accent);
}
.nav-item.active::before { background: var(--accent); }

/* ---------- Sidebar sub-topic groups ---------- */
.nav-group { margin: 2px 0; }
.nav-group-head {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  cursor: pointer;
  border-radius: 6px;
  user-select: none;
}
.nav-group-head:hover { color: var(--accent); }
.nav-group-caret { font-size: 14px; width: 14px; line-height: 1; }
.nav-group-body { overflow: hidden; }
.nav-group.collapsed .nav-group-body { display: none; }
/* Indent grouped items so the dot markers line up under the heading */
.nav-item.in-group { padding-left: 34px; }
.nav-item.in-group::before { left: 20px; }

/* ---------- Content sections ---------- */
.section-heading {
  margin: 0 0 22px;
  padding-bottom: 10px;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--heading);
  border-bottom: 2px solid var(--accent);
}

.card {
  margin-bottom: 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  scroll-margin-top: 76px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 16px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
}

.card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--heading);
}

.card-lang {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-left: 10px;
  padding: 2px 8px;
  border-radius: 4px;
  background: var(--accent-soft);
  vertical-align: middle;
}

.card-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

.card-footer {
  display: flex;
  padding: 9px 16px;
  border-top: 1px solid var(--border-soft);
}

.copy-btn {
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.copy-btn:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.copy-btn.copied { color: var(--good); border-color: var(--good); background: transparent; }

.note-toggle {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  transition: color 0.15s;
}
.note-toggle:hover,
.note-toggle.active { color: var(--accent); }

.note-panel {
  padding: 14px 18px;
  font-size: 13px;
  line-height: 1.65;
  color: var(--text);
  white-space: pre-wrap;
  background: var(--panel-2);
  border-top: 1px solid var(--border);
  border-left: 3px solid var(--accent);
}
.note-panel.note-empty {
  color: var(--muted);
  font-style: italic;
  border-left-color: var(--border);
}

/* ---- Markdown rendering inside notes ---- */
.note-panel.markdown {
  white-space: normal;
}
.note-panel.markdown > :first-child { margin-top: 0; }
.note-panel.markdown > :last-child { margin-bottom: 0; }
.note-panel.markdown p { margin: 0 0 8px; }
.note-panel.markdown ul,
.note-panel.markdown ol { margin: 6px 0; padding-left: 22px; }
.note-panel.markdown li { margin: 2px 0; }
.note-panel.markdown h1,
.note-panel.markdown h2,
.note-panel.markdown h3 {
  margin: 10px 0 6px;
  font-size: 14px;
  font-weight: 700;
  color: var(--heading);
}
.note-panel.markdown a { color: var(--accent); text-decoration: none; }
.note-panel.markdown a:hover { text-decoration: underline; }
.note-panel.markdown strong { font-weight: 700; color: var(--heading); }
.note-panel.markdown code {
  font-family: "Roboto Mono", "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--panel-hover);
  border: 1px solid var(--border-soft);
}
.note-panel.markdown pre {
  margin: 8px 0;
  padding: 12px 14px;
  border-radius: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  overflow-x: auto;
}
.note-panel.markdown pre code {
  padding: 0;
  border: none;
  background: none;
}
.note-panel.markdown blockquote {
  margin: 8px 0;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.note-panel.markdown table {
  border-collapse: collapse;
  margin: 8px 0;
}
.note-panel.markdown th,
.note-panel.markdown td {
  border: 1px solid var(--border);
  padding: 4px 10px;
  text-align: left;
}
.note-panel.markdown img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 8px 0;
  border-radius: 6px;
}

/* ---------- Decision-flow overview (clickable image) ---------- */
.flow-overview {
  position: relative;
  display: inline-block;
  max-width: 100%;
  line-height: 0;
}
.flow-overview img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  background: #fff;
}
.flow-hotspot {
  position: absolute;
  border-radius: 6px;
  transition: background 0.15s, box-shadow 0.15s;
}
.flow-hotspot:hover,
.flow-hotspot:focus-visible {
  background: var(--accent-soft);
  box-shadow: 0 0 0 2px var(--accent);
  outline: none;
}

pre {
  margin: 0;
  overflow-x: auto;
}
pre code.hljs {
  padding: 18px;
  font-size: 13px;
  line-height: 1.6;
  font-family: "Roboto Mono", "SF Mono", Menlo, Consolas, monospace;
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
  background: var(--panel);
}

.empty {
  color: var(--muted);
  font-style: italic;
  padding: 8px 2px;
}

.no-results {
  color: var(--muted);
  padding: 48px 0;
  text-align: center;
  font-size: 14px;
}

/* ---------- Grid view (small windows) ---------- */
.content.grid {
  max-width: 1500px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: 18px;
  align-items: start;
}
.content.grid .section-heading {
  grid-column: 1 / -1;
}
.content.grid .no-results,
.content.grid .empty {
  grid-column: 1 / -1;
}
.content.grid .card {
  margin-bottom: 0;
}
/* Keep each window compact: cap code height and scroll inside */
.content.grid pre code.hljs {
  max-height: 300px;
  overflow: auto;
}

/* ---------- Pager (paged view) ---------- */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-bottom: 20px;
}

.pager-btn {
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 16px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.pager-btn:hover:not(:disabled) { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.pager-btn:disabled { opacity: 0.4; cursor: default; }

.pager-info {
  font-size: 13px;
  color: var(--muted);
  min-width: 56px;
  text-align: center;
}

/* Suppress focus outlines on all control buttons (search keeps its ring) */
.tab:focus, .tab:focus-visible,
.nav-link:focus, .nav-link:focus-visible,
.icon-btn:focus, .icon-btn:focus-visible,
.theme-toggle:focus, .theme-toggle:focus-visible,
.copy-btn:focus, .copy-btn:focus-visible,
.note-toggle:focus, .note-toggle:focus-visible,
.pager-btn:focus, .pager-btn:focus-visible {
  outline: none;
}
