:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --text: #14151a;
  --muted: #6b7080;
  --border: #e3e5ec;
  --brand: #4f46e5;
  --brand-contrast: #ffffff;
  --ok: #16a34a;
  --bad: #dc2626;
  --bar-h: 56px;
  --tab-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1015;
    --surface: #181a22;
    --text: #f1f2f6;
    --muted: #9298ab;
    --border: #272a36;
    --brand: #818cf8;
    --brand-contrast: #0f1015;
  }
}

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

html, body { margin: 0; min-height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

h1, h2, h3, p { margin: 0; }
a { color: var(--brand); text-decoration: none; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.grow { flex: 1; min-width: 0; }

/* App chrome */
.app-bar {
  position: fixed;
  inset: 0 0 auto 0;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 16px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.app-bar h1 { font-size: 1.15rem; }
.app-bar { gap: 12px; }
.ws-select {
  flex: 1;
  min-width: 0;
  max-width: 240px;
  min-height: 40px;
  padding: 0 34px 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%239298ab' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E")
    no-repeat right 12px center;
  color: var(--text);
  font: inherit;
  font-weight: 700;
  appearance: none;
  -webkit-appearance: none;
  text-overflow: ellipsis;
}
.app-bar .pill { margin-left: auto; flex: none; }

.pill {
  font-size: 0.75rem;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--border);
  color: var(--muted);
}
.pill.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.pill.bad { background: color-mix(in srgb, var(--bad) 18%, transparent); color: var(--bad); }

.view {
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 16px) 16px
    calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px);
  max-width: 640px;
  margin: 0 auto;
}
.view > section > * + * { margin-top: 12px; }

.tab-bar {
  position: fixed;
  inset: auto 0 0 0;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 10;
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
  font-size: 0.72rem;
  color: var(--muted);
}
.tab span { font-size: 1.25rem; line-height: 1; }
.tab.active { color: var(--brand); font-weight: 600; }

/* Typography helpers */
.page-title { font-size: 1.4rem; }
.section-title { font-size: 1rem; margin-top: 20px !important; }
.back { display: inline-block; min-height: 32px; }
.empty { text-align: center; color: var(--muted); padding: 24px 0; }
.error { color: var(--bad); font-size: 0.9rem; }

/* Cards & lists */
.card {
  display: block;
  width: 100%;
  background: var(--surface);
  color: inherit;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  text-align: left;
  font: inherit;
}
.card > * + * { margin-top: 8px; }
.card h3 { font-size: 1.05rem; }
.link-card { cursor: pointer; }
.link-card:active { background: color-mix(in srgb, var(--brand) 8%, var(--surface)); }
.conn { appearance: none; }
.conn.disabled { opacity: 0.6; }
.view .card + .card, .view .card + button.card { margin-top: 10px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.chev { color: var(--muted); font-size: 1.4rem; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge.api, .badge.order { background: color-mix(in srgb, var(--brand) 16%, transparent); color: var(--brand); }
.badge.mcp, .badge.sample { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.items { margin: 0; padding-left: 18px; }

.copy-row { display: flex; align-items: center; gap: 10px; }
.copy-row code {
  display: block;
  font-size: 0.78rem;
  word-break: break-all;
  user-select: all;
}

/* Controls */
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-contrast); }
.btn.danger { color: var(--bad); }
.btn.block { display: flex; width: 100%; }
.btn:disabled { opacity: 0.5; }
.icon-btn { appearance: none; border: 0; background: none; color: var(--muted); font-size: 1.1rem; min-width: 44px; min-height: 44px; cursor: pointer; }

input:not([type="checkbox"]), select {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit; /* 16px avoids iOS zoom-on-focus */
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
label { display: block; font-size: 0.85rem; color: var(--muted); }
label > input, label > select { margin-top: 4px; color: var(--text); }
.stack > * + * { margin-top: 12px; }
.divider { border-top: 1px solid var(--border); padding-top: 16px; margin-top: 16px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; color: var(--text); font-size: 1rem; }
.switch-row input { width: 22px; height: 22px; }
.actions { display: flex; gap: 10px; margin-top: 12px; }
.actions .btn { flex: 1; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chip {
  flex: none;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
}
.chip.active { background: var(--brand); border-color: var(--brand); color: var(--brand-contrast); }

.segmented { display: flex; background: var(--border); border-radius: 10px; padding: 3px; margin-bottom: 12px; }
.segmented button {
  flex: 1;
  min-height: 38px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--text); font-weight: 600; }

/* Login */
.login-wrap { min-height: 60vh; display: grid; align-items: center; }
.login > * + * { margin-top: 12px; }

/* Sheet & toast */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head h2 { font-size: 1.1rem; }
.sheet-body > * + * { margin-top: 12px; }
.token-box {
  display: block;
  padding: 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px dashed var(--border);
  word-break: break-all;
  font-size: 0.85rem;
  user-select: all;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.9rem;
  z-index: 60;
}
.toast.bad { background: var(--bad); color: #fff; }

/* Record cards */
.row .section-title { margin-top: 0 !important; }
.meta { display: flex; justify-content: space-between; gap: 12px; font-size: 0.78rem; color: var(--muted); }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.view > section > .chips { margin-top: 8px; }
.view > section > article.card { margin-top: 10px; }
button.card { cursor: pointer; }
