:root, html.dark {
  --bg: #0c0d10;
  --bg2: #14161c;
  --bg3: #1c1f27;
  --line: #2a2e38;
  --text: #e8eaef;
  --muted: #8b93a7;
  --accent: #f0b429;
  --accent2: #c99216;
  --ok: #3dd68c;
  --warn: #f5a524;
  --bad: #f76c6c;
  --user: #3d4d68;
  --perm-bg: #2a2110;
  --perm-line: #6b5318;
  --safe: env(safe-area-inset-bottom, 0px);
  --top: env(safe-area-inset-top, 0px);
}
html[data-channel="premium"],
html[data-channel="premium"].dark {
  --accent: #5eb8ff;
  --accent2: #3a9be0;
}
html.light[data-channel="premium"] {
  --accent: #1a7fd4;
  --accent2: #1466ab;
}
html.light {
  --bg: #f4f1ea;
  --bg2: #fffdf8;
  --bg3: #ece6da;
  --line: #d9d0c0;
  --text: #1c1914;
  --muted: #6b6458;
  --accent: #c99216;
  --accent2: #a67a10;
  --ok: #1a8a52;
  --warn: #b86a00;
  --bad: #c0392b;
  --user: #e8dfcc;
  --perm-bg: #f7ecd4;
  --perm-line: #d4b56a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 20px; line-height: 1.45; font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  overscroll-behavior: none; overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.auth-body { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.auth-card { width: min(400px, 100%); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 24px; }
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 22px; }
.mark { width: 42px; height: 42px; border-radius: 12px; background: var(--accent); color: #1a1403;
  display: grid; place-items: center; font-weight: 800; font-size: 20px; }
.brand h1 { margin: 0; font-size: 20px; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.auth-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.auth-card input { width: 100%; margin-top: 6px; background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px; }
.auth-card button { width: 100%; margin-top: 8px; background: var(--accent); color: #1a1403;
  border: 0; border-radius: 10px; padding: 12px; font-weight: 700; }
.err { color: var(--bad); font-size: 13px; margin: 0 0 12px; }

.app { display: grid; grid-template-columns: 280px 1fr; height: 100%; height: 100dvh; overflow: hidden; }
.sidebar { background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column;
  min-height: 0; padding-top: var(--top); }
.side-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 14px 8px; }
.side-head h2 { margin: 0; font-size: 18px; min-width: 0; }
#newBtn { flex-shrink: 0; }
.icon-btn, .btn { background: var(--bg3); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; color: var(--text); }
#photoBtn { flex-shrink: 0; font-weight: 700; }
#photoBtn.off { pointer-events: none; opacity: .5; }
.sr-file {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.btn.primary { background: var(--accent); color: #1a1403; border-color: transparent; font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.model-sel { background: var(--bg3); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 8px; max-width: 42vw; color: var(--text); flex-shrink: 0; }
.acct-sel { max-width: min(220px, 46vw); }
.chat-item .acct {
  display: inline-block; margin-left: 6px; padding: 1px 6px; border-radius: 999px;
  font-size: 11px; font-weight: 700; letter-spacing: .02em;
  color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent);
  vertical-align: middle;
}
.chats { overflow: auto; flex: 1; padding: 6px; scrollbar-width: none; -ms-overflow-style: none; }
.chats::-webkit-scrollbar { width: 0; height: 0; }
.chat-item { display: flex; align-items: flex-start; gap: 8px; width: 100%; text-align: left; background: transparent; border: 0;
  border-radius: 12px; padding: 12px; margin-bottom: 4px; position: relative; }
.chat-item.active, .chat-item:hover { background: var(--bg3); }
.chat-item.unread { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.chat-item.unread .t { font-weight: 800; }
.chat-item .dot { position: absolute; left: 8px; top: 20px; width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent); }
.chat-item.unread { padding-left: 22px; }
.chat-item .chat-body { flex: 1; min-width: 0; cursor: pointer; }
.chat-item .t {
  font-weight: 600; font-size: 17px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  overflow: hidden; white-space: normal; overflow-wrap: anywhere;
}
.chat-item .s { color: var(--muted); font-size: 14px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .tok {
  color: var(--muted); font-size: 12px; margin-top: 3px; line-height: 1.35;
  white-space: normal; overflow-wrap: anywhere;
}
.chat-item .m { color: var(--muted); font-size: 13px; margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item .m.act { color: var(--accent); }
.chat-del { flex-shrink: 0; width: 36px; height: 36px; margin-top: 2px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); font-size: 16px; line-height: 1; padding: 0; }
.chat-del:hover, .chat-del:active { background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--bad); }
.think-bar { height: 3px; background: var(--bg3); overflow: hidden; flex-shrink: 0; }
.think-bar.hidden { display: none; }
#thinkBar { display: none !important; }
.think-bar.slim { height: 3px; margin-top: 8px; border-radius: 99px; }
.think-bar i { display: block; height: 100%; width: 40%; background: var(--accent);
  animation: think 1.1s ease-in-out infinite; }
@keyframes think {
  0% { transform: translateX(-120%); }
  100% { transform: translateX(280%); }
}
.side-foot { padding: 10px 12px calc(12px + var(--safe)); border-top: 1px solid var(--line);
  display: flex; flex-wrap: wrap; gap: 8px; }
#alertBtn.on { background: var(--bg3); font-weight: 700; }
.side-foot .who { flex: 1; color: var(--muted); font-size: 12px; align-self: center; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: calc(10px + var(--top)) 12px 10px;
  border-bottom: 1px solid var(--line); background: var(--bg); }
.topbar .meta { flex: 1; min-width: 0; }
.topbar .meta h1 {
  margin: 0; font-size: 18px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; white-space: normal; overflow-wrap: anywhere;
}
.topbar .meta p { margin: 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.usage { display: flex; align-items: flex-start; flex-wrap: wrap; gap: 6px 8px; margin-top: 4px; }
.usage.hidden { display: none; }
.ubar { flex: 0 0 72px; height: 6px; background: var(--bg3); border-radius: 99px; overflow: hidden; margin-top: 5px; }
.ubar i { display: block; height: 100%; width: 0; background: var(--ok); }
.ubar.mid i { background: var(--warn); }
.ubar.hi i { background: var(--bad); }
#usageText, #quotaText { color: var(--muted); font-size: 13px; line-height: 1.3; white-space: normal; overflow: visible; flex: 1 1 160px; }
.usage.quota { margin-top: 6px; }
.usage.quota .ubar i { background: var(--accent); }
.acct-sel:disabled { opacity: .85; cursor: default; }
.menu { display: none; }

.stream { flex: 1; overflow: auto; overflow-x: hidden; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 14px;
  scrollbar-width: none; -ms-overflow-style: none; overflow-anchor: none; }
.stream::-webkit-scrollbar { width: 0; height: 0; }
.stream > * { flex: 0 0 auto; min-height: 0; }
.stream > .btn { align-self: center; }
.empty { margin: auto; color: var(--muted); text-align: center; max-width: 280px; flex: 1 1 auto; }
.bubble { max-width: min(760px, 100%); min-width: 0; overflow: hidden; color: var(--text); }
.bubble.user { align-self: flex-end; background: var(--user); border-radius: 14px 14px 4px 14px; padding: 10px 14px;
  margin-top: 6px; word-break: break-word; overflow-wrap: anywhere; }
.bubble.assistant { align-self: stretch; background: var(--bg3); border: 1px solid var(--line);
  border-radius: 14px 14px 14px 4px; padding: 14px 16px; line-height: 1.55; }
.thought { display: none; }
.md { word-break: break-word; }
.md p { margin: 0.85em 0; }
.md p:first-child { margin-top: 0; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0.85em 0; padding-left: 1.35em; }
.md li { margin: 0.4em 0; }
.md li p { margin: 0.25em 0; }
.md h1, .md h2, .md h3 { margin: 1em 0 0.4em; line-height: 1.3; font-weight: 750; }
.md h1:first-child, .md h2:first-child, .md h3:first-child { margin-top: 0; }
.md h1 { font-size: 1.12em; }
.md h2 { font-size: 1.06em; }
.md h3 { font-size: 1em; }
.md strong { font-weight: 750; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.md blockquote { margin: 0.85em 0; padding: 0.15em 0 0.15em 0.85em;
  border-left: 3px solid var(--accent); color: var(--muted); }
.md pre { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px;
  overflow: auto; font-size: 15px; }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 17px; }
.md :not(pre) > code { background: var(--bg3); padding: 1px 5px; border-radius: 5px; }
.md-pics {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0;
  max-width: 100%;
}
.md-pic {
  flex: 0 0 calc(50% - 4px);
  width: calc(50% - 4px);
  max-width: 148px;
  aspect-ratio: 3 / 4;
  overflow: hidden;
  border-radius: 10px;
  background: var(--bg3);
  border: 1px solid var(--line);
}
.md img {
  display: block;
  max-width: min(148px, 100%);
  max-height: 198px;
  width: auto;
  height: auto;
  object-fit: cover;
  border-radius: 10px;
  margin: 0;
  background: var(--bg3);
  cursor: zoom-in;
}
.md-pic img {
  display: block;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  object-fit: cover;
  border-radius: 10px;
  margin: 0;
  background: var(--bg3);
  cursor: zoom-in;
}
.img-zoom {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  background: rgba(0,0,0,.88);
  cursor: zoom-out;
  touch-action: pan-y;
}
.img-zoom.on {
  display: grid;
  justify-items: center;
  align-content: start;
}
.img-zoom img {
  display: block;
  width: auto;
  height: auto;
  max-width: min(100%, 920px);
  max-height: none;
  border-radius: 10px;
}
.tool { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; font-size: 13px; }
.tool .row { display: flex; justify-content: space-between; gap: 8px; }
.tool .kind { color: var(--accent); font-weight: 600; }
.tool .st { color: var(--muted); }
.tool .st.run { color: var(--warn); }
.tool .st.ok { color: var(--ok); }
.tool .st.fail { color: var(--bad); }
.tool pre { margin: 6px 0 0; white-space: pre-wrap; color: var(--muted); max-height: 180px; overflow: auto; }
.sys { color: var(--muted); font-size: 12px; text-align: center; }
#live { display: none; text-align: center; font-size: 13px; color: var(--warn);
  padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--bg2); }
#live.on { display: block; }
.perm { background: var(--perm-bg); border: 1px solid var(--perm-line); border-radius: 12px; padding: 10px; }
.perm .acts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.composer { border-top: 1px solid var(--line); padding: 10px 12px calc(12px + var(--safe)); background: var(--bg2); }
.composer form { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 44px; max-height: 160px; background: var(--bg);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 0 8px; }
.chips:empty { display: none; }
.chip { display: flex; align-items: center; gap: 8px; background: var(--bg3); border: 1px solid var(--line);
  border-radius: 10px; padding: 6px 8px; font-size: 14px; max-width: 100%; }
.chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; }
.chip .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 180px; }
.chip .x { background: transparent; border: 0; color: var(--muted); padding: 0 4px; font-size: 18px; }
.atts { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.atts img { max-width: 180px; max-height: 180px; border-radius: 10px; display: block; }
.atts .filechip { background: var(--bg3); border-radius: 8px; padding: 6px 8px; font-size: 14px; }
.yolo { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; padding: 0 0 8px; }

.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: end center; z-index: 20; }
.modal { width: min(480px, 100%); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 16px 16px 0 0; padding: 16px 16px calc(16px + var(--safe)); }
.modal h3 { margin: 0 0 12px; }
.modal label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.modal input, .modal select { width: 100%; margin-top: 6px; background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px; }
.modal .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.modal .hint { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.modal .hint.hidden, .extra-field.hidden { display: none; }
html[data-channel="premium"] .extra-field { display: none; }
.acct-label { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.acct-picks { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.acct-pick {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  width: 100%; text-align: left; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); cursor: pointer;
}
.acct-pick .name { font-weight: 500; font-size: 14px; }
.acct-pick .meta { font-size: 12px; color: var(--muted); }
.acct-pick.super .name { font-weight: 800; font-size: 16px; letter-spacing: -.01em; }
.acct-pick.super { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.acct-pick.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--bg)); }
.acct-pick.super.on { box-shadow: inset 0 0 0 1px var(--accent); }
#acctSel option.super, #newAcct option.super { font-weight: 800; }
.chat-item .acct.super { font-weight: 800; font-size: 12px; }

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(86vw, 320px); z-index: 15;
    transform: translateX(-105%); transition: transform .2s ease; }
  .sidebar.open { transform: none; box-shadow: 8px 0 40px rgba(0,0,0,.4); }
  .menu { display: inline-flex; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 14; }
  .app.home .sidebar { position: static; width: 100%; transform: none; box-shadow: none; }
  .app.home .main, .app.home .scrim { display: none; }
  .app.home .side-head {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 12px 14px 10px;
  }
  .app.home #newBtn {
    width: 100%;
    padding: 12px 14px;
    font-size: 17px;
  }
  .app.home .chat-item { padding: 14px 12px; }
  .app.home .chat-item .t { font-size: 16px; }
  .topbar { flex-wrap: wrap; align-items: flex-start; }
  .topbar .meta { flex: 1 1 calc(100% - 200px); }
  .app.swiping .sidebar {
    transform: none;
    width: 100%;
    z-index: 1;
  }
  .app.swiping .main {
    position: relative;
    z-index: 12;
    background: var(--bg);
    box-shadow: -16px 0 40px rgba(0,0,0,.28);
    will-change: transform;
  }
  .app.swipe-snap .main { transition: transform .2s ease; }
}

/* Desktop: cột hội thoại giữa màn, rộng hơn một chút; chữ nhỏ hơn mobile */
@media (min-width: 801px) {
  html, body { font-size: 18px; }
  .app { grid-template-columns: 300px 1fr; }
  .stream {
    padding: 22px max(32px, calc(50% - 540px)) 14px;
    gap: 16px;
  }
  .composer {
    padding-left: max(24px, calc(50% - 540px));
    padding-right: max(24px, calc(50% - 540px));
  }
  .model-sel { max-width: 180px; }
  .bubble { max-width: 100%; }
  .bubble.user { max-width: min(760px, 100%); }
  .thought, .tool, .perm { max-width: 100%; }
  .md-pic {
    flex: 0 0 calc(33.333% - 6px);
    width: calc(33.333% - 6px);
    max-width: 220px;
  }
  .md img {
    max-width: min(220px, 100%);
    max-height: 294px;
  }
  .modal-bg { place-items: center; }
  .modal {
    width: min(460px, calc(100% - 48px));
    border-radius: 16px;
    padding: 20px;
  }
}
