/* hey — TTY.
 *
 * A log stream, not a list of cards.
 *
 * Design rule, applied strictly: every pixel either carries information or is a
 * control. No film grain, no scanlines, no entrance animations, no drop
 * shadows, no decorative rules, no priority widget duplicating what the level
 * column already says. If it cannot be clicked and does not tell you something,
 * it is not here.
 *
 * The two remaining animations both encode state: the prompt cursor (input is
 * focused) and the health LED blink (backend is down).
 */

:root {
  --font: ui-monospace, "SF Mono", SFMono-Regular, "Cascadia Mono",
    "JetBrains Mono", "Roboto Mono", Menlo, Consolas, monospace;

  --bg: #0b0d0c;
  --bg-hi: #11150f;
  --sel: #161c14;
  --fg: #c9d1c9;
  --dim: #6b7a6f;
  --faint: #414d45;
  --rule: #1a201c;

  --acc: #8ec07c;
  --link: #83a598;

  --crit: #fb4934;
  --warn: #d79921;
  --info: #8ec07c;
  --note: #83a598;
  --trce: #5c6a5f;

  --row: 1.72;
}

/* Daylight. Same structure, ink on paper — still zero decoration. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f7f5ef; --bg-hi: #efece2; --sel: #e6e2d4;
    --fg: #22251f; --dim: #5d6459; --faint: #8b9186; --rule: #ddd8c9;
    --acc: #4a7c3f; --link: #2a6b7c;
    --crit: #c22e22; --warn: #9a6b06; --info: #3f7a35; --note: #2a6b7c; --trce: #8b9186;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f7f5ef; --bg-hi: #efece2; --sel: #e6e2d4;
  --fg: #22251f; --dim: #5d6459; --faint: #8b9186; --rule: #ddd8c9;
  --acc: #4a7c3f; --link: #2a6b7c;
  --crit: #c22e22; --warn: #9a6b06; --info: #3f7a35; --note: #2a6b7c; --trce: #8b9186;
  color-scheme: light;
}
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font: 13px/var(--row) var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
a { color: inherit; text-decoration: none; }
h1, h2, h3, p, dl, dd { margin: 0; }
[hidden] { display: none !important; }

:focus-visible { outline: 1px solid var(--acc); outline-offset: 1px; }

::selection { background: var(--acc); color: var(--bg); }

/* ------------------------------------------------------------------ shell */

.app {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  height: 100dvh;
}

.bar {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--rule);
  flex-wrap: wrap;
}

.host { color: var(--acc); font-weight: 700; }
.host:hover { text-decoration: underline; }

/* Counts are buttons, not labels — each one applies the filter it describes. */
.count {
  color: var(--dim);
  font-size: 12px;
  white-space: nowrap;
}
.count:hover { color: var(--fg); text-decoration: underline; }
#count-unread[data-n="0"] { color: var(--faint); }
#count-unread:not([data-n="0"]) { color: var(--acc); }

.led {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--info);
  margin-left: auto;
  flex: none;
}
.led[data-state="warn"] { background: var(--warn); }
/* Blink is the one decorative-looking thing kept: it means "backend is down",
   which is exactly the state you must not miss. */
.led[data-state="down"] { background: var(--crit); animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0.2; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; }
}

/* ----------------------------------------------------------------- prompt */

.prompt {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4375rem 1rem;
  border-bottom: 1px solid var(--rule);
}

.prompt__sign { color: var(--acc); font-weight: 700; cursor: text; }

.prompt__input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  padding: 0;
  caret-color: var(--acc);
}
.prompt__input::placeholder { color: var(--faint); }
.prompt__input:focus { outline: none; }

.prompt__clear { color: var(--faint); font-size: 12px; }
.prompt__clear:hover { color: var(--crit); text-decoration: underline; }

.main {
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--rule) transparent;
}
.main::-webkit-scrollbar { width: 8px; }
.main::-webkit-scrollbar-thumb { background: var(--rule); }

/* ------------------------------------------------------------------- keys */

.keys {
  display: flex;
  gap: 1.125rem;
  padding: 0.4375rem 1rem calc(0.4375rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--rule);
  font-size: 11px;
  color: var(--faint);
  overflow-x: auto;
  scrollbar-width: none;
}
.keys::-webkit-scrollbar { display: none; }
.keys button, .keys a { white-space: nowrap; color: inherit; }
.keys button:hover, .keys a:hover { color: var(--fg); }
.keys b { color: var(--dim); font-weight: 700; margin-right: 3px; }
.keys button:hover b, .keys a:hover b { color: var(--acc); }

/* The bar is navigation first. Marking the current section is the difference
   between a legend and a working nav. */
.keys a[aria-current="page"] { color: var(--acc); }
.keys a[aria-current="page"] b { color: var(--acc); }

/* Touch input: hide the entries that only mean something with a keyboard.
   Tapping "j/k move" to shift a cursor you could simply tap is ceremony.
   `hover: none` plus `pointer: coarse` identifies a touch-primary device
   without guessing from screen width — a narrow desktop window still keeps
   its shortcuts. */
@media (hover: none) and (pointer: coarse) {
  .keys__kbd { display: none; }

  /* With the key letters no longer meaningful, the remaining items read as
     plain labels rather than mnemonics. */
  .keys b { color: inherit; font-weight: inherit; margin-right: 0; }

  /* And it becomes a real bottom nav: evenly divided, thumb-sized targets. */
  .keys {
    justify-content: space-around;
    font-size: 12px;
    padding-top: 0.625rem;
    padding-bottom: calc(0.625rem + env(safe-area-inset-bottom));
    overflow-x: visible;
  }
  .keys button, .keys a { padding: 0.125rem 0.375rem; }
}

/* Escape hatch: a tablet with a keyboard attached reports coarse/no-hover but
   can still use the shortcuts, so the first real keydown brings them back. */
html.has-keyboard .keys__kbd { display: revert; }

/* -------------------------------------------------------------------- log */

.log { padding: 0.5rem 0 1.5rem; }

.day {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.875rem 1rem 0.25rem;
  font-size: 11px;
  color: var(--faint);
}
.day::after { content: ""; flex: 1; height: 1px; background: var(--rule); }
.day b { color: var(--dim); font-weight: 400; }

/* One event = one line. Columns are a grid so they align like a real log,
   and every cell is independently clickable. */
.ev {
  display: grid;
  grid-template-columns: 0.75rem 4.75rem 2.75rem 5.5rem 7.5rem minmax(0, 1fr) auto;
  gap: 0 0.75rem;
  align-items: baseline;
  padding: 0.09375rem 1rem;
  cursor: pointer;
}
.ev:hover { background: var(--bg-hi); }
.ev[aria-selected="true"] { background: var(--sel); }
.ev[aria-selected="true"] .ev__msg { color: var(--acc); }

.ev__flag { color: var(--acc); font-weight: 700; }
.ev__time { color: var(--faint); font-variant-numeric: tabular-nums; white-space: nowrap; }
.ev__lvl { font-weight: 700; letter-spacing: 0.04em; }
.ev__cat, .ev__src {
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ev__src { color: var(--faint); }

/* Column cells are filter controls. Underline on hover is the affordance —
   it is the only hover treatment in the app that means "this narrows the list". */
.ev__lvl:hover, .ev__cat:hover, .ev__src:hover { text-decoration: underline; }

.ev__msg { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ev__msg .sub { color: var(--faint); }
.ev__msg .sub::before { content: " — "; }

/* Only rendered when the event actually carries a URL, so its presence is
   information: this one goes somewhere. */
.ev__link { color: var(--link); font-size: 12px; }
.ev__link:hover { text-decoration: underline; }

.p5 .ev__lvl { color: var(--crit); }
.p4 .ev__lvl { color: var(--warn); }
.p3 .ev__lvl { color: var(--info); }
.p2 .ev__lvl { color: var(--note); }
.p1 .ev__lvl { color: var(--trce); }
.p5 .ev__msg { color: var(--crit); }
.p1 { opacity: 0.62; }

/* Phone. The log reflows to two lines rather than scrolling sideways, and the
   metadata stays on one row so a screen still holds a useful number of events —
   density is the entire point of this layout, and a card-per-event on a phone
   throws it away.

   Flex rather than grid areas: the metadata cells are variable-width and one of
   them (source) can be long, so letting them flow and truncate beats pinning
   each to a named cell. */
@media (max-width: 46rem) {
  .ev {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0 0.4375rem;
    padding: 0.4375rem 0.75rem;
    border-bottom: 1px solid var(--rule);
  }
  .ev__flag { width: auto; }
  .ev__src { max-width: 9rem; }
  /* Pushed to the end of the metadata line, still tappable at its own size. */
  .ev__link { margin-left: auto; padding-left: 0.5rem; }

  /* Ordered after the link so the full-width message wraps below it. Without
     this the message breaks the line first and the arrow lands alone on a
     third row, costing a line per event. */
  .ev__msg {
    order: 1;
    flex: 1 0 100%;
    white-space: normal;
    margin-top: 0.0625rem;
    font-weight: 600;
  }
  /* Two lines of body, then ellipsis. Without a clamp one verbose event pushes
     everything else off the screen. */
  .ev__msg .sub {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    font-weight: 400;
    margin-top: 0.0625rem;
  }
  .ev__msg .sub::before { content: none; }

  .day { padding-left: 0.75rem; }
  .prompt { padding-left: 0.75rem; padding-right: 0.75rem; }

  /* Header: one line, scrollable, rather than wrapping to two and eating
     vertical space that the log needs. */
  .bar {
    flex-wrap: nowrap;
    gap: 0.75rem;
    padding: 0.4375rem 0.75rem;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .bar::-webkit-scrollbar { display: none; }
  .host { flex: none; }
  .count { flex: none; font-size: 11px; }
  .led { margin-left: 0.25rem; }

  /* The hostname is already in the URL bar and costs most of the header's
     width, and the devices count duplicates the bottom nav. Dropping both is
     what lets `exit` stay on screen instead of scrolled off the end. */
  .host__at { display: none; }
  .bar a.count[href="#/devices"] { display: none; }

}

/* Rows of records - deliveries, rules, tokens. These had their column widths
   written inline in the JS templates as fixed rem values, which overflowed a
   390px viewport and collapsed the last column to one character per line.
   Declared here instead so they can actually respond. */
.rec {
  display: grid;
  gap: 0 1rem;
  padding: 0.25rem 0;
  align-items: baseline;
}
.rec--delivery { grid-template-columns: 12rem 9rem minmax(0, 1fr); }
.rec--rule { grid-template-columns: 5rem minmax(0, 1fr) auto; }
.rec--token { grid-template-columns: 5rem 10rem minmax(0, 1fr) auto; }
.rec .who { color: var(--dim); overflow: hidden; text-overflow: ellipsis; }
.rec .why { color: var(--faint); overflow: hidden; text-overflow: ellipsis; }

@media (max-width: 46rem) {
  /* One column, with the actions on their own row. Everything stays visible
     rather than being pushed off the right edge. */
  .rec, .rec--delivery, .rec--rule, .rec--token {
    grid-template-columns: minmax(0, 1fr);
    gap: 0.125rem 0;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--rule);
  }
  .rec .why { white-space: normal; }
  .rec .acts, .rec > span:last-child { margin-top: 0.25rem; }
}

/* ----------------------------------------------------------------- detail */

.pane { padding: 0.75rem 1rem 2rem; max-width: 62rem; }

.crumb {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
  color: var(--faint);
  font-size: 12px;
  padding-bottom: 0.625rem;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 0.875rem;
}
.crumb a, .crumb button { color: var(--dim); }
.crumb a:hover, .crumb button:hover { color: var(--acc); text-decoration: underline; }

.pane h2 { font-size: 15px; font-weight: 700; line-height: 1.45; margin-bottom: 0.375rem; }
.pane .lede { color: var(--dim); line-height: 1.65; white-space: pre-wrap; word-break: break-word; }
.pane .lede a { color: var(--link); text-decoration: underline; }

.sect {
  margin-top: 1.375rem;
  font-size: 11px;
  color: var(--faint);
  display: flex;
  align-items: center;
  gap: 0.625rem;
}
.sect::after { content: ""; flex: 1; height: 1px; background: var(--rule); }

/* Key/value readout. Keys are clickable where clicking them means something
   (filter by that source, that category, that tag). */
.kv { display: grid; grid-template-columns: 9.5rem minmax(0, 1fr); gap: 0.0625rem 1rem; margin-top: 0.5rem; }
.kv dt { color: var(--faint); }
.kv dd { color: var(--fg); word-break: break-word; font-variant-numeric: tabular-nums; }
.kv dd.num { color: var(--acc); }
.kv dd a { color: var(--link); }
.kv dd a:hover { text-decoration: underline; }
.kv dd button { color: var(--dim); }
.kv dd button:hover { color: var(--acc); text-decoration: underline; }

.deliv { margin-top: 0.5rem; }
.st-pushed { color: var(--info); }
.st-suppressed_rule, .st-suppressed_no_match, .st-device_inactive { color: var(--faint); }
.st-suppressed_quiet { color: var(--note); }
.st-rate_limited { color: var(--warn); }
.st-failed, .st-endpoint_gone { color: var(--crit); }

/* ------------------------------------------------------------------ forms */

.form { display: grid; gap: 0.5rem; max-width: 34rem; margin-top: 0.625rem; }
.form label { display: grid; grid-template-columns: 9.5rem minmax(0, 1fr); gap: 1rem; align-items: center; }
.form span { color: var(--faint); }
.form input, .form select {
  border: 1px solid var(--rule);
  background: var(--bg-hi);
  color: var(--fg);
  padding: 0.25rem 0.4375rem;
  min-height: 1.875rem;
  width: 100%;
}
.form input:focus, .form select:focus { outline: none; border-color: var(--acc); }
@media (max-width: 46rem) {
  .form label { grid-template-columns: 1fr; gap: 0.125rem; }
}

.act {
  color: var(--dim);
  border: 1px solid var(--rule);
  padding: 0.1875rem 0.625rem;
  min-height: 1.875rem;
}
.act:hover { color: var(--acc); border-color: var(--acc); }
.act--danger:hover { color: var(--crit); border-color: var(--crit); }
.act:disabled { opacity: 0.4; cursor: not-allowed; }
.acts { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.75rem; }

.warn { color: var(--warn); }
.bad { color: var(--crit); }
.ok { color: var(--info); }

.empty { padding: 2.5rem 1rem; color: var(--faint); line-height: 1.8; }
.empty b { color: var(--dim); font-weight: 400; }
.loading { padding: 1rem; color: var(--faint); }

/* ------------------------------------------------------------------- gate */

.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 1.5rem; background: var(--bg); z-index: 50; }
.gate__form { width: min(100%, 30rem); }
.gate__line { color: var(--dim); margin-bottom: 0.75rem; }
.gate__row { display: flex; gap: 0.625rem; align-items: baseline; }
.gate__sign { color: var(--acc); }
.gate__input { flex: 1; min-width: 0; border: 0; border-bottom: 1px solid var(--rule); background: none; padding: 0.25rem 0; caret-color: var(--acc); }
.gate__input:focus { outline: none; border-bottom-color: var(--acc); }
.gate__error { color: var(--crit); margin-top: 0.75rem; }

/* ------------------------------------------------------------------- help */

.help { position: fixed; inset: 0; display: grid; place-items: center; padding: 1.5rem; background: color-mix(in srgb, var(--bg) 88%, transparent); z-index: 60; }
.help__panel { width: min(100%, 40rem); border: 1px solid var(--rule); background: var(--bg); padding: 0.875rem 1rem 1rem; }
.help__head { display: flex; justify-content: space-between; color: var(--acc); border-bottom: 1px solid var(--rule); padding-bottom: 0.5rem; margin-bottom: 0.75rem; }
.help__head button { color: var(--faint); }
.help__head button:hover { color: var(--fg); }
.help__cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: 0 1.5rem; }
.help dl { display: grid; grid-template-columns: 8rem minmax(0, 1fr); gap: 0.0625rem 0.75rem; }
.help dt { color: var(--acc); }
.help dd { color: var(--dim); }
.help__note { color: var(--faint); margin-top: 0.75rem; font-size: 12px; }

/* ----------------------------------------------------------------- toasts */

.toasts { position: fixed; left: 1rem; bottom: calc(2.75rem + env(safe-area-inset-bottom)); z-index: 70; display: grid; gap: 0.25rem; }
.toast { border-left: 2px solid var(--acc); background: var(--bg-hi); padding: 0.25rem 0.625rem; font-size: 12px; }
.toast[data-kind="error"] { border-left-color: var(--crit); color: var(--crit); }

/* ------------------------------------------------- phone: detail & forms
 *
 * These live at the end of the file on purpose. They target base rules that
 * are declared further up, and at equal specificity source order decides -
 * the same overrides placed alongside the log's mobile block were silently
 * dead, which is a quiet way to ship a layout that only half responds.
 */
@media (max-width: 46rem) {
  .pane { padding-left: 0.75rem; padding-right: 0.75rem; }
  .sect { margin-top: 1.125rem; }
  .form { max-width: none; }

  /* Readouts stack. A fixed 9.5rem label column leaves about 140px for the
     value, which breaks ids and timestamps mid-token. */
  .kv { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .kv dt { margin-top: 0.625rem; }
  .kv dd { word-break: break-word; padding-left: 0; }

  /* Long opaque values - event ids, endpoints - need to break anywhere or they
     force the page wider than the screen. */
  .kv dd:last-of-type { word-break: break-all; }

  .crumb { gap: 0.5rem; }
  .help__panel { padding: 0.75rem; }
  .help dl { grid-template-columns: 6.5rem minmax(0, 1fr); }
}
