:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #f1f0ec;
  --text: #131312;
  --muted: #76736c;
  --line: rgba(19, 19, 18, 0.09);
  --line-strong: rgba(19, 19, 18, 0.16);
  --accent: #049fd9;
  --accent-deep: #057aa3;
  --ok: #17a673;
  --warn: #d9930a;
  --down: #e5484d;
  --radius: 16px;
  --radius-sm: 11px;
  --shadow: 0 1px 2px rgba(19, 19, 18, 0.04), 0 8px 24px -12px rgba(19, 19, 18, 0.14);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html[data-scheme='dark'] {
  --bg: #0e0e0d;
  --surface: #171716;
  --surface-2: #1f1f1d;
  --text: #f4f3ef;
  --muted: #9b988f;
  --line: rgba(255, 255, 255, 0.09);
  --line-strong: rgba(255, 255, 255, 0.18);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 12px 28px -16px rgba(0, 0, 0, 0.7);
}

* { box-sizing: border-box; }

/* Component display rules must never beat the hidden attribute. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  letter-spacing: -0.01em;
  overscroll-behavior-y: none;
}

h1, h2, h3, strong { letter-spacing: -0.025em; }
h1 { font-size: 27px; line-height: 1.15; margin: 0 0 6px; font-weight: 640; }
h2 { font-size: 14px; font-weight: 600; margin: 0; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }

.app {
  padding: calc(var(--safe-top) + 10px) 16px calc(var(--safe-bottom) + 28px);
  max-width: 560px;
  margin: 0 auto;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 2px 18px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { border-radius: 8px; display: block; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-size: 15px; font-weight: 650; }
.brand-text span { font-size: 11px; color: var(--muted); letter-spacing: 0.1em; text-transform: uppercase; }

.icon-btn {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
}
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn.spinning svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- status ---------- */
.status-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.status-card[data-state='down'] { border-color: color-mix(in srgb, var(--down) 35%, var(--line)); }
.status-card[data-state='warn'] { border-color: color-mix(in srgb, var(--warn) 35%, var(--line)); }

.status-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.pill {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 5px 10px 5px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 12px; font-weight: 600;
}
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--muted) 18%, transparent);
}
.pill[data-state='ok'] .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 18%, transparent); }
.pill[data-state='warn'] .dot { background: var(--warn); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn) 18%, transparent); }
.pill[data-state='down'] .dot { background: var(--down); box-shadow: 0 0 0 3px color-mix(in srgb, var(--down) 18%, transparent); }
.pill[data-state='busy'] .dot { background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

#status-title { margin-top: 4px; }
#status-sub { margin-bottom: 16px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: var(--radius-sm);
  padding: 12px 16px;
  font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.12s ease, background 0.15s ease;
  text-decoration: none;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; pointer-events: none; }
.btn svg { width: 17px; height: 17px; }
.btn-primary { background: var(--text); color: var(--bg); width: 100%; }
html[data-scheme='dark'] .btn-primary { background: var(--text); color: #0e0e0d; }
.btn-ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--line-strong); width: 100%;
  margin-top: 10px;
}

/* ---------- metrics ---------- */
.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 18px;
}
.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 13px 14px;
  display: flex; flex-direction: column; gap: 2px;
}
.metric-label {
  font-size: 11px; font-weight: 600; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--muted);
}
.metric strong { font-size: 21px; font-weight: 620; font-variant-numeric: tabular-nums; }
.metric-sub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.bar {
  display: block; height: 3px; margin-top: 8px;
  background: var(--surface-2); border-radius: 999px; overflow: hidden;
}
.bar i {
  display: block; height: 100%; width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.5s cubic-bezier(0.22, 1, 0.36, 1), background 0.3s ease;
}
.bar i[data-level='warn'] { background: var(--warn); }
.bar i[data-level='down'] { background: var(--down); }

/* ---------- blocks ---------- */
.block { margin-bottom: 22px; }
.block-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 9px; padding: 0 2px;
}

.list, .timeline { list-style: none; margin: 0; padding: 0; }
.list {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.row {
  display: flex; align-items: center; gap: 11px;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  min-height: 52px;
}
.row:first-child { border-top: 0; }
.row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.row-title {
  font-size: 14px; font-weight: 560;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.row-sub {
  font-size: 12px; color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.row-meta {
  font-size: 12px; color: var(--muted);
  font-variant-numeric: tabular-nums; flex: none;
}
.row-action {
  flex: none; width: 30px; height: 30px;
  display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: 8px;
  background: transparent; color: var(--muted);
  cursor: pointer;
}
.row-action:active { background: var(--surface-2); }
.row-action svg { width: 15px; height: 15px; }

.state-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--muted); }
.state-dot[data-state='ok'] { background: var(--ok); }
.state-dot[data-state='warn'] { background: var(--warn); }
.state-dot[data-state='down'] { background: var(--down); }
.state-dot[data-state='idle'] { background: color-mix(in srgb, var(--muted) 55%, transparent); }

/* ---------- tiles ---------- */
.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.tile {
  display: flex; flex-direction: column; align-items: flex-start; gap: 9px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit; font-size: 13px; font-weight: 560;
  text-align: left;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease;
}
.tile:active { transform: scale(0.98); background: var(--surface-2); }
.tile svg { width: 20px; height: 20px; color: var(--accent); }

/* ---------- timeline ---------- */
.timeline { padding-left: 2px; }
.timeline li {
  position: relative;
  padding: 0 0 13px 20px;
  font-size: 13px;
  color: var(--muted);
}
.timeline li::before {
  content: '';
  position: absolute; left: 0; top: 6px;
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--line-strong);
}
.timeline li::after {
  content: '';
  position: absolute; left: 2.5px; top: 14px; bottom: 0;
  width: 1px; background: var(--line);
}
.timeline li:last-child::after { display: none; }
.timeline b { color: var(--text); font-weight: 560; }
.timeline .err::before { background: var(--down); }
.timeline .ok::before { background: var(--ok); }

/* ---------- switch ---------- */
.switch-row {
  display: flex; align-items: center; gap: 12px;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.switch-row > div { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.switch-row strong { font-size: 14px; font-weight: 560; }
.switch-row .muted { font-size: 12px; }
.switch-row input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch {
  width: 42px; height: 25px; border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  position: relative; flex: none;
  transition: background 0.18s ease, border-color 0.18s ease;
}
.switch::after {
  content: '';
  position: absolute; top: 2px; left: 2px;
  width: 19px; height: 19px; border-radius: 50%;
  background: var(--muted);
  transition: transform 0.2s cubic-bezier(0.22, 1, 0.36, 1), background 0.18s ease;
}
.switch-row input:checked + .switch { background: var(--accent); border-color: var(--accent); }
.switch-row input:checked + .switch::after { transform: translateX(17px); background: #fff; }
.switch-row input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 3px; }

/* ---------- footer ---------- */
.footer {
  display: flex; justify-content: space-between;
  padding: 6px 2px 0;
  font-size: 11px; color: var(--muted);
}

/* ---------- gate ---------- */
.gate {
  min-height: 100vh; min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; padding: 32px; text-align: center;
}
.gate img { border-radius: 14px; }
.gate h1 { font-size: 19px; margin: 8px 0 0; }
.gate p { color: var(--muted); font-size: 14px; max-width: 280px; }
.gate .btn { margin-top: 12px; width: auto; padding: 12px 22px; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: calc(var(--safe-bottom) + 22px);
  transform: translate(-50%, 16px);
  background: var(--text); color: var(--bg);
  padding: 11px 17px; border-radius: 999px;
  font-size: 13px; font-weight: 550;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.24s cubic-bezier(0.22, 1, 0.36, 1);
  max-width: min(88vw, 420px);
  text-align: center;
  z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 70; display: flex; align-items: flex-end; }
.sheet[hidden] { display: none; }
.sheet-backdrop {
  position: absolute; inset: 0;
  background: rgba(0, 0, 0, 0.42);
  backdrop-filter: blur(2px);
  animation: fade 0.18s ease;
}
@keyframes fade { from { opacity: 0; } }
.sheet-body {
  position: relative;
  width: 100%; max-width: 560px; margin: 0 auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  border-top: 1px solid var(--line);
  padding: 10px 18px calc(var(--safe-bottom) + 20px);
  animation: rise 0.26s cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes rise { from { transform: translateY(28px); opacity: 0.6; } }
.sheet-grip {
  display: block; width: 36px; height: 4px;
  background: var(--line-strong); border-radius: 999px;
  margin: 2px auto 14px;
}
.sheet-body h2 { font-size: 18px; margin-bottom: 6px; }
.sheet-body p { font-size: 14px; line-height: 1.5; }
.sheet-field { margin-top: 14px; }
.sheet-field label {
  display: block; font-size: 12px; font-weight: 600;
  letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 6px;
}
.sheet-field select {
  width: 100%; padding: 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 14px;
}
.sheet-actions { display: flex; gap: 10px; margin-top: 18px; }
.sheet-actions .btn { margin: 0; }
.sheet-actions .btn-ghost { flex: 1; }
.sheet-actions .btn-primary { flex: 1.4; }

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