:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --review: #b45309;
  --review-soft: #fef3c7;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.08);
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  line-height: 1.5;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }

/* Header */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1.5rem;
  position: sticky;
  top: 0;
  z-index: 20;
}
header .wrap {
  max-width: 1120px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 2rem;
}
header h1 { font-size: 1.1rem; margin: 0; font-weight: 650; letter-spacing: -0.01em; }
header nav { display: flex; gap: 0.25rem; }
header nav a {
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
}
header nav a:hover { background: var(--bg); color: var(--text); }
header nav a.active { background: var(--accent-soft); color: var(--accent); }

main { max-width: 1120px; margin: 1.5rem auto; padding: 0 1.5rem; }

h2 { font-size: 1.05rem; font-weight: 650; margin: 0 0 1rem; }

/* Summary cards */
.summary-cards { display: flex; gap: 0.85rem; margin-bottom: 1.5rem; flex-wrap: wrap; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1.1rem;
  box-shadow: var(--shadow);
  flex: 1;
  min-width: 130px;
}
.card .value { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.02em; }
.card .label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin-top: 0.1rem; }

/* Tables */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;   /* dense tables scroll horizontally rather than clipping the last column */
}
table { width: 100%; border-collapse: collapse; }
thead th {
  background: #f9fafb;
  text-align: left;
  padding: 0.6rem 0.9rem;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--text); }
thead th .arrow { opacity: 0.4; font-size: 0.7rem; margin-left: 0.2rem; }
thead th.sorted .arrow { opacity: 1; color: var(--accent); }
tbody td { padding: 0.6rem 0.9rem; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.nowrap { white-space: nowrap; }

.subject-link { cursor: pointer; color: var(--accent); }
.subject-link:hover { text-decoration: underline; }

/* Filter bar */
.filters {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
  align-items: end;
}
.filters .field { display: flex; flex-direction: column; gap: 0.2rem; }
.filters label { font-size: 0.7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.filters input, .filters select {
  padding: 0.4rem 0.55rem;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  font-size: 0.85rem;
  background: var(--surface);
  min-width: 150px;
}
.filters input[type="search"] { min-width: 230px; }
.filters .clear-btn {
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 7px;
  cursor: pointer;
  font-size: 0.82rem;
  color: var(--muted);
}
.filters .clear-btn:hover { color: var(--text); }

/* Topic cell with link */
.topic-cell { font-weight: 500; max-width: 320px; }
.topic-cell a { color: var(--text); text-decoration: none; }
.topic-cell a:hover { color: var(--accent); }
.topic-cell .ext { color: var(--muted); font-size: 0.78rem; margin-left: 0.3rem; }

.badge {
  display: inline-block;
  font-size: 0.68rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--review-soft);
  color: var(--review);
  font-weight: 600;
  white-space: nowrap;
}

/* Row actions — Actions column is pinned to the right so Edit is always reachable
   even when the table scrolls horizontally. */
td.actions-cell, th.actions-head {
  white-space: nowrap;
  position: sticky;
  right: 0;
  background: var(--surface);
  box-shadow: -4px 0 6px -4px rgba(16,24,40,0.12);
}
th.actions-head { background: #f9fafb; z-index: 2; }
tbody tr:hover td.actions-cell { background: #fafbfc; }
tr.detail-row td.actions-cell { position: static; box-shadow: none; }
.row-actions { display: inline-flex; gap: 0.35rem; }
.row-actions button {
  padding: 0.25rem 0.55rem;
  font-size: 0.78rem;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  line-height: 1.2;
}
.row-actions button:hover { border-color: var(--accent); color: var(--accent); }
.row-actions button.del:hover { border-color: var(--danger); color: var(--danger); }

/* Expandable detail row */
.expand-toggle { cursor: pointer; user-select: none; color: var(--muted); font-size: 0.9rem; width: 1%; text-align: center; }
.expand-toggle:hover { color: var(--accent); }
tr.detail-row > td { background: #fafbfc; padding: 1rem 1.25rem; }
.detail-grid { display: grid; grid-template-columns: 140px 1fr; gap: 0.4rem 1rem; font-size: 0.88rem; }
.detail-grid .k { color: var(--muted); font-weight: 600; }
.detail-grid .v { min-width: 0; }
.detail-notes { white-space: pre-wrap; margin-top: 0.5rem; padding: 0.75rem; background: var(--surface); border: 1px solid var(--border); border-radius: 7px; font-size: 0.88rem; }
.detail-actions { margin-top: 0.75rem; display: flex; gap: 0.5rem; }

/* Column filter row under headers */
thead tr.filter-row th { padding: 0.4rem 0.6rem; background: var(--surface); }
thead tr.filter-row input, thead tr.filter-row select {
  width: 100%; padding: 0.3rem 0.4rem; border: 1px solid var(--border-strong);
  border-radius: 6px; font-size: 0.8rem; font-weight: 400; text-transform: none; letter-spacing: 0;
}
thead tr.filter-row th.no-filter { background: var(--surface); }

/* View switching */
.view { display: none; }
.view.active { display: block; }

/* Pagination */
.pager { display: flex; justify-content: center; align-items: center; gap: 1rem; margin: 1.25rem 0; font-size: 0.85rem; color: var(--muted); }
.pager button { padding: 0.35rem 0.85rem; border: 1px solid var(--border-strong); background: var(--surface); border-radius: 7px; cursor: pointer; font-size: 0.85rem; }
.pager button:disabled { opacity: 0.4; cursor: default; }
.pager button:not(:disabled):hover { border-color: var(--accent); color: var(--accent); }

/* Edit / Add form */
.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  max-width: 760px;
}
.form-row { display: grid; grid-template-columns: 150px 1fr; gap: 1rem; align-items: start; margin-bottom: 0.9rem; }
.form-row label { font-weight: 600; font-size: 0.85rem; text-align: right; padding-top: 0.5rem; color: #374151; }
.form-row .control { min-width: 0; }
.form-row input, .form-row select, .form-row textarea {
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  font-size: 0.9rem;
  font-family: inherit;
}
.form-row textarea { resize: vertical; min-height: 90px; white-space: pre-wrap; font-family: inherit; }
.form-row .hint { font-size: 0.75rem; color: var(--muted); margin-top: 0.2rem; }
.form-row .readonly { background: #f3f4f6; color: var(--muted); }

.form-actions { display: flex; gap: 0.6rem; margin-top: 1.25rem; padding-left: 166px; flex-wrap: wrap; }

button.primary {
  background: var(--accent); color: #fff; border: none;
  padding: 0.55rem 1.1rem; border-radius: 7px; font-size: 0.88rem; cursor: pointer; font-weight: 550;
}
button.primary:hover { background: #4338ca; }
button.secondary {
  background: var(--surface); color: var(--text); border: 1px solid var(--border-strong);
  padding: 0.55rem 1.1rem; border-radius: 7px; font-size: 0.88rem; cursor: pointer; font-weight: 500;
}
button.secondary:hover { border-color: var(--accent); color: var(--accent); }
button.ghost { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 0.85rem; padding: 0.55rem 0.5rem; }
button.ghost:hover { color: var(--text); }

/* API key bar */
.api-key-bar {
  background: var(--accent-soft);
  border: 1px solid #c7d2fe;
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  margin-bottom: 1.25rem;
  font-size: 0.85rem;
  display: flex;
  gap: 0.6rem;
  align-items: center;
  flex-wrap: wrap;
}
.api-key-bar input { flex: 1; min-width: 200px; padding: 0.4rem 0.6rem; border: 1px solid var(--border-strong); border-radius: 7px; }
.api-key-bar .ok { color: #15803d; font-weight: 600; }
.api-key-bar .no { color: var(--muted); }

/* Toast */
.toast {
  position: fixed; bottom: 1.5rem; right: 1.5rem;
  background: #111827; color: #fff;
  padding: 0.7rem 1.1rem; border-radius: 8px; font-size: 0.88rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  opacity: 0; transform: translateY(10px); transition: all 0.22s; pointer-events: none; z-index: 100;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: #991b1b; }

.empty { text-align: center; color: var(--muted); padding: 2.5rem 1rem; }
.notes-view { white-space: pre-wrap; }

@media (max-width: 680px) {
  .form-row { grid-template-columns: 1fr; }
  .form-row label { text-align: left; padding-top: 0; }
  .form-actions { padding-left: 0; }
}
