:root {
  --bg: #0f1216;
  --surface: #1a1f27;
  --border: #2a3340;
  --text: #e8ecf1;
  --muted: #8b97a8;
  --accent: #5b9fd4;
  --danger: #e85d5d;
  --ok: #6bc96b;
  --tap-min: 44px;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9;
    --surface: #fff;
    --border: #d8dee8;
    --text: #1a1f27;
    --muted: #5c6778;
    --accent: #1a6cad;
    --danger: #c42b2b;
    --ok: #2d7a2d;
  }
}

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

html {
  font-size: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--tap-min) + env(safe-area-inset-bottom, 0px) + 12px);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  min-height: var(--tap-min);
  padding: 0.5rem 1rem;
  padding-top: max(0.5rem, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.main {
  max-width: 42rem;
  margin: 0 auto;
  padding: 1rem;
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  justify-content: space-around;
  align-items: stretch;
  min-height: var(--tap-min);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tab {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap-min);
  padding: 0 0.5rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--muted);
  text-decoration: none;
  border: none;
  background: transparent;
  cursor: pointer;
}

.tab:hover,
.tab:focus-visible {
  color: var(--text);
  outline: none;
}

.tab.active {
  color: var(--accent);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.card-title {
  margin: 0 0 0.5rem;
  font-size: 0.9375rem;
  font-weight: 600;
}

.muted {
  color: var(--muted);
  font-size: 0.875rem;
}

.mono {
  font-family: var(--mono);
  font-size: 0.8125rem;
  word-break: break-all;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  align-items: center;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  padding: 0 1rem;
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 500;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.btn-ghost {
  background: transparent;
}

label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 500;
  margin-bottom: 0.25rem;
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  min-height: var(--tap-min);
  padding: 0.5rem 0.75rem;
  font: inherit;
  font-size: 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

textarea {
  min-height: 5rem;
  resize: vertical;
}

.field {
  margin-bottom: 0.75rem;
}

.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 0.875rem;
}

.alert-error {
  border-color: var(--danger);
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.alert-info {
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tap-min) + env(safe-area-inset-bottom, 0px) + 1rem);
  transform: translateX(-50%);
  max-width: min(90vw, 24rem);
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.25);
  font-size: 0.875rem;
  z-index: 20;
}

.toast.hidden {
  display: none;
}

.toast.error {
  border-color: var(--danger);
}

.json-block {
  margin: 0;
  padding: 0.75rem;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 0.75rem;
  line-height: 1.4;
  background: var(--bg);
  border-radius: calc(var(--radius) - 2px);
  white-space: pre-wrap;
  word-break: break-word;
}

details.expand-json {
  margin-top: 0.5rem;
}

details.expand-json summary {
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  cursor: pointer;
  font-size: 0.8125rem;
  color: var(--accent);
  user-select: none;
}

.h-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.loading {
  color: var(--muted);
  font-size: 0.875rem;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 1rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

@media (min-width: 480px) {
  .modal-backdrop {
    align-items: center;
  }
}

.modal {
  width: 100%;
  max-width: 24rem;
  max-height: 85dvh;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.modal h2 {
  margin: 0 0 0.75rem;
  font-size: 1.0625rem;
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.more-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.more-list a {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 0.15rem;
  min-height: var(--tap-min);
  padding: 0.5rem 0.75rem;
  color: var(--text);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
}

.more-link-title {
  font-weight: 600;
  font-size: 0.9375rem;
}

.more-link-desc {
  font-size: 0.8125rem;
  color: var(--muted);
}

.more-list a:last-child {
  border-bottom: none;
}

.more-list a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 1.5rem;
  padding: 0 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  font-family: var(--mono);
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  color: var(--accent);
}

.badge-ok {
  background: color-mix(in srgb, var(--ok) 22%, transparent);
  color: var(--ok);
}

.badge-warn {
  background: color-mix(in srgb, var(--danger) 18%, transparent);
  color: var(--danger);
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem 1rem;
  margin: 0;
}

.kpi-grid dt {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 500;
}

.kpi-grid dd {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.875rem;
}

.count-list {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 0.8125rem;
}

.count-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--border);
}

.count-list li:last-child {
  border-bottom: none;
}

.count-list .label {
  color: var(--muted);
  word-break: break-word;
}

.count-list .val {
  font-family: var(--mono);
  flex-shrink: 0;
}

.subhead {
  margin: 0 0 0.5rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 0.75rem 0;
}

details.expand-json pre {
  margin-top: 0.5rem;
}
