/* ================================================================
   KNS Ops Console — dark-mode-first, no external fonts, no frameworks.
   ================================================================ */

:root {
  --bg:        #0e1116;
  --bg-2:      #151a22;
  --bg-3:      #1c2230;
  --line:      #242c3a;
  --fg:        #e6edf3;
  --fg-dim:    #8b96a6;
  --fg-mute:   #5c6676;
  --accent:    #64ffa6;
  --accent-2:  #3fb08a;
  --warn:      #ffd166;
  --err:       #ff6b6b;
  --ok:        #4ade80;
  --link:      #7cc5ff;
  --radius:    6px;
  --mono:      ui-monospace, "SFMono-Regular", "Consolas", "Menlo", monospace;
  --sans:      -apple-system, "Segoe UI", "Roboto", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  min-height: 100vh;
}

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

h1, h2, h3 { margin: 0 0 0.5em 0; font-weight: 600; }
h2 { font-size: 18px; border-bottom: 1px solid var(--line); padding-bottom: 6px; }
h3 { font-size: 13px; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.06em; }

.hidden { display: none !important; }
.muted  { color: var(--fg-dim); }
.small  { font-size: 12px; }
.error-text { color: var(--err); }

code, pre { font-family: var(--mono); font-size: 12px; }
pre { white-space: pre-wrap; word-break: break-all; }

/* ---------- Login ---------- */

.centered {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.panel {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.panel .brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--accent);
  margin-bottom: 4px;
}

.panel label {
  margin-top: 8px;
  font-size: 12px;
  color: var(--fg-dim);
}

/* Hint text between a .panel label and its input (aria-describedby). */
.panel > span.muted {
  display: block;
  margin: 2px 0 4px;
  line-height: 1.45;
}
.panel > span.muted code { color: var(--accent-2); }

.panel input {
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--fg);
  padding: 8px 10px;
  border-radius: 4px;
  font-family: inherit;
  font-size: 14px;
}
.panel input:focus { outline: 2px solid var(--accent-2); outline-offset: 0; }

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg-3);
  color: var(--fg);
  border: 1px solid var(--line);
  padding: 8px 14px;
  border-radius: 4px;
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent-2); }
.btn.primary {
  background: var(--accent-2);
  color: #06110d;
  border-color: var(--accent-2);
  font-weight: 600;
  margin-top: 14px;
}
.btn.primary:hover { background: var(--accent); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ---------- Topbar & tabs ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--accent);
  font-weight: 700;
}

.tabs {
  display: flex;
  gap: 4px;
  flex: 1;
}
.tabs a {
  padding: 6px 12px;
  border-radius: 4px;
  color: var(--fg-dim);
}
.tabs a.active {
  background: var(--bg-3);
  color: var(--fg);
}
.tabs a:hover { text-decoration: none; color: var(--fg); }

.session {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ---------- Main content ---------- */

.content {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.tab-pane { animation: fadein 120ms ease-out; }
@keyframes fadein { from { opacity: 0; transform: translateY(2px); } to { opacity: 1; transform: none; } }

/* ---------- Cards ---------- */

.grid.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.card {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card .big {
  font-size: 28px;
  font-weight: 700;
  margin: 4px 0;
  font-family: var(--mono);
}
.card dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; }
.card dt { color: var(--fg-dim); font-size: 12px; }
.card dd { margin: 0; font-family: var(--mono); font-size: 12px; word-break: break-all; }

.card.ok  { border-left: 3px solid var(--ok); }
.card.warn { border-left: 3px solid var(--warn); }
.card.err { border-left: 3px solid var(--err); }

.threshold {
  margin: 8px 0 0 0;
  font-size: 11px;
  color: var(--fg-mute);
}
.dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 2px;
  vertical-align: middle;
}
.dot.green  { background: var(--ok); }
.dot.yellow { background: var(--warn); }
.dot.red    { background: var(--err); }

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

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-top: 12px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
thead th {
  text-align: left;
  padding: 8px 10px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  font-weight: 600;
  color: var(--fg-dim);
  position: sticky;
  top: 0;
}
tbody td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 12px;
}
tbody tr:hover { background: var(--bg-2); }
tbody tr:last-child td { border-bottom: none; }

.status-pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 11px;
  border: 1px solid var(--line);
}
.status-pill.ok  { background: rgba(74,222,128,0.1); color: var(--ok); border-color: rgba(74,222,128,0.3); }
.status-pill.warn{ background: rgba(255,209,102,0.1); color: var(--warn); border-color: rgba(255,209,102,0.3); }
.status-pill.err { background: rgba(255,107,107,0.1); color: var(--err); border-color: rgba(255,107,107,0.3); }

details {
  margin: 10px 0;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
}
details summary { cursor: pointer; color: var(--fg-dim); font-size: 12px; }
details > input { margin-top: 8px; width: 100%; background: var(--bg); color: var(--fg); border: 1px solid var(--line); border-radius: 4px; padding: 6px 8px; font-family: inherit; }

/* ---------- Adapters tab ---------- */

.adapters-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0;
}
.adapters-toolbar .btn { flex-shrink: 0; }

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.chip {
  display: inline-block;
  background: var(--bg-3);
  border: 1px solid var(--line);
  color: var(--fg-dim);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 11px;
  font-family: var(--mono);
  white-space: nowrap;
}

/* ---------- Spinner ---------- */

.spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--fg-mute);
  border-top-color: var(--accent);
  border-radius: 50%;
  display: inline-block;
  animation: spin 600ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Toasts ---------- */

.toast-root {
  position: fixed;
  bottom: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  max-width: 400px;
}
.toast {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  animation: slidein 150ms ease-out;
}
.toast.err  { border-left-color: var(--err); }
.toast.warn { border-left-color: var(--warn); }
.toast.ok   { border-left-color: var(--ok); }
.toast .title { font-weight: 600; margin-bottom: 2px; }
.toast .body  { color: var(--fg-dim); word-break: break-word; }
@keyframes slidein { from { opacity: 0; transform: translateX(10px); } to { opacity: 1; transform: none; } }

/* ---------- Write-surface banner + forms ---------- */

.banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  margin: 0 0 14px 0;
  font-size: 13px;
}
.banner.warn {
  background: rgba(255, 209, 102, 0.08);
  border-color: rgba(255, 209, 102, 0.35);
  color: var(--warn);
}
.banner code {
  background: var(--bg-3);
  color: var(--fg);
  padding: 1px 6px;
  border-radius: 3px;
}

/* Write forms: stacked label/input grid that wraps nicely */
.write-form summary { color: var(--accent); font-weight: 600; }
.write-form[data-requires-write] { border-color: rgba(100, 255, 166, 0.25); }

.write-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px 14px;
  margin-top: 10px;
}
.write-grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--fg-dim);
}
.write-grid label.wide { grid-column: span 2; }
/* Hint paragraphs referenced by aria-describedby span the whole grid. */
.write-grid > p.wide { grid-column: 1 / -1; margin: 0; line-height: 1.5; }
.write-grid > p.wide code { color: var(--accent-2); }
.write-grid input,
.write-grid select {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 13px;
}
.write-grid input:focus,
.write-grid select:focus { outline: 2px solid var(--accent-2); outline-offset: 0; }
.write-actions {
  grid-column: 1 / -1;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* When the feature flag is off, every write form + button is disabled
   and visually muted. app.js toggles body[data-write-enabled]. */
body[data-write-enabled="false"] [data-requires-write] {
  opacity: 0.55;
  pointer-events: none;
  filter: grayscale(0.3);
}
body[data-write-enabled="false"] [data-write-btn]:not([data-bypass-flag]) {
  background: var(--bg-3);
  color: var(--fg-mute);
  border-color: var(--line);
  cursor: not-allowed;
}

.section-head { margin-top: 18px; }
.kv-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  margin: 6px 0 14px 0;
  font-size: 12px;
}
.kv-inline dt { color: var(--fg-dim); }
.kv-inline dd { margin: 0 10px 0 4px; font-family: var(--mono); }

/* Row-level action buttons (delete record, revoke key) */
.row-actions {
  display: inline-flex;
  gap: 4px;
}
.icon-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  border-radius: 4px;
  padding: 3px 6px;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  gap: 3px;
}
.icon-btn:hover { color: var(--fg); border-color: var(--accent-2); }
.icon-btn.danger:hover { color: var(--err); border-color: var(--err); }
.icon-btn[disabled] { opacity: 0.4; cursor: not-allowed; }

/* ---------- Assistant (zone detail) ---------- */

.write-grid label.full { grid-column: 1 / -1; }
.write-grid textarea {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.45;
  min-height: 64px;
  resize: vertical;
}
.write-grid textarea:focus { outline: 2px solid var(--accent-2); outline-offset: 0; }

/* Calm inline notice ("not enabled", "try again") — no toast, no red. */
.assistant-notice {
  margin: 8px 0;
  padding: 8px 12px;
  border: 1px dashed var(--line);
  border-radius: 4px;
  color: var(--fg-dim);
  font-size: 12px;
}
.assistant-thinking { display: flex; align-items: center; gap: 8px; margin: 10px 0; }

.assistant-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 12px 0 6px 0;
}
.assistant-summary-text { flex: 1 1 320px; }
.assistant-model { font-family: var(--mono); font-size: 11px; color: var(--fg-mute); }

.assistant-clarify {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--warn);
  border-radius: var(--radius);
  background: rgba(255, 209, 102, 0.08);
  color: var(--fg);
}
.assistant-warnings {
  margin: 8px 0;
  padding-left: 18px;
  color: var(--warn);
  font-size: 12px;
}
.assistant-warnings li { margin: 2px 0; }

.assistant-changes td.assistant-content { max-width: 320px; overflow-wrap: anywhere; }
.assistant-changes td.assistant-prose {
  font-family: var(--sans);
  font-size: 12px;
  max-width: 260px;
  white-space: normal;
}
.assistant-changes td.assistant-prose ul { margin: 4px 0 0 0; padding-left: 16px; }
.assistant-changes tr.applied td { opacity: 0.6; }
.assistant-changes tr.applied td.row-actions,
.assistant-changes tr.applied .status-pill { opacity: 1; }

/* ---------- Modal ---------- */

.modal-root {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 8, 12, 0.72);
  backdrop-filter: blur(2px);
}
.modal-panel {
  position: relative;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 22px;
  width: 100%;
  max-width: 520px;
  max-height: 86vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
  animation: modalin 140ms ease-out;
}
@keyframes modalin {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}
.modal-panel h3 {
  color: var(--fg);
  font-size: 15px;
  text-transform: none;
  letter-spacing: 0;
  margin-bottom: 4px;
}
.modal-payload {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 10px 12px;
  margin: 12px 0;
  font-size: 12px;
  color: var(--fg-dim);
  max-height: 260px;
  overflow-y: auto;
}
.modal-payload .method { color: var(--accent); font-weight: 700; }
.modal-payload .path   { color: var(--link); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}
.modal-actions .btn.primary.danger {
  background: var(--err);
  border-color: var(--err);
  color: #1a0606;
}
.modal-actions .btn.primary.danger:hover { background: #ff8585; border-color: #ff8585; }

.modal-extra input[type="text"] {
  width: 100%;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  font-family: var(--mono);
  font-size: 13px;
  margin-top: 6px;
}
.secret-reveal {
  background: var(--bg);
  border: 1px solid var(--accent-2);
  border-radius: 4px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
  word-break: break-all;
  margin: 8px 0;
}
.secret-warning {
  color: var(--warn);
  font-size: 12px;
  margin: 4px 0 10px 0;
}

/* Keyboard-shortcut help (the `?` overlay). */
.shortcut-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.shortcut-table td { padding: 4px 8px 4px 0; vertical-align: baseline; }
.shortcut-table td:first-child { width: 34%; white-space: nowrap; }
.shortcut-table kbd {
  display: inline-block;
  background: var(--bg);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 6px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--fg);
}

/* ================================================================
   Accessibility additions — WCAG 2.1 AA baseline
   ----------------------------------------------------------------
   Covers:
     1.4.3 Contrast (Minimum)    — see README for measured ratios
     1.4.11 Non-text Contrast    — focus ring >= 3:1 against both
                                   --bg and --bg-2
     2.1.1 Keyboard              — all interactive controls reachable
     2.4.1 Bypass Blocks         — .skip-to-main anchor
     2.4.7 Focus Visible         — :focus-visible rings below
     2.3.3 Animation from
           Interactions (AAA)    — prefers-reduced-motion guard
   The rules below are ADDITIVE only — they do not override any
   existing selector's color, size, or layout.
   ================================================================ */

/* Visually-hidden utility for screen-reader-only text (labels on
   icon-only buttons, section landmarks, live-region preamble). */
.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* "Skip to main content" — hidden until focused by keyboard.
   Expected markup:
     <a href="#main" class="skip-to-main">Skip to main content</a>
   placed as the first focusable element in <body>. */
.skip-to-main {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 1000;
  background: var(--accent);
  color: #06110d;
  padding: 8px 14px;
  border-radius: 0 0 var(--radius) 0;
  font-weight: 600;
  text-decoration: none;
}
.skip-to-main:focus,
.skip-to-main:focus-visible {
  left: 0;
  outline: 2px solid var(--fg);
  outline-offset: 2px;
}

/* Universal high-contrast focus ring for keyboard users.
   Uses :focus-visible so mouse clicks do not paint rings,
   but Tab navigation and screen-reader focus always do. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* Tables: focus ring on any focusable cell / row action should
   sit *inside* the cell so it isn't clipped by overflow. */
tbody tr:focus-visible,
tbody td:focus-visible {
  outline-offset: -2px;
}

/* Tabs: the active-tab ring needs extra contrast against --bg-3. */
.tabs a:focus-visible {
  outline-color: var(--accent);
  outline-offset: 2px;
  color: var(--fg);
}

/* Buttons: make the ring hug the button, not the text. */
.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn.primary:focus-visible {
  /* Primary buttons are green — ring uses --fg for >=3:1 contrast. */
  outline-color: var(--fg);
}

/* Form inputs: keep the existing 2px --accent-2 box-glow but ensure
   :focus-visible wins over :focus so there's no double ring. */
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Details/summary: expandable collapsibles get a visible ring too. */
details > summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* Modal: the panel itself receives programmatic focus when opened
   (app.js focuses the first tabbable element or the panel as
   fallback) — give it a subtle outline so SR users hear the
   boundary and sighted keyboard users see where focus landed. */
.modal-panel:focus,
.modal-panel:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* Toast live region: visually identical, but ensure the wrapper is
   never clipped by transforms that break aria-live announcements
   in some AT implementations. */
.toast-root { contain: layout; }

/* Error/alert toast: when role="alert" is set in JS, make it ever
   so slightly bolder so low-vision users notice it among peers. */
.toast.err .title,
.toast[role="alert"] .title { color: var(--err); }

/* Respect OS-level "reduce motion" preferences. This disables the
   tab-pane fade, the spinner spin, toast slide-in, and modal
   scale-in — all decorative. Functionality is unchanged. */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .spinner {
    /* Keep the spinner visible but static — users with vestibular
       conditions should still see the busy state. */
    border-top-color: var(--accent);
    animation: none !important;
  }
  .tab-pane,
  .toast,
  .modal-panel {
    animation: none !important;
  }
}

/* Forced-colors / Windows High Contrast mode: let the OS paint
   focus and borders so the console remains usable there. */
@media (forced-colors: active) {
  :focus-visible {
    outline: 2px solid CanvasText;
  }
  .btn,
  .icon-btn,
  .card,
  .modal-panel,
  .toast,
  .banner {
    border: 1px solid CanvasText;
  }
  .dot.green,
  .dot.yellow,
  .dot.red {
    /* Colored dots are the only indicator on some cards; back
       them with a shape the OS can repaint. */
    outline: 1px solid CanvasText;
    outline-offset: 1px;
  }
}
