:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --text: #e6e8ec;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --accent-2: #2dd4bf;
  --user: #2b6cff;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --border: #2a2f3a;
}

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

html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 6px) 10px 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 6px; }
.logo { font-size: 18px; }
.title { font-weight: 600; letter-spacing: .5px; font-size: 15px; }
.status-cluster { display: flex; align-items: center; gap: 8px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.online { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.offline { background: var(--err); }
.icon-btn {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 9px; width: 30px; height: 30px; font-size: 14px; cursor: pointer;
}
.icon-btn:active { transform: scale(.94); }

.status-bar {
  display: flex; gap: 10px; flex-wrap: wrap;
  padding: 4px 10px; font-size: 11px; color: var(--muted);
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.status-bar span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Task bar ---------- */
.task-bar {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 10px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.task-select-m {
  flex: 1; font-size: 12.5px; font-weight: 600;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 5px 8px; min-width: 0;
}

/* ---------- Events ---------- */
.events {
  flex: 1; overflow-y: auto; padding: 8px 8px 70px;
  display: flex; flex-direction: column; gap: 6px;
}

/* Wall-clock tag on every non-user block (compact, mono, right-aligned row) */
.hist-time-m {
  display: block;
  font-size: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--muted);
  opacity: .85;
  letter-spacing: .3px;
  margin-bottom: 2px;
}

.bubble {
  max-width: 92%; padding: 7px 10px; border-radius: 12px; line-height: 1.45;
  white-space: pre-wrap; word-break: break-word;
  font-size: 14px;
}
.bubble.user { align-self: flex-end; background: var(--user); color: #fff; border-bottom-right-radius: 4px; }
.bubble.assistant { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.task-started { align-self: flex-start; background: transparent; border: none; padding: 3px 2px; font-size: 11px; color: var(--muted, #8a8f98); }
.bubble.task-started .hist-time-m { margin-bottom: 0; }

.reasoning {
  align-self: flex-start; max-width: 94%;
  background: rgba(79,140,255,.08); border: 1px dashed rgba(79,140,255,.35);
  border-radius: 10px; padding: 6px 10px; font-size: 12px; color: var(--muted);
}
.reasoning-head { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 3px; color: var(--accent); }
.reasoning-body { white-space: pre-wrap; word-break: break-word; }

.card {
  align-self: stretch; background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; font-size: 12.5px;
}
.card-title { font-weight: 600; margin-bottom: 2px; color: var(--accent-2); font-size: 12.5px; }
.card-body { white-space: pre-wrap; word-break: break-word; color: var(--text); max-height: 160px; overflow: auto; font-size: 12.5px; }
.card.tool .card-title { color: var(--warn); }
.card.result .card-title { color: var(--ok); }
.card.result.fail .card-title { color: var(--err); }
.card.error { border-color: var(--err); }
.card.error .card-title { color: var(--err); }
.card.rag .card-title { color: #c084fc; }
.card.plan .card-title { color: var(--accent); }

/* ---------- Approval ---------- */
.approval {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 16;
  margin: 0 12px 8px; padding: 12px; border-radius: 12px;
  background: rgba(251,191,36,.12); border: 1px solid var(--warn);
}
.approval-title { font-weight: 700; color: var(--warn); margin-bottom: 6px; }
.approval-body { white-space: pre-wrap; word-break: break-word; font-size: 13px; max-height: 140px; overflow: auto; }
.approval-actions { display: flex; gap: 10px; margin-top: 10px; }
.btn {
  flex: 1; padding: 10px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 14px; cursor: pointer;
}
.btn:active { transform: scale(.97); }
.btn-yes { background: var(--ok); color: #04231a; border: none; font-weight: 600; }
.btn-no { background: var(--err); color: #2a0606; border: none; font-weight: 600; }

/* ---------- Composer ---------- */
.composer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  padding: 6px 10px calc(env(safe-area-inset-bottom) + 6px);
  background: var(--panel); border-top: 1px solid var(--border);
}
.composer-row {
  display: flex; align-items: flex-end; gap: 6px;
}
.mic-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 16px; cursor: pointer; flex: none;
}
.mic-btn:active { transform: scale(.94); }
.mic-btn.recording { background: var(--err); border-color: var(--err); animation: mic-pulse 1s infinite; }
@keyframes mic-pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(248,113,113,.5); } 50% { box-shadow: 0 0 0 8px rgba(248,113,113,0); } }
.input {
  flex: 1; resize: none; max-height: 110px; min-height: 38px;
  padding: 9px 12px; border-radius: 18px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 14px; line-height: 1.4;
  outline: none;
}
.input:focus { border-color: var(--accent); }
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: var(--accent); color: #fff; font-size: 16px; cursor: pointer; flex: none;
}
.send-btn:active { transform: scale(.94); }

/* ---------- Skill palette (mobile) ---------- */
.skill-palette-m {
  position: absolute; left: 0; right: 0; bottom: calc(100% + 8px);
  max-height: 40vh; overflow-y: auto;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.4); z-index: 18;
  padding: 4px;
}
.skill-palette-m.hidden { display: none; }
.skill-palette-m .skill-item-m {
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
}
.skill-palette-m .skill-item-m:active { background: var(--panel); }
.skill-palette-m .skill-item-m .skill-name-m { font-weight: 600; color: var(--accent); font-size: 14px; }
.skill-palette-m .skill-item-m .skill-desc-m { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ---------- Drawer ---------- */
.drawer-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20;
}
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: min(86%, 360px);
  background: var(--panel); border-left: 1px solid var(--border);
  transform: translateX(100%); transition: transform .25s ease; z-index: 21;
  display: flex; flex-direction: column; padding: 16px; gap: 16px; overflow-y: auto;
}
.drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; font-size: 17px; font-weight: 600; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field label { font-size: 13px; color: var(--muted); }

.segmented { display: flex; gap: 8px; }
.seg {
  flex: 1; padding: 10px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); cursor: pointer; font-size: 14px;
}
.seg.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.select, .text-input {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text); font-size: 14px; outline: none;
}
.text-input:focus, .select:focus { border-color: var(--accent); }
.conn-info { font-size: 12px; color: var(--muted); word-break: break-all; }

.drawer-actions { display: flex; flex-direction: column; gap: 10px; }
.btn-primary { background: var(--accent); color: #fff; border: none; font-weight: 600; }
.btn-warn { background: transparent; border: 1px solid var(--err); color: var(--err); }
.btn-ghost-m {
  margin-top: 6px; padding: 9px 12px; font-size: 13px; text-align: center;
  background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; cursor: pointer;
}
.btn-ghost-m:active { background: var(--panel-2); }
.drawer-msg { color: var(--ok); font-size: 13px; min-height: 16px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: rgba(0,0,0,.82); color: #fff; padding: 10px 16px; border-radius: 20px;
  font-size: 13px; z-index: 30; max-width: 80%; text-align: center;
}

.hidden { display: none !important; }

/* ---------- Login overlay ---------- */
.login-overlay {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15, 17, 21, 0.6);
  backdrop-filter: blur(4px);
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 320px;
  background: #1b1f27; border-radius: 16px;
  padding: 28px 24px 24px; text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.45);
}
.login-logo { font-size: 40px; line-height: 1; }
.login-title { margin: 8px 0 2px; font-size: 22px; font-weight: 700; color: #f2f4f8; }
.login-sub { margin-bottom: 18px; color: #9aa3b2; font-size: 13px; }
#login-form { display: flex; flex-direction: column; gap: 12px; }
.login-input {
  width: 100%; padding: 12px 14px; font-size: 15px;
  border: 1px solid #2c333f; border-radius: 10px;
  background: #11141a; color: #f2f4f8; outline: none;
  box-sizing: border-box;
}
.login-input:focus { border-color: #4f8cff; }
.login-btn {
  width: 100%; padding: 12px 14px; font-size: 15px; font-weight: 600;
  color: #fff; background: #4f8cff; border: none; border-radius: 10px;
  cursor: pointer;
}
.login-btn:disabled { opacity: .6; cursor: default; }
.login-error { margin-top: 12px; color: #ff6b6b; font-size: 13px; }

/* Closing summary with compressed body: dimmer, smaller, clamped height. */
.bubble.closing-summary .bubble-body {
  display: inline;
  font-size: 12.5px;
}
.bubble.closing-summary .closing-prefix {
  color: var(--muted);
  font-weight: 500;
}
.bubble.closing-summary .closing-body {
  color: var(--muted);
  opacity: .8;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Paperclip icon row appended after the closing summary — hidden file list. */
.files-icon-m { align-items: center; gap: 6px; }
.files-icon-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 12px; font-size: 13px;
  color: var(--text); background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 999px; cursor: pointer;
}
.files-icon-btn:active { border-color: var(--accent); color: var(--accent); }

/* Popup modal listing the generated files. */
.files-popup-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.files-popup {
  width: min(480px, 94vw);
  max-height: 72vh;
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: 0 16px 48px rgba(0,0,0,.5);
  overflow: hidden;
}
.files-popup-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 14px; font-size: 14px; font-weight: 600; color: var(--text);
  background: var(--panel-2); border-bottom: 1px solid var(--border);
}
.files-popup-close {
  background: transparent; border: none; color: var(--muted);
  font-size: 15px; cursor: pointer; padding: 4px 8px; border-radius: 6px;
}
.files-popup-close:active { color: var(--text); background: var(--border); }
.files-popup-list { overflow-y: auto; padding: 8px 12px; }
.files-popup-row { padding: 8px 4px; border-bottom: 1px solid var(--border); }
.files-popup-row:last-child { border-bottom: none; }

/* Generated-file download links */
.file-dl {
  display: block;
  padding: 3px 0;
  font-size: 12.5px;
  color: #3b82f6;
  text-decoration: underline;
  word-break: break-all;
}

/* Inline generated-image preview on mobile */
.file-img-m {
  display: block;
  max-width: 100%;
  border-radius: 8px;
  margin: 4px 0;
  border: 1px solid #ddd;
}

/* Collapsible reasoning + tool cards: expanded while working, collapsed after */
.reasoning-head, .card-title { cursor: pointer; }
.reasoning.collapsed .reasoning-body { display: none; }
.card.collapsed .card-body { display: none; }
