/*
  App mode: the shell and parts used by the portals and client apps.
  Structure follows the Process Registry (slim top bar, narrow sidebar, compact
  tables, pill badges); colours come from tokens.css. Load tokens.css first.

  Page skeleton:
    <body class="app">
      <header class="topbar">…</header>
      <nav class="sidebar">…</nav>
      <main class="main">…</main>
*/
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body.app {
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  grid-template-rows: 52px minmax(0, 1fr);
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--muted); }
.num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }
/* Icons: <svg class="i"><use href="#i-name"/></svg>, drawn as outlines in the text colour */
.i { width: 16px; height: 16px; flex-shrink: 0; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* TOP BAR */
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--bar);
  color: var(--bar-ink);
  border-bottom: 1px solid var(--bar-line);
}
.topbar :focus-visible { outline-color: var(--gold-soft); }
.brand { display: flex; align-items: center; gap: 9px; color: inherit; text-decoration: none; white-space: nowrap; }
.brand-mark {
  display: grid; place-items: center; width: 24px; height: 27px;
  background: var(--gold); color: var(--navy);
  clip-path: polygon(50% 0, 96% 14%, 90% 73%, 50% 100%, 10% 73%, 4% 14%);
  font: 700 15px var(--serif);
}
.brand-name { font: 600 16px var(--serif); }
.brand-app {
  font-family: var(--mono); font-size: 11px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--bar-muted); padding-left: 10px; border-left: 1px solid var(--bar-line);
}
.topbar .sep { flex: 1; }
.bar-btn {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 10px;
  background: transparent; color: var(--bar-ink); border: 1px solid var(--bar-line);
  border-radius: var(--radius); cursor: pointer; font-size: 13px; white-space: nowrap;
}
.bar-btn:hover { border-color: var(--bar-muted); }
.bar-btn svg { width: 16px; height: 16px; }
.avatar {
  display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--gold); color: var(--navy); font-size: 10px; font-weight: 700;
}
.menu-btn { display: none; }

/* SIDEBAR */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  padding: 8px 0 16px;
  overflow-y: auto;
}
.nav-section {
  padding: 0 14px 6px; margin-top: 14px;
  font-family: var(--mono); font-size: 10px; letter-spacing: .1em; text-transform: uppercase;
  color: var(--muted);
}
.nav-item {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 7px 14px; border: 0; border-left: 2px solid transparent;
  background: none; color: var(--muted); font-size: 13px; text-align: left;
  text-decoration: none; cursor: pointer;
}
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.nav-item:hover { color: var(--ink); background: var(--surface-2); }
.nav-item.active { color: var(--ink); font-weight: 600; border-left-color: var(--gold); background: var(--active-bg); }
.nav-count {
  margin-left: auto; font-family: var(--mono); font-size: 11px; font-weight: 400;
  padding: 0 7px; border-radius: 20px; background: var(--surface-2); color: var(--muted);
}
.scrim { display: none; }

/* MAIN */
.main {
  overflow-y: auto;
  padding: 24px 28px 40px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  /* Rows as tall as their content: cards (overflow: hidden) would otherwise be
     squashed to fit the window instead of the page scrolling */
  grid-auto-rows: max-content;
  gap: 18px;
}
/* Several pages inside one .main: each <section class="page"> lays out as if it were .main itself */
.page { display: contents; }
.page-header { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-title { font-size: 21px; font-weight: 700; letter-spacing: -.3px; }
.page-title .dim { color: var(--muted); font-weight: 400; }
.page-header .sep { flex: 1; }
.page-intro { color: var(--muted); max-width: 70ch; }

/* BUTTONS */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px; border: 1px solid transparent; border-radius: var(--radius);
  font-size: 13px; font-weight: 600; text-decoration: none; white-space: nowrap; cursor: pointer;
}
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { opacity: .88; }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { border-color: var(--muted); }
.btn-danger { background: var(--fail); color: var(--surface); }
.btn-danger:hover { opacity: .88; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: default; }

/* TOOLBAR */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.search {
  display: flex; align-items: center; gap: 8px; flex: 1; min-width: 180px; max-width: 380px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 12px;
}
.search svg { width: 15px; height: 15px; color: var(--muted); flex-shrink: 0; }
.search input { background: none; border: 0; outline: 0; width: 100%; font-size: 13px; }
.search:focus-within { border-color: var(--focus); }
.search input::placeholder { color: var(--muted); }
select.filter {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 6px 10px; font-size: 13px; width: 190px; max-width: 100%; cursor: pointer;
}

/* STAT TILES */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat-label { font-family: var(--mono); font-size: 10px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.stat-value { font-size: 24px; font-weight: 700; letter-spacing: -.5px; margin-top: 2px; }
/* DM Mono has no bold: keep mono figures at its heaviest real weight */
.stat-value.num { font-weight: 500; }
.stat-note { font-size: 12px; color: var(--muted); }
.stat-note.ok { color: var(--ok); }
.stat-note.wait { color: var(--wait); }
.stat-note.fail { color: var(--fail); }

/* CARD / TABLE */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.card-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--line); font-weight: 600;
}
.card-head .sep { flex: 1; }
.card-body { padding: 16px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left; padding: 9px 16px; white-space: nowrap;
  font-family: var(--mono); font-size: 11px; font-weight: 500; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); background: var(--surface-2); border-bottom: 1px solid var(--line);
}
tbody tr { border-bottom: 1px solid var(--line); }
tbody tr:last-child { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
tbody td { padding: 9px 16px; vertical-align: middle; }
th.right, td.right { text-align: right; }
td.col-actions { width: 1%; white-space: nowrap; }
.row-title { font-weight: 600; }
.row-sub { font-size: 12px; color: var(--muted); }
.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.row-sub.note { max-width: 28ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.overdue { color: var(--fail); font-weight: 600; }
.nowrap { white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
/* A card that opens and closes: <details class="card"><summary class="card-head"> */
details.card > summary { cursor: pointer; list-style: none; }
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary::after { content: "▾"; margin-left: auto; color: var(--muted); }
details.card[open] > summary::after { content: "▴"; }
details.card:not([open]) > summary { border-bottom: 0; }

/* BADGES */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 20px; white-space: nowrap;
  font-family: var(--mono); font-size: 11px; font-weight: 500;
}
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-wait { background: var(--wait-bg); color: var(--wait); }
.badge-fail { background: var(--fail-bg); color: var(--fail); }
.badge-info { background: var(--info-bg); color: var(--info); }
.badge-done { background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); }
.badge-muted { background: var(--surface-2); color: var(--muted); }
/* A status that can be changed in place: <select class="badge badge-ok"> */
select.badge { border: 0; padding: 4px 8px; cursor: pointer; }
select.badge option { background: var(--surface); color: var(--ink); }
/* Icon-only button in a table row */
.icon-btn { display: inline-grid; place-items: center; width: 30px; height: 30px; border: 0; border-radius: var(--radius); background: none; color: var(--muted); cursor: pointer; }
.icon-btn:hover { background: var(--fail-bg); color: var(--fail); }
.bar-text { font-size: 13px; color: var(--bar-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
a.stat { color: inherit; text-decoration: none; }
a.stat:hover, .stat.active { border-color: var(--gold); }
.show-sm { display: none; }

/* ACTIVITY LIST */
.list { list-style: none; }
.list li { display: flex; gap: 12px; align-items: baseline; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.list time { font-family: var(--mono); font-size: 11px; color: var(--muted); white-space: nowrap; min-width: 86px; }

/* FORMS */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px 18px; }
.field { display: grid; gap: 5px; align-content: start; }
.field.wide { grid-column: 1 / -1; }
.field label, .field .label { font-size: 12px; font-weight: 600; }
.field input, .field select, .field textarea {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 7px 10px; font-size: 13px; width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 0; border-color: var(--focus); box-shadow: 0 0 0 1px var(--focus); }
.field .hint { font-size: 12px; color: var(--muted); }
.field .error { font-size: 12px; color: var(--fail); }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { width: auto; accent-color: var(--accent); }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

/* DIALOG (use the <dialog> element) */
dialog.modal {
  margin: auto; width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); padding: 0;
}
dialog.modal::backdrop { background: rgb(8 20 34 / 55%); }
.modal-title { padding: 14px 18px; font-size: 15px; font-weight: 700; border-bottom: 1px solid var(--line); }
.modal-body { padding: 16px 18px; display: grid; gap: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px; border-top: 1px solid var(--line); }
.kv { display: grid; grid-template-columns: 130px minmax(0, 1fr); gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }

/* NOTICE / TOAST */
.notice {
  display: flex; gap: 10px; align-items: baseline; padding: 10px 14px; font-size: 13px;
  background: var(--info-bg); color: var(--ink); border: 1px solid var(--line); border-left: 3px solid var(--info);
  border-radius: var(--radius);
}
.notice.wait { background: var(--wait-bg); border-left-color: var(--wait); }
.notice.fail { background: var(--fail-bg); border-left-color: var(--fail); }
.notice ul { margin: 4px 0 0 18px; }
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 60; }
.toast {
  background: var(--navy); color: #fff; border-left: 3px solid var(--gold);
  padding: 10px 14px; border-radius: var(--radius); box-shadow: var(--shadow); font-size: 13px;
}

/* SIGN-IN PAGE: <body class="app signin"> with only a .signin-card inside .main */
body.app.signin { grid-template-columns: minmax(0, 1fr); }
/* "safe": a card taller than the window starts at the top and scrolls, instead of being cut off */
body.app.signin .main { place-content: safe center; justify-items: center; }
.signin-card { width: min(380px, 100%); }
.signin-card .card-body { display: grid; gap: 14px; }

/* PHONES AND SMALL TABLETS: the sidebar slides in from the left */
@media (max-width: 800px) {
  body.app { grid-template-columns: minmax(0, 1fr); }
  .menu-btn { display: inline-flex; padding: 0 8px; }
  .brand-app, .bar-btn .wide-only { display: none; }
  .sidebar {
    position: fixed; top: 52px; bottom: 0; left: 0; width: 250px; z-index: 40;
    transform: translateX(-100%); transition: transform .18s ease; box-shadow: var(--shadow);
  }
  .nav-item { padding: 10px 16px; font-size: 14px; }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 52px 0 0 0; z-index: 30; background: rgb(8 20 34 / 45%); }
  .main { padding: 16px 14px 32px; gap: 14px; }
  .page-title { font-size: 19px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .search { max-width: none; flex-basis: 100%; }
  select.filter { flex: 1; width: auto; }
  thead th, tbody td { padding: 9px 6px; }
  tr > :first-child { padding-left: 12px; }
  tr > :last-child { padding-right: 12px; }
  .hide-sm { display: none; }
  .show-sm { display: revert; }
  .kv { grid-template-columns: 1fr; gap: 0 12px; }
  .kv dd { margin-bottom: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
