:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --ink: #16222e;
  --muted: #5d6b78;
  --faint: #8b98a5;
  --line: #e1e8ee;
  --line-soft: #edf1f5;
  --hover: #f7fafc;
  --accent: #0a5a8a;
  --accent-deep: #0b4d75;
  --accent-soft: #e3eef6;
  --amber: #9a6b00;
  --amber-bg: #fcf3dc;
  --red: #b4232a;
  --red-bg: #fbeaea;
  --shadow: 0 1px 2px rgba(22, 34, 46, 0.04), 0 6px 24px rgba(22, 34, 46, 0.06);
  --sans: "IBM Plex Sans", -apple-system, sans-serif;
  --serif: "IBM Plex Serif", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

/* thin clinical accent line across the very top of the page */
body::before {
  content: "";
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: linear-gradient(90deg, var(--accent), #5b9cc6);
  z-index: 500;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1440px;
  margin: 0 auto;
  padding: 26px 32px 0;
}

/* ---------- masthead ---------- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  /* the action buttons make this row ~16px taller than the masthead text, so a
     smaller margin here gives the same *visual* gap as .search-row's 16px */
  margin-bottom: 9px;
}

.masthead {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 0;
  font-weight: normal;
  white-space: nowrap;
}

.masthead .org {
  font-family: var(--sans);
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--accent);
}

.masthead .sep { color: var(--line); font-size: 15px; }

.masthead .page {
  font-family: var(--sans);
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--ink);
}

.eyebrow {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--accent);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.save-status {
  font-size: 12px;
  color: var(--faint);
  transition: color 0.2s;
}
.save-status.error { color: var(--red); }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 500;
  padding: 7px 15px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
}
.btn svg { width: 13px; height: 13px; }
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: default; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 1px 3px rgba(10, 90, 138, 0.3);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-deep); color: #fff; }

:is(button, input, .pill):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- search ---------- */

.search-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 520px;
}

.search-box svg {
  position: absolute;
  left: 14px;
  top: 50%;
  width: 14px;
  height: 14px;
  transform: translateY(-50%);
  color: var(--faint);
  pointer-events: none;
}

.search-box input {
  width: 100%;
  padding: 9px 16px 9px 38px;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search-box input::placeholder { color: var(--faint); }
.search-box input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.count-label {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

/* ---------- filter pills ---------- */

.pill-row {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 9px;
}

.pill-label {
  flex: 0 0 52px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--faint);
}

/* pills wrap onto extra lines when they outgrow the row */
.pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  row-gap: 7px;
}

.pill {
  font-family: var(--sans);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.pill:hover { border-color: var(--accent); color: var(--accent); }

.pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.pill .n {
  font-family: var(--mono);
  font-size: 10.5px;
  margin-left: 5px;
  opacity: 0.65;
}

/* ---------- type & status dropdowns ---------- */

.dd-group {
  display: flex;
  gap: 6px;
  flex: none;
  margin-left: 10px;
}

.dropdown { position: relative; }

.dd-prefix { flex: none; opacity: 0.7; }

.dd-trigger {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 340px;
  font-family: var(--sans);
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.dd-trigger:hover { border-color: var(--accent); color: var(--accent); }
.dd-trigger.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-deep);
}

.dd-trigger-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dd-chevron { width: 12px; height: 12px; flex: none; opacity: 0.55; }

.dd-panel {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 250;
  width: 280px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(22, 34, 46, 0.16);
  padding: 8px;
}

.dd-search {
  width: 100%;
  padding: 7px 10px;
  font: 400 13px var(--sans);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 6px;
}
.dd-search::placeholder { color: var(--faint); }
.dd-search:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.dd-clear {
  border: 0;
  background: none;
  color: var(--accent);
  font: 500 12px var(--sans);
  cursor: pointer;
  padding: 2px 4px 8px;
}
.dd-clear:hover { text-decoration: underline; }

.dd-options { max-height: 244px; overflow-y: auto; }

.dd-option {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  border-radius: 7px;
  padding: 6px 8px;
  font: 400 13px var(--sans);
  color: var(--ink);
  cursor: pointer;
}
.dd-option:hover { background: var(--hover); }
.dd-option.checked .dd-name { color: var(--accent-deep); font-weight: 500; }

.dd-check {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: #fff;
  transition: background 0.1s, border-color 0.1s;
}
.dd-option.checked .dd-check { background: var(--accent); border-color: var(--accent); }
.dd-check svg { width: 10px; height: 10px; }

.dd-option .n {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--faint);
}

.dd-empty {
  margin: 0;
  padding: 8px;
  font-size: 12.5px;
  color: var(--faint);
}

/* ---------- toggle & segmented filters ---------- */

.toggle-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.toggle-btn .n {
  font-family: var(--mono);
  font-size: 10.5px;
  margin-left: 5px;
  opacity: 0.65;
}

.seg {
  display: inline-flex;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 3px 2px 12px;
}

.seg-prefix {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  opacity: 0.7;
  margin-right: 7px;
}

.seg-btn {
  border: 0;
  background: none;
  font: 500 12px var(--sans);
  color: var(--muted);
  border-radius: 999px;
  padding: 3px 10px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.seg-btn:hover:not(.active) { color: var(--accent); }
.seg-btn.active { background: var(--accent); color: #fff; }

/* ---------- grid ---------- */

.grid-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 240px;
  margin-top: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

#grid { flex: 1; min-height: 0; }

.add-row-strip {
  flex: 0 0 auto;
  border: 0;
  border-top: 1px solid var(--line-soft);
  background: none;
  padding: 9px 14px;
  text-align: left;
  font: 500 12.5px var(--sans);
  color: var(--faint);
  cursor: pointer;
  transition: color 0.15s, background 0.15s;
}
.add-row-strip:hover { color: var(--accent); background: var(--hover); }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: var(--surface);
}
.empty p { color: var(--muted); margin: 0; }

.hint {
  padding: 10px 2px 14px;
  font-size: 11.5px;
  color: var(--faint);
  text-align: center;
}
kbd {
  font-family: var(--mono);
  font-size: 10.5px;
  padding: 1px 5px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
}

/* ---------- handsontable overrides ---------- */

.handsontable {
  font-family: var(--sans);
  font-size: 13.5px;
  color: var(--ink);
}

.handsontable th {
  background: #fafbfc;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  border-color: var(--line) !important;
  vertical-align: middle;
  padding: 4px 6px;
}

.handsontable td {
  border-color: var(--line-soft) !important;
  padding: 7px 10px;
  vertical-align: top;
  line-height: 1.45;
  transition: background 0.1s;
}

.handsontable tbody tr:hover td { background: var(--hover); }

.handsontable .columnSorting:hover { color: var(--accent); }

.handsontable .htAutocompleteArrow { color: transparent; }
.handsontable tr:hover .htAutocompleteArrow { color: var(--faint); }

.handsontable td.cell-del { text-align: center; padding: 4px 0; }

.handsontable td.cell-funded { text-align: center; }
.handsontable td.cell-funded input[type="checkbox"] { accent-color: var(--accent); }

/* archive column: icon header + always-visible (but subtle) row buttons */
.handsontable th .arch-head { display: inline-flex; vertical-align: middle; }
.handsontable th .arch-head svg { width: 14px; height: 14px; }

/* subtle by default, stronger on row hover, accent on button hover — color only:
   opacity transitions make Safari re-composite the layer and the icons shift ~1px */
.row-arch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: none;
  color: var(--line);
  width: 26px;
  height: 26px;
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.1s, background 0.1s;
}
.row-arch svg { width: 15px; height: 15px; }
.handsontable tr:hover .row-arch { color: var(--faint); }
.row-arch:hover { color: var(--accent) !important; background: var(--accent-soft); }

/* drag-to-reorder grip: same subtle→hover ramp as the archive button */
.handsontable td.cell-grip { text-align: center; padding: 4px 0; }
.row-grip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 24px;
  border-radius: 5px;
  color: var(--line);
  cursor: grab;
  transition: color 0.1s;
}
.row-grip svg { width: 13px; height: 13px; }
.handsontable tr:hover .row-grip { color: var(--faint); }
.row-grip:hover { color: var(--accent); }

body.row-dragging { user-select: none; -webkit-user-select: none; }
body.row-dragging * { cursor: grabbing !important; }

/* SortableJS reorder: the picked row becomes a soft placeholder that rides
   along while its neighbours animate out of the way */
.handsontable tbody tr.row-drag-ghost td {
  background: var(--accent-soft) !important;
  box-shadow: inset 0 1px 0 var(--accent), inset 0 -1px 0 var(--accent);
}
/* the floating clone adds nothing over the animated placeholder — hide it
   (a detached <tr> also loses its column widths and renders mangled) */
.row-drag-fallback { opacity: 0 !important; }

/* rows another user just changed flash softly, then fade */
@keyframes remote-flash {
  from { background-color: var(--accent-soft); }
  to { background-color: transparent; }
}
.handsontable tbody tr.row-remote td { animation: remote-flash 1.6s ease-out; }

/* autocomplete dropdown — keep row height at Handsontable's expected ~23px,
   or the list container is sized too short and the last options get cut off */
.handsontable.autocompleteEditor td {
  padding: 0 12px;
  line-height: 23px;
  vertical-align: middle;
  border: 0 !important;
  font-size: 13px;
}
.handsontable.autocompleteEditor td.current,
.handsontable.autocompleteEditor td:hover {
  background: var(--accent-soft);
  color: var(--accent-deep);
}
.handsontable.autocompleteEditor .htCore {
  border: 1px solid var(--line);
  box-shadow: 0 10px 28px rgba(22, 34, 46, 0.14);
}

/* context menu */
.htContextMenu table.htCore {
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(22, 34, 46, 0.18);
  overflow: hidden;
}
.htContextMenu table tbody tr td {
  background: var(--surface);
  border: 0 !important;
  padding: 8px 16px;
  font: 400 13px var(--sans);
  color: var(--ink);
}
.htContextMenu table tbody tr td.current,
.htContextMenu table tbody tr td:hover {
  background: var(--accent-soft);
  color: var(--accent-deep);
}
.htContextMenu table tbody tr td.htSeparator {
  height: 1px;
  padding: 0;
  background: var(--line-soft);
}

/* ---------- add modal ---------- */

dialog {
  border: 0;
  border-radius: 14px;
  padding: 0;
  width: min(560px, calc(100vw - 40px));
  box-shadow: 0 24px 64px rgba(22, 34, 46, 0.25);
}
dialog::backdrop { background: rgba(22, 34, 46, 0.35); }

dialog form { padding: 26px 28px; }

dialog h2 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 19px;
  margin: 0 0 18px;
}

dialog label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 14px;
}

dialog input,
dialog textarea {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 8px 11px;
  font: 400 13.5px var(--sans);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  resize: vertical;
}
dialog :is(input, textarea):focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 14px;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 8px;
}
.check-field input[type="checkbox"] {
  display: inline-block;
  width: auto;
  margin: 0;
  accent-color: var(--accent);
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

/* ---------- tooltip ---------- */

.tip {
  position: fixed;
  transform: translate(-50%, -100%);
  background: var(--ink);
  color: #f5f8fa;
  font: 500 11.5px var(--sans);
  padding: 4px 9px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 600;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--ink);
  color: #f5f8fa;
  font-size: 13.5px;
  padding: 11px 18px;
  border-radius: 999px;
  box-shadow: 0 8px 28px rgba(22, 34, 46, 0.35);
  z-index: 300;
  animation: toast-in 0.18s ease-out;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

.toast-undo {
  border: 0;
  background: none;
  color: #7fc4ee;
  font: 600 13.5px var(--sans);
  cursor: pointer;
  padding: 0;
}
.toast-undo:hover { text-decoration: underline; }

/* ---------- password gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 400;
}

.gate-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(360px, calc(100vw - 48px));
  padding: 38px 34px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.gate-title {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 24px;
  margin: 2px 0 10px;
}

.gate-card input {
  padding: 10px 13px;
  font: inherit;
  text-align: center;
  border: 1px solid var(--line);
  border-radius: 999px;
}
.gate-card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.gate-error {
  font-size: 12.5px;
  color: var(--red);
  margin: 0;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

@media (max-width: 720px) {
  #app { padding: 18px 14px 0; }
  .top { flex-direction: column; align-items: flex-start; gap: 12px; }
  .masthead { flex-wrap: wrap; white-space: normal; }
  .search-row { flex-wrap: wrap; }
  .form-grid { grid-template-columns: 1fr; }
  .pill-row { flex-wrap: wrap; }
  .dd-group { margin-left: 0; }
}
