:root {
  color-scheme: light dark;
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #1b2230;
  --muted: #5d6a82;
  --border: #d5dbe6;
  --primary: #2358d6;
  --primary-text: #ffffff;
  --danger: #c62828;
  --ok: #1e8a4c;
  --warn: #b26a00;
  --off: #8a94a8;
  /* chart series (validated for CVD separation and 3:1 contrast on --surface) */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --shadow: 0 1px 2px rgba(20, 30, 50, .08);
  --radius: 12px;
  --tabbar-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151d;
    --surface: #1a2030;
    --surface-2: #232b3f;
    --text: #e6eaf2;
    --muted: #9aa6bd;
    --border: #2f394f;
    --primary: #6b97ff;
    --primary-text: #0b1226;
    --danger: #ff6b6b;
    --ok: #4cd08a;
    --warn: #f0b24a;
    --off: #6c7790;
    --series-1: #3987e5;
    --series-2: #d95926;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.spacer { flex: 1; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- controls ---------- */
.btn {
  appearance: none;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:focus-visible, input:focus-visible, select:focus-visible, .tab:focus-visible, summary:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: var(--primary-text); border-color: transparent; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--primary); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.small { min-height: 44px; padding: 0 12px; font-size: .9rem; }

input[type="text"], input[type="password"], input[type="number"], input[type="search"], select {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.field { display: flex; flex-direction: column; gap: 4px; margin: 12px 0; }
.field > span { font-size: .9rem; color: var(--muted); }
.inline { display: inline-flex; align-items: center; gap: 8px; }
.inline select { width: auto; }
.check { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.check input { width: 22px; height: 22px; margin: 0; }
.form-error { color: var(--danger); margin: 8px 0; }

/* ---------- layout ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(100%, 380px); display: flex; flex-direction: column; }
.login-title { font-size: 1.5rem; }
.login-card .btn { margin-top: 8px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}
.app-title { font-size: 1.15rem; }

.tabs { display: flex; gap: 6px; padding: 12px 16px 0; max-width: 1200px; margin: 0 auto; }
.tab {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 18px;
  border-radius: 10px; text-decoration: none; color: var(--muted);
  border: 1px solid transparent; font-weight: 600;
}
.tab[aria-current="page"] { background: var(--surface); color: var(--primary); border-color: var(--border); }

.panel { max-width: 1200px; margin: 0 auto; padding: 14px 16px 32px; display: flex; flex-direction: column; gap: 14px; }
.panel-error { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); border: 1px solid var(--danger); color: var(--text); border-radius: 10px; padding: 10px 14px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.section-title { font-size: 1.05rem; margin-top: 4px; }
.scan-box { display: grid; gap: 8px; border-style: dashed; }
.scan-box p { margin: 0; }
.scan-box .badge { max-width: 60%; }
.badge.ellipsis { display: inline-block; }

/* ---------- badges / dots ---------- */
.badge {
  display: inline-flex; align-items: center;
  padding: 2px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.ok { color: var(--ok); border-color: var(--ok); }
.badge.warn { color: var(--warn); border-color: var(--warn); }
.badge.off { color: var(--off); }
.badge.danger { color: var(--danger); border-color: var(--danger); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--off); flex: none; }
.dot.ok { background: var(--ok); }
.dot.off { background: transparent; border: 2px solid var(--off); }
.dot.unknown { background: var(--warn); opacity: .6; }

/* ---------- cards ---------- */
.item { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.item-head { display: flex; align-items: center; gap: 8px; min-width: 0; }
.item-title { font-weight: 700; min-width: 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; font-size: .92rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.out {
  margin: 0; padding: 8px 10px; border-radius: 8px; background: var(--surface-2);
  font: .85rem/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 200px; overflow: auto;
}
.toggle-list { display: flex; flex-direction: column; }
.toggle-list .check { justify-content: space-between; border-bottom: 1px solid var(--border); }
.toggle-list .check:last-child { border-bottom: 0; }
.group-title { display: flex; align-items: center; gap: 8px; margin: 6px 0; }
details > summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 600; }
.img-row { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 8px 0; border-top: 1px solid var(--border); }

/* ---------- dialogs ---------- */
.dialog {
  width: min(94vw, 760px);
  max-height: 92vh;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
.dialog::backdrop { background: rgba(0, 0, 0, .5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.dialog-title { font-size: 1.1rem; overflow-wrap: anywhere; }
.dialog-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.log-body {
  margin: 0; height: 55vh; overflow: auto; padding: 10px; border-radius: 8px;
  background: #0d1117; color: #d7dde8;
  font: .8rem/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ---------- toasts ---------- */
.toasts {
  position: fixed; right: 16px; bottom: 16px; z-index: 50;
  display: flex; flex-direction: column; gap: 8px; max-width: min(92vw, 420px); pointer-events: none;
}
.toast {
  pointer-events: auto;
  padding: 10px 14px; border-radius: 10px; background: var(--text); color: var(--bg);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .3); overflow-wrap: anywhere;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

/* ---------- phone ---------- */
/* ---------- system (task manager) ---------- */
.host-line { margin: 0 2px; }
.stats { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.stat { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stat-line { display: flex; align-items: baseline; gap: 8px; }
.stat-value { font-size: 1.75rem; font-weight: 600; line-height: 1.2; }
.legend { display: flex; gap: 14px; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }

.chart { position: relative; margin-top: auto; padding-top: 8px; outline-offset: 2px; border-radius: 6px; }
.chart:focus-visible { outline: 2px solid var(--primary); }
.chart-svg { display: block; width: 100%; overflow: visible; touch-action: pan-y; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-threshold { stroke: var(--warn); stroke-width: 1; opacity: .7; }
.chart-tick { fill: var(--muted); font-size: 10px; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round; }
.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { stroke: none; fill-opacity: .1; }
.chart-dot { stroke: var(--surface); stroke-width: 2; }
.chart-cross { stroke: var(--muted); stroke-width: 1; pointer-events: none; }
.chart-line.s1, .chart-cross.s1 { stroke: var(--series-1); }
.chart-line.s2 { stroke: var(--series-2); }
.chart-area.s1, .chart-dot.s1 { fill: var(--series-1); }
.chart-area.s2, .chart-dot.s2 { fill: var(--series-2); }
.chart-x { display: flex; justify-content: space-between; font-size: .75rem; margin-top: 2px; }
.chart-tip {
  position: absolute; top: -6px; z-index: 2; pointer-events: none; white-space: nowrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .15); padding: 4px 8px; font-size: .8rem;
}
.tip-row { display: flex; align-items: center; gap: 6px; }
.tip-key { display: inline-block; width: 12px; height: 2px; border-radius: 1px; }
.tip-key.s1 { background: var(--series-1); }
.tip-key.s2 { background: var(--series-2); }

.meters { display: flex; flex-direction: column; gap: 6px; }
.meter-row { display: grid; grid-template-columns: minmax(4.5rem, 7rem) 1fr auto; align-items: center; gap: 10px; font-size: .9rem; }
.meter-label { color: var(--muted); }
.meter { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.meter-fill { display: block; height: 100%; border-radius: 4px; background: var(--primary); }
.meter-fill.warn { background: var(--warn); }
.meter-fill.crit { background: var(--danger); }
.meter-value { font-variant-numeric: tabular-nums; text-align: right; min-width: 3.5rem; }
.meter-row.stacked { grid-template-columns: 1fr auto; grid-template-areas: "label value" "bar bar"; row-gap: 4px; }
.meter-row.stacked .meter-label { grid-area: label; color: var(--text); }
.meter-row.stacked .meter { grid-area: bar; }
.meter-row.stacked .meter-value { grid-area: value; color: var(--muted); }
.sub-h { margin-top: 6px; }
.rates { display: flex; flex-wrap: wrap; column-gap: 12px; }
.rates span { white-space: nowrap; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; }
.proc-table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: .9rem; }
.proc-table th, .proc-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; }
.proc-table th { padding: 0; }
.proc-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.proc-table th:nth-child(2) { width: 4.5rem; }
.proc-table th:nth-child(3) { width: 7rem; }
.proc-table th:nth-child(4) { width: 4.5rem; }
.proc-table th:nth-child(5) { width: 5.5rem; }
.th-btn {
  width: 100%; min-height: 40px; padding: 6px 8px; border: 0; background: none; color: var(--muted);
  font: inherit; font-weight: 600; text-align: inherit; cursor: pointer;
}
th[aria-sort] .th-btn { color: var(--text); }
th[aria-sort="descending"] .th-btn::after { content: " ▼"; font-size: .7em; }
th[aria-sort="ascending"] .th-btn::after { content: " ▲"; font-size: .7em; }
.col-name { overflow: hidden; }
.proc-name {
  display: block; width: 100%; padding: 4px 0; border: 0; background: none; color: inherit;
  font: inherit; text-align: left; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.proc-detail-row td { background: var(--surface-2); }
.proc-cmd { overflow-wrap: anywhere; }

@media (max-width: 639px) {
  /* collapse (not remove) the user column so the detail row's colspan still matches */
  .proc-table th:nth-child(3), .proc-table td.col-user { width: 0; padding: 0; font-size: 0; overflow: hidden; }
  .proc-table th:nth-child(2) { width: 3.6rem; }
  .proc-table th:nth-child(5) { width: 5rem; }
  body { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }
  .topbar { padding: 8px 12px; }
  #updated { display: none; }
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    margin: 0; padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--border);
    gap: 4px;
  }
  .tab { flex: 1; min-height: 48px; padding: 0 6px; }
  .panel { padding: 12px; }
  .toasts { right: 12px; left: 12px; bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom)); max-width: none; }
  .grid { grid-template-columns: 1fr; }
  .dialog { width: 100vw; max-width: 100vw; max-height: 100vh; height: 100vh; border-radius: 0; margin: 0; }
  .log-body { height: calc(100vh - 190px); }
}

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: toast-in .15s ease-out; }
  @keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
}

.offscreen { position: fixed; left: -9999px; top: 0; opacity: 0; }
.routes-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.routes-row .field { flex: 1 1 220px; margin: 0; }
.empty { text-align: center; padding: 28px 12px; }
