:root {
  --bg: #0b0b0d;
  --panel: #141417;
  --card: #1a1a1e;
  --card-2: #202026;
  --line: #2a2a31;
  --text: #f2f2f4;
  --muted: #9b9ba6;
  --accent: #d8a05a;
  --accent-soft: #3a2c1a;
  --green: #7fd18b;
  --red: #f07c7c;
  --radius: 18px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); min-height: 20px; }

/* Login */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #1b1712 0%, var(--bg) 60%);
}
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 28px 24px;
  text-align: center;
}
.login-card h1 { font-size: 22px; margin: 14px 0 6px; }
.login-card p { margin: 0 0 18px; font-size: 13px; }
.logo {
  width: 56px; height: 56px; border-radius: 16px;
  display: grid; place-items: center;
  margin: 0 auto;
  background: linear-gradient(145deg, #e8b46c, #a26b2e);
  color: #1a1208; font-weight: 800; font-size: 26px;
}
.login form { display: flex; flex-direction: column; gap: 10px; }
.login input {
  width: 100%; padding: 13px 14px;
  background: #0f0f12; border: 1px solid var(--line); border-radius: 12px;
  outline: none;
}
.login input:focus { border-color: var(--accent); }
.login button[type="submit"] {
  padding: 13px 14px; border-radius: 12px;
  background: var(--accent); color: #1a1208; font-weight: 700;
}

/* App shell */
.app { height: 100%; display: flex; }
.sidebar {
  width: 268px; flex: 0 0 268px;
  background: #101013;
  border-right: 1px solid var(--line);
  padding: 14px 12px;
  display: flex; flex-direction: column; gap: 12px;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px; }
.sidebar-head strong { font-size: 15px; letter-spacing: .2px; }
#close-sidebar { display: none; }
.new-session {
  text-align: left; padding: 12px 12px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); font-weight: 600;
}
.new-session:hover { border-color: #3a3a44; }
.section-label { color: var(--muted); font-size: 12px; padding: 8px 8px 2px; }
.recent {
  display: flex; align-items: center; gap: 9px;
  padding: 11px 12px; border-radius: 12px;
  color: #d6d6dc; font-size: 14px;
  width: 100%; border: 0; background: transparent;
  text-align: left; cursor: pointer; font-family: inherit;
}
.recent:hover { background: #1b1b20; }
.recent.active { background: #24242a; color: #fff; }
.recent .dot { width: 8px; height: 8px; border-radius: 50%; background: #4a4a55; }
.recent.active .dot { background: var(--accent); }
.sessions { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.recent-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.recent-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-time { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mode-banner {
  padding: 8px 10px; border-radius: 10px;
  background: rgba(216, 160, 90, .12);
  border: 1px solid rgba(216, 160, 90, .35);
  color: #e8c48f; font-size: 12px; line-height: 1.45;
}
.remember {
  display: flex; align-items: center; gap: 8px;
  color: var(--muted); font-size: 13px;
  margin-top: 12px; cursor: pointer; user-select: none;
}
.remember input { width: 16px; height: 16px; accent-color: var(--accent); }
.lang-select {
  background: var(--card-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 9px; padding: 6px 8px; font: inherit; font-size: 13px;
}
.sidebar-foot {
  margin-top: auto; display: flex; align-items: center; gap: 8px;
  padding: 12px 10px 4px; color: var(--muted); font-size: 12px;
  border-top: 1px solid var(--line);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: #4a4a55; display: inline-block; }
.dot.online { background: var(--green); }
.dot.busy { background: var(--accent); }
.dot.error { background: var(--red); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; }
.topbar {
  height: 60px; flex: 0 0 60px;
  display: flex; align-items: center; gap: 10px;
  padding: 0 10px; border-bottom: 1px solid var(--line);
  background: rgba(11,11,13,.86); backdrop-filter: blur(12px);
}
.topbar .title { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.topbar .title strong { font-size: 15px; }
.topbar .title span { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#open-sidebar { display: none; }
.icon {
  width: 42px; height: 42px; border-radius: 12px;
  display: grid; place-items: center; font-size: 18px; color: #d8d8de;
}
.icon:hover { background: #1e1e24; }
.ghost { color: var(--muted); }
.ghost:hover { color: var(--text); }

/* Chat */
.chat {
  flex: 1; overflow-y: auto; padding: 18px 14px 12px;
  display: flex; flex-direction: column; gap: 12px;
  overscroll-behavior: contain;
}
.msg { max-width: min(760px, 92%); display: flex; }
.msg.user { align-self: flex-end; justify-content: flex-end; flex-direction: column; align-items: flex-end; }
.msg.assistant { align-self: flex-start; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 7px; justify-content: flex-end; }
.attachment-image {
  max-width: min(300px, 72vw); max-height: 260px;
  border-radius: 12px; border: 1px solid var(--line); object-fit: cover;
}
.attachment-file {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 11px; border-radius: 11px;
  background: var(--card); border: 1px solid var(--line);
  color: #d8d8de; text-decoration: none; font-size: 12px;
}
.files { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 4px; }
.file-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 10px; border-radius: 10px; background: var(--card); border: 1px solid var(--line);
  font-size: 12px;
}
.file-row a { color: var(--accent); text-decoration: none; }
.file-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bubble {
  padding: 11px 14px; border-radius: 16px;
  white-space: pre-wrap; word-break: break-word;
  background: var(--card); border: 1px solid var(--line);
}
.msg.user .bubble { background: #2b2419; border-color: #3d3120; }
.msg.assistant .bubble { background: var(--card); }
.bubble.streaming::after { content: "▍"; color: var(--accent); margin-left: 2px; }

.card {
  align-self: stretch; max-width: min(760px, 96%);
  background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 12px;
}
.card.approval { border-color: #5a4526; background: #1d1811; }
.card.question { border-color: #2e4a5a; background: #121a1f; }
.card.error { border-color: #5a2626; background: #1d1212; }
.card.warning { border-color: #4a4226; background: #1a1812; }
.card-title { font-weight: 650; font-size: 13px; display: flex; align-items: center; gap: 8px; }
.card pre {
  margin: 9px 0 6px; padding: 10px; border-radius: 10px;
  background: #0d0d10; border: 1px solid #24242b;
  overflow-x: auto; white-space: pre-wrap; word-break: break-word;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.card .meta { color: var(--muted); font-size: 12px; margin: 4px 0; }
.card .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.card .actions button {
  padding: 9px 12px; border-radius: 10px;
  background: var(--accent); color: #1a1208; font-weight: 650; font-size: 13px;
}
.card .actions button.alt { background: #2a2a31; color: var(--text); }
.card .actions button.danger { background: #3a2020; color: #ffb4b4; }
.card .actions button:disabled { opacity: .55; cursor: default; }
.status-pill {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: #2a2a31; color: var(--muted); font-weight: 600;
}
.status-pill.running { background: var(--accent-soft); color: #e9bd82; }
.status-pill.ok { background: #1b2a1d; color: var(--green); }
.status-pill.fail { background: #2e1a1a; color: var(--red); }

.q { margin-top: 8px; }
.q:first-child { margin-top: 0; }
.q-header { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.q-text { margin: 3px 0 8px; font-weight: 600; font-size: 14px; }
.q-options { display: flex; flex-direction: column; gap: 6px; }
.q-option {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 10px; border-radius: 10px;
  background: #0f1519; border: 1px solid #263943; font-size: 13px;
}
.q-option input { accent-color: var(--accent); }
.q-other { width: 100%; padding: 10px 11px; border-radius: 10px; background: #0d0d10; border: 1px solid var(--line); margin-top: 6px; }

/* Composer */
.composer {
  flex: 0 0 auto; display: flex; flex-direction: column; gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: #0d0d10;
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.pending { display: flex; flex-wrap: wrap; gap: 6px; }
.pending-chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 9px; border-radius: 10px;
  background: var(--card); border: 1px solid var(--line);
  font-size: 12px; color: #d8d8de; max-width: 100%;
}
.pending-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.pending-chip button { color: var(--muted); font-size: 14px; line-height: 1; }
.composer textarea {
  flex: 1; resize: none; max-height: 140px;
  padding: 12px 13px; border-radius: 14px;
  background: var(--card); border: 1px solid var(--line); outline: none;
  line-height: 1.4;
}
.composer textarea:focus { border-color: #4a4a55; }
.send, .stop {
  width: 42px; height: 42px; border-radius: 14px;
  display: grid; place-items: center; font-size: 18px;
}
.send { background: var(--accent); color: #1a1208; font-weight: 800; }
.stop { background: #3a2020; color: #ffb4b4; font-size: 13px; }

/* Settings sheet */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: min(520px, 100%);
  max-height: 88vh; overflow-y: auto;
  background: #121215; border: 1px solid var(--line);
  border-radius: 24px 24px 0 0;
  padding: 16px 16px calc(18px + env(safe-area-inset-bottom));
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head strong { font-size: 18px; }
.tunnel-card {
  background: linear-gradient(150deg, #1d1a15, #141417 60%);
  border: 1px solid #3a2e1d; border-radius: 18px;
  padding: 14px;
}
.tunnel-state { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 650; }
.tunnel-url {
  margin-top: 8px; font: 13px/1.4 ui-monospace, Menlo, Consolas, monospace;
  word-break: break-all; color: #f0d6b0;
}
.qr-wrap { display: flex; gap: 14px; align-items: center; margin: 16px 0; }
#qr { width: 132px; height: 132px; background: #000; border-radius: 14px; border: 1px solid var(--line); }
.qr-copy { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.small-button {
  padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); font-size: 12px;
}
.setting-row {
  display: flex; justify-content: space-between; gap: 14px;
  padding: 11px 2px; border-bottom: 1px solid #1f1f25; font-size: 13px;
}
.setting-row span { color: var(--muted); }
.setting-row code { font-size: 12px; text-align: right; word-break: break-all; color: #d8d8de; }
.wide { width: 100%; margin-top: 16px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); }

@media (max-width: 820px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 15;
    transform: translateX(-100%); transition: transform .2s ease;
    width: min(300px, 84vw); flex-basis: auto;
  }
  .sidebar.open { transform: translateX(0); }
  #close-sidebar, #open-sidebar { display: grid; }
  .chat { padding: 14px 10px 8px; }
  .msg, .card { max-width: 100%; }
  .sheet { border-radius: 20px 20px 0 0; }
}

/* Live QR scanner */
.scanner {
  position: fixed; inset: 0; z-index: 60;
  background: #000;
  display: flex; align-items: center; justify-content: center;
}
.scanner video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  background: #000;
}
.scanner-frame {
  position: relative;
  width: min(72vw, 320px); aspect-ratio: 1;
  border-radius: 22px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .58);
}
.scanner-frame .corner {
  position: absolute; width: 30px; height: 30px;
  border: 3px solid var(--accent);
  border-radius: 10px;
}
.scanner-frame .tl { top: -2px; left: -2px; border-right: 0; border-bottom: 0; }
.scanner-frame .tr { top: -2px; right: -2px; border-left: 0; border-bottom: 0; }
.scanner-frame .bl { bottom: -2px; left: -2px; border-right: 0; border-top: 0; }
.scanner-frame .br { bottom: -2px; right: -2px; border-left: 0; border-top: 0; }
.scanner-beam {
  position: absolute; left: 6%; right: 6%; height: 2px; top: 50%;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: scanner-beam 2.2s ease-in-out infinite;
}
@keyframes scanner-beam {
  0%, 100% { transform: translateY(-42%); opacity: .45; }
  50% { transform: translateY(42%); opacity: 1; }
}
.scanner-top {
  position: absolute; top: calc(env(safe-area-inset-top, 0px) + 12px);
  left: 12px; right: 12px;
  display: flex; align-items: center; justify-content: space-between;
}
.scanner-status {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 26px);
  left: 16px; right: 16px;
  margin: 0; text-align: center;
  color: #f2f2f4; font-size: 14px;
  text-shadow: 0 1px 4px rgba(0, 0, 0, .8);
}
.scanner-status.error { color: var(--red); }
