:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --bg-elev: #fff;
  --fg: #000;
  --fg-2: rgba(60, 60, 67, .6);
  --sep: #c6c6c8;
  --accent: #0a84ff;
  --ok: #34c759;
  --warn: #ff9500;
  --danger: #ff3b30;
  --muted: #8e8e93;
  --top: calc(52px + env(safe-area-inset-top));
  --bottom: calc(58px + env(safe-area-inset-bottom));
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-size: 16px;
  background: var(--bg);
  color: var(--fg);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000;
    --bg-elev: #1c1c1e;
    --fg: #fff;
    --fg-2: rgba(235, 235, 245, .6);
    --sep: #38383a;
    --ok: #30d158;
    --warn: #ff9f0a;
    --danger: #ff453a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--fg); }
body { padding: var(--top) 0 var(--bottom); }
button, input, textarea, select { font: inherit; font-size: 16px; color: inherit; }
button, select, input { min-height: 44px; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }

#topbar {
  position: fixed; z-index: 20; inset: 0 0 auto;
  height: var(--top); padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  display: grid; grid-template-columns: 48px 1fr auto; align-items: center;
  background: color-mix(in srgb, var(--bg-elev) 90%, transparent);
  border-bottom: .5px solid var(--sep); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
}
#topbar h1 { margin: 0; font-size: 17px; text-align: center; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.top-actions { min-width: 48px; display: flex; justify-content: flex-end; gap: 4px; }
.icon-button { width: 44px; padding: 0; border: 0; background: transparent; color: var(--accent); font-size: 28px; }
.icon-button[hidden], button[hidden] { display: none; }

#view { width: min(100%, 720px); min-height: calc(100vh - var(--top) - var(--bottom)); margin: 0 auto; padding: 16px 14px 30px; outline: none; }
#tabs {
  position: fixed; z-index: 20; inset: auto 0 0; height: var(--bottom);
  padding: 4px max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--bg-elev) 92%, transparent); border-top: .5px solid var(--sep);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
}
#tabs a { min-height: 48px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; color: var(--muted); font-size: 10px; }
#tabs a span { font-size: 20px; line-height: 22px; font-weight: 600; }
#tabs a.active { color: var(--accent); }

.connection { position: fixed; z-index: 30; top: var(--top); left: 50%; transform: translateX(-50%); padding: 5px 14px; border-radius: 0 0 10px 10px; background: var(--warn); color: #000; font-size: 12px; font-weight: 700; }
.section-title { margin: 22px 12px 7px; color: var(--fg-2); font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .02em; }
.card { margin-bottom: 14px; overflow: hidden; border-radius: 12px; background: var(--bg-elev); }
.card.pad { padding: 16px; }
.row { width: 100%; min-height: 56px; padding: 10px 14px; display: flex; align-items: center; gap: 12px; border: 0; border-bottom: .5px solid var(--sep); background: var(--bg-elev); color: var(--fg); text-align: left; }
.row:last-child { border-bottom: 0; }
.row-body { min-width: 0; flex: 1; }
.row-title { display: flex; align-items: center; gap: 7px; font-weight: 600; }
.row-subtitle { margin-top: 3px; color: var(--fg-2); font-size: 13px; overflow-wrap: anywhere; }
.row-meta { color: var(--fg-2); font-size: 12px; text-align: right; }
.chevron { color: var(--muted); font-size: 22px; }
.status-dot { width: 9px; height: 9px; flex: 0 0 9px; border-radius: 50%; background: var(--muted); }
.status-dot.online { background: var(--ok); }
.status-dot.offline { background: var(--muted); }

.badge, .chip { display: inline-flex; align-items: center; min-height: 24px; padding: 2px 8px; border-radius: 999px; background: color-mix(in srgb, var(--muted) 16%, transparent); color: var(--muted); font-size: 11px; font-weight: 700; white-space: nowrap; }
.badge.blue { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.badge.purple { background: rgba(175, 82, 222, .18); color: #af52de; }
.badge.orange { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.green { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.red { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.chip { min-height: 32px; border: 0; padding: 5px 12px; }
.chip.active { background: var(--accent); color: #fff; }
.chips { display: flex; flex-wrap: wrap; gap: 7px; margin: 0 0 12px; }

.primary, .secondary, .danger { min-height: 44px; padding: 9px 14px; border-radius: 10px; border: 0; font-weight: 650; }
.primary { background: var(--accent); color: #fff; }
.primary.compact { min-height: 34px; padding: 5px 11px; border-radius: 8px; }
.secondary { background: color-mix(in srgb, var(--accent) 12%, var(--bg-elev)); color: var(--accent); }
.danger { background: color-mix(in srgb, var(--danger) 15%, var(--bg-elev)); color: var(--danger); }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.button-row > * { flex: 1 1 120px; }

.field { display: grid; gap: 6px; margin: 0 0 14px; }
.field label, label.field { color: var(--fg-2); font-size: 13px; }
.field input, .field textarea, .field select, input.field, textarea.field, select.field {
  width: 100%; min-height: 44px; padding: 10px 12px; border: .5px solid var(--sep); border-radius: 10px; background: var(--bg); color: var(--fg);
}
.field textarea { min-height: 96px; resize: vertical; }
.field-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.field-check { min-height: 44px; display: flex; align-items: center; gap: 10px; color: var(--fg); }
.field-check input { width: 22px; height: 22px; }
.hint { margin: 5px 0 0; color: var(--fg-2); font-size: 12px; line-height: 1.4; }
.counter { color: var(--fg-2); font-size: 12px; text-align: right; }
.argv { display: flex; flex-wrap: wrap; gap: 5px; min-height: 24px; }
.argv code { padding: 3px 6px; border-radius: 5px; background: var(--bg); font-size: 12px; }

.kv { display: grid; grid-template-columns: minmax(100px, .8fr) minmax(0, 1.5fr); gap: 8px 12px; padding: 12px 14px; border-bottom: .5px solid var(--sep); }
.kv:last-child { border-bottom: 0; }
.kv dt { color: var(--fg-2); font-size: 13px; }
.kv dd { margin: 0; font-size: 14px; overflow-wrap: anywhere; }

.empty, .error-banner { padding: 24px 18px; border-radius: 12px; background: var(--bg-elev); color: var(--fg-2); text-align: center; }
.error-banner { color: var(--danger); text-align: left; }
.error-banner strong { display: block; margin-bottom: 5px; }
.log { max-height: 60vh; overflow: auto; padding: 12px; border-radius: 10px; background: #111214; color: #e6e6e6; font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.log [data-stream="stderr"] { color: #ff7b72; }
.timeline { display: grid; gap: 8px; padding: 14px; }
.timeline-item { display: grid; grid-template-columns: auto 1fr auto; align-items: start; gap: 8px; font-size: 13px; }
.chat { display: grid; gap: 12px; }
.bubble { max-width: 92%; padding: 11px 13px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.user { justify-self: end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.bubble.assistant { justify-self: start; background: var(--bg-elev); border-bottom-left-radius: 4px; }
.system-line { color: var(--fg-2); font-size: 12px; text-align: center; }
.composer { position: sticky; bottom: calc(var(--bottom) + 4px); z-index: 5; margin-top: 16px; padding: 10px; display: grid; grid-template-columns: 1fr auto; gap: 8px; border-radius: 14px; background: color-mix(in srgb, var(--bg-elev) 94%, transparent); border: .5px solid var(--sep); backdrop-filter: blur(18px); }
.composer textarea { min-height: 44px; max-height: 150px; padding: 10px; border: 0; border-radius: 9px; background: var(--bg); resize: vertical; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; margin-bottom: 14px; padding: 2px; border-radius: 9px; background: color-mix(in srgb, var(--muted) 18%, transparent); }
.segmented button { min-height: 32px; border: 0; border-radius: 7px; background: transparent; font-size: 13px; }
.segmented button.active { background: var(--bg-elev); box-shadow: 0 1px 3px rgba(0,0,0,.18); }

.sheet-backdrop { position: fixed; z-index: 40; inset: 0; display: flex; align-items: flex-end; justify-content: center; padding-top: env(safe-area-inset-top); background: rgba(0,0,0,.35); }
.sheet { width: min(100%, 720px); max-height: 92vh; overflow: auto; padding: 12px 16px calc(18px + env(safe-area-inset-bottom)); border-radius: 18px 18px 0 0; background: var(--bg-elev); box-shadow: 0 -8px 40px rgba(0,0,0,.25); }
.sheet-handle { width: 36px; height: 5px; margin: 0 auto 12px; border-radius: 3px; background: var(--sep); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.sheet-head h2 { margin: 0; font-size: 20px; }
.sheet-close { width: 44px; border: 0; border-radius: 22px; background: var(--bg); color: var(--fg-2); }

#toast-host { position: fixed; z-index: 60; left: 50%; bottom: calc(var(--bottom) + 12px); width: min(calc(100% - 28px), 540px); transform: translateX(-50%); display: grid; gap: 8px; pointer-events: none; }
.toast { padding: 11px 14px; border-radius: 10px; background: #29292d; color: #fff; box-shadow: 0 8px 25px rgba(0,0,0,.3); font-size: 14px; animation: toast-in .18s ease-out; }
.toast.error { background: #7d1b17; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

.install-card { border: 1px solid color-mix(in srgb, var(--accent) 50%, transparent); }
.install-card h2 { margin: 0 0 8px; font-size: 19px; }
.install-card p { color: var(--fg-2); line-height: 1.45; }
.route-chip { text-transform: uppercase; letter-spacing: .04em; }
.overflow { position: relative; }
.overflow-menu { position: absolute; right: 0; top: 46px; z-index: 10; min-width: 160px; padding: 6px; border: .5px solid var(--sep); border-radius: 10px; background: var(--bg-elev); box-shadow: 0 8px 25px rgba(0,0,0,.2); }
.overflow-menu button { width: 100%; border: 0; border-radius: 7px; background: transparent; text-align: left; }

@media (min-width: 760px) {
  #topbar { grid-template-columns: 56px minmax(0, 608px) 56px; justify-content: center; }
  .sheet-backdrop { align-items: center; padding: 24px; }
  .sheet { border-radius: 18px; max-height: 86vh; }
}
