/* 外围桥 PWA · 手写样式：CSS 变量 + 暗色优先 + safe-area + 单手优先布局 */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-muted: #eceef1;
  --text: #1c2128;
  --text-muted: #6a737d;
  --accent: #2f81f7;
  --accent-text: #ffffff;
  --danger: #cf222e;
  --warn-bg: #fff8c5;
  --warn-text: #4d2d00;
  --ok: #1a7f37;
  --busy: #9a6700;
  --border: #d8dee4;
  --radius: 12px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #1c2128;
    --surface-muted: #262c36;
    --text: #e6edf3;
    --text-muted: #8b949e;
    --accent: #388bfd;
    --accent-text: #ffffff;
    --danger: #f85149;
    --warn-bg: #3a2d00;
    --warn-text: #e3b341;
    --ok: #3fb950;
    --busy: #d29922;
    --border: #323942;
  }
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* ===== 顶栏 ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex: none;
}

.topbar .title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
  background: var(--text-muted);
}

.status-dot.ok { background: var(--ok); }
.status-dot.down { background: var(--danger); }

/* ===== 主内容区 ===== */
.main {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px calc(env(safe-area-inset-bottom) + 76px);
}

.main-full {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* ===== 卡片 ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 10px;
}

.card.tap {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.card-title {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}

.badge {
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--text-muted);
  flex: none;
}

.badge.busy {
  background: var(--busy);
  color: #fff;
}

/* ===== 会话详情 ===== */
.message-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 0;
}

.message-row {
  margin: 6px 12px;
  max-width: 92%;
}

.message-row .role {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.message-row.user {
  align-self: flex-end;
  margin-left: auto;
}

.message-bubble {
  background: var(--surface-muted);
  border-radius: var(--radius);
  padding: 8px 12px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15px;
}

.message-row.user .message-bubble {
  background: var(--accent);
  color: var(--accent-text);
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex: none;
}

.composer input[type="text"] {
  flex: 1;
  min-width: 0;
  font-size: 16px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.composer button,
.inline-btn {
  font-size: 15px;
  padding: 10px 16px;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  flex: none;
}

.composer button:disabled,
.inline-btn:disabled,
.card.disabled {
  opacity: 0.5;
  pointer-events: none;
}

.target-switch {
  display: flex;
  gap: 8px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text-muted);
  background: var(--surface);
}

.target-switch .option {
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.target-switch .option.active {
  border-color: var(--accent);
  color: var(--accent);
}

.warn-note {
  font-size: 13px;
  color: var(--warn-text);
  background: var(--warn-bg);
  border-radius: 8px;
  padding: 6px 10px;
  margin: 0 12px 6px;
}

/* ===== 权限卡 ===== */
.permission-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.permission-actions .inline-btn {
  flex: 1;
  padding: 10px 4px;
  font-size: 14px;
}

.inline-btn.danger {
  background: var(--danger);
}

.patterns {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: var(--text-muted);
  word-break: break-all;
}

/* ===== 引导页 / 设置 ===== */
.gate {
  max-width: 420px;
  margin: 20% auto 0;
  padding: 0 20px;
}

.gate h1 {
  font-size: 22px;
  margin: 0 0 8px;
}

.gate p {
  color: var(--text-muted);
  font-size: 14px;
}

.gate input,
.settings-row input {
  width: 100%;
  font-size: 16px;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  margin: 12px 0;
}

.settings-section {
  margin-bottom: 20px;
}

.settings-section h2 {
  font-size: 15px;
  color: var(--text-muted);
  margin: 16px 4px 8px;
}

.settings-hint {
  font-size: 13px;
  color: var(--text-muted);
  margin: 8px 0;
}

.diagnostics {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-all;
}

/* ===== 底部 tab ===== */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}

.tabbar .tab {
  flex: 1;
  text-align: center;
  padding: 10px 0 8px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: relative;
}

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

.tab .badge-count {
  position: absolute;
  top: 4px;
  left: calc(50% + 10px);
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  min-width: 18px;
  height: 18px;
  line-height: 18px;
  border-radius: 999px;
}

/* ===== toast ===== */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 20;
  max-width: 86%;
  text-align: center;
}

.empty-note {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  padding: 40px 20px;
}
