/*
 * S2RC Operator console.
 *
 * Brand: S2RC Red #ED1C24, S2RC Black #040405, S2RC Grey #939598.
 * Principle, from the brand guide: "When in doubt, choose legibility over
 * style." This is a console somebody reads at seven in the morning on a plant
 * floor tablet, so that rule decides every argument here.
 *
 * Red carries meaning rather than decoration. On this server red means
 * attention: an accident notice, an expired award, a refusal, CAN'T CONFIRM.
 * If every Save button were red, red would stop meaning anything, so primary
 * actions are S2RC Black and red is kept for the brand mark and for things
 * that need somebody to look. White on #ED1C24 is 4.37:1, which is under the
 * 4.5:1 needed for body text anyway.
 *
 * No web fonts are fetched. An air-gapped server has no route to a font CDN,
 * and the only outbound traffic Operator makes is the licence check-in
 * (OP-NF-3). Magistral is the brand typeface; if it is licensed for web, drop
 * the woff2 files into assets/fonts/ and the @font-face rules below pick them
 * up with no other change.
 */

/* Magistral, when it is available. The console works without it. */
@font-face {
  font-family: "Magistral";
  src: url("fonts/magistral-extralight.woff2") format("woff2");
  font-weight: 200;
  font-display: swap;
}
@font-face {
  font-family: "Magistral";
  src: url("fonts/magistral-book.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "Magistral Condensed";
  src: url("fonts/magistral-condensed-black.woff2") format("woff2");
  font-weight: 900;
  font-display: swap;
}

:root {
  --s2rc-red: #ed1c24;
  --s2rc-black: #040405;
  --s2rc-grey: #939598;

  /* A darker red for the few places red sits behind white text. */
  --red-deep: #b8151b;
  --red-wash: #fdeaeb;

  --bg: #f4f5f6;
  --surface: #ffffff;
  --surface-sunk: #fafbfb;
  --border: #dcdee1;
  --border-strong: #b9bdc2;

  --ink: #17181a;
  --ink-soft: #55585d;
  --ink-faint: #74787e;

  --ok: #12704a;
  --ok-wash: #e8f4ee;
  --warn: #8a5a00;
  --warn-wash: #fdf3e0;
  --unknown: #5c6067;
  --unknown-wash: #eceef0;

  --nav-bg: #040405;
  --nav-ink: #e9eaec;
  --nav-ink-faint: #9a9da2;
  --nav-hover: #1a1b1e;

  --font: "Magistral", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-head: "Magistral Condensed", "Magistral", system-ui, -apple-system, "Segoe UI Variable Display", "Segoe UI", sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;

  --radius: 6px;
  --radius-lg: 10px;
  --shadow: 0 1px 2px rgba(4, 4, 5, .06), 0 2px 8px rgba(4, 4, 5, .05);
  --shadow-lg: 0 4px 24px rgba(4, 4, 5, .14);

  --sidebar-w: 248px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0c0d0f;
  --surface: #141619;
  --surface-sunk: #101215;
  --border: #2a2d32;
  --border-strong: #3d4147;

  --ink: #eceef0;
  --ink-soft: #b4b8be;
  --ink-faint: #8b9097;

  --red-wash: #2a1113;
  --ok: #5bc292;
  --ok-wash: #10241c;
  --warn: #e0a44a;
  --warn-wash: #241c0e;
  --unknown: #9aa0a8;
  --unknown-wash: #1c1f23;

  --nav-bg: #08090a;
  --nav-hover: #17191c;

  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-lg: 0 4px 24px rgba(0, 0, 0, .55);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c0d0f;
    --surface: #141619;
    --surface-sunk: #101215;
    --border: #2a2d32;
    --border-strong: #3d4147;
    --ink: #eceef0;
    --ink-soft: #b4b8be;
    --ink-faint: #8b9097;
    --red-wash: #2a1113;
    --ok: #5bc292;
    --ok-wash: #10241c;
    --warn: #e0a44a;
    --warn-wash: #241c0e;
    --unknown: #9aa0a8;
    --unknown-wash: #1c1f23;
    --nav-bg: #08090a;
    --nav-hover: #17191c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 4px 24px rgba(0, 0, 0, .55);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  font-family: var(--font-head);
  font-weight: 800;
  letter-spacing: -0.01em;
  margin: 0;
  line-height: 1.2;
}

h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.02rem; }

a { color: inherit; }

/* Focus is never removed, only made clearer. Somebody keyboarding through a
   long list of people has to be able to see where they are (OP-NF-15). */
:focus-visible {
  outline: 3px solid var(--s2rc-red);
  outline-offset: 2px;
  border-radius: 3px;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Skip link, so a keyboard user is not trapped in the navigation. */
.skip {
  position: absolute; left: 8px; top: -60px; z-index: 100;
  background: var(--surface); color: var(--ink);
  padding: 10px 16px; border-radius: var(--radius);
  box-shadow: var(--shadow-lg); transition: top .12s;
}
.skip:focus { top: 8px; }

/* ---------- Layout ---------- */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--nav-bg);
  color: var(--nav-ink);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar__brand {
  padding: 22px 20px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, .08);
}

/* Clear space equal to the height of the "S", per the brand guide. The
   stacked mark is 216 tall and the wordmark's S is about 40 of that, so the
   padding above works out at roughly that buffer at this size. */
.sidebar__brand img { width: 92px; height: auto; display: block; }

.sidebar__company {
  margin-top: 14px;
  font-size: .82rem;
  color: var(--nav-ink-faint);
  line-height: 1.35;
}
.sidebar__company strong { color: var(--nav-ink); font-weight: 600; display: block; }

.nav { padding: 12px 10px; flex: 1; }
.nav__group + .nav__group { margin-top: 18px; }
.nav__label {
  font-family: var(--font-head);
  font-size: .68rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--nav-ink-faint);
  padding: 0 10px 6px;
}

.nav__item {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0; background: none;
  color: var(--nav-ink);
  font: inherit; text-align: left;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  position: relative;
}
.nav__item:hover { background: var(--nav-hover); }
.nav__item[aria-current="page"] { background: var(--nav-hover); font-weight: 600; }
/* The one place red is purely decorative, and it is 3px wide. */
.nav__item[aria-current="page"]::before {
  content: ""; position: absolute; left: -10px; top: 6px; bottom: 6px;
  width: 3px; background: var(--s2rc-red); border-radius: 0 3px 3px 0;
}
.nav__item svg { width: 17px; height: 17px; flex: none; opacity: .85; }
.nav__count {
  margin-left: auto; font-size: .76rem; color: var(--nav-ink-faint);
  font-variant-numeric: tabular-nums;
}

.sidebar__foot {
  padding: 14px 20px 18px;
  border-top: 1px solid rgba(255, 255, 255, .08);
  font-size: .78rem;
  color: var(--nav-ink-faint);
}
.sidebar__foot button {
  background: none; border: 0; color: var(--nav-ink); font: inherit;
  cursor: pointer; padding: 4px 0; text-decoration: underline;
  text-underline-offset: 3px;
}

.main { min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.topbar__title { flex: 1; min-width: 0; }
.topbar__title p { margin: 2px 0 0; color: var(--ink-faint); font-size: .86rem; }

.content { padding: 24px 28px 64px; flex: 1; }
.content--narrow { max-width: 760px; }

/* ---------- Licence and state banners ---------- */

.banner {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid;
  margin-bottom: 18px;
  font-size: .9rem;
}
.banner strong { display: block; margin-bottom: 2px; }
.banner--warn { background: var(--warn-wash); border-color: var(--warn); color: var(--ink); }
.banner--stop { background: var(--red-wash); border-color: var(--s2rc-red); color: var(--ink); }
.banner--info { background: var(--unknown-wash); border-color: var(--border-strong); color: var(--ink); }
.banner--ok { background: var(--ok-wash); border-color: var(--ok); color: var(--ink); }

/* ---------- Cards and tiles ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}
.card__head {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.card__head h2 { flex: 1; min-width: 0; }
.card__body { padding: 20px; }
.card__body--flush { padding: 0; }

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.tile__label {
  font-size: .78rem; text-transform: uppercase; letter-spacing: .07em;
  color: var(--ink-faint); font-family: var(--font-head); font-weight: 700;
  display: flex; align-items: center; gap: 6px;
}
.tile__value {
  font-family: var(--font-head);
  font-size: 2.1rem; font-weight: 900; line-height: 1.1;
  margin-top: 6px; font-variant-numeric: tabular-nums;
}
.tile__note { font-size: .8rem; color: var(--ink-faint); margin-top: 4px; }

/* The fail-safe tile. Grey, never green, and never a number (OP-ANA-12). */
.tile--unknown { border-color: var(--border-strong); background: var(--unknown-wash); }
.tile--unknown .tile__value {
  font-size: 1.05rem; color: var(--unknown); letter-spacing: .04em;
  font-weight: 800; margin-top: 8px;
}
.tile--good .tile__value { color: var(--ok); }
.tile--attention .tile__value { color: var(--red-deep); }
:root[data-theme="dark"] .tile--attention .tile__value { color: #ff6b70; }

/* ---------- Verdict and status pills ---------- */

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px; border-radius: 999px;
  font-size: .78rem; font-weight: 600; white-space: nowrap;
  border: 1px solid transparent;
}
.pill--ok { background: var(--ok-wash); color: var(--ok); border-color: var(--ok); }
.pill--warn { background: var(--warn-wash); color: var(--warn); border-color: var(--warn); }
.pill--stop { background: var(--red-wash); color: var(--red-deep); border-color: var(--s2rc-red); }
:root[data-theme="dark"] .pill--stop { color: #ff8a8e; }
.pill--unknown { background: var(--unknown-wash); color: var(--unknown); border-color: var(--border-strong); }
.pill--quiet { background: var(--surface-sunk); color: var(--ink-faint); border-color: var(--border); }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 8px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  font: inherit; font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--surface-sunk); border-color: var(--ink-faint); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn--primary {
  background: var(--s2rc-black); border-color: var(--s2rc-black); color: #fff;
}
.btn--primary:hover:not(:disabled) { background: #232427; border-color: #232427; }
:root[data-theme="dark"] .btn--primary { background: #f0f1f2; border-color: #f0f1f2; color: #040405; }
:root[data-theme="dark"] .btn--primary:hover:not(:disabled) { background: #fff; }

/* Red is reserved for the irreversible. */
.btn--danger { background: var(--red-deep); border-color: var(--red-deep); color: #fff; }
.btn--danger:hover:not(:disabled) { background: #9c1117; border-color: #9c1117; }

.btn--quiet { border-color: transparent; background: none; }
.btn--quiet:hover:not(:disabled) { background: var(--surface-sunk); border-color: var(--border); }

.btn--small { padding: 5px 11px; font-size: .86rem; }

.btn-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.btn-row--end { justify-content: flex-end; }

/* ---------- Forms ---------- */

.field { margin-bottom: 18px; }
.field__label {
  display: block; font-weight: 600; margin-bottom: 4px; font-size: .92rem;
}
.field__required { color: var(--red-deep); margin-left: 3px; }
:root[data-theme="dark"] .field__required { color: #ff8a8e; }

/* The module's own help text, written for somebody who has never seen a
   database (OP-MOD-4). It sits above the input, not below, because that is
   where it is read before the mistake rather than after. */
.field__help {
  font-size: .84rem; color: var(--ink-faint); margin: 0 0 6px;
}
.field__error {
  font-size: .86rem; color: var(--red-deep); margin-top: 5px; font-weight: 500;
}
:root[data-theme="dark"] .field__error { color: #ff8a8e; }

input[type="text"], input[type="email"], input[type="password"], input[type="number"],
input[type="date"], input[type="search"], input[type="datetime-local"],
select, textarea {
  width: 100%;
  padding: 8px 11px;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
}
input:disabled, select:disabled, textarea:disabled {
  background: var(--surface-sunk); color: var(--ink-faint);
}
textarea { min-height: 88px; resize: vertical; }

.field--invalid input, .field--invalid select, .field--invalid textarea {
  border-color: var(--s2rc-red);
  background: var(--red-wash);
}

.check { display: flex; align-items: flex-start; gap: 9px; margin-bottom: 10px; }
.check input { margin-top: 3px; flex: none; width: 16px; height: 16px; accent-color: var(--s2rc-red); }
.check span { font-size: .93rem; }
.check small { display: block; color: var(--ink-faint); font-size: .84rem; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; }
@media (max-width: 700px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; }

table.data {
  width: 100%; border-collapse: collapse; font-size: .92rem;
}
table.data th, table.data td {
  text-align: left; padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
table.data thead th {
  font-family: var(--font-head); font-weight: 700;
  font-size: .76rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--ink-faint);
  background: var(--surface-sunk);
  position: sticky; top: 0; z-index: 1;
  white-space: nowrap;
}
table.data thead th button {
  background: none; border: 0; font: inherit; color: inherit; cursor: pointer;
  padding: 0; display: inline-flex; gap: 4px; align-items: center;
  text-transform: inherit; letter-spacing: inherit;
}
table.data tbody tr:hover { background: var(--surface-sunk); }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data .row-actions { white-space: nowrap; text-align: right; }

.empty {
  padding: 44px 20px; text-align: center; color: var(--ink-faint);
}
.empty h3 { margin-bottom: 6px; color: var(--ink); }

/* ---------- Toolbar over a list ---------- */

.toolbar {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--border);
}
.toolbar__search { flex: 1; min-width: 200px; max-width: 380px; }

.pager {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 20px; border-top: 1px solid var(--border);
  font-size: .88rem; color: var(--ink-soft);
}
.pager__count { margin-right: auto; font-variant-numeric: tabular-nums; }

/* ---------- Sign in ---------- */

.signin {
  min-height: 100vh;
  display: grid; place-items: center;
  padding: 24px;
  background: var(--bg);
}
.signin__panel {
  width: 100%; max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 34px 32px 30px;
}
.signin__brand { margin-bottom: 26px; }
.signin__brand img { width: 100%; max-width: 280px; height: auto; display: block; }
.signin__tagline {
  font-family: var(--font-head);
  font-size: .78rem; letter-spacing: .09em; text-transform: uppercase;
  color: var(--ink-faint); margin-top: 12px;
}
.signin__server {
  margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--border);
  font-size: .8rem; color: var(--ink-faint);
}

/* ---------- Drawer, for create and edit ---------- */

.drawer-backdrop {
  position: fixed; inset: 0; background: rgba(4, 4, 5, .45);
  z-index: 40; display: flex; justify-content: flex-end;
}
.drawer {
  background: var(--surface);
  width: min(620px, 100%);
  height: 100%;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg);
}
.drawer__head {
  padding: 18px 24px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
}
.drawer__head h2 { flex: 1; }
.drawer__body { padding: 22px 24px; overflow-y: auto; flex: 1; }
.drawer__foot {
  padding: 16px 24px; border-top: 1px solid var(--border);
  display: flex; gap: 10px; justify-content: flex-end;
  background: var(--surface-sunk);
}

/* ---------- Misc ---------- */

.muted { color: var(--ink-faint); }
.mono { font-family: var(--font-mono); font-size: .88em; }
.stack > * + * { margin-top: 14px; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.definition {
  font-size: .84rem; color: var(--ink-faint);
  border-left: 2px solid var(--border-strong);
  padding-left: 10px; margin-top: 10px;
}

.toast-stack {
  position: fixed; bottom: 20px; right: 20px; z-index: 60;
  display: flex; flex-direction: column; gap: 10px; max-width: 420px;
}
.toast {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-left: 4px solid var(--ink);
  border-radius: var(--radius); box-shadow: var(--shadow-lg);
  padding: 12px 16px; font-size: .9rem;
}
.toast--bad { border-left-color: var(--s2rc-red); }
.toast--good { border-left-color: var(--ok); }
.toast strong { display: block; margin-bottom: 2px; }

.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--s2rc-red);
  animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.5s; }
  * { transition: none !important; }
}

.loading { display: flex; align-items: center; gap: 10px; color: var(--ink-faint); padding: 40px; justify-content: center; }

/* ---------- Narrow screens: the sidebar folds to a top bar ---------- */

@media (max-width: 880px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: static; height: auto;
    flex-direction: row; align-items: center; gap: 12px;
    overflow-x: auto; padding: 0 12px;
  }
  .sidebar__brand { border-bottom: 0; padding: 12px 8px; flex: none; }
  .sidebar__brand img { width: 52px; }
  .sidebar__company { display: none; }
  .nav { display: flex; gap: 6px; padding: 8px 0; }
  .nav__group { display: flex; gap: 6px; }
  .nav__group + .nav__group { margin-top: 0; }
  .nav__label { display: none; }
  .nav__item { padding: 8px 12px; white-space: nowrap; }
  .nav__item[aria-current="page"]::before { left: 0; right: 0; top: auto; bottom: -8px; width: auto; height: 3px; border-radius: 3px 3px 0 0; }
  .nav__count { display: none; }
  .sidebar__foot { border-top: 0; padding: 12px; margin-left: auto; white-space: nowrap; }
  .content, .topbar { padding-left: 16px; padding-right: 16px; }
}

@media print {
  .sidebar, .topbar, .toolbar, .pager, .btn { display: none !important; }
  .content { padding: 0; }
  .card { border: 0; box-shadow: none; }
  body { background: #fff; }
}
