/* Switchboard — dashboard-precise dark operator console.
   Tokens + rules per DESIGN.md (inherits design-system Layer 1). Phone-first:
   designed at 390px, relaxes up to a centered 640px column. */

/* ------------------------------------------------------------- tokens */
:root {
  --bg-base: #0b0e12;
  --bg-surface: #131820;
  --bg-elevated: #1b212b;
  --text-primary: hsl(213 30% 93%);
  --text-secondary: hsl(214 14% 70%);
  --text-tertiary: hsl(215 11% 50%);
  --accent: #4da3ff;
  --accent-hover: #74b8ff;
  --accent-dim: hsl(212 100% 65% / 0.14);
  --success: #3fb950;
  --warning: #d29922;
  --danger: #f85149;
  --info: hsl(215 11% 58%);
  --border: #222a35;

  --space-1: 4px; --space-2: 8px; --space-3: 12px;
  --space-4: 16px; --space-5: 24px; --space-6: 32px;

  --radius-ctl: 6px; --radius-card: 10px; --radius-sheet: 14px;
  --radius-badge: 4px; /* rectangular badges (kind-chip) — distinct from pill chips */

  --fs-2xs: 10px; /* uppercase micro-badges only */
  --fs-xs: 12px; --fs-sm: 13px; --fs-md: 15px; --fs-lg: 17px; --fs-xl: 20px;
  --fs-2xl: 24px; /* login title only */

  --dot-size: 8px; /* one size for all status dots: conn, severity, unread */
  --shadow-pop: 0 12px 32px rgb(0 0 0 / 0.5); /* floating surfaces: menu, toast */

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ------------------------------------------------------------- base */
* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3, p { margin: 0; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  background: var(--bg-elevated);
  border-radius: 4px;
  padding: 1px 5px;
}
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-ctl);
}
/* display:flex/grid on components would otherwise beat the UA's [hidden] rule */
[hidden] { display: none !important; }

/* ------------------------------------------------------------- login */
#view-login {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: var(--space-5);
}
.login-wrap {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}
.login-logo { border-radius: 16px; margin-bottom: var(--space-4); }
.login-title { font-size: var(--fs-2xl); font-weight: 700; letter-spacing: -0.02em; line-height: 1.2; }
.login-sub { color: var(--text-tertiary); font-size: var(--fs-sm); margin-top: var(--space-1); }
.login-form {
  width: 100%;
  margin-top: var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  text-align: left;
}
.field-label {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.input {
  width: 100%;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-ctl);
  color: var(--text-primary);
  font-size: var(--fs-md);
  padding: 10px var(--space-3);
  transition: border-color 150ms ease;
}
/* No .input:focus outline override — the global :focus-visible 2px ring applies
   to inputs as-is (never replace a focus ring with something weaker). */
.form-error { color: var(--danger); font-size: var(--fs-sm); }
.form-error::before { content: "⚠ "; } /* color is never the only error signal */

/* ------------------------------------------------------------- buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  border-radius: var(--radius-ctl);
  font-size: var(--fs-md);
  font-weight: 600;
  padding: 10px var(--space-4);
  min-height: 42px;
  transition: background-color 150ms ease, color 150ms ease, opacity 150ms ease;
}
.btn:active { opacity: 0.8; }
.btn[disabled] { opacity: 0.45; cursor: default; }
.btn-primary { background: var(--accent); color: #06121f; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: var(--bg-elevated); color: var(--text-primary); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--text-tertiary); }
.btn-danger-ghost { background: none; color: var(--danger); border: 1px solid var(--border); }
.btn-danger-ghost:hover { border-color: var(--danger); }
.btn-row { display: flex; gap: var(--space-2); flex-wrap: wrap; margin-top: var(--space-3); }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-ctl);
  color: var(--text-secondary);
  transition: background-color 150ms ease, color 150ms ease;
}
.icon-btn:hover { background: var(--bg-elevated); color: var(--text-primary); }
.icon-btn svg { width: 20px; height: 20px; }

/* ------------------------------------------------------------- topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: calc(var(--safe-top) + var(--space-2)) var(--space-4) var(--space-2);
  background: color-mix(in srgb, var(--bg-base) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar-side { display: flex; align-items: center; gap: var(--space-2); min-width: 0; }
.app-title { font-size: var(--fs-lg); font-weight: 700; letter-spacing: -0.01em; }

.conn-dot {
  width: var(--dot-size);
  height: var(--dot-size);
  border-radius: 50%;
  flex: none;
  background: var(--text-tertiary);
}
.conn-dot[data-state="open"] { background: var(--success); }
.conn-dot[data-state="error"] { background: var(--danger); }
.conn-dot[data-state="connecting"] { background: var(--warning); }
@media (prefers-reduced-motion: no-preference) {
  .conn-dot[data-state="connecting"] { animation: pulse 1.6s ease-in-out infinite; }
  @keyframes pulse { 50% { opacity: 0.35; } }
}

.unread-badge {
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  background: var(--accent-dim);
  color: var(--accent);
  font-size: var(--fs-xs);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------- filters */
.filters {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 672px;
  margin: 0 auto;
  width: 100%;
}
.filters::-webkit-scrollbar { display: none; }
/* Norman's signifiers (Layer 1 · Affordances & feedback): the rail scrolls
   with its scrollbar suppressed, so a right-edge fade says "more here" while
   chips sit offscreen, and releases once the end is reached. */
.filters-wrap { position: relative; }
.filters-fade {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 44px;
  background: linear-gradient(90deg, transparent, var(--bg-base) 80%);
  pointer-events: none;
  opacity: 0;
  transition: opacity 200ms ease;
}
.filters-wrap.has-overflow .filters-fade { opacity: 1; }
.chip-group { display: flex; gap: var(--space-1); flex: none; }
.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 var(--space-3);
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-size: var(--fs-sm);
  font-weight: 600;
  transition: background-color 150ms ease, color 150ms ease, border-color 150ms ease;
}
.chip:hover { border-color: var(--text-tertiary); }
.chip.is-active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}
.chip-select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 26px;
  /* chevron stroke = --text-tertiary (hsl(215 11% 50%) ≈ #717d8e); data-URIs can't read vars */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%23717d8e' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  max-width: 46vw;
  text-overflow: ellipsis;
}
.dot { width: var(--dot-size); height: var(--dot-size); border-radius: 50%; flex: none; }
.dot-error { background: var(--danger); }
.dot-warning { background: var(--warning); }
.dot-success { background: var(--success); }
.dot-info { background: var(--info); }

/* ------------------------------------------------------ reconnect rail */
.conn-rail {
  max-width: 672px;
  margin: 0 auto;
  width: 100%;
  padding: var(--space-3) var(--space-4) 0;
}
.conn-rail-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--warning);
  border-radius: var(--radius-card);
  padding: 10px var(--space-3);
  font-size: var(--fs-sm);
  color: var(--text-secondary);
}
.conn-rail-bar b { color: var(--text-primary); font-weight: 600; }
.conn-rail-icon { flex: none; color: var(--warning); }
.conn-rail-msg { min-width: 0; }
.btn-reconnect {
  margin-left: auto;
  flex: none;
  height: 30px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-ctl);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: var(--fs-sm);
  font-weight: 600;
  transition: background-color 150ms ease;
}
.btn-reconnect:hover { background: var(--accent-dim); }
.btn-reconnect[disabled] { opacity: 0.5; cursor: default; }

/* ------------------------------------------------------------- feed */
.feed {
  max-width: 672px;
  margin: 0 auto;
  width: 100%;
  padding: 0 var(--space-4) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-5); /* between day groups: 2× the within-group gap */
}
.day-group { display: flex; flex-direction: column; gap: var(--space-2); }
.day-head {
  font-size: var(--fs-xs);
  font-weight: 700;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding-top: var(--space-3);
}

.event-card {
  position: relative;
  display: flex;
  gap: var(--space-3);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  overflow: hidden;
  cursor: pointer;
  /* border-color at 600ms = the arrival flash decay; nothing else moves it */
  transition: background-color 150ms ease, border-color 600ms ease;
}
.event-card:hover { background: var(--bg-elevated); }
.sev-bar {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--info);
}
.sev-error   > .sev-bar { background: var(--danger); }
.sev-warning > .sev-bar { background: var(--warning); }
.sev-success > .sev-bar { background: var(--success); }

.event-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: var(--space-1); }
.event-top { display: flex; align-items: center; gap: var(--space-2); }
.source-chip {
  flex: none;
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-elevated);
  border-radius: 999px;
  padding: 2px 9px;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.kind-chip {
  flex: none;
  font-size: var(--fs-2xs);
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-secondary);
  border: 1px solid var(--text-tertiary);
  border-radius: var(--radius-badge); /* rectangular badge — distinct from the pill source chip */
  padding: 1px 6px;
}
.event-time {
  font-size: var(--fs-xs);
  color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.event-top .spacer { flex: 1; }
.kebab { width: 32px; height: 32px; margin: -6px -4px -6px 0; }

.event-title {
  font-size: var(--fs-md);
  font-weight: 400;
  line-height: 1.35;
  overflow-wrap: anywhere;
  display: flex;
  align-items: baseline;
  gap: var(--space-2);
}
.unread .event-title { font-weight: 600; }
.unread-dot {
  flex: none;
  width: var(--dot-size); height: var(--dot-size);
  border-radius: 50%;
  background: var(--accent);
  transform: translateY(-1px);
  display: none;
}
.unread .unread-dot { display: inline-block; }

.event-body {
  font-size: var(--fs-sm);
  color: var(--text-secondary);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-width: 65ch;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.event-card.expanded .event-body { display: block; -webkit-line-clamp: none; }
.event-body:empty { display: none; }

.event-link {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: var(--space-1);
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  align-self: flex-start;
}
.event-link:hover { color: var(--accent-hover); text-decoration: underline; }
.event-link svg { width: 13px; height: 13px; }

/* SSE arrival (targeted insert): 220ms settle-in + accent border flash that
   decays over 600ms (transition above); badge pops once per arrival. All of
   it sits inside the no-preference gate — reduced motion gets an instant,
   flash-free insert. */
@media (prefers-reduced-motion: no-preference) {
  .event-card.is-arriving { animation: card-arrive 220ms cubic-bezier(0.2, 0.8, 0.3, 1); }
  @keyframes card-arrive { from { opacity: 0; transform: translateY(-10px); } }
  .event-card.is-flash { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
  .unread-badge.is-pop { animation: badge-pop 300ms ease; }
  @keyframes badge-pop { 50% { transform: scale(1.22); } }
}

/* loading skeleton — mirrors .event-card geometry so the swap is shift-free */
.skeleton-card {
  position: relative;
  display: flex;
  gap: var(--space-3);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  overflow: hidden;
}
.skeleton-bar {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--bg-elevated);
}
.skeleton-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: var(--space-2); }
.skeleton-top { display: flex; align-items: center; justify-content: space-between; }
.skeleton-block { background: var(--bg-elevated); border-radius: 5px; }
.skeleton-pill { width: 74px; height: 18px; border-radius: 999px; }
.skeleton-time { width: 28px; height: 11px; }
.skeleton-title { height: 12px; width: 62%; }
.skeleton-body { height: 10px; width: 88%; }
.skeleton-card:nth-child(3) .skeleton-title { width: 48%; }
.skeleton-card:nth-child(3) .skeleton-body { width: 74%; }
.skeleton-card:nth-child(4) .skeleton-title { width: 70%; }
.skeleton-card:nth-child(4) .skeleton-body { width: 82%; }
.skeleton-head { width: 64px; height: 12px; margin-top: var(--space-3); }
.skeleton-shine {
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(100deg, transparent 35%, rgb(255 255 255 / 0.05) 50%, transparent 65%);
}
@media (prefers-reduced-motion: no-preference) {
  .skeleton-shine { animation: skeleton-sweep 1.5s linear infinite; }
  @keyframes skeleton-sweep { to { transform: translateX(100%); } }
}

.feed-foot { max-width: 672px; margin: 0 auto; width: 100%; padding: 0 var(--space-4) calc(var(--safe-bottom) + var(--space-6)); }
#btn-more { width: 100%; }

/* ------------------------------------------------------------- empty */
.empty {
  max-width: 672px;
  margin: 0 auto;
  padding: var(--space-6) var(--space-5);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  text-align: center;
}
.empty-glyph { width: 56px; height: 56px; color: var(--text-tertiary); margin-bottom: var(--space-2); }
.empty-title { font-size: var(--fs-lg); font-weight: 700; }
.empty-hint { color: var(--text-tertiary); font-size: var(--fs-sm); max-width: 40ch; }
.empty .btn { margin-top: var(--space-3); }

/* ------------------------------------------------------------- settings */
.settings-body {
  max-width: 672px;
  margin: 0 auto;
  width: 100%;
  padding: var(--space-4) var(--space-4) calc(var(--safe-bottom) + var(--space-6));
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sheet);
  padding: var(--space-4);
}
.card-title { font-size: var(--fs-lg); font-weight: 700; }
.card-sub { font-size: var(--fs-sm); color: var(--text-secondary); margin-top: var(--space-1); max-width: 60ch; }
.version-line { font-size: var(--fs-xs); color: var(--text-tertiary); margin-top: var(--space-3); }

.sources-list { display: flex; flex-direction: column; margin-top: var(--space-2); }
.source-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap; /* two selects + switch wrap below the name at 390px */
  gap: var(--space-2) var(--space-3);
  padding: var(--space-3) 0;
}
.source-row + .source-row { border-top: 1px solid var(--border); }
.source-meta { flex: 1; min-width: 180px; }
.source-controls {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.source-name { font-weight: 600; font-size: var(--fs-md); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source-detail { font-size: var(--fs-xs); color: var(--text-tertiary); margin-top: 2px; }
.source-detail .hb-ok { color: var(--success); }
.source-detail .hb-stale { color: var(--danger); }
.select {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-ctl);
  color: var(--text-primary);
  font-size: var(--fs-sm);
  padding: 6px var(--space-2);
}

/* switch */
.switch { display: inline-flex; align-items: center; gap: var(--space-2); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  width: 44px; height: 26px;
  border-radius: 999px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  position: relative;
  flex: none;
  transition: background-color 150ms ease, border-color 150ms ease;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--text-secondary);
  transition: transform 150ms ease, background-color 150ms ease;
}
.switch input:checked + .switch-track { background: var(--accent-dim); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); background: var(--accent); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-label { font-size: var(--fs-sm); font-weight: 600; }

.quiet-row { display: flex; flex-direction: column; gap: var(--space-3); margin-top: var(--space-3); }
.quiet-times { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; }
.quiet-times .field-label { text-transform: none; letter-spacing: 0; font-size: var(--fs-sm); }
.input-time { width: auto; padding: 6px var(--space-2); }
.quiet-times.is-disabled { opacity: 0.45; pointer-events: none; }

/* ------------------------------------------------------------- context menu */
.ctx-menu {
  position: fixed;
  z-index: 50;
  min-width: 200px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: var(--space-1);
  box-shadow: var(--shadow-pop);
}
.ctx-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: 9px var(--space-3);
  border-radius: var(--radius-ctl);
  font-size: var(--fs-sm);
  font-weight: 600; /* declared weight scale is 400/600/700 */
  color: var(--text-primary);
  text-align: left;
  transition: background-color 120ms ease;
}
.ctx-item:hover, .ctx-item:focus-visible { background: var(--bg-surface); outline: none; }
.ctx-item svg { width: 16px; height: 16px; color: var(--text-tertiary); flex: none; }
.ctx-divider { height: 1px; background: var(--border); margin: var(--space-1) var(--space-2); }
/* right-aligned muted key caps — the menu teaches the fast path */
.ctx-key {
  margin-left: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: var(--fs-2xs);
  font-weight: 600;
  color: var(--text-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-badge);
  padding: 1px 6px;
  min-width: 20px;
  text-align: center;
}
.ctx-hint {
  padding: var(--space-1) var(--space-3);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: var(--fs-2xs);
  color: var(--text-tertiary);
  letter-spacing: 0.02em;
}

/* ------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + var(--space-5));
  transform: translateX(-50%);
  z-index: 60;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-primary);
  border-radius: 999px;
  padding: 9px var(--space-4);
  font-size: var(--fs-sm);
  font-weight: 600;
  box-shadow: var(--shadow-pop);
  max-width: calc(100vw - 32px);
}

/* ------------------------------------------------------------- motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
