/* The signed-in console: /app (customers) and /ops (admins).
 *
 * Layout and components only. Every color, size, space and radius comes from
 * static/tokens.css; see docs/design-system.md for the rules behind them.
 */

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

html { background: var(--iot-ground); }

body {
  margin: 0;
  background: var(--iot-ground);
  color: var(--iot-text);
  font-family: var(--iot-font);
  font-size: var(--iot-size-body);
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--iot-text-accent); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

h1, h2, h3 { font-weight: 600; line-height: 1.25; margin: 0; }
h1 { font-size: var(--iot-size-title); margin-bottom: var(--iot-space-5); }
h2 { font-size: var(--iot-size-heading); margin: var(--iot-space-6) 0 var(--iot-space-3); }
h2:first-child { margin-top: 0; }

p { margin: 0 0 var(--iot-space-3); max-width: 70ch; }

code, .mono {
  font-family: var(--iot-font-mono);
  font-size: 0.87em;
}

::placeholder { color: var(--iot-text-muted); opacity: 1; }

/* ── Top bar ─────────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  gap: var(--iot-space-4);
  min-height: 56px;
  padding: var(--iot-space-2) var(--iot-space-4);
  background: var(--iot-surface);
  border-bottom: 1px solid var(--iot-border);
}

.topbar .brand { font-weight: 600; }
.topbar .surface-name { color: var(--iot-text-muted); }
.topbar .spacer { flex: 1; }
.topbar .who {
  color: var(--iot-text-muted);
  font-size: var(--iot-size-small);
  max-width: 28ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar .signout { display: inline; margin: 0; }

/* Labelled with the theme it switches TO; naming the current one reads as a
   status, not a control. Hidden until the script has run, because without
   JavaScript it cannot do anything. */
.theme-btn .to-light { display: none; }
[data-theme="dark"] .theme-btn .to-light { display: inline; }
[data-theme="dark"] .theme-btn .to-dark { display: none; }

/* ── Layout ──────────────────────────────────────────────────────────── */

.shell { display: flex; align-items: flex-start; }

.sidebar {
  flex: 0 0 208px;
  display: flex;
  flex-direction: column;
  gap: var(--iot-space-1);
  padding: var(--iot-space-4) var(--iot-space-3);
  border-right: 1px solid var(--iot-border);
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
}

.sidebar a {
  display: flex;
  align-items: center;
  min-height: var(--iot-touch);
  padding: 0 var(--iot-space-3);
  border-left: 2px solid transparent;
  border-radius: 0 var(--iot-radius-control) var(--iot-radius-control) 0;
  color: var(--iot-text-secondary);
  text-decoration: none;
}
.sidebar a:hover { background: var(--iot-surface); color: var(--iot-text); }
/* Current page: weight and a bar, not color alone. */
.sidebar a[aria-current="page"] {
  border-left-color: var(--iot-accent);
  background: var(--iot-surface);
  color: var(--iot-text);
  font-weight: 600;
}
.sidebar-sep {
  height: 1px;
  background: var(--iot-border);
  margin: var(--iot-space-3) var(--iot-space-3);
}

main {
  flex: 1;
  min-width: 0;   /* without this a wide table stretches the whole page */
  padding: var(--iot-space-5) var(--iot-space-6) var(--iot-space-7);
}

/* ── The drawer toggle (phones) ──────────────────────────────────────── */

/* Visually hidden rather than display:none, so it stays focusable. */
.nav-toggle {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.nav-toggle-btn {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: var(--iot-space-1);
  width: var(--iot-touch);
  height: var(--iot-touch);
  padding: var(--iot-space-3) var(--iot-space-3);
  border: 1px solid var(--iot-border-strong);
  border-radius: var(--iot-radius-control);
  cursor: pointer;
}
.nav-toggle-btn span {
  display: block;
  height: 2px;
  background: var(--iot-text-secondary);
}
.nav-toggle:focus-visible + .topbar .nav-toggle-btn {
  outline: 2px solid var(--iot-accent);
  outline-offset: 2px;
}

/* ── Panels, stats, tables ───────────────────────────────────────────── */

.panel {
  background: var(--iot-surface);
  border: 1px solid var(--iot-border);
  border-radius: var(--iot-radius-panel);
  padding: var(--iot-space-4);
  margin-bottom: var(--iot-space-4);
}

.stats {
  display: grid;
  gap: var(--iot-space-3);
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  margin-bottom: var(--iot-space-5);
}
.stat {
  background: var(--iot-surface);
  border: 1px solid var(--iot-border);
  border-radius: var(--iot-radius-panel);
  padding: var(--iot-space-3) var(--iot-space-4);
}
a.stat { display: block; color: inherit; text-decoration: none; }
a.stat:hover { border-color: var(--iot-border-strong); }
.stat .n {
  font-size: var(--iot-size-title);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.stat .l {
  color: var(--iot-text-muted);
  font-size: var(--iot-size-small);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.scroll { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  text-align: left;
  padding: var(--iot-space-2) var(--iot-space-3);
  border-bottom: 1px solid var(--iot-border);
  vertical-align: middle;
}
th {
  color: var(--iot-text-muted);
  font-weight: 600;
  font-size: var(--iot-size-small);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
tr:last-child td { border-bottom: none; }
tr.is-error td { background: var(--iot-state-error-bg); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { white-space: nowrap; text-align: right; }
td.truncate {
  max-width: 32ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tag {
  display: inline-block;
  padding: 0 var(--iot-space-2);
  border: 1px solid var(--iot-border-strong);
  border-radius: var(--iot-radius-control);
  color: var(--iot-text-secondary);
  font-size: var(--iot-size-small);
}

/* ── Forms and buttons ───────────────────────────────────────────────── */

form.inline { display: inline; margin: 0; }
label.inline { display: inline-flex; align-items: center; margin: 0; }
.wide { width: 100%; }
.narrow { width: 8em; max-width: 100%; }
.small { font-size: var(--iot-size-small); }
/* Audit log: the sentence wraps; raw detail stays out of the way. */
table.audit td { vertical-align: top; }
table.audit td:nth-child(3) { white-space: normal; min-width: 16em; }
details.raw { margin-top: var(--iot-space-1); }
details.raw summary { cursor: pointer; color: var(--iot-text-muted); font-size: var(--iot-size-small); }
details.raw pre { margin-top: var(--iot-space-1); max-width: 48em; white-space: pre-wrap; word-break: break-word; }

label { display: block; font-weight: 600; margin-bottom: var(--iot-space-1); }
.field { margin-bottom: var(--iot-space-4); }
.hint { color: var(--iot-text-muted); font-size: var(--iot-size-small); }

input[type=text], input[type=search], input[type=email], input[type=password],
input[type=tel], input[type=number], input[type=url], input[type=date], select, textarea {
  min-height: var(--iot-touch);
  padding: var(--iot-space-2) var(--iot-space-3);
  background: var(--iot-raised);
  color: var(--iot-text);
  border: 1px solid var(--iot-border-strong);
  border-radius: var(--iot-radius-control);
  font: inherit;
  max-width: 100%;
}
textarea { width: 100%; min-height: 96px; resize: vertical; }

/* Small layout helpers. */
.half { flex: 1 1 280px; min-width: 0; }
.indent { margin-left: var(--iot-space-6); }
.condition-row { align-items: flex-end; }
.condition-row .field { flex: 1 1 180px; margin-bottom: var(--iot-space-2); }
.condition-row .field input, .condition-row .field select { width: 100%; }
.honeypot { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
.toolbar { margin-bottom: var(--iot-space-3); }
/* A row of controls directly above a panel never touches it. */
.row + .panel { margin-top: var(--iot-space-3); }
.toolbar + .panel { margin-top: 0; }

/* Two or three views of one page (Activity: events and messages). */
.tabs { display: flex; flex-wrap: wrap; gap: var(--iot-space-4); border-bottom: 1px solid var(--iot-border);
        margin-bottom: var(--iot-space-4); }
.tabs a { display: inline-flex; align-items: center; min-height: var(--iot-touch); color: var(--iot-text-secondary);
          text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--iot-text); }
.tabs a[aria-current="page"] { color: var(--iot-text); border-bottom-color: var(--iot-accent); }
.meter { height: 8px; background: var(--iot-surface); border: 1px solid var(--iot-border);
         border-radius: var(--iot-radius-control); overflow: hidden; margin: var(--iot-space-2) 0; }
.meter span { display: block; height: 100%; background: var(--iot-accent); }
.meter.is-high span { background: var(--iot-state-degraded); }
.meter.is-full span { background: var(--iot-state-error); }
.spaced { margin-top: var(--iot-space-4); }
h3.subhead { font-size: var(--iot-size-body); margin: var(--iot-space-5) 0 var(--iot-space-2); }
fieldset.plain { border: 0; padding: 0; margin: 0 0 var(--iot-space-4); }
fieldset.plain legend { font-weight: 600; padding: 0; margin-bottom: var(--iot-space-2); }

/* Copyable values: URLs, secrets, payload templates. */
pre.code {
  margin: 0 0 var(--iot-space-3);
  padding: var(--iot-space-3);
  background: var(--iot-raised);
  border: 1px solid var(--iot-border);
  border-radius: var(--iot-radius-control);
  font-family: var(--iot-font-mono);
  font-size: var(--iot-size-small);
  white-space: pre-wrap;
  word-break: break-all;
  overflow-x: auto;
}
.copy-row { display: flex; gap: var(--iot-space-2); align-items: flex-start; flex-wrap: wrap; }
.copy-row pre.code { flex: 1 1 280px; margin: 0; }
details.templates summary { cursor: pointer; min-height: var(--iot-touch); display: flex; align-items: center; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: var(--iot-space-2) var(--iot-space-4); margin: 0 0 var(--iot-space-4); }
dl.facts dt { color: var(--iot-text-secondary); }
dl.facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
@media (max-width: 768px) { dl.facts { grid-template-columns: 1fr; } dl.facts dd { margin-bottom: var(--iot-space-2); } }

/* Checkboxes with a wrapping label, and errors under a field. */
.check {
  display: flex;
  gap: var(--iot-space-3);
  align-items: flex-start;
  font-weight: 400;
  margin-bottom: var(--iot-space-1);
}
.check input {
  flex: none;
  width: 20px;
  height: 20px;
  margin-top: 3px;
  accent-color: var(--iot-accent);
}

.field-error { margin: var(--iot-space-1) 0 0; font-size: var(--iot-size-small); }
.field-error .state { white-space: normal; color: var(--iot-text); }
input[aria-invalid="true"] { border-color: var(--iot-state-error); }

/* Secondary is the default: outlined. Exactly one .primary per view. */
button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--iot-space-2);
  min-height: var(--iot-touch);
  padding: 0 var(--iot-space-4);
  background: transparent;
  color: var(--iot-text-accent);
  border: 1px solid var(--iot-accent-outline);
  border-radius: var(--iot-radius-control);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
button:hover, .btn:hover { background: var(--iot-surface); }
button:active, .btn:active { background: var(--iot-raised); }

button.primary, .btn.primary {
  background: var(--iot-accent);
  border-color: var(--iot-accent);
  color: var(--iot-accent-fg);
}
button.primary:hover, .btn.primary:hover {
  background: var(--iot-accent-hover);
  border-color: var(--iot-accent-hover);
}
button.primary:active, .btn.primary:active {
  background: var(--iot-accent-pressed);
  border-color: var(--iot-accent-pressed);
}

/* Destructive: never primary-styled, and distinct from what sits beside it. */
button.danger {
  color: var(--iot-state-error);
  border-color: var(--iot-border-strong);
}
button.danger:hover { background: var(--iot-state-error-bg); }

button.plain, .btn.plain {
  border-color: transparent;
  color: var(--iot-text-secondary);
}

/* A submit button that reads as a link (sign out). */
button.linkish {
  min-height: var(--iot-touch);
  padding: 0;
  border: none;
  background: none;
  color: var(--iot-text-secondary);
  font-weight: 400;
  text-decoration: underline;
  text-underline-offset: 2px;
}
button.linkish:hover { background: none; color: var(--iot-text); }

button:disabled, button[aria-disabled="true"] {
  cursor: not-allowed;
  color: var(--iot-text-muted);
  border-color: var(--iot-border);
  background: transparent;
}

.row { display: flex; gap: var(--iot-space-2); align-items: center; flex-wrap: wrap; }
.muted { color: var(--iot-text-muted); }

/* Empty states are centered: the one place application text is. */
.empty {
  padding: var(--iot-space-6) var(--iot-space-4);
  text-align: center;
  color: var(--iot-text-secondary);
}
.empty p { margin-left: auto; margin-right: auto; }

.flash {
  display: flex;
  align-items: flex-start;
  gap: var(--iot-space-2);
  padding: var(--iot-space-3) var(--iot-space-4);
  margin: 0 0 var(--iot-space-4);
  border: 1px solid var(--iot-border);
  border-radius: var(--iot-radius-panel);
  background: var(--iot-surface);
  max-width: none;
}
.flash.error {
  border-color: var(--iot-state-error);
  background: var(--iot-state-error-bg);
}
.flash .state { white-space: normal; color: var(--iot-text); }

.pager { display: flex; gap: var(--iot-space-2); align-items: center; margin-top: var(--iot-space-4); }

/* ── Phones ───────────────────────────────────────────────────────────
 * 768px, the same breakpoint as the admin kit, so both surfaces change shape
 * together. The sidebar becomes a drawer, and every table becomes its own
 * scroll container instead of being squeezed. */
@media (max-width: 768px) {
  .topbar { padding: var(--iot-space-2) var(--iot-space-3); gap: var(--iot-space-3); }
  .topbar .who, .topbar .surface-name { display: none; }
  .nav-toggle-btn { display: flex; }

  .shell { display: block; }

  .sidebar {
    display: none;
    position: static;
    max-height: none;
    padding: var(--iot-space-2) var(--iot-space-3);
    border-bottom: 1px solid var(--iot-border);
  }
  .nav-toggle:checked ~ .shell .sidebar { display: flex; }

  main { padding: var(--iot-space-4) var(--iot-space-3) var(--iot-space-6); }

  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  input[type=text], input[type=search], input[type=email], input[type=password],
  input[type=tel], input[type=number], input[type=url], input[type=date], select, textarea {
    width: 100%;
  }
}

/* Home statistics: server-rendered bars in state colors (never accent). */
.chart { margin: var(--iot-space-4) 0 0; }
.chart svg { display: block; max-width: 100%; }
.chart .axis { stroke: var(--iot-border); stroke-width: 1; }
.chart .tick { fill: var(--iot-text-muted); font-size: 11px; }
.chart figcaption { display: flex; flex-wrap: wrap; gap: var(--iot-space-4); margin-top: var(--iot-space-2);
                    font-size: var(--iot-size-small); color: var(--iot-text-secondary); }
.chart .key { display: inline-flex; align-items: center; gap: var(--iot-space-2); }
.chart .swatch { width: 10px; height: 10px; display: inline-block; }
.series-delivered { fill: var(--iot-state-online); background: var(--iot-state-online); }
.series-sent { fill: var(--iot-text-secondary); background: var(--iot-text-secondary); }
.series-failed { fill: var(--iot-state-error); background: var(--iot-state-error); }
.series-dropped { fill: var(--iot-state-muted); background: var(--iot-state-muted); }
.chart details { margin-top: var(--iot-space-3); }
.chart summary { cursor: pointer; color: var(--iot-text-muted); font-size: var(--iot-size-small); }


/* Inquiry threads: what we sent, and what came back. */
.thread { list-style: none; margin: 0 0 var(--iot-space-4); padding: 0; }
.thread > li {
  padding: var(--iot-space-3) 0 0;
  border-top: 1px solid var(--iot-border);
  margin-top: var(--iot-space-3);
}
.thread > li:first-child { border-top: 0; margin-top: 0; padding-top: 0; }
.thread .who {
  display: flex;
  flex-wrap: wrap;
  gap: var(--iot-space-2);
  align-items: baseline;
  margin: 0 0 var(--iot-space-2);
  font-size: var(--iot-size-small);
  color: var(--iot-text-muted);
}
.thread .who strong { color: var(--iot-text); }
.thread .said { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.thread > li.ours { padding-left: var(--iot-space-3); border-left: 3px solid var(--iot-accent); }

/* A count beside a nav link: answers waiting to be read. */
.badge {
  display: inline-block;
  margin-left: var(--iot-space-2);
  min-width: 18px;
  padding: 0 var(--iot-space-1);
  border-radius: 9px;
  background: var(--iot-accent);
  color: var(--iot-accent-fg);
  font-size: var(--iot-size-small);
  font-weight: 600;
  line-height: 18px;
  text-align: center;
}

/* Trigger form: recipes to start from, and choices that explain themselves. */
.recipes { list-style: none; margin: 0; padding: 0; }
.recipes > li { padding: var(--iot-space-2) 0; border-top: 1px solid var(--iot-border); }
.recipes > li:first-child { border-top: 0; }
.recipes > li > a { font-weight: 600; }
.recipes .muted { display: block; font-size: var(--iot-size-small); }
.group-heading {
  margin: var(--iot-space-4) 0 var(--iot-space-2);
  font-size: var(--iot-size-small);
  font-weight: 600;
  color: var(--iot-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.group-heading:first-of-type { margin-top: var(--iot-space-2); }
label.check.choice { align-items: flex-start; margin-bottom: var(--iot-space-3); }
label.check.choice .what {
  display: block;
  font-size: var(--iot-size-small);
  color: var(--iot-text-muted);
}
label.check.choice .tag { margin-left: var(--iot-space-2); }
ul.field-help { margin: 0 0 var(--iot-space-3) var(--iot-space-5); padding: 0; }
ul.field-help li { margin-bottom: var(--iot-space-1); }

/* Condition rows: the remove button sits at the end of the row, level with
   the inputs rather than the labels, and is the first thing to wrap. */
.condition-row .row { align-items: flex-end; flex-wrap: wrap; }
/* The basis is only the wrap threshold: the inputs still grow to share the
   row, and the button keeps its place until the screen is genuinely narrow. */
.condition-row .row > .field { flex: 1 1 130px; }
.condition-row .field.row-action { flex: 0 0 auto; margin-bottom: var(--iot-space-2); }
.condition-row .field.row-action button { min-height: var(--iot-touch); }
button.quiet {
  background: none;
  border: 1px solid var(--iot-border);
  color: var(--iot-text-secondary);
}
button.quiet:hover { border-color: var(--iot-border-strong); color: var(--iot-text); }
