/* b4e-kb console — one stylesheet, no framework, light and dark. */

:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #f0f2ee;
  --border: #dfe3dc;
  --text: #1d231d;
  --muted: #6b7566;
  --accent: #2f7d4f;
  --accent-soft: #e4f1e8;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --warn: #8a6100;
  --warn-soft: #fdf1d6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 20, .06), 0 8px 24px rgba(20, 30, 20, .06);
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #12150f;
  --surface: #1a1e17;
  --surface-2: #222719;
  --border: #323726;
  --text: #e7ebe2;
  --muted: #9aa393;
  --accent: #6fbf8b;
  --accent-soft: #1f3327;
  --danger: #f2837a;
  --danger-soft: #3a1e1c;
  --warn: #e4bb63;
  --warn-soft: #362c14;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The `hidden` attribute is only a UA-stylesheet `display: none`, so ANY author
   `display` on the same element silently beats it and the element keeps
   painting. Every panel here is toggled with `.hidden = true` and carries its
   own display value, so without this the modal overlay and the drawer sit on
   top of the page forever and the console looks empty. Do not remove. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.muted { color: var(--muted); }
.error { color: var(--danger); }
.spacer { flex: 1; }
h1, h2, h3 { margin: 0 0 .4em; line-height: 1.25; }

/* ── buttons & inputs ─────────────────────────────────────────────── */

button, .button {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: .45rem .8rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  white-space: nowrap;
  transition: background .12s ease, border-color .12s ease, opacity .12s ease;
}
button:hover, .button:hover { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary, .button.primary {
  background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600;
}
button.primary:hover { filter: brightness(1.06); background: var(--accent); }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
button.danger:hover { background: var(--danger-soft); }
button.ghost { background: transparent; }
button.ghost:hover { background: var(--surface-2); }

input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .45rem .6rem;
}
input:focus-visible, select:focus-visible, button:focus-visible, .button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ── login gate ───────────────────────────────────────────────────── */

.gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1rem;
}
.gate-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
  width: min(360px, 100%);
  display: grid;
  gap: .6rem;
}
.gate-card p { margin: 0 0 1rem; }
.gate-card label { font-size: .85rem; font-weight: 600; }

.gate-card .gate-hint {
  margin: .8rem 0 0;
  font-size: .8rem;
  line-height: 1.45;
  border-top: 1px solid var(--border);
  padding-top: .8rem;
}

/* ── failure banner ───────────────────────────────────────────────── */

/* A toast disappears in three seconds. A page that failed to load needs to say
   so until someone does something about it. */
.banner {
  display: flex;
  align-items: center;
  gap: .8rem;
  flex-wrap: wrap;
  margin: 1rem 1.2rem 0;
  padding: .7rem 1rem;
  border-radius: var(--radius);
  background: var(--danger-soft);
  border: 1px solid var(--danger);
  color: var(--danger);
}
.banner strong { font-weight: 650; }
.banner .banner-msg { flex: 1; min-width: 200px; }

/* ── top bar ──────────────────────────────────────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .7rem 1.2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: .6rem; }
.brand .logo { font-size: 1.4rem; }
.brand div { display: flex; flex-direction: column; line-height: 1.25; }
.brand .muted { font-size: .8rem; }
.topbar-actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* ── stat tiles ───────────────────────────────────────────────────── */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: .6rem;
  padding: 1rem 1.2rem .2rem;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .7rem .9rem;
}
.tile .n { font-size: 1.5rem; font-weight: 650; letter-spacing: -.02em; }
.tile .k { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* ── controls ─────────────────────────────────────────────────────── */

.controls {
  display: flex;
  gap: .5rem;
  padding: 1rem 1.2rem;
  flex-wrap: wrap;
  align-items: center;
}
.controls #search { flex: 1 1 320px; min-width: 200px; }
.controls #f-year-min { width: 100px; }

.bulkbar {
  display: flex;
  gap: .5rem;
  align-items: center;
  margin: 0 1.2rem;
  padding: .6rem .9rem;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  flex-wrap: wrap;
}
.bulkbar #bulk-count { font-weight: 600; }

/* ── table ────────────────────────────────────────────────────────── */

/* No `overflow-x` here on purpose. Any overflow value other than `visible`
   makes this element a scroll container, and a `position: sticky` header inside
   it then offsets against *this box* instead of the viewport — which parks the
   header permanently on top of the first row. A wide table scrolls the page
   instead; the narrow-screen rules below drop the columns that cause it. */
main { padding: 1rem 1.2rem; }

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .9rem;
  /* No `overflow: hidden` here, tempting as it is for clipping the corners.
     It would make the table a scroll container, and the sticky header would
     then offset from the top of the *table* rather than the viewport — landing
     it squarely on top of the first row. The corners are rounded per-cell
     below instead. */
}

/* Rounded corners without clipping, so `position: sticky` keeps working. */
thead th:first-child { border-top-left-radius: var(--radius); }
thead th:last-child  { border-top-right-radius: var(--radius); }
tbody tr:last-child td:first-child { border-bottom-left-radius: var(--radius); }
tbody tr:last-child td:last-child  { border-bottom-right-radius: var(--radius); }
thead th {
  position: sticky;
  /* Measured from the real toolbar at runtime (see app.js). The fallback only
     applies for the instant before the first measurement. */
  top: var(--topbar-h, 56px);
  background: var(--surface-2);
  text-align: left;
  font-size: .76rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  padding: .6rem .7rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
thead th[data-sort] { cursor: pointer; user-select: none; }
thead th[data-sort]:hover { color: var(--text); }
thead th .arrow { opacity: .9; }
th.check, td.check { width: 34px; padding-left: .8rem; }
th.num, td.num { text-align: right; }
th.actions { width: 1%; }

tbody td { padding: .55rem .7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.selected { background: var(--accent-soft); }

.title-cell { max-width: 420px; }
.title-cell a { color: inherit; font-weight: 550; text-decoration: none; cursor: pointer; }
.title-cell a:hover { text-decoration: underline; }
.title-cell .sub { font-size: .8rem; color: var(--muted); margin-top: .15rem; }
.venue-cell { max-width: 200px; font-size: .85rem; color: var(--muted); }
td .row-actions { display: flex; gap: .3rem; }
td .row-actions button { padding: .2rem .5rem; font-size: .8rem; }

.chip {
  display: inline-block;
  font-size: .72rem;
  padding: .1rem .45rem;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  margin: 0 .2rem .2rem 0;
  white-space: nowrap;
}
.chip.ok    { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.chip.warn  { background: var(--warn-soft);   color: var(--warn);   border-color: transparent; }
.chip.off   { background: var(--danger-soft); color: var(--danger); border-color: transparent; }

.empty { text-align: center; color: var(--muted); padding: 3rem 0; }

.pager {
  display: flex;
  gap: .6rem;
  align-items: center;
  justify-content: center;
  padding: 0 1.2rem 2.5rem;
}

/* ── drawer ───────────────────────────────────────────────────────── */

.drawer {
  position: fixed;
  inset: 0 0 0 auto;
  width: min(620px, 100%);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 40;
  display: flex;
  flex-direction: column;
}
.drawer-head {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .7rem 1rem;
  border-bottom: 1px solid var(--border);
}
.drawer-actions { margin-left: auto; display: flex; gap: .4rem; flex-wrap: wrap; }
#drawer-body { padding: 1.2rem; overflow-y: auto; }
#drawer-body h2 { font-size: 1.15rem; }
#drawer-body .abstract { white-space: pre-wrap; margin: .8rem 0 1.2rem; }
#drawer-body dl { display: grid; grid-template-columns: 150px 1fr; gap: .35rem .8rem; margin: 0 0 1.2rem; }
#drawer-body dt { color: var(--muted); font-size: .82rem; }
#drawer-body dd { margin: 0; overflow-wrap: anywhere; font-size: .88rem; }
#drawer-body table { font-size: .82rem; }
#drawer-body a { color: var(--accent); }

/* ── modal ────────────────────────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 15, .45);
  display: grid;
  place-items: center;
  z-index: 60;
  padding: 1rem;
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  width: min(720px, 100%);
  max-height: 85vh;
  overflow-y: auto;
  padding: 1.4rem;
}
.modal-card .row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin: .5rem 0; }
.modal-card .actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1.2rem; }
.modal-card label.opt {
  display: flex; gap: .5rem; align-items: flex-start; padding: .6rem .7rem;
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer; margin-bottom: .4rem;
}
.modal-card label.opt:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.modal-card label.opt .hint { display: block; font-size: .8rem; color: var(--muted); }

pre.log {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .7rem;
  max-height: 340px;
  overflow: auto;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .8s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }

/* ── toast ────────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  bottom: 1.2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: .6rem 1rem;
  border-radius: 999px;
  z-index: 80;
  box-shadow: var(--shadow);
  font-size: .9rem;
}

@media (max-width: 720px) {
  /* The toolbar wraps and stops being a reliable offset on narrow screens. */
  thead th { top: 0; }
  .venue-cell, td.venue-cell { display: none; }
}
