:root {
  --bg: #0f1115; --panel: #161a22; --panel2: #1d222c; --line: #262c38;
  --text: #e8eaee; --dim: #9aa3b2; --accent: #4f8cff; --mine: #2f6feb;
  --theirs: #232833; --ok: #3fb96f;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, 'PingFang SC', 'HarmonyOS Sans SC', 'Microsoft YaHei', sans-serif;
  display: flex; overflow: hidden;
}
.hidden { display: none !important; }
button {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px;
  padding: 8px 14px; cursor: pointer; font-size: 14px;
}
button:hover { filter: brightness(1.1); }
input, textarea {
  background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 9px 12px; font-size: 14px; outline: none;
  font-family: inherit;
}
input:focus, textarea:focus { border-color: var(--accent); }

/* ---- 登录 ---- */
#login { flex: 1; display: flex; align-items: center; justify-content: center; }
.login-card {
  width: 320px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 32px 28px; text-align: center;
}
.login-card .logo { font-size: 44px; }
.login-card h1 { margin: 8px 0 20px; font-weight: 600; }
.login-card input { width: 100%; margin-bottom: 12px; }
.login-card button { width: 100%; padding: 10px; }
.err { color: #ff7b72; font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---- 主布局 ---- */
#app { flex: 1; display: flex; height: 100vh; }
#side {
  width: 300px; min-width: 300px; background: var(--panel);
  border-right: 1px solid var(--line); display: flex; flex-direction: column;
}
#side header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; }
#btn-admin { background: var(--panel2); border: 1px solid var(--line); padding: 4px 10px; }
#conv-list { flex: 1; overflow-y: auto; }
.conv {
  position: relative; padding: 12px 16px; cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.conv:hover { background: var(--panel2); }
.conv.active { background: var(--panel2); border-left: 3px solid var(--accent); }
.conv-main { display: flex; align-items: center; gap: 8px; }
.conv-name { font-weight: 500; }
.conv-sub {
  color: var(--dim); font-size: 12px; margin-top: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-x {
  position: absolute; right: 8px; bottom: 10px; display: none;
  background: transparent; color: var(--dim); border: 0;
  padding: 2px 6px; font-size: 12px;
}
.conv:hover .conv-x { display: block; }
.conv-x:hover { color: #ff7b72; }
.dot {
  width: 8px; height: 8px; border-radius: 50%; background: #555;
  display: inline-block; flex: none;
}
.dot.on { background: var(--ok); box-shadow: 0 0 6px var(--ok); }
.badge {
  position: absolute; right: 14px; top: 14px; background: #e5484d; color: #fff;
  border-radius: 10px; font-size: 11px; padding: 1px 7px;
}

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-head {
  display: flex; align-items: center; gap: 10px; padding: 13px 18px;
  border-bottom: 1px solid var(--line); background: var(--panel);
  font-weight: 500;
}
#btn-back { background: var(--panel2); border: 1px solid var(--line); padding: 2px 12px; font-size: 18px; }
#msgs { flex: 1; overflow-y: auto; padding: 18px; }
.msg { margin-bottom: 14px; max-width: 76%; }
.msg.mine { margin-left: auto; text-align: right; }
.msg .meta { color: var(--dim); font-size: 11px; margin-bottom: 4px; }
.bubble {
  display: inline-block; text-align: left; padding: 9px 13px; border-radius: 14px;
  line-height: 1.55; font-size: 14.5px; word-break: break-word;
}
.bubble code, .bubble pre.code {
  background: rgba(0,0,0,.35); border-radius: 6px; font-family: ui-monospace, 'Cascadia Mono', Consolas, monospace;
  font-size: 13px;
}
.bubble code { padding: 1px 5px; }
.bubble pre.code { display: block; padding: 10px 12px; overflow-x: auto; margin: 6px 0; }
.msg.mine .bubble { background: var(--mine); color: #fff; }
.msg.theirs .bubble { background: var(--theirs); }
.bubble a { color: #9ec1ff; }

#composer {
  display: flex; gap: 10px; padding: 12px 14px;
  border-top: 1px solid var(--line); background: var(--panel);
}
#input { flex: 1; resize: none; max-height: 120px; }
.btn-file {
  display: flex; align-items: center; justify-content: center;
  width: 42px; background: var(--panel2); border: 1px solid var(--line);
  border-radius: 8px; cursor: pointer; font-size: 18px;
}
.btn-file:hover { border-color: var(--accent); }
.sr-input { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; }
#msgs.dragover, #main.dragover { outline: 2px dashed var(--accent); outline-offset: -6px; }
#btn-mic {
  width: 42px; padding: 0; background: var(--panel2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; font-size: 18px;
}
#btn-mic.rec {
  background: #b91c1c; color: #fff; border-color: #ef4444;
  animation: pulse 1s infinite;
}
@keyframes pulse { 50% { opacity: .65; } }
.tr { font-size: 12.5px; color: var(--text); margin-top: 5px; border-top: 1px dashed var(--line); padding-top: 4px; }
audio { height: 36px; max-width: 260px; }

/* ---- 附件 ---- */
.fimg {
  max-width: 320px; max-height: 320px; border-radius: 10px;
  display: block; cursor: zoom-in; margin: 4px 0;
}
.file-row {
  display: inline-block; background: rgba(0,0,0,.3); padding: 8px 12px;
  border-radius: 10px; color: #9ec1ff; text-decoration: none; font-size: 13.5px;
}
.fname { color: var(--dim); font-size: 11px; margin-top: 3px; }

/* ---- 卡片（决议 13）---- */
.card {
  background: rgba(0,0,0,.22); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; min-width: 200px;
}
.card.confirm { border-color: #b8860b; }
.card-title { font-weight: 600; margin-bottom: 6px; }
.card-note { color: var(--dim); font-size: 12.5px; margin-top: 6px; }
.chip {
  display: inline-block; padding: 2px 10px; border-radius: 10px;
  font-size: 12px; background: #2c3340; color: var(--text);
}
.chip.st-running { background: #1d4ed8; }
.chip.st-done, .st-done { background: #15803d; }
.chip.st-failed, .st-failed { background: #b91c1c; }
.chip.st-confirm { background: #b45309; }
.bar {
  height: 8px; background: #0b0d11; border-radius: 5px; overflow: hidden; margin: 6px 0;
}
.bar i { display: block; height: 100%; background: var(--accent); transition: width .3s; }
.confirm-btns { display: flex; gap: 8px; margin-top: 10px; }
.confirm-btns .c-yes { background: #15803d; }
.confirm-btns .c-no { background: #6b2226; }
.confirm-btns button:disabled { opacity: .45; cursor: default; }

/* ---- 管理台 ---- */
#admin {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.admin-card {
  width: 420px; max-width: 94vw; max-height: 84vh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px;
}
.admin-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.admin-head button { background: var(--panel2); border: 1px solid var(--line); }
.admin-new { display: flex; gap: 8px; margin-bottom: 12px; }
.admin-new input { flex: 1; }
#ag-token {
  background: var(--panel2); border: 1px dashed var(--accent);
  border-radius: 10px; padding: 10px 12px; margin-bottom: 12px; font-size: 13px;
}
.token-row { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.token-row code {
  flex: 1; background: #000; padding: 8px 10px; border-radius: 8px;
  font-family: ui-monospace, Consolas, monospace; font-size: 12px; word-break: break-all;
}
.ag-item {
  display: flex; align-items: center; gap: 8px; padding: 10px 8px;
  border-bottom: 1px solid var(--line);
}
.ag-name { flex: 1; }
.ag-status { color: var(--dim); font-size: 12px; }
.ag-revoke { background: #a13034; padding: 4px 10px; font-size: 12px; }
.admin-foot { margin-top: 14px; text-align: right; }
.admin-foot button { background: var(--panel2); border: 1px solid var(--line); }
.admin-sec { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; }
.admin-sec > b { display: block; margin-bottom: 10px; }
.gp-agents { display: flex; flex-wrap: wrap; gap: 8px 14px; margin: 10px 0; }
.gp-agents .ck { display: flex; align-items: center; gap: 5px; font-size: 13.5px; cursor: pointer; }
.audit-list { max-height: 180px; overflow-y: auto; font-size: 12px; }
.au-row { display: flex; gap: 8px; padding: 3px 0; border-bottom: 1px dashed var(--line); }
.au-t { color: var(--dim); flex: none; }
.au-a { color: var(--accent); flex: none; max-width: 40%; overflow: hidden; text-overflow: ellipsis; }
.dim { color: var(--dim); }

/* ---- 移动端（≤1024：手机/折叠屏/平板竖屏）----
   布局保证：头部钉顶、输入框钉底、消息区独立滚动（chat 打开时为全屏覆盖层） */
#btn-back { display: none; }
@media (max-width: 1024px) {
  #app { display: block; height: 100vh; height: 100dvh; position: relative; overflow: hidden; }

  /* 会话列表 = 整屏 */
  #side { width: 100% !important; min-width: 0 !important; height: 100vh; height: 100dvh; overflow-y: auto; }
  body.chat-open #side { display: none !important; }

  /* 聊天 = 固定定位全屏覆盖层（不受页面滚动影响） */
  #main {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 20;
    flex-direction: column;
    background: var(--bg);
  }
  body.chat-open #main { display: flex !important; }

  /* 头部钉顶（含刘海安全区） */
  #chat-head {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    padding-top: calc(12px + env(safe-area-inset-top, 0px));
    background: var(--panel);
    border-bottom: 1px solid var(--line);
  }
  #btn-back {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    height: 40px;
    background: var(--panel2);
    border: 1px solid var(--line);
    border-radius: 10px;
    color: var(--text);
    font-size: 22px;
    cursor: pointer;
    flex: none;
  }

  /* 消息区：唯一滚动区域 */
  #msgs {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px;
  }
  .msg { max-width: 88%; }

  /* 输入区钉底（含手势条安全区） */
  #composer {
    flex-shrink: 0;
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    background: var(--panel);
    border-top: 1px solid var(--line);
  }
  #msg-input { flex: 1; }
}
