/* ForwardHub admin UI. Mobile first; follows the system light/dark setting. */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6b6a64;
  --hairline: #e1e0d9;
  --input-border: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --primary: #2a78d6;
  --primary-hover: #256abf;
  --link: #256abf;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --critical-text: #b02a2a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 2px 8px rgba(11, 11, 11, 0.04);
  --font: system-ui, -apple-system, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #252523;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #9c9a93;
    --hairline: #2c2c2a;
    --input-border: #45453f;
    --border: rgba(255, 255, 255, 0.1);
    --link: #86b6ef;
    --critical-text: #f08a8a;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 15px/1.55 var(--font);
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

h1 { font-size: 22px; line-height: 1.3; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 16px; margin: 0; }
p { margin: 0 0 10px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.id { font-family: var(--mono); font-size: 12.5px; color: var(--muted); }
code { font-family: var(--mono); font-size: 13px; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* --- top bar & tabs ------------------------------------------------------------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--hairline);
}
.topbar__row {
  max-width: 1120px; margin: 0 auto; padding: 10px 16px 4px;
  display: flex; align-items: center; gap: 12px;
}
.brand { font-weight: 700; font-size: 17px; color: var(--ink); letter-spacing: -0.01em; }
.brand:hover { text-decoration: none; }
.topbar__status { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; min-width: 0; }
.topbar__logout { margin: 0; }
.tabs {
  max-width: 1120px; margin: 0 auto; padding: 0 8px;
  display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: 10px 12px; color: var(--ink-2); white-space: nowrap; font-weight: 500;
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--ink); text-decoration: none; }
.tab--active { color: var(--ink); border-bottom-color: var(--primary); }

.page { max-width: 1120px; margin: 0 auto; padding: 20px 16px 64px; }
.section { margin-top: 28px; }
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 12px; margin-bottom: 12px;
}
.section-head h2 { margin: 0; }
.lead { color: var(--ink-2); margin: -4px 0 16px; }

/* --- status: icon + label, never color alone ------------------------------------- */

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 1px 10px 1px 3px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
  font-size: 12.5px; font-weight: 500; line-height: 1.7; color: var(--ink); white-space: nowrap;
}
.status-icon {
  display: inline-grid; place-items: center; flex: none;
  width: 16px; height: 16px; border-radius: 50%;
  font-size: 10.5px; font-weight: 800; line-height: 1;
  background: var(--muted); color: #fff;
}
.status-icon--good { background: var(--good); }
.status-icon--warning { background: var(--warning); color: #0b0b0b; }
.status-icon--serious { background: var(--serious); color: #0b0b0b; }
.status-icon--critical { background: var(--critical); }

.banner {
  display: flex; align-items: center; flex-wrap: wrap; gap: 10px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 4px solid var(--muted); border-radius: var(--radius-sm);
  padding: 10px 12px; margin-bottom: 10px;
}
.banner--warning { border-left-color: var(--warning); }
.banner--serious { border-left-color: var(--serious); }
.banner--critical { border-left-color: var(--critical); }
.banner__text { margin: 0; flex: 1 1 220px; }
.banner form { margin: 0; }

.flash {
  padding: 10px 14px; margin-bottom: 16px; border-radius: var(--radius-sm);
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--good);
}
.flash--error { border-left-color: var(--critical); }
.flash--warning { border-left-color: var(--warning); }

/* --- stat tiles -------------------------------------------------------------------- */

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 760px) { .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; min-width: 0;
}
.tile__label { font-size: 13px; color: var(--ink-2); }
.tile__value { font-size: 30px; font-weight: 600; line-height: 1.25; margin-top: 4px; }
.tile__value--badge { font-size: 15px; padding: 9px 0 3px; }
.tile__of { font-size: 16px; font-weight: 500; color: var(--muted); }
.tile__sub {
  font-size: 13px; color: var(--muted); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* --- cards ------------------------------------------------------------------------- */

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
}
.card--muted { opacity: 0.68; }
.card--danger { border-left: 4px solid var(--critical); }
.card__head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px;
}
.card__head h2, .card__head h3 { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr)); gap: 12px; }
.grid .card { margin-bottom: 0; }
.route-card h3 a { color: var(--ink); }

.facts { margin: 0; display: grid; gap: 6px; }
.facts > div { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: 8px; align-items: baseline; }
.facts dt { color: var(--muted); font-size: 13px; }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.chat-title { font-weight: 500; }

.card__stats {
  display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 12px; padding-top: 10px;
  border-top: 1px solid var(--hairline); font-size: 13px; color: var(--ink-2);
}
.card__stats strong { color: var(--ink); font-size: 15px; }

.chips { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-block; padding: 0 8px; border-radius: 999px; font-size: 12.5px;
  background: var(--surface-2); border: 1px solid var(--border);
}

.problem {
  margin: 8px 0 0; padding: 6px 10px; border-radius: var(--radius-sm); font-size: 13px;
  background: var(--surface-2); border-left: 3px solid var(--critical); color: var(--ink);
}
.problem--soft { border-left-color: var(--serious); color: var(--ink-2); }
.problem--inline { display: block; margin-top: 6px; }
.empty { color: var(--muted); padding: 12px 0; }

details.card > summary { cursor: pointer; font-weight: 600; font-size: 16px; list-style: none; }
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary::after { content: "＋"; float: right; color: var(--muted); }
details.card[open] > summary { margin-bottom: 10px; }
details.card[open] > summary::after { content: "－"; }

/* --- buttons ----------------------------------------------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--input-border); background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn--primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-hover); }
.btn--danger { color: var(--critical-text); border-color: var(--critical); background: transparent; }
.btn--danger:hover { background: var(--surface-2); }
.btn--ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn--ghost:hover { color: var(--ink); }
.btn--sm { min-height: 34px; padding: 0 11px; font-size: 13.5px; }
.btn--block { width: 100%; }

/* --- forms ------------------------------------------------------------------------- */

.form { display: grid; gap: 16px; }
.form--inline { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; }
.form--inline .field { flex: 1 1 220px; }
.field { display: grid; gap: 6px; border: 0; padding: 0; margin: 0; min-width: 0; }
.field__label { font-weight: 600; font-size: 14px; padding: 0; }
.field__help { color: var(--muted); font-size: 13px; }
.field-row { display: grid; gap: 16px; }
@media (min-width: 760px) { .field-row { grid-template-columns: 1fr 1fr; } }

input:not([type="checkbox"]):not([type="radio"]), select, textarea {
  width: 100%; min-height: 42px; padding: 8px 12px;
  border: 1px solid var(--input-border); border-radius: var(--radius-sm);
  background: var(--page); color: var(--ink);
  font: inherit; font-size: 16px; /* 16px keeps iOS from zooming in */
}
textarea { resize: vertical; min-height: 76px; line-height: 1.45; }
input:disabled { color: var(--muted); }
input::placeholder, textarea::placeholder { color: var(--muted); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
[hidden] { display: none !important; }

.stack { display: grid; gap: 16px; }
.stack > .card { margin-bottom: 0; }
.stack-sm { display: grid; gap: 8px; }
.radios { display: flex; flex-wrap: wrap; gap: 8px; }
.radios .check { flex: 1 1 220px; }
.radios--stack { display: grid; }
.spaced-top { margin-top: 16px; }
.facts--wide > div { grid-template-columns: 110px minmax(0, 1fr); }
.facts + form, .facts + p { margin-top: 14px; }

.setup { border-left: 4px solid var(--primary); }
.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.step {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.step__text { flex: 1; min-width: 0; }
.step--done .step__text { color: var(--muted); text-decoration: line-through; }
.step form { margin: 0; }

.checks { display: grid; gap: 6px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); }
.check {
  display: flex; align-items: flex-start; gap: 10px; padding: 9px 12px; cursor: pointer;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface);
}
.check input { width: 18px; height: 18px; margin: 2px 0 0; flex: none; accent-color: var(--primary); }
.check__help { display: block; margin-top: 2px; }

.form-error {
  margin: 0; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--surface-2); border-left: 4px solid var(--critical);
}
.form-actions, .inline-forms { display: flex; flex-wrap: wrap; gap: 8px; }
.inline-forms { margin-top: 12px; }
.inline-forms form, .spaced { margin: 0; }
.spaced { margin-top: 10px; }
.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; margin-bottom: 16px; }
.field--inline { flex: 1 1 160px; max-width: 260px; }

/* --- tables (stack into cards on phones) ------------------------------------------ */

.table-wrap {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.card .table-wrap { border: 0; border-radius: 0; box-shadow: none; background: transparent; }
.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left; font-size: 12.5px; font-weight: 600; color: var(--muted);
  padding: 10px 12px; border-bottom: 1px solid var(--hairline); white-space: nowrap;
}
.table td { padding: 12px; border-bottom: 1px solid var(--hairline); vertical-align: top; }
.table td:first-child { min-width: 7.5em; }
.table tbody tr:last-child td { border-bottom: 0; }
.table--compact td { padding: 8px 12px; }
.table__actions { text-align: right; white-space: nowrap; }
.table__actions > * { vertical-align: middle; }
.table__actions form { display: inline; }
.row--muted td { opacity: 0.65; }
.card .table th:first-child, .card .table td:first-child { padding-left: 0; }

@media (max-width: 720px) {
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { padding: 12px 14px; border-bottom: 1px solid var(--hairline); }
  .card .table tr { padding: 10px 0; }
  .table tbody tr:last-child { border-bottom: 0; }
  .table td, .card .table td:first-child {
    display: grid; grid-template-columns: 84px minmax(0, 1fr); gap: 8px;
    padding: 3px 0; border: 0;
  }
  .table td::before { content: attr(data-label); color: var(--muted); font-size: 12.5px; }
  .table td > * { grid-column: 2; justify-self: start; }
  .table td > .problem { justify-self: stretch; }
  .table td.table__actions { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 10px; text-align: left; }
  .table td.table__actions::before, .table td[colspan]::before { content: none; }
  .table td[colspan] { display: block; }
}

/* --- activity ---------------------------------------------------------------------- */

.activity {
  list-style: none; margin: 0; padding: 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.activity__item { padding: 12px 14px; border-bottom: 1px solid var(--hairline); }
.activity__item:last-child { border-bottom: 0; }
.activity .empty { padding: 16px 14px; }
.activity__meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; font-size: 13.5px; }
.activity__route { font-weight: 600; }
.activity__text {
  margin: 6px 0 0; font-size: 14px; overflow-wrap: anywhere;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.activity__detail { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.activity__detail--failed { color: var(--critical-text); }

/* --- login ------------------------------------------------------------------------- */

.login { max-width: 380px; margin: 12vh auto 0; text-align: center; }
.login__brand { font-size: 28px; }
.login .card { text-align: left; margin-top: 20px; }
