:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-alt: #f0f1f4;
  --border: #e0e2e7;
  --text: #1c1e26;
  --text-muted: #6b7080;
  --accent: #1a73e8;
  --accent-soft: #e8f0fe;
  --code-bg: #f3f1fb;
  --code-text: #5b3fd6;
  --mark-bg: #fff1a8;
  --star: #f5a623;
  --shadow: 0 1px 2px rgba(20, 22, 30, 0.06), 0 1px 8px rgba(20, 22, 30, 0.04);
  --radius: 10px;
  --sidebar-w: 260px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14151b;
    --surface: #1c1e26;
    --surface-alt: #22242e;
    --border: #2d2f3a;
    --text: #e8e9ee;
    --text-muted: #9498a8;
    --accent: #6ea8ff;
    --accent-soft: #1f2b40;
    --code-bg: #241f38;
    --code-text: #b7a2ff;
    --mark-bg: #5c4c0f;
    --star: #f5c04f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 12px rgba(0, 0, 0, 0.25);
  }
}

:root[data-theme="dark"] {
  --bg: #14151b;
  --surface: #1c1e26;
  --surface-alt: #22242e;
  --border: #2d2f3a;
  --text: #e8e9ee;
  --text-muted: #9498a8;
  --accent: #6ea8ff;
  --accent-soft: #1f2b40;
  --code-bg: #241f38;
  --code-text: #b7a2ff;
  --mark-bg: #5c4c0f;
  --star: #f5c04f;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 12px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

code, .mono {
  font-family: "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

/* ---------- Header ---------- */

.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 17px;
  white-space: nowrap;
}

.brand-icon { font-size: 20px; }

.search-wrap {
  flex: 1;
  position: relative;
  max-width: 560px;
}

.search-wrap input {
  width: 100%;
  padding: 10px 38px 10px 38px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

.search-wrap input:focus {
  border-color: var(--accent);
  background: var(--surface);
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
  font-size: 14px;
}

.clear-btn {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 6px;
  line-height: 1;
  border-radius: 6px;
}

.clear-btn:hover { background: var(--border); }

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}

.icon-btn:hover { border-color: var(--accent); }
.icon-btn.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.icon-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn.primary:hover { opacity: 0.92; }

.kbd-hint {
  display: none;
}

@media (min-width: 720px) {
  .kbd-hint {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 6px;
    font-size: 11px;
    border-radius: 4px;
    background: var(--border);
    color: var(--text-muted);
  }
}

/* ---------- Sync banner ---------- */

.sync-banner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 9px 24px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12.5px;
  text-align: center;
}

/* ---------- Layout ---------- */

.layout {
  max-width: 1280px;
  margin: 0 auto;
  display: flex;
  align-items: flex-start;
  gap: 24px;
  padding: 24px;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  position: sticky;
  top: 76px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.top-level {
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.sidebar-folder + .sidebar-folder {
  margin-top: 4px;
}

.folder-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 700;
  border-radius: 8px;
}

.folder-toggle:hover { background: var(--surface-alt); }

.folder-icon { font-size: 14px; }
.folder-toggle span:nth-child(2) { flex: 1; text-align: left; }

.folder-count {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-alt);
  border-radius: 20px;
  padding: 1px 7px;
  text-transform: none;
  letter-spacing: 0;
}

.chevron {
  transition: transform 0.15s ease;
  font-size: 11px;
}

.folder-toggle[aria-expanded="false"] .chevron { transform: rotate(-90deg); }

.category-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
  max-height: 1000px;
  transition: max-height 0.2s ease, opacity 0.15s ease;
  opacity: 1;
}

.category-list.collapsed {
  max-height: 0;
  opacity: 0;
}

.cat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px;
  border: none;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13.5px;
  text-align: left;
}

.cat-btn:hover { background: var(--surface-alt); }
.cat-btn.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.cat-icon { font-size: 15px; width: 18px; text-align: center; }
.cat-label { flex: 1; }
.cat-count {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--surface-alt);
  border-radius: 20px;
  padding: 1px 7px;
}
.cat-btn.active .cat-count { background: var(--surface); color: var(--accent); }

.content { flex: 1; min-width: 0; }

.content-bar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 14px;
  padding: 0 2px;
}

#resultCount {
  color: var(--text-muted);
  font-size: 13px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 14px;
}

/* ---------- Card ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.card-title-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.card-cat-icon { font-size: 15px; flex-shrink: 0; }

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  word-break: break-word;
}

.card-head-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.star-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 18px;
  color: var(--text-muted);
  padding: 0 2px;
  line-height: 1;
  flex-shrink: 0;
}

.star-btn.active { color: var(--star); }

.edit-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
  padding: 2px 4px;
  line-height: 1;
  flex-shrink: 0;
  border-radius: 5px;
}

.edit-btn:hover { color: var(--accent); background: var(--surface-alt); }

.card-syntax-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--code-bg);
  border-radius: 7px;
  padding: 8px 10px;
}

.card-syntax {
  flex: 1;
  color: var(--code-text);
  font-size: 12.5px;
  word-break: break-all;
  background: transparent;
}

.copy-btn {
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  font-size: 11.5px;
  padding: 4px 9px;
  cursor: pointer;
}

.copy-btn:hover { border-color: var(--accent); color: var(--accent); }
.copy-btn.copied { background: var(--accent); border-color: var(--accent); color: #fff; }
.copy-btn.small { font-size: 11px; padding: 3px 8px; }

.card-desc {
  margin: 0;
  color: var(--text-muted);
  font-size: 13.5px;
}

.card-example {
  background: var(--surface-alt);
  border-radius: 7px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.example-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.example-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  width: 52px;
  flex-shrink: 0;
}

.example-formula {
  flex: 1;
  font-size: 12.5px;
  color: var(--text);
  word-break: break-all;
  background: transparent;
}

.example-result {
  font-size: 12.5px;
  color: var(--text-muted);
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  font-size: 10.5px;
  background: var(--surface-alt);
  color: var(--text-muted);
  border-radius: 20px;
  padding: 2px 9px;
}

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

.badge {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 20px;
  padding: 2px 8px;
  flex-shrink: 0;
}

.badge-warn {
  background: #fdecc8;
  color: #8a5a00;
}

:root[data-theme="dark"] .badge-warn { background: #4a3a12; color: #f0c674; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .badge-warn { background: #4a3a12; color: #f0c674; }
}

/* ---------- Empty state ---------- */

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state .empty-icon {
  font-size: 36px;
  margin-bottom: 10px;
  display: block;
}

/* ---------- Footer ---------- */

.site-footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 12.5px;
  padding: 30px 20px 50px;
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 15, 0.5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 50;
  overflow-y: auto;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 480px;
  box-shadow: var(--shadow);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 { margin: 0; font-size: 16px; }

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}

.modal-close:hover { background: var(--surface-alt); }

.modal-body {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-body label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

.modal-body .hint {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
}

.modal-body input,
.modal-body select,
.modal-body textarea {
  font-family: inherit;
  font-size: 13.5px;
  color: var(--text);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  resize: vertical;
}

.modal-body input:focus,
.modal-body select:focus,
.modal-body textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 4px;
}

.modal-status {
  font-size: 12px;
  color: var(--text-muted);
}

.modal-status.error { color: #d64545; }

.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 7px;
  padding: 9px 16px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:hover { opacity: 0.92; }
.primary-btn:disabled { opacity: 0.6; cursor: default; }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .layout {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    position: static;
    padding: 10px;
  }
  .category-list {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .cat-btn { width: auto; }
}

@media (max-width: 560px) {
  .header-inner { flex-wrap: wrap; }
  .brand { order: 1; }
  .header-actions { order: 2; margin-left: auto; }
  .search-wrap { order: 3; max-width: 100%; width: 100%; }
  .grid { grid-template-columns: 1fr; }
}
