/* =========================================================================
   AI Chat Platform — style.css
   Kích thước KHÔNG cố định: mọi cỡ chữ/khoảng cách đi qua clamp() nên tự giãn
   liên tục theo bề rộng màn hình. Ngoài ra còn tách riêng theo:
     - bề rộng   (@media max-width)      : điện thoại / tablet / PC / màn lớn
     - loại trỏ  (@media pointer: coarse): ngón tay thì vùng bấm to hơn
     - có hover  (@media hover: hover)   : chỉ PC mới chạy hiệu ứng hover
   ========================================================================= */

:root {
  color-scheme: dark;

  /* ---- Màu: nhiều lớp nền để chiều sâu rõ hơn, viền dùng alpha cho nét ---- */
  --bg:        #070a11;
  --bg-2:      #0c111b;
  --bg-3:      #141b29;
  --bg-4:      #1c2537;
  --line:      rgba(255, 255, 255, .07);
  --line-2:    rgba(255, 255, 255, .13);
  --text:      #e9eef8;
  --muted:     #9aa7c0;
  --faint:     #6d7b96;
  --brand:     #5b8cff;
  --brand-2:   #8b5cff;
  --brand-ink: #cddcff;
  --ok:        #35d399;
  --warn:      #fbbf24;
  --err:       #fb6f78;

  --grad: linear-gradient(135deg, var(--brand) 0%, var(--brand-2) 100%);
  --glow: rgba(91, 140, 255, .30);

  /* ---- Thang chữ: min cho điện thoại, max cho PC, ở giữa giãn theo vw ---- */
  --t--1: clamp(11.5px, 0.70rem + 0.10vw, 13px);
  --t-0:  clamp(14px,   0.84rem + 0.16vw, 15.5px);
  --t-1:  clamp(15px,   0.90rem + 0.28vw, 17.5px);
  --t-2:  clamp(17.5px, 1.02rem + 0.50vw, 22px);
  --t-3:  clamp(22px,   1.25rem + 1.00vw, 32px);

  /* ---- Khoảng cách & khung ---- */
  --gap:       clamp(10px, 2.2vw, 18px);
  --pad-x:     clamp(14px, 3.2vw, 26px);
  --sidebar-w: 288px;
  --reading-w: 880px;

  --r-sm: 10px;
  --r:    14px;
  --r-lg: 20px;

  --sh-1: 0 1px 2px rgba(0, 0, 0, .45);
  --sh-2: 0 10px 30px -12px rgba(0, 0, 0, .65);
  --sh-3: 0 34px 70px -22px rgba(0, 0, 0, .8);
  --ring: 0 0 0 3px rgba(91, 140, 255, .34);

  --speed: .18s;
  --ease:  cubic-bezier(.4, 0, .2, 1);

  --font: "Inter", "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system,
          Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", "Cascadia Code", Consolas, monospace;
}
/* Màn hình lớn: nới khung đọc và cột bên cho đỡ trống trải */
@media (min-width: 1600px) {
  :root { --sidebar-w: 320px; --reading-w: 960px; }
}

/* ====== Reset & nền ====== */
*, *::before, *::after { box-sizing: border-box; }

html {
  /* dvh: trên điện thoại thanh địa chỉ trượt lên/xuống không làm layout nhảy */
  height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--t-0);
  line-height: 1.6;
  /* Chữ nét hơn trên màn Retina/HiDPI */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  font-feature-settings: "cv11", "ss01";
  -webkit-tap-highlight-color: transparent;
}

/* Nền phát sáng rất nhẹ ở hai góc, cố định phía sau mọi thứ */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(58vw 52vh at 8% -6%,  rgba(91, 140, 255, .13), transparent 62%),
    radial-gradient(52vw 48vh at 98% 104%, rgba(139, 92, 255, .11), transparent 60%);
}

button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); text-decoration-thickness: 1px; text-underline-offset: 2px; }
h1, h2, h3 { letter-spacing: -.018em; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ====== Thanh cuộn mảnh ====== */
* { scrollbar-width: thin; scrollbar-color: var(--bg-4) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb {
  background: var(--bg-4);
  border: 3px solid transparent;
  background-clip: content-box;
  border-radius: 999px;
}
*::-webkit-scrollbar-thumb:hover { background: #33405c; }

/* ====== Nút ====== */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--line-2);
  background: linear-gradient(180deg, var(--bg-3), var(--bg-2));
  color: var(--text);
  border-radius: 11px;
  padding: 10px 15px;
  font-size: var(--t-0);
  font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: var(--sh-1);
  transition: transform var(--speed) var(--ease), box-shadow var(--speed) var(--ease),
              border-color var(--speed) var(--ease), background var(--speed) var(--ease),
              opacity var(--speed) var(--ease);
}
.btn:active { transform: translateY(1px) scale(.995); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }
.btn.primary {
  background: var(--grad);
  border-color: transparent;
  color: #fff;
  font-weight: 650;
  box-shadow: 0 1px 0 rgba(255, 255, 255, .18) inset, 0 8px 22px -10px var(--glow);
}
.btn.ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
}
.btn.danger {
  background: linear-gradient(180deg, #2c1620, #23111a);
  border-color: rgba(251, 111, 120, .32);
  color: #ffb9be;
}
.btn.block { width: 100%; }
.btn.sm { padding: 6px 11px; font-size: var(--t--1); border-radius: 9px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  background: transparent;
  border: 0;
  cursor: pointer;
  font-size: 20px;
  width: 40px; height: 40px;
  border-radius: 11px;
  color: var(--muted);
  transition: background var(--speed) var(--ease), color var(--speed) var(--ease);
}

/* Hover chỉ áp dụng cho thiết bị có con trỏ thật (PC), tránh trạng thái
   "dính hover" sau khi chạm trên điện thoại. */
@media (hover: hover) and (pointer: fine) {
  .btn:hover { border-color: var(--line-2); transform: translateY(-1px); box-shadow: var(--sh-2); }
  .btn.primary:hover {
    box-shadow: 0 1px 0 rgba(255, 255, 255, .22) inset, 0 14px 30px -10px var(--glow);
    filter: saturate(1.08) brightness(1.05);
  }
  .btn.ghost:hover { background: var(--bg-3); color: var(--text); }
  .btn.danger:hover { border-color: rgba(251, 111, 120, .5); }
  .icon-btn:hover { background: var(--bg-3); color: var(--text); }
}
/* ====== Form ====== */
input, select, textarea {
  width: 100%;
  background: rgba(0, 0, 0, .32);
  border: 1px solid var(--line-2);
  border-radius: 11px;
  padding: 11px 13px;
  outline: none;
  /* 16px là ngưỡng iOS Safari tự phóng to trang khi focus -> giữ >= 16px
     trên điện thoại (xem @media max-width: 640px ở cuối file). */
  font-size: var(--t-0);
  transition: border-color var(--speed) var(--ease), box-shadow var(--speed) var(--ease),
              background var(--speed) var(--ease);
}
input::placeholder, textarea::placeholder { color: var(--faint); }
input:focus, select:focus, textarea:focus {
  border-color: rgba(91, 140, 255, .65);
  box-shadow: var(--ring);
  background: rgba(0, 0, 0, .45);
}
label {
  display: block;
  font-size: var(--t--1);
  color: var(--muted);
  font-weight: 550;
  margin: 14px 0 6px;
}
select {
  background-color: var(--bg-3);
  cursor: pointer;
  /* mũi tên riêng, gọn hơn mũi tên mặc định của từng OS */
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 52%, calc(100% - 12px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}
select option { background: var(--bg-3); color: var(--text); }

/* ====== Nhãn trạng thái ====== */
.badge {
  display: inline-flex;
  align-items: center;
  font-size: var(--t--1);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--line-2);
  background: var(--bg-3);
  color: var(--muted);
  white-space: nowrap;
}
.badge.ok   { color: #7ff0c4; border-color: rgba(53, 211, 153, .32); background: rgba(53, 211, 153, .1); }
.badge.warn { color: #ffd88a; border-color: rgba(251, 191, 36, .32); background: rgba(251, 191, 36, .1); }
.badge.err  { color: #ffb3b8; border-color: rgba(251, 111, 120, .32); background: rgba(251, 111, 120, .1); }
.badge.vip  {
  color: #1a1305;
  border-color: transparent;
  background: linear-gradient(135deg, #ffd479, #f5a524);
  font-weight: 700;
}

/* ====== Đăng nhập / đăng ký ====== */
.auth-screen {
  height: 100dvh;
  overflow-y: auto;
  display: grid;
  place-items: center;
  padding: max(20px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: linear-gradient(170deg, rgba(23, 31, 47, .92), rgba(10, 15, 24, .94));
  border: 1px solid var(--line-2);
  border-radius: var(--r-lg);
  padding: clamp(22px, 5vw, 34px);
  box-shadow: var(--sh-3);
  backdrop-filter: blur(14px);
  animation: rise .45s var(--ease) both;
}
/* Vệt sáng mảnh chạy dọc mép trên thẻ — chi tiết nhỏ tạo cảm giác "kính" */
.auth-card { position: relative; overflow: hidden; }
.auth-card::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(139, 168, 255, .75), transparent);
}
.auth-title {
  margin: 0;
  font-size: var(--t-3);
  font-weight: 700;
  letter-spacing: -.03em;
  background: linear-gradient(120deg, #ffffff 10%, var(--brand-ink) 55%, #c9b3ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.auth-sub { margin: 8px 0 20px; color: var(--muted); font-size: var(--t-0); }
.tabs {
  display: flex;
  gap: 4px;
  background: rgba(0, 0, 0, .35);
  padding: 4px;
  border-radius: 12px;
  border: 1px solid var(--line);
}
.tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 9px 10px;
  border-radius: 9px;
  font-size: var(--t-0);
  font-weight: 550;
  cursor: pointer;
  transition: background var(--speed) var(--ease), color var(--speed) var(--ease);
}
.tab.active {
  background: linear-gradient(180deg, var(--bg-4), var(--bg-3));
  color: var(--text);
  font-weight: 650;
  box-shadow: var(--sh-1);
}
.auth-form .btn { margin-top: 20px; }
.auth-message {
  margin: 14px 0 0;
  font-size: var(--t--1);
  min-height: 18px;
  color: var(--warn);
}
.auth-message.err { color: var(--err); }
.auth-message.ok  { color: var(--ok); }

@keyframes rise {
  from { opacity: 0; transform: translateY(14px) scale(.985); }
  to   { opacity: 1; transform: none; }
}

/* ====== Bố cục ứng dụng ====== */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  height: 100dvh;
}

/* Nền mờ của ngăn kéo sidebar CHỈ tồn tại trên màn hình nhỏ. Trên PC bắt buộc
   display:none: #scrim là con thứ 3 của .app, nếu còn hiển thị nó sẽ chiếm một
   ô grid và đẩy vùng chat xuống hàng dưới. Bản đầy đủ ở @media (max-width: 900px). */
#scrim { display: none; }

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: linear-gradient(180deg, rgba(13, 18, 29, .96), rgba(8, 12, 20, .96));
  border-right: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.sidebar-head { padding: 14px; border-bottom: 1px solid var(--line); }
.sidebar-foot {
  padding: 12px 14px max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  display: grid;
  gap: 8px;
}
.session-list {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px;
  display: grid;
  /* minmax(0, 1fr) chứ KHÔNG để cột tự động: cột `auto` phình theo nội dung
     dài nhất (tên đoạn chat không ngắt dòng) nên danh sách sẽ rộng hơn cột bên
     và bị cắt mất mép phải. Xem ghi chú ở .chat. */
  grid-template-columns: minmax(0, 1fr);
  gap: 4px;
  align-content: start;
}
.session-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 10px 11px;
  border-radius: 11px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background var(--speed) var(--ease), border-color var(--speed) var(--ease);
}
.session-item.active {
  background: linear-gradient(100deg, rgba(91, 140, 255, .16), rgba(139, 92, 255, .08));
  border-color: rgba(91, 140, 255, .3);
}
/* Vạch sáng bên trái đánh dấu đoạn chat đang mở */
.session-item.active { position: relative; }
.session-item.active::before {
  content: "";
  position: absolute;
  left: -1px; top: 50%;
  translate: 0 -50%;
  width: 3px; height: 58%;
  border-radius: 0 3px 3px 0;
  background: var(--grad);
}
.session-item .title {
  font-size: var(--t-0);
  font-weight: 600;
  letter-spacing: -.01em;
}
.session-item .preview { font-size: var(--t--1); color: var(--muted); }
.session-item .preview,
.session-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Nút xoá: PC thì ẩn cho gọn, hiện khi trỏ vào. Điện thoại luôn hiện vì
   không có hover để mà hiện ra. */
.session-item .del { opacity: 0; transition: opacity var(--speed) var(--ease); }
.session-item.active .del { opacity: 1; }
@media (hover: hover) and (pointer: fine) {
  .session-item:hover { background: var(--bg-3); }
  .session-item:hover .del { opacity: 1; }
}
@media (pointer: coarse) {
  .session-item .del { opacity: 1; }
}

.empty {
  color: var(--muted);
  font-size: var(--t--1);
  padding: 18px 12px;
  text-align: center;
}

.user-box { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  flex: 0 0 auto;
  width: 36px; height: 36px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700; color: #fff;
  background: var(--grad);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .14) inset, 0 6px 16px -8px var(--glow);
}
.user-meta { display: grid; gap: 3px; min-width: 0; }
.user-meta strong {
  font-size: var(--t--1);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.user-meta .badge { justify-self: start; }

/* ====== Khu chat ====== */
.chat {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  /* BẮT BUỘC khai báo cột. Nếu để grid tự sinh cột `auto`, cột đó nở tới
     max-content của nội dung rộng nhất (khối code trong câu trả lời) — trên
     điện thoại làm cả header, vùng tin nhắn và ô nhập rộng hơn màn hình ~110px
     rồi bị cắt mất mép phải. minmax(0, 1fr) ghim cột đúng bề rộng khung nhìn,
     khối code tự cuộn ngang trong bóng chat như thiết kế. */
  grid-template-columns: minmax(0, 1fr);
  min-height: 0;
  min-width: 0;
}
.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px var(--pad-x);
  padding-top: max(11px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: rgba(10, 15, 24, .72);
  backdrop-filter: blur(14px) saturate(1.4);
}
.chat-head h2 {
  margin: 0;
  font-size: var(--t-1);
  font-weight: 650;
  flex: 1;
  min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.head-right { display: flex; align-items: center; gap: 8px; min-width: 0; }
.head-right select {
  width: auto;
  min-width: 190px;
  max-width: 46vw;
  font-size: var(--t--1);
  padding: 8px 30px 8px 11px;
  /* Tên model dài thì cắt bằng dấu … thay vì cắt ngang giữa chữ */
  text-overflow: ellipsis;
}

.messages {
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
  padding: clamp(16px, 3vw, 26px) var(--pad-x) 10px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: clamp(14px, 2.4vw, 22px);
  align-content: start;
}
/* Kiểu hiển thị theo lối các trợ lý lớn (ChatGPT/Claude): câu trả lời của AI là
   văn bản trôi thẳng trong cột đọc, không đóng khung — dễ đọc đoạn dài hơn; còn
   tin của người dùng là một bóng chat lệch phải để nhìn ra ngay ai đang nói. */
.msg {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr);
  gap: 10px;
  max-width: var(--reading-w);
  width: 100%;
  margin: 0 auto;
  animation: msg-in .32s var(--ease) both;
}
@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}
.msg .who {
  width: 30px; height: 30px;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 11px; font-weight: 700;
  letter-spacing: .02em;
  background: var(--bg-3);
  border: 1px solid var(--line-2);
  box-shadow: var(--sh-1);
}
/* Tin người dùng: không cần avatar, chỉ cần bóng chat lệch phải. */
.msg.user { grid-template-columns: minmax(0, 1fr); }
.msg.user .who { display: none; }
.msg .bubble { overflow-wrap: anywhere; }
.msg.ai .bubble { padding-top: 3px; }
.msg.user .bubble {
  background: linear-gradient(180deg, rgba(91, 140, 255, .16), rgba(139, 92, 255, .1));
  border: 1px solid rgba(91, 140, 255, .26);
  border-radius: var(--r);
  border-bottom-right-radius: 6px;
  padding: 11px 14px;
  margin-left: auto;
  max-width: min(76%, 600px);
  box-shadow: var(--sh-1);
}
.msg.error .bubble {
  border: 1px solid rgba(251, 111, 120, .38);
  border-radius: var(--r);
  background: linear-gradient(180deg, rgba(251, 111, 120, .12), rgba(30, 12, 16, .9));
  padding: 11px 14px;
}
.msg .bubble > :first-child { margin-top: 0; }
.msg .bubble > :last-child { margin-bottom: 0; }
.msg .bubble p { margin: 0 0 10px; }
.msg .bubble h1, .msg .bubble h2, .msg .bubble h3 {
  margin: 16px 0 8px;
  font-size: var(--t-1);
  line-height: 1.35;
}
.msg .bubble ul, .msg .bubble ol { margin: 8px 0; padding-left: 22px; }
.msg .bubble li { margin: 3px 0; }
.msg .bubble li > ul, .msg .bubble li > ol { margin: 4px 0 2px; }
.msg .bubble hr { border: 0; border-top: 1px solid var(--line-2); margin: 14px 0; }
.msg .bubble a { color: var(--brand-ink); text-decoration: underline; text-underline-offset: 2px; }
.msg .bubble blockquote {
  margin: 10px 0;
  padding: 2px 0 2px 13px;
  border-left: 2px solid rgba(91, 140, 255, .5);
  color: var(--muted);
}
.msg .bubble blockquote > :last-child { margin-bottom: 0; }

/* Khối code có thanh tiêu đề + nút sao chép, giống ChatGPT/Claude. */
.code-block {
  margin: 12px 0;
  border: 1px solid var(--line-2);
  border-radius: var(--r-sm);
  overflow: hidden;
  background: #05080e;
}
.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 8px 6px 12px;
  background: rgba(255, 255, 255, .035);
  border-bottom: 1px solid var(--line);
  font-size: var(--t--1);
  color: var(--faint);
  font-family: var(--mono);
}
.code-copy {
  border: 1px solid var(--line-2);
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-family: inherit;
  padding: 3px 9px;
  border-radius: 7px;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}
.code-copy:hover { color: var(--text); border-color: var(--brand); background: rgba(91, 140, 255, .12); }
.code-copy.done { color: var(--ok); border-color: rgba(63, 209, 152, .5); }
.msg .bubble pre {
  background: #05080e;
  border: 1px solid var(--line-2);
  border-radius: var(--r-sm);
  padding: 13px 14px;
  overflow-x: auto;
  margin: 12px 0;
  box-shadow: var(--sh-1) inset;
}
/* Trong .code-block thì khung/bo góc do chính .code-block lo. */
.msg .bubble .code-block pre { margin: 0; border: 0; border-radius: 0; box-shadow: none; }
/* Bảng rộng tự cuộn ngang trong bóng chat thay vì kéo giãn cột grid. */
.table-scroll { overflow-x: auto; margin: 10px 0; max-width: 100%; }
.table-scroll table { margin: 0; }
.msg .bubble code {
  font-family: var(--mono);
  font-size: .89em;
  font-variant-ligatures: none;
}
.msg .bubble :not(pre) > code {
  background: rgba(0, 0, 0, .5);
  border: 1px solid var(--line);
  padding: 1.5px 5px;
  border-radius: 6px;
  color: var(--brand-ink);
}
.msg .bubble table { margin: 10px 0; font-size: var(--t--1); }
.msg .meta {
  font-size: var(--t--1);
  color: var(--faint);
  margin-top: 7px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
}

/* Ba chấm "đang trả lời" */
.typing { display: inline-flex; align-items: center; gap: 5px; padding: 3px 0; }
.typing span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--brand);
  animation: bounce 1.25s infinite var(--ease);
}
.typing span:nth-child(2) { animation-delay: .16s; }
.typing span:nth-child(3) { animation-delay: .32s; }
@keyframes bounce {
  0%, 65%, 100% { opacity: .28; transform: translateY(0) scale(.9); }
  30%           { opacity: 1;   transform: translateY(-4px) scale(1); }
}

.welcome {
  text-align: center;
  color: var(--muted);
  padding: clamp(30px, 9vh, 70px) 16px;
  max-width: 560px;
  margin: 0 auto;
  animation: rise .5s var(--ease) both;
}
.welcome h3 {
  color: var(--text);
  margin: 0 0 8px;
  font-size: var(--t-2);
  font-weight: 700;
  letter-spacing: -.025em;
}
.welcome p { margin: 0; font-size: var(--t-0); }

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
  padding: 10px var(--pad-x) 4px;
  /* Cùng công thức với .messages (padding 2 bên + bề rộng khung đọc) nên mép ô
     nhập thẳng hàng tuyệt đối với mép bóng chat phía trên. Trước đây cộng cứng
     46px nên ô nhập lệch 3px và hẹp hơn cột tin nhắn 64px. */
  max-width: calc(var(--reading-w) + 2 * var(--pad-x));
  width: 100%;
  margin: 0 auto;
}
.composer textarea {
  resize: none;
  /* Chiều cao đúng một dòng chữ: 1.55em (line-height) + 24px padding + 2px
     viền. Để trình duyệt tự tính theo rows="1" thì ô bị hụt mất phần dưới của
     dòng chữ trên điện thoại (cỡ chữ bị ép 16px). */
  min-height: calc(1.55em + 26px);
  max-height: 180px;
  border-radius: var(--r);
  padding: 12px 14px;
  line-height: 1.55;
  background: rgba(0, 0, 0, .4);
  box-shadow: var(--sh-2);
}
.btn.send {
  width: 48px; height: 46px;
  padding: 0;
  font-size: 17px;
  border-radius: var(--r);
}
.hint {
  max-width: calc(var(--reading-w) + 2 * var(--pad-x));
  margin: 3px auto 0;
  padding: 0 var(--pad-x) max(10px, env(safe-area-inset-bottom));
  font-size: var(--t--1);
  color: var(--faint);
  min-height: 16px;
}

/* ====== Trang quản trị ====== */
.admin {
  max-width: 1320px;
  margin: 0 auto;
  padding: clamp(14px, 3vw, 26px) var(--pad-x) 40px;
  height: 100dvh;
  overflow-y: auto;
}
.admin-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 18px;
}
.admin-head h1 {
  margin: 0;
  font-size: var(--t-2);
  font-weight: 700;
  flex: 1;
  min-width: 0;
}
/* Nhóm nút bên phải đi liền một khối và luôn dạt phải, thay vì rơi lả tả từng
   nút một xuống các hàng khác nhau khi màn hình hẹp. */
.head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-left: auto;
}
/* Email admin không viết hoa toàn bộ như badge thường, dài thì cắt bằng dấu … */
#whoami {
  text-transform: none;
  letter-spacing: 0;
  max-width: min(46vw, 340px);
  overflow: hidden;
  text-overflow: ellipsis;
}
.admin-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.admin-tabs .tab {
  flex: 0 0 auto;
  padding: 9px 15px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  border-radius: 11px;
}
.admin-tabs .tab.active {
  background: linear-gradient(180deg, rgba(91, 140, 255, .2), rgba(139, 92, 255, .1));
  border-color: rgba(91, 140, 255, .38);
  color: var(--text);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(clamp(150px, 42vw, 200px), 1fr));
  gap: 12px;
}
.stat {
  position: relative;
  overflow: hidden;
  background: linear-gradient(170deg, rgba(20, 27, 41, .92), rgba(12, 17, 27, .92));
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 15px 16px;
  box-shadow: var(--sh-1);
}
.stat::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(139, 168, 255, .5), transparent);
}
.stat .label {
  font-size: var(--t--1);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  font-weight: 600;
}
.stat .value {
  font-size: clamp(22px, 1.2rem + 1.1vw, 30px);
  font-weight: 700;
  letter-spacing: -.03em;
  margin-top: 5px;
  font-variant-numeric: tabular-nums;
}
.stat .sub { font-size: var(--t--1); color: var(--faint); }
.card {
  background: linear-gradient(180deg, rgba(13, 18, 29, .92), rgba(10, 14, 22, .92));
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: clamp(13px, 2.4vw, 18px);
  margin-top: 16px;
  box-shadow: var(--sh-1);
}
.card > h3, .card-head h3 {
  margin: 0;
  font-size: var(--t-1);
  font-weight: 650;
}
.card > h3 { margin-bottom: 12px; }
.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.card-head h3 { flex: 1; min-width: 0; }
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr));
  gap: 0 14px;
}
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.table-wrap {
  overflow-x: auto;
  /* thanh cuộn ngang mượt trên điện thoại */
  -webkit-overflow-scrolling: touch;
  border-radius: var(--r-sm);
  /* Dấu hiệu "còn nội dung bên phải": hai lớp gradient nền (attachment local)
     trôi theo nội dung và che đi hai lớp bóng (attachment scroll) ghim ở mép
     khung. Cuộn hết sang một bên là bóng bên đó tự mất — CSS thuần, không JS.
     Không có nó, bảng nhiều cột trên điện thoại trông như bị cắt mất chữ. */
  background:
    linear-gradient(90deg,  #0b1018 34%, rgba(11, 16, 24, 0)) left center  / 36px 100% no-repeat local,
    linear-gradient(270deg, #0b1018 34%, rgba(11, 16, 24, 0)) right center / 36px 100% no-repeat local,
    radial-gradient(farthest-side at 0   50%, rgba(0, 0, 0, .5), rgba(0, 0, 0, 0)) left center  / 14px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, .5), rgba(0, 0, 0, 0)) right center / 14px 100% no-repeat scroll;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--t--1);
}
th, td {
  text-align: left;
  padding: 10px 11px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
th {
  color: var(--muted);
  font-weight: 650;
  font-size: var(--t--1);
  text-transform: uppercase;
  letter-spacing: .05em;
  white-space: nowrap;
  position: sticky;
  top: 0;
  background: var(--bg-2);
}
tbody tr { transition: background var(--speed) var(--ease); }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
@media (hover: hover) and (pointer: fine) {
  tbody tr:hover { background: rgba(255, 255, 255, .035); }
}

.bars { display: grid; gap: 7px; }
.bar-row {
  display: grid;
  /* Cột số liệu để `auto` (rộng theo nội dung). Trước đây kẹp cứng
     clamp(84px, 24vw, 124px) trong khi chuỗi "24 req · 15.048 tok · 5 lỗi" cần
     ~150px và có white-space: nowrap → chữ tràn ra ngoài rồi bị cắt mất, kể cả
     trên PC. */
  grid-template-columns: minmax(0, max-content) minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  font-size: var(--t--1);
  font-variant-numeric: tabular-nums;
}
.bar-row > :last-child { text-align: right; }
.bar {
  height: 10px;
  background: rgba(0, 0, 0, .45);
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: hidden;
}
.bar > i {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: var(--grad);
  box-shadow: 0 0 12px -2px var(--glow);
  transition: width .5s var(--ease);
}

/* ====== Hộp thoại ====== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(3, 5, 9, .74);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 40;
  animation: fade .2s var(--ease) both;
}
.modal {
  width: 100%;
  max-width: 580px;
  max-height: min(88dvh, 800px);
  overflow-y: auto;
  background: linear-gradient(170deg, rgba(22, 30, 45, .97), rgba(11, 16, 25, .97));
  border: 1px solid var(--line-2);
  border-radius: var(--r-lg);
  padding: clamp(17px, 3.4vw, 24px);
  box-shadow: var(--sh-3);
  animation: rise .26s var(--ease) both;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.modal h3 { margin: 0 0 6px; font-size: var(--t-1); font-weight: 650; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 22px;
  flex-wrap: wrap;
}
.check-list {
  display: grid;
  gap: 4px;
  max-height: 190px;
  overflow-y: auto;
  border: 1px solid var(--line-2);
  border-radius: var(--r-sm);
  padding: 10px;
  background: rgba(0, 0, 0, .35);
}
.check-list label {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0;
  padding: 4px 2px;
  color: var(--text);
  font-size: var(--t--1);
  font-weight: 450;
  cursor: pointer;
}
.check-list input, .inline-check input {
  width: auto;
  accent-color: var(--brand);
  cursor: pointer;
}
.inline-check {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-top: 14px;
  font-size: var(--t-0);
}

/* ====== Thông báo nổi ====== */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: linear-gradient(180deg, rgba(28, 37, 55, .98), rgba(16, 22, 34, .98));
  border: 1px solid var(--line-2);
  padding: 11px 19px;
  border-radius: 999px;
  z-index: 60;
  box-shadow: var(--sh-3);
  font-size: var(--t-0);
  max-width: min(92vw, 520px);
  animation: toast-in .28s var(--ease) both;
}
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 12px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}
.toast.err { border-color: rgba(251, 111, 120, .45); color: #ffc0c4; }
.toast.ok  { border-color: rgba(53, 211, 153, .45);  color: #a8f2d2; }

/* =========================================================================
   TỰ ĐIỀU CHỈNH THEO THIẾT BỊ
   ========================================================================= */

.only-mobile { display: none; }

/* ---- Tablet & điện thoại ngang: thu cột bên, ẩn thành ngăn kéo ---- */
@media (max-width: 900px) {
  :root { --sidebar-w: 100%; }

  .app { grid-template-columns: minmax(0, 1fr); }
  .only-mobile { display: inline-grid; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(84vw, 330px);
    z-index: 30;
    transform: translateX(-100%);
    transition: transform .26s var(--ease);
    box-shadow: var(--sh-3);
    border-right: 1px solid var(--line-2);
  }
  .sidebar.open { transform: translateX(0); }

  /* Lớp mờ phía sau ngăn kéo, bấm vào để đóng (xử lý ở chat.js) */
  #scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 29;
    background: rgba(3, 5, 9, .6);
    backdrop-filter: blur(3px);
    opacity: 0;
    pointer-events: none;
    transition: opacity .26s var(--ease);
  }
  #scrim.show { opacity: 1; pointer-events: auto; }

  .head-right select { min-width: 0; max-width: 42vw; }
}
/* ---- Điện thoại ---- */
@media (max-width: 640px) {
  :root {
    --reading-w: 100%;
    --r: 13px;
    --r-lg: 17px;
  }

  /* iOS Safari phóng to trang khi focus vào ô chữ nhỏ hơn 16px.
     Ép đúng 16px ở đây để không bị giật layout khi bấm vào ô nhập. */
  input, select, textarea { font-size: 16px; }

  /* Điện thoại: bỏ avatar, câu trả lời chiếm hết bề rộng như Claude/ChatGPT bản
     mobile. Cột avatar 26px + khe 8px ăn mất ~9% bề rộng đọc trên máy 360px. */
  .msg { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .msg .who { display: none; }
  .msg.user .bubble { padding: 10px 12px; max-width: 88%; }
  .msg.error .bubble { padding: 10px 12px; }
  .code-head { padding: 5px 6px 5px 10px; }
  .msg .bubble .code-block pre { padding: 11px 12px; }

  .chat-head { gap: 6px; padding-inline: 12px; }
  .chat-head h2 { font-size: var(--t-0); }

  .composer { gap: 8px; }
  .btn.send { width: 44px; height: 44px; }

  .admin-head h1 { flex: 1; font-size: var(--t-1); }
  /* Ba nút chia đều một hàng riêng: gọn hơn kiểu mỗi nút một hàng */
  .head-actions { width: 100%; }
  .head-actions > * { flex: 1; }
  .admin-tabs { gap: 5px; }
  .admin-tabs .tab { padding: 8px 12px; font-size: var(--t--1); }

  .modal { max-height: 92dvh; border-radius: var(--r-lg) var(--r-lg) 0 0; }
  .modal-backdrop { place-items: end center; padding: 0; }
  .modal-foot .btn { flex: 1; }

  /* Điện thoại: nhãn ngày + số liệu nằm một hàng, cột tiến trình xuống hàng
     dưới chiếm hết bề rộng. Nhồi cả ba vào một hàng thì số liệu bị cắt cụt. */
  .bars { gap: 12px; }
  .bar-row {
    grid-template-columns: minmax(0, max-content) minmax(0, 1fr);
    gap: 4px 8px;
  }
  .bar-row > :nth-child(1) { grid-area: 1 / 1; }
  .bar-row > :nth-child(3) { grid-area: 1 / 2; }
  .bar-row > .bar { grid-area: 2 / 1 / 3 / 3; }
}

/* ---- Màn rất hẹp (điện thoại nhỏ / chia đôi màn hình) ---- */
@media (max-width: 380px) {
  .head-right select { max-width: 36vw; }
  .session-item { padding: 9px; }
  .auth-card { padding: 18px 16px; }
}

/* ---- Điều khiển bằng ngón tay: vùng bấm tối thiểu 44px theo WCAG ---- */
@media (pointer: coarse) {
  .btn { padding: 12px 16px; min-height: 44px; }
  .btn.sm { padding: 8px 12px; min-height: 36px; }
  .icon-btn { width: 44px; height: 44px; }
  .tab { padding: 12px 10px; }
  .session-item { padding: 12px 11px; }
  .check-list label { padding: 7px 2px; }
}
/* ---- Màn hình ngang thấp (điện thoại nằm ngang): giảm khoảng trắng dọc ---- */
@media (max-height: 480px) and (orientation: landscape) {
  .auth-screen { place-items: start center; }
  .auth-card { padding: 16px 20px; }
  .auth-sub { margin-bottom: 12px; }
  .welcome { padding: 18px 16px; }
}

/* ---- Tôn trọng cài đặt giảm hiệu ứng của hệ thống ---- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---- Máy in / lưu PDF: bỏ nền tối, giữ chữ đen trên trắng ---- */
@media print {
  body { background: #fff; color: #000; height: auto; overflow: visible; }
  body::before, .sidebar, .composer, .hint, .chat-head, .toast, #scrim { display: none !important; }
  .app, .chat { display: block; height: auto; }
  .messages { overflow: visible; height: auto; display: block; }
  .msg { break-inside: avoid; margin-bottom: 12px; }
  .msg .bubble { border: 1px solid #bbb; background: #fff; box-shadow: none; padding: 10px 12px; }
  .code-copy { display: none; }
}



















