/* HUGIN Console — the independent visual system.
 *
 * This file is the enforcement point for parity-spec FR-UI-013 ("own palette, own
 * type scale, own icon set, own chart defaults, specified and enforced in code") and
 * FR-UI-014 ("distinct layout signature"). The choices below are deliberate and the
 * reasoning is recorded here so a design review can check it, per FR-UI-021.
 *
 * Palette: a cool slate ground with an OCHRE primary and a TEAL secondary. No magenta
 * or pink anywhere, as a primary or in the categorical chart sequence — that is an
 * explicit FR-UI-013 prohibition. Ochre was chosen because it reads as "instrument
 * panel" rather than "web app", and because warm-on-cool gives a single accent enough
 * contrast to carry both selection and focus without a second hue.
 *
 * Type: the platform UI stack and the platform mono stack. No web font, because a web
 * font means a network fetch and HUGIN makes no connection the operator did not
 * configure. The scale is a 1.2 ratio from 12px, truncated to six steps.
 *
 * Geometry: 3px radius, 1px borders, 8px spacing unit. Tight and square rather than
 * rounded and airy — a distinct layout signature is the point, and a dense grid is
 * also the right answer for an operator reading a thousand rows.
 *
 * Layout signature: a persistent LEFT RAIL plus a command palette as the primary
 * navigation device, with a 40px header that holds context and nothing else. Both
 * halves of FR-UI-014's specified default.
 *
 * Light and dark are both first class: dark is the default for a console people watch
 * at 03:00, light follows the system preference, and `data-theme` overrides either.
 */

:root {
  color-scheme: dark light;

  /* ---------------------------------------------------------------- palette */
  --ground: #0e1116;
  --surface: #161b22;
  --surface-2: #1c222b;
  --surface-3: #232a35;
  --line: #2a323e;
  --line-strong: #3a4552;

  --ink: #d9dee7;
  --ink-strong: #f2f5f9;
  --ink-muted: #8c98a9;
  --ink-faint: #616c7c;

  /* Ochre primary, teal secondary. Never magenta. */
  --accent: #d99b3d;
  --accent-ink: #1a1206;
  --accent-soft: rgba(217, 155, 61, 0.14);
  --accent-line: rgba(217, 155, 61, 0.42);
  --accent-2: #4fb3a4;

  --ok: #6fae5c;
  --warn: #d9a93d;
  --danger: #d4625c;
  --danger-soft: rgba(212, 98, 92, 0.13);
  --info: #5f8fb0;

  /* Categorical chart sequence. Ochre and teal lead; the rest are muted earth and
   * cool tones, ordered for distinguishability at small size. No pink, no magenta,
   * and not anyone else's default sequence. */
  --series-1: #d99b3d;
  --series-2: #4fb3a4;
  --series-3: #7c86d6;
  --series-4: #8aa35c;
  --series-5: #c2765a;
  --series-6: #5f8fb0;
  --series-7: #9b7fb8;
  --series-8: #b0a05f;

  /* ------------------------------------------------------------------- type */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", monospace;

  --t-xs: 11px;
  --t-sm: 12px;
  --t-md: 13px;
  --t-lg: 16px;
  --t-xl: 19px;
  --t-2xl: 23px;

  /* --------------------------------------------------------------- geometry */
  --r: 3px;
  --r-lg: 5px;
  --u: 8px;
  --rail: 196px;
  --rail-narrow: 52px;
  --header: 40px;
  --stroke: 1.5; /* the declared icon stroke weight, per FR-UI-013 */
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.42);
}

:root:not([data-theme="dark"]) {
  @media (prefers-color-scheme: light) {
    --ground: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eef0f3;
    --surface-3: #e4e7ec;
    --line: #d7dbe2;
    --line-strong: #bcc3ce;
    --ink: #232a35;
    --ink-strong: #0e1116;
    --ink-muted: #5d6877;
    --ink-faint: #8792a1;
    --accent: #9c6612;
    --accent-ink: #fffaf0;
    --accent-soft: rgba(156, 102, 18, 0.1);
    --accent-line: rgba(156, 102, 18, 0.4);
    --accent-2: #2c7f73;
    --shadow: 0 6px 20px rgba(14, 17, 22, 0.14);
  }
}

:root[data-theme="light"] {
  --ground: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --surface-3: #e4e7ec;
  --line: #d7dbe2;
  --line-strong: #bcc3ce;
  --ink: #232a35;
  --ink-strong: #0e1116;
  --ink-muted: #5d6877;
  --ink-faint: #8792a1;
  --accent: #9c6612;
  --accent-ink: #fffaf0;
  --accent-soft: rgba(156, 102, 18, 0.1);
  --accent-line: rgba(156, 102, 18, 0.4);
  --accent-2: #2c7f73;
  --shadow: 0 6px 20px rgba(14, 17, 22, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--ground);
  color: var(--ink);
  font: 400 var(--t-md) / 1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ====================================================================== shell */

#shell {
  display: grid;
  grid-template-columns: var(--rail) 1fr;
  height: 100vh;
}

#shell[data-rail="narrow"] {
  grid-template-columns: var(--rail-narrow) 1fr;
}

/* The left rail. Vertical, persistent, grouped — the layout signature. */
#rail {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--u);
  height: var(--header);
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.brand .mark {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  color: var(--accent);
}

.brand .name {
  font-size: var(--t-md);
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-strong);
  white-space: nowrap;
}

#shell[data-rail="narrow"] .brand .name,
#shell[data-rail="narrow"] .nav-label,
#shell[data-rail="narrow"] .rail-group-title,
#shell[data-rail="narrow"] .rail-foot-text {
  display: none;
}

#rail nav {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--u) 0;
}

.rail-group-title {
  font-size: var(--t-xs);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-faint);
  padding: 12px 14px 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  margin: 1px 6px;
  border-radius: var(--r);
  color: var(--ink-muted);
  cursor: pointer;
  white-space: nowrap;
  border: 1px solid transparent;
  background: none;
  width: calc(100% - 12px);
  text-align: left;
  font: inherit;
}

.nav-item:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.nav-item[aria-current="page"] {
  background: var(--accent-soft);
  border-color: var(--accent-line);
  color: var(--ink-strong);
  font-weight: 550;
}

.nav-item .icon {
  flex: 0 0 auto;
  color: inherit;
}

.rail-foot {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  padding: var(--u);
  display: flex;
  align-items: center;
  gap: var(--u);
  font-size: var(--t-xs);
  color: var(--ink-faint);
}

/* ===================================================================== header */

#main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

#header {
  height: var(--header);
  flex: 0 0 auto;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  display: flex;
  align-items: center;
  gap: var(--u);
  padding: 0 12px;
}

#header h1 {
  margin: 0;
  font-size: var(--t-md);
  font-weight: 600;
  color: var(--ink-strong);
}

#header .sub {
  color: var(--ink-faint);
  font-size: var(--t-sm);
}

.spacer {
  flex: 1 1 auto;
}

#view {
  flex: 1 1 auto;
  overflow: auto;
  padding: 16px;
}

#view.flush {
  padding: 0;
}

/* ==================================================================== controls */

button,
.btn {
  font: inherit;
  font-size: var(--t-sm);
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--r);
  padding: 5px 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

button:hover:not(:disabled),
.btn:hover {
  background: var(--surface-3);
  border-color: var(--ink-faint);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

button.primary:hover:not(:disabled) {
  filter: brightness(1.08);
}

button.danger {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-soft);
}

button.ghost {
  background: none;
  border-color: transparent;
  color: var(--ink-muted);
}

button.ghost:hover {
  background: var(--surface-2);
  color: var(--ink);
}

input,
select,
textarea {
  font: inherit;
  font-size: var(--t-sm);
  color: var(--ink);
  background: var(--ground);
  border: 1px solid var(--line-strong);
  border-radius: var(--r);
  padding: 5px 8px;
  width: 100%;
}

textarea,
input.mono,
.mono {
  font-family: var(--mono);
}

label {
  display: block;
  font-size: var(--t-xs);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-faint);
  margin-bottom: 4px;
}

.field {
  margin-bottom: 12px;
}

.field .hint {
  font-size: var(--t-xs);
  color: var(--ink-faint);
  margin-top: 4px;
  text-transform: none;
  letter-spacing: 0;
}

.row {
  display: flex;
  gap: var(--u);
  align-items: center;
  flex-wrap: wrap;
}

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}

/* ====================================================================== cards */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  margin-bottom: 12px;
  overflow: hidden;
}

.card > header {
  display: flex;
  align-items: baseline;
  gap: var(--u);
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
}

.card > header h2 {
  margin: 0;
  font-size: var(--t-md);
  font-weight: 600;
  color: var(--ink-strong);
}

.card > header .q {
  font-size: var(--t-sm);
  color: var(--ink-muted);
}

.card .body {
  padding: 12px;
}

.card .body > :last-child {
  margin-bottom: 0;
}

/* A panel with nothing to show. Deliberately prominent: a gap that looks like a zero
 * is the failure mode the Operations Center exists to avoid. */
.gap {
  border-left: 2px solid var(--warn);
  background: var(--surface-2);
  padding: 10px 12px;
  border-radius: 0 var(--r) var(--r) 0;
  color: var(--ink-muted);
  font-size: var(--t-sm);
}

.gap strong {
  color: var(--warn);
  display: block;
  margin-bottom: 4px;
  font-weight: 600;
}

.notice {
  border-left: 2px solid var(--info);
  background: var(--surface-2);
  padding: 10px 12px;
  border-radius: 0 var(--r) var(--r) 0;
  color: var(--ink-muted);
  font-size: var(--t-sm);
  margin-bottom: 12px;
}

.error {
  border-left: 2px solid var(--danger);
  background: var(--danger-soft);
  padding: 10px 12px;
  border-radius: 0 var(--r) var(--r) 0;
  color: var(--ink);
  font-size: var(--t-sm);
  margin-bottom: 12px;
}

.error ul {
  margin: 6px 0 0;
  padding-left: 18px;
}

.ok-note {
  border-left: 2px solid var(--ok);
  background: var(--surface-2);
  padding: 10px 12px;
  border-radius: 0 var(--r) var(--r) 0;
  font-size: var(--t-sm);
  margin-bottom: 12px;
}

/* ====================================================================== tables */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--t-sm);
}

th {
  text-align: left;
  font-weight: 600;
  font-size: var(--t-xs);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--ink-faint);
  padding: 6px 10px;
  border-bottom: 1px solid var(--line-strong);
  position: sticky;
  top: 0;
  background: var(--surface-2);
  white-space: nowrap;
}

td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

tbody tr:hover td {
  background: var(--surface-2);
}

td.num {
  text-align: right;
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

.tag {
  display: inline-block;
  font-size: var(--t-xs);
  padding: 1px 6px;
  border-radius: var(--r);
  border: 1px solid var(--line-strong);
  color: var(--ink-muted);
  background: var(--surface-2);
  white-space: nowrap;
}

.tag.hot {
  border-color: var(--accent-line);
  color: var(--accent);
  background: var(--accent-soft);
}
.tag.ok {
  border-color: var(--ok);
  color: var(--ok);
}
.tag.bad {
  border-color: var(--danger);
  color: var(--danger);
}
.tag.warn {
  border-color: var(--warn);
  color: var(--warn);
}

/* ====================================================================== search */

#searchbar {
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  padding: 10px 12px;
  flex: 0 0 auto;
}

#searchbar .qrow {
  display: flex;
  gap: var(--u);
  align-items: stretch;
}

#q {
  font-family: var(--mono);
  font-size: var(--t-md);
  min-height: 34px;
  resize: vertical;
  line-height: 1.45;
}

.toggle {
  display: inline-flex;
  border: 1px solid var(--line-strong);
  border-radius: var(--r);
  overflow: hidden;
}

.toggle button {
  border: none;
  border-radius: 0;
  background: var(--surface-2);
  font-size: var(--t-xs);
  padding: 4px 9px;
}

.toggle button[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

#results {
  display: grid;
  grid-template-columns: 236px 1fr;
  gap: 0;
  flex: 1 1 auto;
  min-height: 0;
}

#sidebar {
  border-right: 1px solid var(--line);
  overflow: auto;
  padding: 10px;
  background: var(--surface);
}

#sidebar h3 {
  font-size: var(--t-xs);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-faint);
  margin: 10px 0 4px;
}

.fieldrow {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  padding: 3px 6px;
  border-radius: var(--r);
  cursor: pointer;
  font-size: var(--t-sm);
}

.fieldrow:hover {
  background: var(--surface-2);
}

.fieldrow .fname {
  font-family: var(--mono);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#rows {
  overflow: auto;
  min-width: 0;
}

.event {
  border-bottom: 1px solid var(--line);
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: var(--t-sm);
  cursor: pointer;
}

.event:hover {
  background: var(--surface-2);
}

.event .ts {
  color: var(--accent-2);
  margin-right: var(--u);
}

.event pre {
  margin: var(--u) 0 0;
  padding: var(--u);
  background: var(--ground);
  border: 1px solid var(--line);
  border-radius: var(--r);
  overflow: auto;
  font-size: var(--t-xs);
}

.statbar {
  font-size: var(--t-xs);
  color: var(--ink-faint);
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  font-family: var(--mono);
}

/* ====================================================================== charts */

.chart {
  width: 100%;
  display: block;
}

.chart .axis {
  stroke: var(--line);
  stroke-width: 1;
}

.chart .gridline {
  stroke: var(--line);
  stroke-width: 1;
  stroke-dasharray: 2 3;
  opacity: 0.7;
}

.chart text {
  fill: var(--ink-faint);
  font: var(--t-xs) / 1 var(--font);
}

.chart .bar {
  fill: var(--accent);
}

.chart .bar:hover {
  fill: var(--accent-2);
}

.legend {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  font-size: var(--t-xs);
  color: var(--ink-muted);
  padding-top: 6px;
}

.legend .swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 1px;
  margin-right: 5px;
  vertical-align: baseline;
}

/* ============================================================ command palette */

#palette-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding-top: 14vh;
  z-index: 50;
}

#palette-backdrop[data-open="true"] {
  display: flex;
}

#palette {
  width: min(560px, 92vw);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}

#palette input {
  border: none;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  padding: 11px 13px;
  font-size: var(--t-lg);
  background: var(--surface);
}

#palette ul {
  list-style: none;
  margin: 0;
  padding: 5px;
  max-height: 48vh;
  overflow: auto;
}

#palette li {
  padding: 7px 10px;
  border-radius: var(--r);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--u);
  font-size: var(--t-sm);
}

#palette li[data-active="true"] {
  background: var(--accent-soft);
  color: var(--ink-strong);
}

#palette li .where {
  margin-left: auto;
  color: var(--ink-faint);
  font-size: var(--t-xs);
}

/* ======================================================================= misses */

.empty {
  color: var(--ink-faint);
  padding: 28px 12px;
  text-align: center;
  font-size: var(--t-sm);
}

.kbd {
  font-family: var(--mono);
  font-size: var(--t-xs);
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: var(--r);
  padding: 0 4px;
  color: var(--ink-muted);
  background: var(--surface-2);
}

#toasts {
  position: fixed;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: var(--u);
  z-index: 60;
}

.toast {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-left-width: 2px;
  border-radius: var(--r);
  box-shadow: var(--shadow);
  padding: var(--u) 12px;
  font-size: var(--t-sm);
  max-width: 420px;
}

.toast.ok {
  border-left-color: var(--ok);
}
.toast.bad {
  border-left-color: var(--danger);
}
.toast.info {
  border-left-color: var(--info);
}

pre.block {
  margin: 0;
  padding: var(--u) 10px;
  background: var(--ground);
  border: 1px solid var(--line);
  border-radius: var(--r);
  overflow: auto;
  font-size: var(--t-xs);
  font-family: var(--mono);
  max-height: 340px;
}

.diff {
  font-family: var(--mono);
  font-size: var(--t-xs);
  border: 1px solid var(--line);
  border-radius: var(--r);
  overflow: auto;
  max-height: 480px;
}

.diff div {
  padding: 1px 10px;
  white-space: pre-wrap;
}

.diff .add {
  background: rgba(111, 174, 92, 0.13);
  color: var(--ok);
}
.diff .remove {
  background: var(--danger-soft);
  color: var(--danger);
}

#document-editor {
  min-height: 460px;
  font-size: var(--t-sm);
  line-height: 1.55;
  resize: vertical;
}

/* Keep the grid usable on a narrow window without pretending to be a phone app:
 * a log console is a desktop instrument, and the rail collapses rather than hides. */
@media (max-width: 900px) {
  #shell {
    grid-template-columns: var(--rail-narrow) 1fr;
  }
  #results {
    grid-template-columns: 1fr;
  }
  #sidebar {
    border-right: none;
    border-bottom: 1px solid var(--line);
    max-height: 180px;
  }
}
