:root {
  --bg: #0b0e14;
  --bg2: #11151f;
  --card: #161b26;
  --line: rgba(255, 255, 255, 0.08);
  --text: #e8ecf4;
  --dim: #8d97ab;
  --dim2: #5d6678;
  --accent: #5b8cff;
  --accent2: #21d4a8;
  --warn: #ffb340;
  --danger: #ff6b6b;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  overscroll-behavior: none;
}

body {
  background:
    radial-gradient(120% 60% at 50% -10%, rgba(91, 140, 255, 0.18), transparent 60%),
    radial-gradient(90% 50% at 100% 100%, rgba(33, 212, 168, 0.07), transparent 60%),
    var(--bg);
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 560px;
  margin: 0 auto;
  position: relative;
}

/* ---------- 顶栏 ---------- */
.topbar {
  height: 54px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  padding-top: env(safe-area-inset-top, 0px);
  box-sizing: content-box;
}

.brand { display: flex; align-items: center; gap: 9px; font-size: 15px; font-weight: 600; letter-spacing: 0.3px; }
.logo {
  width: 18px; height: 18px; border-radius: 6px;
  background: linear-gradient(140deg, var(--accent), var(--accent2));
  box-shadow: 0 0 14px rgba(91, 140, 255, 0.5);
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dim2); display: inline-block; transition: 0.3s; }
.dot[data-state="on"] { background: var(--accent2); box-shadow: 0 0 8px var(--accent2); }
.dot[data-state="wait"] { background: var(--warn); animation: blink 1.2s infinite; }
.dot[data-state="err"] { background: var(--danger); }
@keyframes blink { 50% { opacity: 0.25; } }

.lat {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--dim);
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}
.lat.good { color: var(--accent2); border-color: rgba(33, 212, 168, 0.35); }
.lat.bad { color: var(--warn); border-color: rgba(255, 179, 64, 0.35); }

/* ---------- 视图 ---------- */
main { flex: 1 1 auto; position: relative; overflow: hidden; }
.view {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  min-height: 0;
}
.view.active { display: flex; }
#view-chat { padding: 0 16px; }

.page-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 8px;
}
.page-head h2 { margin: 0; font-size: 20px; }
.page-body { flex: 1; overflow-y: auto; padding: 4px 20px 28px; -webkit-overflow-scrolling: touch; }
.page-body h3 { font-size: 13px; color: var(--dim); font-weight: 500; margin: 22px 0 8px; letter-spacing: 0.5px; }
.pad { padding: 0 20px 14px; }
.tiny { font-size: 12px; color: var(--dim2); line-height: 1.7; margin: 6px 0; }
.link { background: none; border: 0; color: var(--accent); font-size: 13px; padding: 6px; }
.danger { color: var(--danger) !important; }

/* ---------- 舞台 / 球 ---------- */
.stage {
  flex: 0 0 auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 6px 0 10px;
  min-height: 232px;
}

.orb {
  position: relative;
  width: 132px; height: 132px;
  border-radius: 50%;
  border: 0; padding: 0;
  background: transparent;
  display: grid; place-items: center;
  cursor: pointer;
}
.orb .core {
  width: 96px; height: 96px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 22px; color: #fff;
  background: radial-gradient(circle at 35% 30%, #7aa2ff, #3b62d9 60%, #24327a);
  box-shadow: 0 10px 34px rgba(59, 98, 217, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.3);
  transition: transform 0.25s, box-shadow 0.25s, background 0.4s;
}
.orb .pulse {
  position: absolute; inset: 10px; border-radius: 50%;
  border: 1px solid rgba(122, 162, 255, 0.35);
  opacity: 0; transform: scale(0.85);
}
.orb .p2 { inset: 0; border-color: rgba(122, 162, 255, 0.18); }

.orb[data-state="idle"] .core { background: radial-gradient(circle at 35% 30%, #4c5568, #2a3040 70%); box-shadow: 0 8px 26px rgba(0,0,0,0.5); }
.orb[data-state="connecting"] .core { animation: breathe 1.6s ease-in-out infinite; }
.orb[data-state="listening"] .core {
  background: radial-gradient(circle at 35% 30%, #6ff0cd, #17a583 62%, #0d6b57);
  box-shadow: 0 0 0 6px rgba(33, 212, 168, 0.1), 0 12px 40px rgba(33, 212, 168, 0.35);
}
.orb[data-state="listening"] .pulse { animation: ripple 2.4s ease-out infinite; }
.orb[data-state="listening"] .p2 { animation-delay: 1.2s; }
.orb[data-state="listening"] .core { transform: scale(var(--lvl, 1)); }
.orb[data-state="thinking"] .core {
  background: radial-gradient(circle at 35% 30%, #ffd479, #e39a23 62%, #8a5c0f);
  box-shadow: 0 12px 40px rgba(227, 154, 35, 0.4);
  animation: breathe 1.1s ease-in-out infinite;
}
.orb[data-state="speaking"] .core {
  background: radial-gradient(circle at 35% 30%, #a6bcff, #5b8cff 60%, #2d4aa8);
  box-shadow: 0 0 0 6px rgba(91, 140, 255, 0.1), 0 14px 44px rgba(91, 140, 255, 0.45);
}
.orb[data-state="speaking"] .pulse { animation: ripple 1.6s ease-out infinite; border-color: rgba(122, 162, 255, 0.5); }
.orb[data-state="error"] .core { background: radial-gradient(circle at 35% 30%, #ff9d9d, #d93b3b 65%); }

@keyframes ripple {
  0% { transform: scale(0.9); opacity: 0.9; }
  100% { transform: scale(1.55); opacity: 0; }
}
@keyframes breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

.state-text { margin-top: 16px; font-size: 14.5px; font-weight: 500; letter-spacing: 0.4px; }
.hint { margin-top: 5px; font-size: 12px; color: var(--dim2); min-height: 16px; }

/* ---------- 对话流 ---------- */
.turns {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 6px 2px 18px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.turns::-webkit-scrollbar { display: none; }

.turn { margin-bottom: 18px; animation: rise 0.28s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

.bubble {
  margin-left: auto;
  width: fit-content;
  max-width: 82%;
  background: rgba(91, 140, 255, 0.14);
  border: 1px solid rgba(91, 140, 255, 0.2);
  color: #cfe0ff;
  padding: 9px 13px;
  border-radius: 16px 16px 4px 16px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
}
.bubble.placeholder { color: var(--dim2); }
.answer { margin-top: 9px; }
.answer p {
  margin: 0 0 6px;
  font-size: 15.5px;
  line-height: 1.72;
  word-break: break-word;
}
.answer p:last-child { margin-bottom: 0; }
.answer p.typing::after {
  content: "";
  display: inline-block;
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  margin-left: 5px;
  animation: blink 1s infinite;
  vertical-align: middle;
}
.turn .meta { margin-top: 7px; font-size: 11.5px; color: var(--dim2); font-variant-numeric: tabular-nums; }
.turn.aborted .answer p { opacity: 0.45; }

.empty-tip { text-align: center; color: var(--dim2); font-size: 12.5px; padding: 18px 0 6px; line-height: 1.9; }

/* ---------- 设置 ---------- */
.field { display: block; margin-bottom: 20px; }
.field > span { display: block; font-size: 13.5px; margin-bottom: 8px; color: #cdd5e4; }
.field > span b { color: var(--accent); font-weight: 500; margin-left: 6px; font-variant-numeric: tabular-nums; }
.field > em { display: block; font-size: 11.5px; color: var(--dim2); font-style: normal; margin-top: 6px; line-height: 1.6; }
.field em.inline { display: block; margin: 2px 0 0; }
.field.row { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.field.row > span { margin: 0; }
.field.disabled { opacity: 0.45; }

input[type="url"], input[type="text"], input[type="number"], select, #pairCode, #startServer {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 11px;
  padding: 11px 12px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
}
input:focus, select:focus { border-color: rgba(91, 140, 255, 0.55); }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--dim) 50%), linear-gradient(135deg, var(--dim) 50%, transparent 50%); background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }

input[type="range"] {
  width: 100%; -webkit-appearance: none; appearance: none;
  height: 4px; border-radius: 4px; background: rgba(255, 255, 255, 0.12); outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%;
  background: var(--accent); border: 3px solid #0f1420; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

.switch { width: 46px; height: 27px; appearance: none; -webkit-appearance: none; background: rgba(255,255,255,0.12); border-radius: 999px; position: relative; transition: 0.2s; flex: 0 0 auto; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 21px; height: 21px; border-radius: 50%; background: #fff; transition: 0.2s; }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(19px); }

.kv { font-size: 12.5px; color: var(--dim); background: var(--card); border: 1px solid var(--line); border-radius: 11px; padding: 11px 12px; line-height: 1.7; word-break: break-all; }
.actions { display: flex; gap: 10px; margin-top: 10px; }

.btn {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  border-radius: 11px;
  padding: 10px 14px;
  font-size: 13.5px;
  font-family: inherit;
  cursor: pointer;
}
.btn:active { transform: scale(0.98); }
.btn.ghost { background: transparent; color: var(--dim); flex: 1; }
.btn.primary {
  background: linear-gradient(140deg, #6b96ff, #3f63d8);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(63, 99, 216, 0.35);
}
.btn.big { width: 100%; padding: 14px; font-size: 15.5px; border-radius: 13px; }

/* ---------- 历史 ---------- */
.h-item { padding: 12px 0; border-bottom: 1px solid var(--line); }
.h-item .q { font-size: 13px; color: var(--dim); margin-bottom: 4px; }
.h-item .a { font-size: 14px; line-height: 1.6; }
.h-item .t { font-size: 11px; color: var(--dim2); margin-top: 5px; font-variant-numeric: tabular-nums; }

/* ---------- 关于 ---------- */
.about-line { display: flex; align-items: center; gap: 8px; margin: 14px 0 6px; font-size: 15px; }
.tag { font-size: 11px; color: var(--accent2); border: 1px solid rgba(33, 212, 168, 0.35); padding: 2px 7px; border-radius: 999px; }
.proto { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.proto th { text-align: left; font-weight: 400; color: var(--dim2); padding: 7px 10px 7px 0; vertical-align: top; width: 78px; }
.proto td { color: #c6cede; padding: 7px 0; line-height: 1.6; }
.proto tr + tr { border-top: 1px solid var(--line); }
.proto tr.sum th, .proto tr.sum td { color: var(--warn); }

/* ---------- 底部导航 ---------- */
.tabs {
  flex: 0 0 auto;
  display: flex;
  border-top: 1px solid var(--line);
  background: rgba(11, 14, 20, 0.86);
  backdrop-filter: blur(16px);
  padding-bottom: var(--safe-b);
}
.tab {
  flex: 1;
  background: none; border: 0;
  color: var(--dim2);
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 9px 0 8px;
  font-size: 10.5px;
  font-family: inherit;
  cursor: pointer;
}
.tab svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.tab.active { color: var(--accent); }

/* ---------- 启动浮层 ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  padding: 24px;
  background: radial-gradient(80% 60% at 50% 0%, rgba(91, 140, 255, 0.2), transparent 65%), rgba(7, 9, 14, 0.92);
  backdrop-filter: blur(10px);
  transition: opacity 0.3s;
}
.overlay.hide { opacity: 0; pointer-events: none; }

.start-card {
  width: 100%; max-width: 340px;
  background: linear-gradient(180deg, rgba(28, 34, 50, 0.92), rgba(17, 21, 31, 0.95));
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 22px;
  padding: 30px 24px 22px;
  text-align: center;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55);
}
.start-logo {
  width: 46px; height: 46px; margin: 0 auto 14px; border-radius: 14px;
  background: linear-gradient(140deg, var(--accent), var(--accent2));
  box-shadow: 0 8px 26px rgba(91, 140, 255, 0.45);
}
.start-card h1 { font-size: 21px; margin: 0 0 6px; letter-spacing: 1px; }
.start-card .sub { color: var(--dim); font-size: 13px; margin: 0 0 22px; letter-spacing: 2px; }
.pair-row { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.pair-row.hide { display: none; }
#pairCode { text-align: center; letter-spacing: 6px; font-size: 17px; }
#startServer { margin-bottom: 16px; font-size: 12.5px; }
.start-card .tiny { margin-top: 14px; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: calc(84px + var(--safe-b));
  transform: translate(-50%, 16px);
  background: rgba(30, 36, 50, 0.96);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: 0.28s;
  max-width: 84vw;
  text-align: center;
  z-index: 80;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
