/* SimBridge 控制台 — mobile-first (iPhone Safari, also fine on desktop).
   System fonts only, no URL functions, no imports, no external resources (protocol/CONSOLE.md §0).
   Light/dark through custom properties; iOS safe areas through env(). */

:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --card: #ffffff;
  --field: #ffffff;
  --text: #1c1c1e;
  --muted: #6c6c70;
  --line: #d8d8dc;
  --accent: #0064d2;
  --on-accent: #ffffff;
  --danger: #c4001a;
  --ok: #1f7a35;
  --switch-on: #34c759;
  --warn-bg: #fff3cd;
  --warn-text: #5c4400;
  --in: #e9e9eb;
  --out: #0064d2;
  --on-out: #ffffff;
  --press: #e5e5ea;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06);
  --tabbar-h: 56px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --field: #2c2c2e;
    --text: #f2f2f7;
    --muted: #a1a1a6;
    --line: #3a3a3c;
    --accent: #0a84ff;
    --danger: #ff453a;
    --ok: #32d74b;
    --switch-on: #30d158;
    --warn-bg: #3d3000;
    --warn-text: #ffd60a;
    --in: #2c2c2e;
    --out: #0a6ee0;
    --press: #2c2c2e;
    --shadow: none;
  }
}

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

html {
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Helvetica Neue",
    "Microsoft YaHei", "Noto Sans CJK SC", "Segoe UI", sans-serif;
  overflow-wrap: anywhere;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, p { margin: 0; }
small { font-size: 13px; }
.block { display: block; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); font-size: 14px; }
.err { color: var(--danger); font-size: 15px; }
.ok { color: var(--ok); font-size: 15px; }
.warn {
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
}
.empty { padding: 32px 0; color: var(--muted); text-align: center; }
.loading { padding: 24px 0; color: var(--muted); text-align: center; }
.boot { padding: 40vh 16px 0; color: var(--muted); text-align: center; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
}
.btn.secondary { border-color: var(--line); background: transparent; color: var(--accent); }
.btn.ghost { padding: 0 8px; background: transparent; color: var(--accent); }
.btn.danger { background: var(--danger); color: #ffffff; }
.btn.small { min-height: 40px; padding: 0 12px; font-size: 15px; font-weight: 500; }
.btn.block { display: flex; width: 100%; }
.btn:disabled { opacity: .45; cursor: default; }
.btn:active:not(:disabled) { opacity: .7; }

input:not([type="checkbox"]), textarea, select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--field);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* >= 16px: iOS does not zoom on focus */
  -webkit-appearance: none;
  appearance: none;
}
textarea { display: block; resize: vertical; line-height: 1.4; }
select {
  padding-right: 34px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 19px) 52%, calc(100% - 13px) 52%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}
input:disabled, textarea:disabled, select:disabled { opacity: .55; }
::placeholder { color: var(--muted); opacity: .8; }

.field { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 14px; }
.field .ctl { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 16px; }

.switch-row { display: flex; align-items: center; gap: 12px; min-height: 48px; cursor: pointer; }
.switch { position: relative; flex: none; width: 51px; height: 31px; }
.switch input { position: absolute; inset: 0; z-index: 1; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.slider { position: absolute; inset: 0; border-radius: 31px; background: var(--line); transition: background .2s; }
.slider::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .2s;
}
.switch input:checked + .slider { background: var(--switch-on); }
.switch input:checked + .slider::after { transform: translateX(20px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled + .slider { opacity: .5; }

fieldset.plain { min-width: 0; margin: 0; padding: 0; border: 0; }

/* ---------- login ---------- */
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(24px, var(--safe-t)) max(16px, var(--safe-r)) max(24px, var(--safe-b)) max(16px, var(--safe-l));
}
.login-card { width: 100%; max-width: 380px; }
.login-card h1 { font-size: 22px; text-align: center; }
.login-icon { display: block; width: 64px; height: 64px; margin: 0 auto; border-radius: 14px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 14px; }
#login-code { letter-spacing: .3em; font-variant-numeric: tabular-nums; }
#login-code::placeholder { letter-spacing: normal; }
.msg { min-height: 1.4em; color: var(--danger); font-size: 14px; text-align: center; }
.msg.info { color: var(--muted); }

/* ---------- shell ---------- */
.top {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(var(--safe-t) + 8px) max(16px, var(--safe-r)) 8px max(16px, var(--safe-l));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.top h1 { flex: 1; min-width: 0; font-size: 20px; font-weight: 700; }
.agent {
  flex: none;
  min-height: 32px;
  padding: 4px 12px;
  border: 0;
  border-radius: 999px;
  background: var(--card);
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.agent::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: currentColor;
  vertical-align: 1px;
}
.agent.on { color: var(--ok); }
.agent.off { color: var(--danger); }

.banner {
  margin: 8px max(16px, var(--safe-r)) 0 max(16px, var(--safe-l));
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
}

.views {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px max(16px, var(--safe-r)) calc(var(--tabbar-h) + var(--safe-b) + 24px) max(16px, var(--safe-l));
}

.tabbar {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  padding: 0 var(--safe-r) var(--safe-b) var(--safe-l);
  border-top: 1px solid var(--line);
  background: var(--card);
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  min-height: var(--tabbar-h);
  padding: 6px 0;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  cursor: pointer;
  touch-action: manipulation;
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--accent); }

/* hide the tab bar while the on-screen keyboard is up (touch devices only) */
@media (hover: none) and (pointer: coarse) {
  body.typing .tabbar { display: none; }
  body.typing .views { padding-bottom: calc(var(--safe-b) + 24px); }
  body.typing .toast { bottom: calc(var(--safe-b) + 12px); }
}

/* ---------- building blocks ---------- */
.toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 0 0 12px;
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.card > h2 { font-size: 17px; }
.kv { display: flex; gap: 12px; font-size: 15px; }
.kv .k { flex: none; width: 5.5em; color: var(--muted); }
.kv .v { flex: 1; min-width: 0; white-space: pre-wrap; }

.list { margin-bottom: 12px; overflow: hidden; border-radius: 12px; background: var(--card); box-shadow: var(--shadow); }
.card .list { margin: 0; box-shadow: none; border: 1px solid var(--line); }
.item {
  display: block;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: var(--card);
  color: inherit;
  font: inherit;
  text-align: left;
}
.item:last-child { border-bottom: 0; }
button.item { min-height: 64px; cursor: pointer; }
button.item:active { background: var(--press); }
.item-top { display: flex; align-items: baseline; gap: 8px; }
.item-title { flex: 1; min-width: 0; overflow: hidden; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.item-time { flex: none; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.item-sub { color: var(--muted); font-size: 14px; }
.item-snip {
  display: -webkit-box;
  overflow: hidden;
  color: var(--muted);
  font-size: 14px;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}
.item-body { margin-top: 4px; font-size: 15px; white-space: pre-wrap; }
.badge { flex: none; color: var(--muted); font-size: 12px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.num-row { padding-top: 8px; }
.num-row + .num-row { margin-top: 8px; border-top: 1px dashed var(--line); }
.num { font-variant-numeric: tabular-nums; }

.call-ic { flex: none; width: 1.2em; color: var(--muted); text-align: center; }
.call-missed .call-ic, .call-missed .item-title, .call-rejected .call-ic { color: var(--danger); }
.call-blocked .call-ic { color: var(--warn-text); }

pre, .pre {
  max-height: 60vh;
  margin: 0;
  padding: 10px 12px;
  overflow: auto;
  border-radius: 10px;
  background: var(--bg);
  font: 14px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.sms-full { font: inherit; font-size: 16px; }

/* ---------- SMS ---------- */
.thread-head { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.thread-head h2 { font-size: 20px; }
.msgs { display: flex; flex-direction: column; gap: 6px; padding: 8px 0; }
.bubble { max-width: 82%; padding: 8px 12px; border-radius: 18px; }
.bubble .body { white-space: pre-wrap; }
.bubble .meta { margin-top: 2px; font-size: 12px; opacity: .75; }
.bubble.in { align-self: flex-start; border-bottom-left-radius: 6px; background: var(--in); }
.bubble.out { align-self: flex-end; border-bottom-right-radius: 6px; background: var(--out); color: var(--on-out); }
.bubble.failed { border: 2px solid var(--danger); background: transparent; color: var(--text); }
.bubble.failed .meta { color: var(--danger); font-weight: 600; opacity: 1; }
.bubble.pending { opacity: .7; }
.composer { margin-top: 12px; }
.counter { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }

/* ---------- filter ---------- */
.cat { display: flex; flex-direction: column; gap: 4px; padding-top: 10px; border-top: 1px solid var(--line); }
.cat-head { display: flex; align-items: center; gap: 8px; }
.cat-name { flex: 1; min-width: 0; font-weight: 600; }
.cat-head select { flex: none; width: auto; min-width: 9.5em; }
.list-ed { display: flex; flex-direction: column; gap: 6px; padding-top: 10px; border-top: 1px solid var(--line); }
.list-ed label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.result { display: flex; flex-direction: column; gap: 6px; }
.logout { margin: 24px 0 8px; }

/* ---------- dialog & toast ---------- */
dialog {
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  max-height: calc(100dvh - 48px);
  padding: 16px;
  overflow: auto;
  border: 0;
  border-radius: 14px;
  background: var(--card);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
.dlg-body { display: flex; flex-direction: column; gap: 8px; }
.dlg-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 16px; }

.toast {
  position: fixed;
  right: 16px;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  left: 16px;
  z-index: 30;
  width: -webkit-fit-content;
  width: fit-content;
  max-width: 480px;
  margin: 0 auto;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(28, 28, 30, .94);
  color: #ffffff;
  font-size: 15px;
  text-align: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .2);
}
.toast.bad { background: var(--danger); }
.offscreen { position: fixed; top: 0; left: -9999px; opacity: 0; }
