:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 16px rgba(15, 23, 42, 0.04);
  --font: "SF Pro Text", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

/* .login-screen sets display:grid, which outranks the UA sheet's [hidden] rule.
   Without this the login card stays painted over the app after signing in. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.app, .login-screen { min-height: 100vh; }

/* Login */
.login-screen {
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 20% 20%, rgba(37, 99, 235, 0.08), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(180, 83, 9, 0.08), transparent 40%),
    var(--bg);
}
.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  padding: 32px;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 20px; letter-spacing: -0.01em; }
.login-sub { color: var(--muted); margin: 8px 0 20px; }

.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface);
}
.field textarea { min-height: 74px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid rgba(37, 99, 235, 0.35); outline-offset: -1px; border-color: var(--accent);
}

.btn {
  padding: 9px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--bad); border-color: var(--bad-bg); }
.btn.small { padding: 5px 9px; font-size: 12px; }
.btn.ghost { border-color: transparent; background: transparent; }
.error { color: var(--bad); font-size: 13px; margin: 0 0 12px; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 17px; letter-spacing: -0.01em; }
.topbar-left { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.logo-mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, #0f172a, #334155);
  display: inline-block;
}

.tabs {
  display: flex; gap: 2px; background: var(--surface-2);
  padding: 3px; border-radius: 9px;
  /* Eight kind tabs overflow a phone; let the strip scroll instead of clipping. */
  max-width: 100%; overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: transparent; padding: 5px 11px; border-radius: 7px;
  cursor: pointer; color: var(--muted); font-size: 13px; white-space: nowrap;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(15,23,42,.1); font-weight: 600; }

.search { padding: 7px 11px; border: 1px solid var(--border); border-radius: 8px; min-width: 200px; }

/* Layout */
main { padding: 20px; max-width: 1400px; margin: 0 auto; }

.strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.card .big { font-size: 26px; font-weight: 650; letter-spacing: -0.02em; }
.card .sub { color: var(--muted); font-size: 12px; }
.card.alert { border-color: #fecaca; background: linear-gradient(var(--bad-bg), var(--surface) 60%); }
.card.warn { border-color: #fde68a; background: linear-gradient(var(--warn-bg), var(--surface) 60%); }
.card ul { margin: 6px 0 0; padding-left: 0; list-style: none; font-size: 13px; }
.card li { padding: 3px 0; border-bottom: 1px solid var(--border); }
.card li:last-child { border-bottom: 0; }
.card li .when { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-right: 6px; }
.card li button { all: unset; cursor: pointer; }
.card li button:hover { text-decoration: underline; }

/* Table */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; padding: 10px 12px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted); border-bottom: 1px solid var(--border);
  white-space: nowrap; background: var(--surface);
}
td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
.org { font-weight: 600; }
.role { color: var(--muted); }
.nowrap { white-space: nowrap; }
.mono { font-family: var(--mono); font-size: 12px; }

.pill {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; background: var(--surface-2); color: var(--muted);
  white-space: nowrap;
}
.pill.live { background: #dbeafe; color: #1d4ed8; }
.pill.good { background: var(--good-bg); color: var(--good); }
.pill.bad { background: var(--bad-bg); color: var(--bad); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.p1 { background: #ede9fe; color: #6d28d9; }

.due { font-family: var(--mono); font-size: 12px; }
.due.soon { color: var(--warn); font-weight: 600; }
.due.past { color: var(--bad); font-weight: 600; }

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

/* Drawer */
.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .35);
  display: flex; justify-content: flex-end; z-index: 50;
}
.drawer {
  width: min(560px, 100%); height: 100%; background: var(--bg);
  overflow-y: auto; box-shadow: -8px 0 32px rgba(15,23,42,.15);
}
.drawer-head {
  position: sticky; top: 0; background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 16px 20px; display: flex; align-items: flex-start; gap: 12px;
}
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-head .role { font-size: 13px; }
.drawer-body { padding: 20px; display: grid; gap: 16px; }
.drawer-body section { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.drawer-body section h3 { margin: 0 0 12px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > .field { flex: 1 1 150px; margin-bottom: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.timeline li:last-child { border-bottom: 0; }
.timeline .date { font-family: var(--mono); font-size: 11px; color: var(--muted); min-width: 74px; }
.timeline .body { flex: 1; }
.timeline .note { color: var(--muted); }

.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .35);
  display: grid; place-items: center; padding: 20px; z-index: 60;
}
.modal {
  width: min(640px, 100%); max-height: 90vh; overflow-y: auto;
  background: var(--surface); border-radius: calc(var(--radius) + 4px);
  padding: 24px; box-shadow: var(--shadow);
}
.modal h2 { margin: 0 0 16px; font-size: 17px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; z-index: 100; box-shadow: var(--shadow);
}

pre.digest {
  font-family: var(--mono); font-size: 12px; white-space: pre-wrap;
  background: var(--surface-2); padding: 14px; border-radius: 8px; margin: 0;
}

@media (max-width: 700px) {
  main { padding: 12px; }
  .grid-2 { grid-template-columns: 1fr; }
  .search { min-width: 140px; flex: 1; }
  .hide-sm { display: none; }
}

/* Sortable column headers */
th[data-sort] { cursor: pointer; user-select: none; position: relative; padding-right: 22px; }
th[data-sort]:hover { color: var(--text); }
th[data-sort]:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
th[data-sort]::after {
  content: "";
  position: absolute; right: 9px; top: 50%;
  /* Neutral columns show a faint marker so the whole row reads as clickable. */
  border: 4px solid transparent; border-top-color: var(--border);
  transform: translateY(-1px);
}
th[aria-sort="ascending"] { color: var(--text); }
th[aria-sort="descending"] { color: var(--text); }
th[aria-sort="ascending"]::after {
  border-top-color: transparent; border-bottom-color: var(--accent);
  transform: translateY(-7px);
}
th[aria-sort="descending"]::after { border-top-color: var(--accent); }
