/* ==========================================================================
   Minh Dũng AI Chat — chat.css (v20261119f)
   Dark (mặc định) / Light theme · Responsive desktop / tablet / mobile
   Hoàn toàn tương thích chat.html (#7) & quy ước chat.js (#8)
   ========================================================================== */

/* ---------- 1. CSS Variables & Theming ---------- */
:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg-app: #0b0f19;
  --bg-surface: #0f172a;
  --bg-elevated: #1e293b;
  --bg-hover: rgba(148, 163, 184, 0.10);
  --bg-active: rgba(56, 189, 248, 0.14);
  --bg-input: #0f172a;
  --bg-overlay: rgba(2, 6, 23, 0.68);
  --border: #334155;
  --border-soft: rgba(51, 65, 85, 0.65);
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --accent: #38bdf8;
  --accent-strong: #0284c7;
  --accent-2: #6366f1;
  --accent-3: #14b8a6;
  --accent-contrast: #ffffff;
  --accent-soft: rgba(56, 189, 248, 0.14);
  --gradient-accent: linear-gradient(135deg, #0284c7 0%, #6366f1 100%);
  --gradient-user: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  --focus-ring: 0 0 0 3px rgba(56, 189, 248, 0.45);

  --bubble-assistant-bg: #1e293b;
  --bubble-assistant-border: #334155;
  --bubble-user-text: #ffffff;

  --code-bg: #0d1117;
  --code-header-bg: #161b22;
  --code-border: #30363d;
  --code-text: #e6edf3;
  --inline-code-bg: rgba(148, 163, 184, 0.16);
  --inline-code-text: #e2e8f0;

  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #38bdf8;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 6px 20px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.55);
  --shadow-input: 0 8px 30px rgba(0, 0, 0, 0.35), 0 0 0 1px rgba(51, 65, 85, 0.6);

  --scrollbar-thumb: rgba(148, 163, 184, 0.28);
  --scrollbar-thumb-hover: rgba(148, 163, 184, 0.45);
}

[data-theme="light"] {
  color-scheme: light;
  --bg-app: #f8fafc;
  --bg-surface: #ffffff;
  --bg-elevated: #f1f5f9;
  --bg-hover: rgba(15, 23, 42, 0.05);
  --bg-active: rgba(2, 132, 199, 0.10);
  --bg-input: #ffffff;
  --bg-overlay: rgba(15, 23, 42, 0.45);
  --border: #e2e8f0;
  --border-soft: rgba(226, 232, 240, 0.9);
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  --accent: #0284c7;
  --accent-strong: #0369a1;
  --accent-soft: rgba(2, 132, 199, 0.10);
  --focus-ring: 0 0 0 3px rgba(2, 132, 199, 0.30);

  --bubble-assistant-bg: #ffffff;
  --bubble-assistant-border: #e2e8f0;

  --inline-code-bg: rgba(15, 23, 42, 0.07);
  --inline-code-text: #be185d;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 6px 20px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.18);
  --shadow-input: 0 8px 30px rgba(15, 23, 42, 0.08), 0 0 0 1px rgba(226, 232, 240, 1);

  --scrollbar-thumb: rgba(100, 116, 139, 0.28);
  --scrollbar-thumb-hover: rgba(100, 116, 139, 0.45);
}

:root {
  --header-h: 60px;
  --sidebar-w: 280px;
  --content-max: 840px;
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 999px;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono: "JetBrains Mono", "Fira Code", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* ---------- 2. Reset & Base ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.25s var(--ease), color 0.25s var(--ease);
}

/* Quy ước ẩn hiện chuẩn */
[hidden],
.hidden {
  display: none !important;
}

.visually-hidden,
.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;
}

/* Skip link hỗ trợ a11y */
.skip-link {
  position: absolute;
  top: -100px;
  left: 16px;
  z-index: 1000;
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-sm);
  font-weight: 600;
  text-decoration: none;
  transition: top 0.2s var(--ease);
}

.skip-link:focus {
  top: 16px;
}

img,
svg {
  max-width: 100%;
}

svg {
  flex-shrink: 0;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

button,
input,
textarea,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  padding: 0;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

:focus {
  outline: none;
}

:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

::selection {
  background: rgba(56, 189, 248, 0.32);
}

/* Scrollbar mỏng */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) transparent;
}

*::-webkit-scrollbar {
  width: 7px;
  height: 7px;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

*::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: var(--radius-full);
  border: 2px solid transparent;
  background-clip: content-box;
}

*::-webkit-scrollbar-thumb:hover {
  background: var(--scrollbar-thumb-hover);
  background-clip: content-box;
}

/* ---------- 3. App Shell ---------- */
.app-shell,
#app-shell {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

/* ---------- 4. Header ---------- */
.chat-header,
#chat-header {
  position: relative;
  z-index: 30;
  flex: 0 0 auto;
  height: calc(var(--header-h) + var(--safe-top));
  padding: var(--safe-top) calc(16px + var(--safe-right)) 0 calc(16px + var(--safe-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: color-mix(in srgb, var(--bg-surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}

.header-left,
.header-center,
.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.header-left {
  flex: 0 1 auto;
}

.header-center {
  flex: 1 1 auto;
  justify-content: center;
}

.header-right {
  flex: 0 0 auto;
}

/* Brand */
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--text-primary);
  text-decoration: none !important;
}

.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  object-fit: cover;
}

.brand-title,
#brand-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(135deg, var(--text-primary) 30%, var(--accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* Icon button & buttons */
.icon-btn,
.sidebar-toggle {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  transition: background-color 0.2s var(--ease), border-color 0.2s var(--ease), color 0.2s var(--ease);
}

.icon-btn:hover,
.sidebar-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

/* Toggle sidebar: ẩn mặc định trên desktop */
#sidebar-toggle-btn,
.sidebar-toggle {
  display: none;
}

/* Model Button (Header center) */
.model-btn,
#current-model-btn {
  height: 36px;
  max-width: 280px;
  padding: 0 12px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 13.5px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s var(--ease), border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.model-btn:hover,
#current-model-btn:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
}

.model-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  flex-shrink: 0;
}

.model-name,
#current-model-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-btn .chevron {
  width: 14px;
  height: 14px;
  color: var(--text-muted);
  transition: transform 0.2s var(--ease);
  flex-shrink: 0;
}

/* KC Badge */
.kc-badge,
#kc-balance-badge {
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #f59e0b;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.32);
  white-space: nowrap;
}

[data-theme="light"] .kc-badge,
[data-theme="light"] #kc-balance-badge {
  color: #b45309;
  background: rgba(245, 158, 11, 0.12);
}

/* Nạp KC Link */
.topup-link,
#topup-kc-link {
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius-full);
  background: var(--gradient-accent);
  color: #fff;
  font-size: 12.5px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  text-decoration: none !important;
  box-shadow: 0 3px 12px rgba(2, 132, 199, 0.35);
  transition: filter 0.15s var(--ease), transform 0.15s var(--ease);
}

.topup-link:hover,
#topup-kc-link:hover {
  filter: brightness(1.1);
  color: #fff;
}

.topup-link:active,
#topup-kc-link:active {
  transform: scale(0.97);
}

/* Key status button */
.key-status-btn,
#api-key-status-btn {
  height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.2s var(--ease), border-color 0.2s var(--ease), color 0.2s var(--ease);
}

.key-status-btn:hover,
#api-key-status-btn:hover {
  color: var(--text-primary);
  border-color: var(--text-muted);
  background: var(--bg-hover);
}

/* Key status state: ready / has-key vs missing / no-key */
.key-status-btn[data-state="ready"],
.key-status-btn.has-key,
#api-key-status-btn.has-key {
  color: var(--success);
  border-color: rgba(34, 197, 94, 0.4);
  background: rgba(34, 197, 94, 0.08);
}

.key-status-btn[data-state="missing"],
.key-status-btn.no-key,
#api-key-status-btn.no-key {
  color: var(--warning);
  border-color: rgba(245, 158, 11, 0.4);
  background: rgba(245, 158, 11, 0.08);
}

/* Theme toggle icon switching */
[data-theme="dark"] #theme-toggle-btn .icon-moon {
  display: none;
}
[data-theme="dark"] #theme-toggle-btn .icon-sun {
  display: inline-block;
}
[data-theme="light"] #theme-toggle-btn .icon-sun {
  display: none;
}
[data-theme="light"] #theme-toggle-btn .icon-moon {
  display: inline-block;
}

/* ---------- 5. Main Layout & Sidebar ---------- */
.chat-container,
#chat-container {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

/* Sidebar */
.chat-sidebar,
#chat-sidebar {
  position: relative;
  z-index: 20;
  flex: 0 0 var(--sidebar-w);
  width: var(--sidebar-w);
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px 12px calc(12px + var(--safe-bottom)) calc(12px + var(--safe-left));
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  transition: transform 0.3s ease, background-color 0.25s var(--ease);
}

.sidebar-top {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.new-chat-btn,
#new-chat-btn {
  width: 100%;
  height: 42px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  background: var(--gradient-accent);
  box-shadow: 0 4px 16px rgba(2, 132, 199, 0.32);
  transition: transform 0.15s var(--ease), box-shadow 0.2s var(--ease), filter 0.2s var(--ease);
}

.new-chat-btn:hover,
#new-chat-btn:hover {
  filter: brightness(1.08);
  box-shadow: 0 6px 22px rgba(2, 132, 199, 0.45);
}

.new-chat-btn:active,
#new-chat-btn:active {
  transform: scale(0.98);
}

.new-chat-btn svg {
  width: 18px;
  height: 18px;
}

/* Search Box */
.search-box {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.search-box svg {
  position: absolute;
  left: 12px;
  width: 16px;
  height: 16px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-box input[type="search"] {
  width: 100%;
  height: 38px;
  padding: 0 12px 0 36px;
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  border: 1px solid transparent;
  color: var(--text-primary);
  font-size: 13.5px;
  transition: border-color 0.2s var(--ease), background-color 0.2s var(--ease);
}

.search-box input[type="search"]:focus {
  border-color: var(--accent);
  background-color: var(--bg-input);
}

/* Conversation navigation */
.conversation-nav {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.conversation-list,
#conversation-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0 -4px;
  padding: 0 4px;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13.5px;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease);
}

.conversation-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.conversation-item.active {
  background: var(--bg-active);
  color: var(--text-primary);
  font-weight: 500;
}

.conversation-item.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 8px;
  bottom: 8px;
  width: 3px;
  border-radius: 3px;
  background: var(--accent);
}

.conversation-item svg {
  width: 16px;
  height: 16px;
  opacity: 0.7;
}

.conversation-info {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  justify-content: center;
}

.conversation-title {
  display: block;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.35;
}

.conversation-meta,
.conversation-time {
  display: block;
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}

/* Fallback khi title và meta cùng cấp không bọc trong .conversation-info */
.conversation-item > .conversation-title {
  flex: 1 1 auto;
  min-width: 0;
  margin-right: 8px;
}

.conversation-item > .conversation-meta,
.conversation-item > .conversation-time {
  margin-left: auto;
  padding-left: 6px;
  flex-shrink: 0;
}

.conversation-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.15s var(--ease);
}

.conversation-item:hover .conversation-actions,
.conversation-item.active .conversation-actions {
  opacity: 1;
}

.conversation-actions button,
.conv-action-btn {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease);
}

.conversation-actions button:hover,
.conv-action-btn:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.12);
}

.conversation-empty,
#conversation-empty {
  padding: 24px 12px;
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
}

/* Sidebar Footer */
.sidebar-footer {
  flex: 0 0 auto;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sidebar-settings-btn,
.sidebar-model-btn,
.sidebar-clear-btn,
#sidebar-settings-btn,
#sidebar-model-btn,
#clear-all-conversations-btn {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-secondary);
  font-size: 13.5px;
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease);
}

.sidebar-settings-btn:hover,
.sidebar-model-btn:hover,
#sidebar-settings-btn:hover,
#sidebar-model-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.sidebar-clear-btn:hover,
#clear-all-conversations-btn:hover {
  background: rgba(239, 68, 68, 0.12);
  color: #ef4444;
}

.sidebar-footer svg {
  width: 18px;
  height: 18px;
}

.app-version,
#app-version {
  margin: 6px 0 0;
  padding: 0 10px;
  font-size: 11.5px;
  color: var(--text-muted);
}

/* Backdrop Mobile */
.sidebar-backdrop,
#sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: var(--bg-overlay);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

/* ---------- 6. Chat Main Area ---------- */
.chat-main,
#chat-main {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-app);
}

/* Empty State */
.empty-state,
#empty-state {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px 20px;
  text-align: center;
  animation: fadeInUp 0.5s var(--ease-out) both;
}

.empty-logo {
  width: 72px;
  height: 72px;
  margin-bottom: 12px;
  border-radius: 20px;
  box-shadow: 0 12px 40px rgba(2, 132, 199, 0.4);
  animation: floatY 4s ease-in-out infinite;
  object-fit: cover;
}

.empty-title,
#empty-state-title {
  margin: 0;
  max-width: 620px;
  font-size: clamp(22px, 3.2vw, 30px);
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.02em;
  color: var(--text-primary);
}

.empty-subtitle {
  margin: 0 0 20px;
  max-width: 520px;
  font-size: 15px;
  color: var(--text-secondary);
}

/* Prompt grid & cards */
.prompt-grid {
  width: 100%;
  max-width: 680px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.prompt-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  min-height: 84px;
  padding: 14px 16px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-primary);
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  transition: transform 0.2s var(--ease), border-color 0.2s var(--ease),
    box-shadow 0.2s var(--ease), background-color 0.2s var(--ease);
}

.prompt-card::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--gradient-accent);
  opacity: 0;
  transition: opacity 0.2s var(--ease);
  pointer-events: none;
}

.prompt-card:hover {
  transform: translateY(-2px) scale(1.02);
  border-color: var(--accent);
  box-shadow: 0 8px 24px rgba(2, 132, 199, 0.18), 0 0 0 1px var(--accent-soft);
}

.prompt-card:hover::before {
  opacity: 0.06;
}

.prompt-card:active {
  transform: scale(0.99);
}

.prompt-card > * {
  position: relative;
}

.prompt-icon {
  font-size: 20px;
  line-height: 1;
  margin-bottom: 4px;
}

.prompt-title {
  margin: 0;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text-primary);
}

.prompt-desc {
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-secondary);
}

/* ---------- 7. Messages Container ---------- */
.messages-container,
#messages-container {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  padding: 20px max(20px, calc((100% - var(--content-max)) / 2)) 28px;
}

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto 22px;
  animation: messageIn 0.3s var(--ease-out) both;
}

.message-avatar {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-secondary);
  box-shadow: var(--shadow-sm);
}

.message-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.message-avatar svg {
  width: 20px;
  height: 20px;
}

.message-row.assistant .message-avatar {
  background: var(--gradient-accent);
  border-color: transparent;
  color: #fff;
}

.message-body {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.message-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 6px;
}

.message-images img,
.message-content img {
  display: block;
  max-width: min(100%, 360px);
  max-height: 360px;
  border-radius: var(--radius-md);
  object-fit: cover;
  border: 1px solid var(--border);
  cursor: zoom-in;
}

.message-content {
  position: relative;
  max-width: 100%;
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  font-size: 15px;
  line-height: 1.6;
  word-break: break-word;
  overflow-wrap: anywhere;
  color: var(--text-primary);
}

/* Assistant message bubble */
.message-row.assistant .message-content,
.message-row:not(.user) .message-content {
  background: var(--bubble-assistant-bg);
  border: 1px solid var(--bubble-assistant-border);
  border-top-left-radius: 6px;
  box-shadow: var(--shadow-sm);
}

/* User message bubble */
.message-row.user {
  flex-direction: row-reverse;
}

.message-row.user .message-body {
  align-items: flex-end;
}

.message-row.user .message-content {
  max-width: min(85%, 640px);
  background: var(--gradient-user);
  color: var(--bubble-user-text);
  border: none;
  border-top-right-radius: 6px;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.28);
  white-space: pre-wrap;
}

.message-row.user .message-content a {
  color: #dbeafe;
  text-decoration: underline;
}

.message-row.user .message-content code {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}

.message-row.user .message-avatar {
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  border-color: transparent;
  color: #fff;
}

/* Error message */
.message-row.error .message-content,
.message-error {
  background: rgba(239, 68, 68, 0.1) !important;
  border: 1px solid rgba(239, 68, 68, 0.35) !important;
  color: var(--text-primary);
}

.error-action-btn {
  margin-top: 8px;
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  background: var(--danger);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* Message actions (copy / regenerate) */
.message-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 28px;
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity 0.2s var(--ease), transform 0.2s var(--ease);
}

.message-row:hover .message-actions,
.message-row:focus-within .message-actions,
.message-row:last-child .message-actions {
  opacity: 0.75;
  transform: translateY(0);
}

.message-actions:hover {
  opacity: 1 !important;
}

.msg-action-btn,
.message-actions button {
  height: 28px;
  min-width: 28px;
  padding: 0 8px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease);
}

.msg-action-btn:hover,
.message-actions button:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.msg-action-btn.copied,
.message-actions button.copied {
  color: var(--success);
}

.msg-action-btn svg,
.message-actions svg {
  width: 15px;
  height: 15px;
}

/* Typing indicator */
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 2px;
}

.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typingBounce 1.3s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.3s;
}

/* Streaming cursor */
.stream-cursor,
.streaming-cursor {
  display: inline-block;
  width: 8px;
  height: 1.1em;
  margin-left: 2px;
  vertical-align: text-bottom;
  border-radius: 2px;
  background: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}

.message-row.streaming .message-content > :last-child::after {
  content: "";
  display: inline-block;
  width: 8px;
  height: 1.05em;
  margin-left: 3px;
  vertical-align: text-bottom;
  border-radius: 2px;
  background: var(--accent);
  animation: blink 1s steps(2, start) infinite;
}

.message-row.streaming .message-content > pre:last-child::after,
.message-row.streaming .message-content > table:last-child::after,
.message-row.streaming .message-content > .table-wrapper:last-child::after {
  content: none;
}

/* Scroll to bottom button */
.scroll-bottom-btn,
#scroll-bottom-btn {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 12px);
  z-index: 5;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-50%);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  box-shadow: var(--shadow-md);
  transition: color 0.15s var(--ease), background-color 0.15s var(--ease);
}

.scroll-bottom-btn:hover,
#scroll-bottom-btn:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

/* ---------- 8. Markdown & Code Typography ---------- */
.message-content > :first-child {
  margin-top: 0;
}

.message-content > :last-child {
  margin-bottom: 0;
}

.message-content p {
  margin: 0 0 0.85em;
}

.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4,
.message-content h5,
.message-content h6 {
  margin: 1.3em 0 0.55em;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.01em;
  color: var(--text-primary);
}

.message-content h1 { font-size: 1.5em; }
.message-content h2 {
  font-size: 1.3em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
}
.message-content h3 { font-size: 1.15em; }
.message-content h4,
.message-content h5,
.message-content h6 { font-size: 1em; }

.message-content strong,
.message-content b {
  font-weight: 650;
}

.message-content a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: rgba(56, 189, 248, 0.4);
  text-underline-offset: 2px;
}

.message-content a:hover {
  text-decoration-color: currentColor;
}

.message-content ul,
.message-content ol {
  margin: 0 0 0.85em;
  padding-left: 1.6em;
}

.message-content li {
  margin: 0.3em 0;
}

.message-content li > ul,
.message-content li > ol {
  margin: 0.25em 0;
}

.message-content blockquote {
  margin: 0 0 0.85em;
  padding: 8px 16px;
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--accent-soft);
  color: var(--text-secondary);
  font-style: italic;
}

.message-content blockquote p:last-child {
  margin-bottom: 0;
}

.message-content hr {
  margin: 1.4em 0;
  border: none;
  border-top: 1px solid var(--border);
}

/* Inline code */
.message-content code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--inline-code-bg);
  color: var(--inline-code-text);
  word-break: break-word;
}

/* Code block pre & header */
.message-content pre {
  position: relative;
  margin: 0 0 1em;
  padding: 0;
  border-radius: 8px;
  border: 1px solid var(--code-border);
  background: var(--code-bg);
  color: var(--code-text);
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: 13.5px;
  line-height: 1.6;
  tab-size: 4;
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 38px;
  padding: 0 8px 0 14px;
  background: var(--code-header-bg);
  border-bottom: 1px solid var(--code-border);
  color: #8b949e;
  font-family: var(--font-mono);
  font-size: 12px;
}

.code-header span {
  text-transform: lowercase;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #8b949e;
}

.copy-code-btn {
  height: 26px;
  padding: 0 10px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  color: #8b949e;
  background: transparent;
  border: 1px solid transparent;
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease), border-color 0.15s var(--ease);
}

.copy-code-btn:hover {
  color: #e6edf3;
  background: rgba(110, 118, 129, 0.2);
  border-color: var(--code-border);
}

.copy-code-btn.copied {
  color: #3fb950;
}

.message-content pre code {
  display: block;
  padding: 14px 16px;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font-size: inherit;
  white-space: pre;
  word-break: normal;
  overflow-wrap: normal;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Syntax Highlighting (Tokens) */
.tok-keyword {
  color: #ff7b72;
  font-weight: 600;
}
.tok-string {
  color: #a5d6ff;
}
.tok-comment {
  color: #8b949e;
  font-style: italic;
}
.tok-number {
  color: #79c0ff;
}
.tok-tag {
  color: #7ee787;
  font-weight: 600;
}

/* Light Theme Syntax Highlighting */
[data-theme="light"] .tok-keyword,
html.theme-light .tok-keyword {
  color: #cf222e;
}
[data-theme="light"] .tok-string,
html.theme-light .tok-string {
  color: #0a3069;
}
[data-theme="light"] .tok-comment,
html.theme-light .tok-comment {
  color: #6e7781;
}
[data-theme="light"] .tok-number,
html.theme-light .tok-number {
  color: #0550ae;
}
[data-theme="light"] .tok-tag,
html.theme-light .tok-tag {
  color: #116329;
}

/* LaTeX Math Styling */
.math-inline {
  font-family: 'KaTeX_Math', 'Cambria Math', 'Latin Modern Math', 'Times New Roman', serif;
  font-style: italic;
  padding: 1px 6px;
  margin: 0 2px;
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.15);
  border-radius: 4px;
  font-size: 1.05em;
  color: var(--accent);
  white-space: nowrap;
}

.math-block {
  font-family: 'KaTeX_Math', 'Cambria Math', 'Latin Modern Math', 'Times New Roman', serif;
  font-style: italic;
  padding: 14px 18px;
  margin: 1em 0;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  overflow-x: auto;
  font-size: 1.12em;
  color: var(--text);
  line-height: 1.7;
}

/* Tables */
.table-wrapper {
  width: 100%;
  margin: 0 0 1em;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  -webkit-overflow-scrolling: touch;
}

.message-content table {
  display: block;
  width: max-content;
  max-width: 100%;
  margin: 0 0 1em;
  overflow-x: auto;
  border-collapse: collapse;
  border-spacing: 0;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.table-wrapper table {
  display: table;
  width: 100%;
  max-width: none;
  margin: 0;
  border: none;
  border-radius: 0;
}

.message-content th,
.message-content td {
  padding: 9px 14px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border-soft);
}

.message-content th:last-child,
.message-content td:last-child {
  border-right: none;
}

.message-content tr:last-child td {
  border-bottom: none;
}

.message-content th {
  font-weight: 700;
  white-space: nowrap;
  color: var(--text-primary);
  background: var(--bg-elevated);
}

[data-theme="dark"] .message-content th {
  background: rgba(15, 23, 42, 0.7);
}

.message-content tbody tr:hover {
  background: var(--bg-hover);
}

/* ---------- 9. Chat Input Area ---------- */
.chat-input-area,
#chat-input-area {
  position: relative;
  flex: 0 0 auto;
  width: 100%;
  padding: 8px 20px calc(12px + var(--safe-bottom));
  background: linear-gradient(to bottom, transparent, var(--bg-app) 28%);
}

.chat-input-area > * {
  max-width: var(--content-max);
  margin-left: auto;
  margin-right: auto;
}

/* Image & File preview - nằm trực quan bên trong ô nhập chat */
.image-preview-container,
#image-preview-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 4px 6px 8px;
  margin-bottom: 4px;
  border-bottom: 1px dashed var(--border);
  animation: fadeInUp 0.2s var(--ease-out) both;
}

.image-preview-container[hidden],
#image-preview-container[hidden] {
  display: none !important;
}

#image-preview-img {
  display: block;
  width: 52px;
  height: 52px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-xs);
}

.file-preview-card,
#file-preview-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  background: var(--bg-surface-elevated, rgba(255, 255, 255, 0.06));
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  min-width: 140px;
  max-width: calc(100% - 36px);
}

.file-preview-card[hidden],
#file-preview-card[hidden] {
  display: none !important;
}

.file-preview-icon,
#file-preview-icon {
  font-size: 24px;
  line-height: 1;
  flex-shrink: 0;
}

.file-preview-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.file-preview-name,
#file-preview-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-preview-size,
#file-preview-size {
  font-size: 11px;
  color: var(--text-muted);
}

.message-attachment-file {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  margin-bottom: 8px;
  background: var(--bg-surface-elevated, rgba(255, 255, 255, 0.08));
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  max-width: 100%;
  box-shadow: var(--shadow-sm);
}

.message-attachment-file .file-icon {
  font-size: 22px;
  line-height: 1;
  flex-shrink: 0;
}

.message-attachment-file .file-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.message-attachment-file .file-name {
  font-weight: 600;
  color: var(--text-primary);
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-attachment-file .file-size {
  font-size: 11px;
  color: var(--text-muted);
}

.remove-image-btn,
#remove-image-btn {
  position: static;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  line-height: 1;
  color: var(--text-muted);
  background: var(--bg-hover);
  border: 1px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
  margin-left: auto;
  transition: background-color 0.15s var(--ease), color 0.15s var(--ease), transform 0.15s var(--ease);
}

.remove-image-btn:hover,
#remove-image-btn:hover {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
  transform: scale(1.08);
}

/* Chat form & input */
.chat-form,
#chat-form {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 6px 8px 8px;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  box-shadow: var(--shadow-input);
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.chat-form-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
}

.chat-form:focus-within {
  border-color: var(--accent);
  box-shadow: var(--shadow-input), var(--focus-ring);
}

#chat-input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 40px;
  max-height: 200px;
  padding: 9px 6px;
  border: none;
  outline: none;
  resize: none;
  overflow-y: auto;
  background: transparent;
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  box-shadow: none !important;
  scrollbar-width: thin;
}

#chat-input:focus,
#chat-input:focus-visible {
  outline: none;
  box-shadow: none;
}

#chat-input::-webkit-scrollbar {
  width: 4px;
}

.attach-btn,
.send-btn,
.stop-btn,
#attach-image-btn,
#send-btn,
#stop-btn {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s var(--ease), color 0.2s var(--ease),
    transform 0.15s var(--ease), box-shadow 0.2s var(--ease), opacity 0.2s var(--ease);
}

#attach-image-btn {
  color: var(--text-secondary);
  background: transparent;
}

#attach-image-btn:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

#attach-image-btn.is-text-only,
#attach-image-btn.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#attach-image-btn.is-text-only:hover,
#attach-image-btn.disabled:hover {
  color: var(--text-muted);
  background: transparent;
  transform: none;
}

#send-btn {
  color: #fff;
  background: var(--gradient-accent);
  box-shadow: 0 4px 14px rgba(2, 132, 199, 0.4);
}

#send-btn:hover:not(:disabled) {
  transform: scale(1.06);
  box-shadow: 0 6px 20px rgba(2, 132, 199, 0.5);
}

#send-btn:active:not(:disabled) {
  transform: scale(0.95);
}

#send-btn:disabled {
  opacity: 1;
  color: var(--text-muted);
  background: var(--bg-elevated);
  box-shadow: none;
}

#stop-btn {
  color: var(--bg-app);
  background: var(--text-primary);
  animation: pulseRing 1.8s ease-out infinite;
}

#stop-btn:hover {
  transform: scale(1.06);
}

/* Trạng thái streaming của toàn trang */
body.is-streaming #send-btn {
  display: none !important;
}

body.is-streaming #stop-btn {
  display: inline-flex !important;
}

.input-disclaimer {
  display: block;
  margin: 8px 0 0;
  text-align: center;
  font-size: 11.5px;
  line-height: 1.4;
  color: var(--text-muted);
}

/* ---------- 10. Modals ---------- */
.modal,
#api-key-modal,
#model-picker-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px calc(16px + var(--safe-right)) calc(20px + var(--safe-bottom)) calc(16px + var(--safe-left));
  animation: fadeIn 0.2s var(--ease) both;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: var(--bg-overlay);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.modal-dialog {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 480px;
  max-height: min(86dvh, 720px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-primary);
  box-shadow: var(--shadow-lg);
  animation: scaleUp 0.25s var(--ease-out) both;
}

.modal-dialog-lg {
  max-width: 560px;
}

.modal-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text-muted);
  border: 1px solid transparent;
}

.modal-close:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.modal-tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 14px 20px 0;
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
}

.modal-tab {
  flex: 1 1 0;
  height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-secondary);
  border: none;
  cursor: pointer;
  transition: background-color 0.2s var(--ease), color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.modal-tab:hover {
  color: var(--text-primary);
}

.modal-tab.active,
.modal-tab[aria-selected="true"] {
  color: var(--text-primary);
  background: var(--bg-surface);
  box-shadow: var(--shadow-sm);
}

.modal-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.tab-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-hint {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text-muted);
}

.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.key-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.text-input,
#api-key-input {
  width: 100%;
  height: 42px;
  padding: 0 42px 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 14px;
  letter-spacing: 0.02em;
  transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.text-input:focus,
#api-key-input:focus {
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}

.toggle-key-visibility {
  position: absolute;
  right: 6px;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.toggle-key-visibility:hover {
  color: var(--text-primary);
}

.checkbox-row {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
}

.checkbox-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent-strong);
  cursor: pointer;
}

/* Account keys list */
.account-keys-list,
#account-keys-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  list-style: none;
}

.account-key-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-app);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s var(--ease), background-color 0.15s var(--ease);
}

.account-key-item:hover {
  border-color: var(--accent);
}

.account-key-item.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 1px var(--accent);
}

/* Modal Footer */
.modal-footer {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border);
}

.get-key-link {
  font-size: 13px;
  color: var(--accent);
  font-weight: 500;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn {
  height: 40px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: background-color 0.2s var(--ease), border-color 0.2s var(--ease),
    color 0.2s var(--ease), transform 0.15s var(--ease), box-shadow 0.2s var(--ease);
}

.btn:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--text-muted);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-primary,
#save-key-btn {
  background: var(--gradient-accent);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 4px 14px rgba(2, 132, 199, 0.35);
}

.btn-primary:hover:not(:disabled),
#save-key-btn:hover:not(:disabled) {
  filter: brightness(1.08);
  box-shadow: 0 6px 20px rgba(2, 132, 199, 0.45);
}

.btn-ghost,
#close-key-modal-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled),
#close-key-modal-btn:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* Model picker search & list */
.model-search-box {
  margin-bottom: 8px;
}

.model-filter-tabs,
#model-filter-tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  padding: 2px 2px 8px;
  margin-bottom: 8px;
  scrollbar-width: none;
}

.model-filter-tabs::-webkit-scrollbar {
  display: none;
}

.model-filter-tab {
  flex: 0 0 auto;
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius-full);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s var(--ease);
}

.model-filter-tab:hover {
  color: var(--text-primary);
  border-color: var(--accent);
  background: var(--bg-hover);
}

.model-filter-tab.active,
.model-filter-tab[aria-selected="true"] {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(2, 132, 199, 0.3);
}

.model-groups {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.model-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.model-group-title {
  position: sticky;
  top: -18px;
  z-index: 1;
  margin: 0;
  padding: 8px 4px 6px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  background: var(--bg-surface);
}

.model-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.model-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  text-align: left;
  cursor: pointer;
  color: var(--text-primary);
  transition: background-color 0.15s var(--ease), border-color 0.15s var(--ease);
}

.model-item:hover {
  background: var(--bg-hover);
}

.model-item.selected,
.model-item.active {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.model-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-id {
  font-size: 12px;
  color: var(--text-muted);
}

.model-price {
  font-size: 12px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.model-badge {
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  color: var(--accent);
  background: var(--accent-soft);
}

.model-badge.free {
  color: var(--success);
  background: rgba(34, 197, 94, 0.12);
}

.model-badge.payg {
  color: #a78bfa;
  background: rgba(167, 139, 250, 0.14);
}

.model-badge.model-badge-vision {
  color: #38bdf8;
  background: rgba(56, 189, 248, 0.12);
  border: 1px solid rgba(56, 189, 248, 0.28);
}

.model-badge.model-badge-text {
  color: #94a3b8;
  background: rgba(148, 163, 184, 0.1);
  border: 1px solid rgba(148, 163, 184, 0.2);
}

[data-theme="light"] .model-badge.model-badge-vision {
  color: #0284c7;
  background: #e0f2fe;
  border-color: #bae6fd;
}

[data-theme="light"] .model-badge.model-badge-text {
  color: #64748b;
  background: #f1f5f9;
  border-color: #e2e8f0;
}

.model-empty,
#model-empty {
  padding: 24px 12px;
  text-align: center;
  font-size: 13.5px;
  color: var(--text-muted);
}

/* ---------- 11. Toasts ---------- */
.toast-container,
#toast-container {
  position: fixed;
  top: calc(var(--header-h) + var(--safe-top) + 12px);
  right: calc(16px + var(--safe-right));
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  padding: 12px 14px 12px 16px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  border-left: 4px solid var(--info);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.45;
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  animation: toastIn 0.35s var(--ease-out) both, toastOut 0.4s var(--ease) 3.6s forwards;
}

.toast.toast-success { border-left-color: var(--success); }
.toast.toast-error { border-left-color: var(--danger); }
.toast.toast-warning { border-left-color: var(--warning); }
.toast.toast-info { border-left-color: var(--info); }

.toast-message {
  flex: 1 1 auto;
  min-width: 0;
  word-break: break-word;
}

.toast-close {
  flex: 0 0 auto;
  margin: -2px -4px 0 4px;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.toast-close:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

/* Noscript warning */
.noscript-warning {
  padding: 16px;
  text-align: center;
  background: var(--danger);
  color: #fff;
  font-weight: 600;
}

/* ---------- 12. Animations ---------- */
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes scaleUp {
  from { opacity: 0; transform: translateY(8px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes messageIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(110%); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateX(0); }
  to { opacity: 0; transform: translateX(30%); visibility: hidden; }
}

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-5px); opacity: 1; }
}

@keyframes floatY {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes pulseRing {
  0% { box-shadow: 0 0 0 0 rgba(148, 163, 184, 0.45); }
  70% { box-shadow: 0 0 0 8px rgba(148, 163, 184, 0); }
  100% { box-shadow: 0 0 0 0 rgba(148, 163, 184, 0); }
}

/* ---------- 13. Responsive ---------- */

/* Tablet (max-width: 1024px) */
@media (max-width: 1024px) {
  :root {
    --sidebar-w: 248px;
  }

  .model-btn,
  #current-model-btn {
    max-width: 180px;
  }

  .topup-link .btn-label,
  #topup-kc-link .btn-label,
  .key-status-text,
  #api-key-status-text {
    display: none;
  }
}

/* Mobile (max-width: 768px) */
@media (max-width: 768px) {
  :root {
    --header-h: 56px;
    --sidebar-w: min(84vw, 300px);
  }

  body {
    font-size: 15px;
  }

  .chat-header,
  #chat-header {
    gap: 8px;
    padding-left: calc(8px + var(--safe-left));
    padding-right: calc(8px + var(--safe-right));
  }

  .header-left {
    flex: 0 0 auto;
    gap: 6px;
  }

  .brand {
    gap: 6px;
  }

  .brand-logo {
    width: 28px;
    height: 28px;
  }

  #sidebar-toggle-btn,
  .sidebar-toggle {
    display: inline-flex;
  }

  .brand-title,
  #brand-title {
    font-size: 14px;
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-center {
    flex: 1 1 auto;
    min-width: 0;
    justify-content: center;
    padding: 0 2px;
  }

  .model-btn,
  #current-model-btn {
    height: 34px;
    max-width: 130px;
    padding: 0 8px;
    font-size: 12px;
    gap: 6px;
  }

  .model-dot {
    width: 7px;
    height: 7px;
  }

  .header-right {
    flex: 0 0 auto;
    gap: 4px;
  }

  .icon-btn,
  .key-status-btn,
  #api-key-status-btn,
  #theme-toggle-btn,
  #sidebar-toggle-btn {
    height: 34px;
    width: 34px;
    padding: 0;
    justify-content: center;
  }

  .key-status-text,
  #api-key-status-text,
  .topup-link .btn-label,
  #topup-kc-link .btn-label {
    display: none;
  }

  /* Sidebar off-canvas mobile */
  .chat-sidebar,
  #chat-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 50;
    width: var(--sidebar-w);
    padding-top: calc(14px + var(--safe-top));
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: none;
    will-change: transform;
  }

  .chat-sidebar.sidebar-open,
  #chat-sidebar.sidebar-open,
  body.sidebar-open #chat-sidebar {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  /* Backdrop: hiện khi sidebar mở (kể cả khi không có hidden) */
  .sidebar-backdrop:not([hidden]),
  #sidebar-backdrop:not([hidden]) {
    display: block;
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-backdrop.active,
  #sidebar-backdrop.active,
  body.sidebar-open #sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
    display: block;
  }

  .conversation-actions {
    opacity: 1;
  }

  /* Messages on Mobile */
  .messages-container,
  #messages-container {
    padding: 16px 12px 20px;
  }

  .message-row {
    gap: 8px;
    margin-bottom: 18px;
  }

  .message-avatar {
    flex-basis: 30px;
    width: 30px;
    height: 30px;
    font-size: 13px;
  }

  .message-avatar svg {
    width: 17px;
    height: 17px;
  }

  .message-content {
    padding: 10px 13px;
    font-size: 15px;
  }

  .message-row.user .message-content {
    max-width: 88%;
  }

  .message-actions {
    opacity: 0.6;
    transform: none;
  }

  .message-content pre code {
    font-size: 12.5px;
    padding: 12px;
  }

  /* Empty state */
  .empty-state,
  #empty-state {
    justify-content: flex-start;
    padding: 24px 14px 16px;
  }

  .empty-logo {
    width: 60px;
    height: 60px;
    margin-top: 3vh;
    border-radius: 16px;
  }

  .empty-subtitle {
    margin-bottom: 16px;
    font-size: 14px;
  }

  .prompt-grid {
    gap: 8px;
  }

  .prompt-card {
    min-height: 72px;
    padding: 12px;
  }

  .prompt-card:hover {
    transform: none;
  }

  /* Input area */
  .chat-input-area,
  #chat-input-area {
    padding: 6px 10px calc(8px + var(--safe-bottom));
  }

  .chat-form,
  #chat-form {
    padding: 5px 6px;
    border-radius: 20px;
    gap: 4px;
    align-items: flex-end;
  }

  #chat-input {
    font-size: 16px; /* Chống zoom tự động trên iOS */
    min-height: 42px;
    max-height: 160px;
    padding: 10px 8px;
    line-height: 1.45;
    box-sizing: border-box;
  }

  .attach-btn,
  .send-btn,
  .stop-btn,
  #attach-image-btn,
  #send-btn,
  #stop-btn {
    width: 38px;
    height: 38px;
  }

  .input-disclaimer {
    margin-top: 6px;
    font-size: 10.5px;
  }

  .text-input,
  input[type="text"],
  input[type="password"],
  input[type="search"] {
    font-size: 16px; /* Chống zoom tự động trên iOS */
  }

  /* Modals: Bottom sheet */
  .modal,
  #api-key-modal,
  #model-picker-modal {
    align-items: flex-end;
    padding: 0;
  }

  .modal-dialog,
  .modal-dialog-lg {
    max-width: 100%;
    max-height: 88dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: none;
    padding-bottom: var(--safe-bottom);
    animation: sheetUp 0.3s var(--ease-out) both;
  }

  .modal-header {
    padding: 16px 16px 12px;
  }

  .modal-tabs {
    margin: 10px 16px 0;
  }

  .modal-body {
    padding: 14px 16px;
  }

  .modal-footer {
    padding: 12px 16px 14px;
  }

  .modal-actions {
    flex: 1 1 auto;
    justify-content: flex-end;
  }

  .modal-actions .btn {
    flex: 1 1 0;
  }

  /* Toast: Top center trên mobile */
  .toast-container,
  #toast-container {
    top: calc(var(--header-h) + var(--safe-top) + 8px);
    right: 50%;
    transform: translateX(50%);
    align-items: center;
    width: calc(100vw - 24px);
  }

  .toast {
    animation: toastInMobile 0.35s var(--ease-out) both, toastOutMobile 0.4s var(--ease) 3.6s forwards;
  }
}

/* Medium-small phones (max-width: 600px) */
@media (max-width: 600px) {
  .brand-title,
  #brand-title {
    display: none;
  }

  .model-btn,
  #current-model-btn {
    max-width: 145px;
  }
}

/* Small phones (max-width: 480px) */
@media (max-width: 480px) {
  .brand-title,
  #brand-title {
    display: none;
  }

  .kc-badge,
  #kc-balance-badge {
    height: 30px;
    padding: 0 9px;
    font-size: 12px;
  }

  .topup-link,
  #topup-kc-link {
    height: 30px;
    padding: 0 10px;
    font-size: 12px;
  }

  .model-btn,
  #current-model-btn {
    max-width: 105px;
  }

  #back-to-shop-link {
    display: none;
  }

  .prompt-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .prompt-desc {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
}

/* Extra small (max-width: 360px) */
@media (max-width: 360px) {
  .prompt-grid {
    grid-template-columns: 1fr;
  }

  .kc-badge,
  #kc-balance-badge {
    display: none;
  }
}

@keyframes sheetUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@keyframes toastInMobile {
  from { opacity: 0; transform: translateY(-16px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastOutMobile {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-12px); visibility: hidden; }
}

/* Hover-capable devices only: lift effects */
@media (hover: none) {
  .prompt-card:hover,
  .send-btn:hover:not(:disabled),
  #send-btn:hover:not(:disabled) {
    transform: none;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Print */
@media print {
  .chat-header,
  #chat-header,
  .chat-sidebar,
  #chat-sidebar,
  .sidebar-backdrop,
  #sidebar-backdrop,
  .chat-input-area,
  #chat-input-area,
  .message-actions,
  .toast-container,
  #toast-container {
    display: none !important;
  }

  body {
    height: auto;
    overflow: visible;
  }

  .messages-container,
  #messages-container {
    overflow: visible;
  }
}
