/* ── AI Chat Console — Global Styles ─────────────────────────────────────────
   All theme variables (accent engine, 14 color themes, light/dark/oled,
   radius/shadow/spacing/motion scales) live in assets/css/tokens.css, which
   MUST be linked before this file on every page. This file holds component
   styles only. */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; } /* global default */

body {
  font-family: var(--font-family);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* Themed text selection */
::selection { background: hsl(var(--primary-h) var(--primary-s) 55% / .28); color: inherit; }

/* Accent focus rings — replaces browser default on interactive elements */
:focus-visible {
  outline: 2px solid hsl(var(--primary-h) var(--primary-s) var(--primary-l));
  outline-offset: 2px;
}

/* Themed checkboxes, radio buttons, and range sliders — one rule covers all */
input[type="checkbox"],
input[type="radio"],
input[type="range"] {
  accent-color: var(--primary);
}

/* Smooth theme/mode transitions */
body, .app-sidebar, .chat-header, .chat-input-wrap, .msg-bubble, .session-item,
.modal-card, .settings-section-card, .admin-main, .admin-sidebar {
  transition: background-color .2s ease, border-color .2s ease, color .15s ease;
}
@media (prefers-reduced-motion: reduce) {
  body, .app-sidebar, .chat-header, .chat-input-wrap, .msg-bubble, .session-item,
  .modal-card, .settings-section-card, .admin-main, .admin-sidebar {
    transition: none;
  }
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── AUTH PAGES ────────────────────────────────────────────────────────── */
/* .auth-page on <body> and .auth-wrap as a wrapper div are both supported */
.auth-wrap,
body.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg,
    hsl(var(--primary-h) var(--primary-s) 25%) 0%,
    hsl(var(--primary-h) 20% 13%) 100%);
}

.auth-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 36px 32px;
  width: 100%;
  max-width: 420px;
}

.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 28px;
}

/* Bare logo mark rendered directly inside .auth-logo (forgot/reset/verify/
   resend-verify) stays accent-coloured. Scoped with `>` on purpose: when the
   mark sits inside a .auth-logo-icon gradient tile (login.php) its stroke must
   inherit the tile's white --on-accent, and this rule's higher specificity
   would otherwise force it back to blue — invisible against the blue tile. */
.auth-logo > svg { color: var(--primary); }

.auth-logo h1 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
}

.auth-logo p { font-size: .75rem; color: var(--muted); }

.auth-title { font-size: 1.1rem; font-weight: 600; margin-bottom: 4px; }
.auth-sub   { font-size: .82rem; color: var(--muted); margin-bottom: 20px; }

/* ── FORMS ─────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }

label {
  display: block;
  font-size: .8rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 5px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="url"],
input[type="tel"],
input[type="number"],
input[type="search"],
select,
textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: .9rem;
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
  -webkit-appearance: none;
  appearance: none;
  box-sizing: border-box;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}

textarea { resize: vertical; min-height: 80px; }
#chat-input { min-height: unset; }

/* ── BUTTONS ────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border: none;
  border-radius: 8px;
  font-size: .85rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s, transform .1s, box-shadow .15s;
  text-decoration: none;
}

.btn:active { transform: scale(.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); text-decoration: none; }

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); }

.btn-danger  { background: var(--danger);  color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-ghost   { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-sm { padding: 5px 10px; font-size: .78rem; }
.btn-full { width: 100%; justify-content: center; }
.btn-icon { padding: 7px; border-radius: 7px; }

/* ── ALERTS ─────────────────────────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: 8px;
  font-size: .83rem;
  margin-bottom: 16px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.alert-error   { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.alert-info    { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }

/* ── APP SHELL ──────────────────────────────────────────────────────────── */
.app-shell {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* Sidebar */
.app-sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  /* Sibling of the flex:1 .session-list — only the list may give up space. */
  flex-shrink: 0;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.sidebar-logo-text h2 { font-size: 0.875rem; font-weight: 700; }
.sidebar-logo-text p  { font-size: 0.75rem; color: var(--muted); }

.sidebar-new-chat {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.session-list {
  /* `flex: 1 1 0` + `min-height: 0` + `height: 0` is the bulletproof form of
     "this flex child takes the leftover space and scrolls internally".
     All three are load-bearing here:
       - min-height:0  — a flex child's auto min-height resolves to its CONTENT
                         height, not 0, so without it a long list grows past
                         its allotted space instead of clipping.
       - height:0      — belt and braces. With flex-grow:1 the used height comes
                         ENTIRELY from flex leftover space, so there is no code
                         path left where content height can leak into the box
                         height.
     This element is only ever used in dashboard.php's sidebar (verified), so
     hard-constraining it here is safe.

     History: an earlier windowed virtual-scroll implementation (J1 in
     dashboard-ui.js) used to write a multi-thousand-pixel padding-bottom on
     this element on every scroll event to fake a tall scroll area — the
     recurring root cause of several "session list expands over everything"
     reports. J1 now renders real DOM in fixed batches with a "Show more"
     button instead and never touches padding at all, which removes that
     failure mode at the source. These properties (plus overflow-anchor below)
     are kept anyway as defense in depth — cheap, and they cost nothing if
     nothing ever writes to this element's padding again. */
  flex: 1 1 0;
  min-height: 0;
  height: 0;
  overflow-y: auto;
  padding: 6px 8px;
  overflow-anchor: none;
}

.session-list::-webkit-scrollbar { width: 4px; }
.session-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.session-list::-webkit-scrollbar-thumb:hover { background: hsl(var(--primary-h) var(--primary-s) var(--primary-l) / .6); }

.session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--muted);
  transition: background .15s, color .15s;
  position: relative;
  box-sizing: border-box;
  min-height: 36px;
  max-height: 36px;
}

.session-item:hover { background: var(--bg); color: var(--text); }
.session-item.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }

.session-item-title { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

.session-item-del {
  display: none;
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  line-height: 1;
}
.session-item:hover .session-item-del { display: flex; }

.session-list-label {
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 10px 10px 4px;
}

.session-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 32px 16px;
  color: var(--muted);
  font-size: .8rem;
  text-align: center;
}

/* ── Skeleton loading placeholders ─────────────────────────────────────── */
@keyframes shimmer {
  0%   { background-position: -400px 0; }
  100% { background-position: 400px 0; }
}

.skeleton {
  background: linear-gradient(90deg, var(--border) 25%, var(--bg) 50%, var(--border) 75%);
  background-size: 800px 100%;
  animation: shimmer 1.4s infinite;
  border-radius: 6px;
}

.skeleton-session {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
}

.skeleton-session-icon {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  flex-shrink: 0;
}

.skeleton-session-text {
  flex: 1;
  height: 12px;
  border-radius: 6px;
}

.skeleton-session-text.short {
  max-width: 60%;
}

/* ── Sidebar footer ─────────────────────────────────────────────────────── */
.sf-footer {
  border-top: 1px solid var(--border);
  /* Fixed at the tighter value permanently — this used to be the roomier
     "empty chat" default (11px 12px 14px), swapped for 8px 12px 9px via a
     body.chat-active class once a chat had messages. Now that #ctx-meter-wrap
     (dashboard.css) is always visible, there's no "roomier" state left to
     account for, so the tight padding is just the one true value. */
  padding: 8px 12px 9px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.sf-footer.guest-mode {
  /* Guest footer holds only the login/sign-up CTA (no credit badge, no
     account menu), so the tighter logged-in padding above reads cramped —
     give it back some breathing room. */
  padding: 12px 12px 12px;
}

/* User row: avatar + name/email side by side */
.sf-user {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}

.sf-avatar {
  width: 34px;
  height: 34px;
  min-width: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.sf-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
}
.sf-info strong {
  display: block;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
  line-height: 1.4;
}
.sf-info span {
  display: block;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
}

/* Nav row: action buttons evenly spread */
.sf-nav {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 2px;
}

.sf-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 4px;
  border-radius: 8px;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
  transition: background .15s, color .15s;
}
.sf-btn:hover {
  background: rgba(99,102,241,.1);
  color: var(--primary);
  text-decoration: none;
}

/* Main content */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Chat header */
.chat-header {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.chat-header-model { flex: 1; }

.model-selector {
  display: flex;
  align-items: center;
  gap: 8px;
}

.model-selector select { max-width: 300px; }

/* Chat messages */
.chat-messages {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  /* NOTE: scroll-behavior is intentionally NOT 'smooth' here. A global smooth
     turns every programmatic scrollTop change (streaming follow, re-pins) into an
     animation that drifts across responses and gets cancelled mid-flight by async
     height changes (images/code/math) — the cause of the "jumping" scroll.
     Intentional smooth scrolls pass { behavior:'smooth' } explicitly in JS. */
}

.chat-messages::-webkit-scrollbar { width: 5px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.chat-messages::-webkit-scrollbar-thumb:hover { background: hsl(var(--primary-h) var(--primary-s) var(--primary-l) / .55); }

/* Welcome screen */
.welcome-screen {
  margin: auto;
  text-align: center;
  max-width: 500px;
  padding: 40px 20px;
}

.welcome-screen h2 { font-size: 1.4rem; font-weight: 700; margin-bottom: 8px; }
.welcome-screen p  { color: var(--muted); margin-bottom: 24px; }

.welcome-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.chip {
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: .8rem;
  cursor: pointer;
  background: var(--surface);
  transition: background .15s, border-color .15s;
}
.chip:hover { background: var(--primary-light); border-color: var(--primary); color: var(--primary); }

/* Message bubbles — nearly full width (not a narrow centered reading
   column), just a small fixed breathing-room gap on the right so text
   doesn't run flush to the very edge of the screen. Same cap for .msg-ai
   and .msg-user so a full-width AI reply and a right-aligned user bubble
   above it end at the same right edge. */
.msg {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: calc(100% - 40px);
  min-width: 0;
}

.msg-user { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .7rem;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-ai .msg-avatar   { background: var(--primary); color: #fff; }
.msg-user .msg-avatar { background: var(--primary); color: #fff; }

.msg-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }

.msg-bubble {
  min-width: 0;
  overflow: hidden;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: .88rem;
  line-height: 1.65;
  word-break: break-word;
  max-width: 100%;
}

.msg-ai .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: 2px;
}

.msg-user .msg-bubble {
  background: var(--primary);
  color: #fff;
  border-top-right-radius: 2px;
  align-self: flex-end;
}
/* Light-primary dark-mode edge cases — use dark text */
html[data-color-theme="whatsapp"][data-theme="dark"] .msg-user .msg-bubble,
html[data-color-theme="graphite"][data-theme="dark"] .msg-user .msg-bubble { color: #0f172a; }

/* Markdown inside bubbles */
.msg-bubble p            { margin: 0 0 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble h1,.msg-bubble h2,.msg-bubble h3 { margin: 12px 0 6px; font-size: 1em; font-weight: 700; }
.msg-bubble ul,.msg-bubble ol { padding-left: 20px; margin: 6px 0; }
.msg-bubble li { margin: 3px 0; }
.msg-bubble strong { font-weight: 700; }
.msg-bubble em { font-style: italic; }
.msg-bubble a  { color: var(--primary); }
.msg-user .msg-bubble a { color: #bfdbfe; }

.msg-bubble code {
  font-family: 'Fira Code', 'Consolas', monospace;
  font-size: .85em;
  background: rgba(0,0,0,.08);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,.07);
}

.msg-user .msg-bubble code { background: rgba(255,255,255,.2); border-color: rgba(255,255,255,.15); }

/* ── Code block: wrapper + header + body ─────────────────────── */
.code-block-wrap {
  margin: 10px 0;
  border-radius: 8px;
  overflow: hidden;
  max-width: 100%;
  border: 1px solid rgba(255,255,255,.1);
  background: #1e1e2e;
}
.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px 6px 14px;
  background: #161625;
  border-bottom: 1px solid rgba(255,255,255,.08);
  min-height: 34px;
}
.code-lang {
  font-family: 'Fira Code','Consolas',monospace;
  font-size: .7rem;
  color: #a6adc8;
  letter-spacing: .04em;
}
.code-copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 5px;
  color: #cdd6f4;
  font-size: .72rem;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s;
  white-space: nowrap;
  flex-shrink: 0;
}
.code-copy-btn:hover { background: rgba(255,255,255,.18); }
.code-block-wrap pre {
  margin: 0; padding: 0;
  background: transparent; border: none; border-radius: 0; overflow: hidden;
}
.code-block-wrap pre code {
  display: block;
  padding: 14px 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  font-size: .82rem;
  line-height: 1.65;
  background: #1e1e2e;
  color: #cdd6f4;
  border: none; border-radius: 0;
  white-space: pre;
  word-break: normal;
  word-wrap: normal; tab-size: 2;
  font-family: 'Fira Code','Consolas',monospace;
}
/* Bare pre fallback */
.msg-bubble pre {
  margin: 10px 0; border-radius: 8px;
  overflow: hidden; background: #1e1e2e;
  border: 1px solid rgba(255,255,255,.08);
}
.msg-bubble pre code {
  display: block; padding: 14px 16px;
  overflow-x: auto; font-size: .82rem; line-height: 1.65;
  background: transparent; color: #cdd6f4;
  border: none; white-space: pre; tab-size: 2;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
}

.msg-time { font-size: .68rem; color: var(--muted); }

.msg-user .msg-meta { justify-content: flex-end; }

.copy-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: .72rem;
  display: none;
  align-items: center;
  gap: 3px;
}
.msg:hover .copy-btn { display: flex; }
.copy-btn:hover { background: var(--bg); color: var(--text); }

/* Typing indicator */
.typing-indicator { display: flex; gap: 4px; padding: 10px 14px; }
.typing-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: bounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: .2s; }
.typing-dot:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,80%,100% { transform: translateY(0); } 40% { transform: translateY(-6px); } }

/* Chat input */
.chat-input-wrap {
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 10px;
  transition: border-color .15s, box-shadow .15s;
}

.chat-input-row:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

#chat-input {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font-size: .9rem;
  font-family: inherit;
  color: var(--text);
  outline: none;
  max-height: 160px;
  overflow-y: auto;
  line-height: 1.5;
}

#chat-input::placeholder { color: var(--muted); }

.send-btn {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  width: 34px; height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}
.send-btn:hover   { background: var(--primary-hover); }
.send-btn:active  { transform: scale(.93); }
.send-btn:disabled { opacity: .4; cursor: not-allowed; }

.chat-hint { font-size: .68rem; color: var(--muted); margin-top: 6px; text-align: center; }

/* ── SETTINGS / ACCOUNT PAGES ──────────────────────────────────────────── */
html, body { height: 100%; }

.settings-wrap {
  display: flex;
  height: 100vh;
  overflow: hidden;
}
.settings-sidebar {
  width: 220px;
  min-width: 220px;
  flex-shrink: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 0;
  gap: 2px;
  /* Scrollbar matched to the sidebar surface */
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.settings-sidebar::-webkit-scrollbar { width: 6px; }
.settings-sidebar::-webkit-scrollbar-track { background: transparent; }
.settings-sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
.settings-sidebar::-webkit-scrollbar-thumb:hover { background: hsl(var(--primary-h) var(--primary-s) var(--primary-l) / .5); }
.settings-nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 1px 8px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: .86rem;
  font-weight: 500;
  letter-spacing: .005em;
  color: var(--muted);
  text-decoration: none;
  transition: background .15s, color .15s;
  cursor: pointer;
  width: calc(100% - 16px);
}
.settings-nav-link:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.settings-nav-link.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }

.settings-main,
.settings-main-panel {
  flex: 1;
  min-width: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
}
/* .settings-main itself is intentionally unpadded — account.php wraps its
   content in its own inner <div style="padding:..."> (see account.php),
   which is the single source of truth for that page's left/right spacing.
   .settings-main-panel has no such inner wrapper anywhere it's used, so it
   keeps its own padding here. */
.settings-main-panel { padding: 32px; }

.settings-h1 { font-size: 1.2rem; font-weight: 700; color: var(--text); margin-bottom: 4px; }
.settings-h1-sub { font-size: .82rem; color: var(--muted); }

/* ── Mobile settings ───────────────────────────────────────── */
@media (max-width: 768px) {
  .settings-wrap { flex-direction: column; height: 100dvh; }
  .settings-sidebar {
    width: 100%;
    min-width: unset;
    height: auto;
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
    gap: 4px;
  }
  .settings-nav-link { white-space: nowrap; margin: 0; flex-shrink: 0; padding: 7px 12px; font-size: .8rem; width: auto; }
  .settings-main, .settings-main-panel { height: auto; flex: 1; overflow-y: auto; }
  .settings-main-panel { padding: 20px; }
}

.settings-section { margin-bottom: 36px; }
.settings-section h2 { font-size: 1rem; font-weight: 700; margin-bottom: 4px; }
.settings-section p.desc { font-size: .8rem; color: var(--muted); margin-bottom: 16px; }

.provider-grid {
  display: grid;
  gap: 14px;
}

.provider-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  background: var(--surface);
}

.provider-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.provider-card-header h3 { font-size: .88rem; font-weight: 600; flex: 1; }
.provider-badge { font-size: .7rem; padding: 2px 8px; border-radius: 10px; }
.badge-free { background: #dcfce7; color: #166534; }
.badge-paid { background: #fef9c3; color: #854d0e; }

.key-input-row {
  display: flex;
  gap: 8px;
}

.key-input-row input { flex: 1; font-family: monospace; }

/* ── ADMIN PAGES ────────────────────────────────────────────────────────── */
/*  Cross-browser strategy:
 *  - body.admin-page is the scroll container (no global overflow:hidden)
 *  - .admin-wrap is pinned to viewport via explicit top/right/bottom/left
 *    (NOT `inset` — that's CSS Logical Properties Level 1, missing in older Safari)
 *  - Each pane uses both flex sizing AND an explicit height so old engines
 *    that botch `min-height: 0` still get correct overflow.
 */
body.admin-page {
  height: 100vh;
  height: 100dvh;           /* mobile dynamic viewport (Safari 15.4+) */
  margin: 0;
  overflow: hidden;
  background: var(--bg);
}

.admin-wrap {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;   /* avoid `inset` shorthand */
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  overflow: hidden;
  z-index: 1;
}

.admin-sidebar {
  width: 220px;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  min-height: 0;
  height: 100%;
  background: #1e293b;
  color: #e2e8f0;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 0;
  /* Scrollbar matched to the dark sidebar */
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,.18) transparent;
}
.admin-sidebar::-webkit-scrollbar { width: 6px; }
.admin-sidebar::-webkit-scrollbar-track { background: transparent; }
.admin-sidebar::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,.16);
  border-radius: 6px;
}
.admin-sidebar::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,.30); }

.admin-sidebar .logo {
  padding: 20px 18px;
  font-size: .95rem;
  font-weight: 700;
  border-bottom: 1px solid #334155;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 8px;
}

.admin-nav-link {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 18px;
  font-size: .82rem;
  color: #94a3b8;
  transition: background .15s, color .15s;
}
.admin-nav-link:hover { background: #334155; color: #e2e8f0; text-decoration: none; }
.admin-nav-link.active { background: #1d4ed8; color: #fff; }

.admin-main {
  -webkit-box-flex: 1;
  -ms-flex: 1 1 0%;
  flex: 1 1 0%;
  min-height: 0;       /* lets the flex child shrink so overflow:auto triggers */
  min-width: 0;
  height: 100%;
  padding: 28px 32px;
  background: var(--bg);
  overflow-y: auto;
  overflow-x: auto;   /* allow .table-wrap to scroll horizontally on small screens */
  -webkit-overflow-scrolling: touch;
  outline: none;       /* tabindex="-1" focus ring suppression */
}
.admin-main:focus { outline: none; }
.admin-main > *:first-child { margin-top: 0; }
.admin-main > *:last-child  { margin-bottom: 0; }
@media (max-width: 768px) {
  .admin-main { padding: 18px 16px; }
}

.admin-main h1 { font-size: 1.3rem; font-weight: 700; margin-bottom: 20px; }

.stat-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}

.stat-card {
  background: var(--surface);
  border-radius: 10px;
  padding: 18px;
  border: 1px solid var(--border);
}

.stat-card .stat-val { font-size: 1.8rem; font-weight: 700; color: var(--primary); }
.stat-card .stat-lbl { font-size: .78rem; color: var(--muted); margin-top: 4px; }

/* Tables */
.table-wrap {
  background: var(--surface);
  border-radius: 10px;
  border: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}
.table-wrap::-webkit-scrollbar { height: 5px; }
.table-wrap::-webkit-scrollbar-track { background: transparent; }
.table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.table-wrap::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.table-wrap table { min-width: 600px; }

table { width: 100%; border-collapse: collapse; }
thead th {
  padding: 10px 14px;
  text-align: left;
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
tbody td {
  padding: 11px 14px;
  font-size: .83rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg); }

.status-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 20px;
  font-size: .72rem;
  font-weight: 600;
}
.status-pending   { background: #fef9c3; color: #854d0e; }
.status-active    { background: #dcfce7; color: #166534; }
.status-suspended { background: #fee2e2; color: #991b1b; }
.status-inactive  { background: #f1f5f9; color: #64748b; border: 1px solid #e2e8f0; }
.status-admin     { background: #ede9fe; color: #5b21b6; }

/* Mobile sidebar toggle */
.menu-toggle { display: none; }

@media (max-width: 768px) {
  .app-sidebar { position: fixed; left: -260px; top: 0; bottom: 0; z-index: 50; transition: left .25s; }
  .app-sidebar.open { left: 0; box-shadow: var(--shadow); }
  .menu-toggle { display: flex; }

  /* ── Settings nav: horizontal scroll strip (account.php) ─────────────── */
  /* account.php inline styles handle the details; we must NOT hide it here */

  /* ── Admin sidebar → horizontal scroll top bar ───────────────────────── */
  .admin-wrap   { flex-direction: column !important; }
  .admin-sidebar {
    display: flex !important;
    flex-direction: row !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    position: sticky !important;
    top: 0 !important;
    z-index: 20 !important;
    padding: 6px 8px !important;
    gap: 2px !important;
    scrollbar-width: none !important;
    -webkit-overflow-scrolling: touch !important;
    flex-shrink: 0 !important;
    border-bottom: 1px solid #334155 !important;
    border-right: none !important;
  }
  .admin-sidebar::-webkit-scrollbar { display: none !important; }
  .admin-sidebar .logo             { display: none !important; }  /* hide "AI Chat Console Admin" logo on mobile */
  .admin-nav-link {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
    padding: 7px 12px !important;
    font-size: .78rem !important;
    border-radius: 8px !important;
    margin-top: 0 !important;       /* override margin-top:auto on "Go to Chat" */
  }
  .admin-main {
    flex: 1 !important;
    height: auto !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    padding: 16px 14px !important;
  }
}

/* ── New feature styles ─────────────────────────────────────────────────── */

/* Temperature control */
.temp-control {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  flex-shrink: 0;
}
.temp-control input[type="range"] {
  -webkit-appearance: none;
  height: 4px;
  border-radius: 4px;
  background: var(--border);
  cursor: pointer;
  border: none;
  box-shadow: none;
  padding: 0;
}
.temp-control input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px; height: 13px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
}

/* Header tool buttons */
.header-tools {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

/* System prompt bar */
.sys-prompt-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  background: var(--primary-light);
  border-bottom: 1px solid var(--border);
  color: var(--primary);
  font-size: .78rem;
  flex-shrink: 0;
}
.sys-prompt-bar span { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.sys-prompt-clear {
  background: none; border: none; cursor: pointer;
  color: var(--primary); font-size: .75rem; font-weight: 600;
  padding: 2px 6px; border-radius: 4px;
  flex-shrink: 0;
}
.sys-prompt-clear:hover { background: rgba(37,99,235,.12); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 24px;
  width: 100%;
  max-width: 500px;
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.modal-header h3 { font-size: 1rem; font-weight: 700; }

/* Streaming indicator */
.chat-input-row.streaming { border-color: var(--primary); }
.chat-input-row.streaming .send-btn { background: var(--danger); }

/* Cursor blink */
.cursor-blink {
  display: inline-block;
  animation: blink 1s step-end infinite;
  font-weight: 300;
  color: var(--primary);
}
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }


/* Blockquote */
.msg-bubble blockquote {
  border-left: 3px solid var(--primary);
  padding-left: 12px;
  color: var(--muted);
  margin: 8px 0;
}

/* HR inside bubble */
.msg-bubble hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }

/* del / strikethrough */
.msg-bubble del { text-decoration: line-through; opacity: .7; }

/* ── Misc ──────────────────────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

.text-muted { color: var(--muted); }
.text-sm    { font-size: .8rem; }
.mt-1  { margin-top: 6px; }
.mt-2  { margin-top: 12px; }
.gap-2 { gap: 8px; }
.flex  { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }

/* ── DASHBOARD NEW FEATURES ─────────────────────────────────────────────── */

/* Session search */
.session-search-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 6px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
}
.session-search {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 0.75rem;
  color: var(--text);
  outline: none;
  font-family: inherit;
  padding: 0;
  width: 100%;
  min-width: 0;
}
.session-search::placeholder { color: var(--muted); }

/* Bulk toolbar */
.bulk-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--primary-light);
  border-bottom: 1px solid var(--border);
  gap: 8px;
  flex-shrink: 0;
}

/* Session item — extended with pin, checkbox, actions */
.session-item {
  position: relative;
}
.session-check {
  display: none;
  width: 13px;
  height: 13px;
  flex-shrink: 0;
  cursor: pointer;
  accent-color: var(--primary);
}
.session-item:hover .session-check,
.session-item .session-check:checked,
body.bulk-active .session-check { display: block; }

.session-icon { flex-shrink: 0; color: var(--muted); }
.session-item:hover .session-icon,
.session-item.active .session-icon { display: none; }
.session-item:hover .session-check { display: block; }

.session-item-actions {
  display: none;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.session-item:hover .session-item-actions { display: flex; }

.session-item-pin {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  padding: 2px 3px;
  border-radius: 4px;
  line-height: 1;
  display: flex;
  align-items: center;
}
.session-item-pin:hover { color: var(--primary); }
.session-item.pinned .session-item-pin { color: var(--primary); }

.session-item-del:hover { color: var(--danger); }

/* Pinned label */
.pinned-label {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--primary);
}

/* Inline rename input */
.rename-input {
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  font-size: inherit;
  font-family: inherit;
  color: var(--text);
  padding: 0;
}

/* Model badge on AI messages */
.msg-badge-row {
  padding: 0 4px 2px;
}
.model-badge {
  display: inline-block;
  font-size: .65rem;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 10px;
  background: var(--primary-light);
  color: var(--primary);
  border: 1px solid color-mix(in srgb, var(--primary) 15%, transparent);
}

/* Timestamps — always visible */
.msg-time { display: inline; }

/* Image attachment */
.img-preview-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 6px;
  background: var(--bg);
  position: relative;
  width: fit-content;
}
.img-preview-wrap img {
  height: 60px;
  border-radius: 6px;
  object-fit: cover;
  max-width: 100px;
}
.img-remove-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  background: var(--danger);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}

.attach-btn.has-image { color: var(--primary); }



/* Keyboard shortcuts modal */
.kbd-list { display: flex; flex-direction: column; gap: 2px; }
.kbd-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 4px;
  border-bottom: 1px solid var(--border);
  font-size: .83rem;
}
.kbd-row:last-child { border-bottom: none; }
.kbd-keys { display: flex; align-items: center; gap: 3px; }
kbd {
  display: inline-block;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: .72rem;
  background: var(--bg);
  font-family: inherit;
  color: var(--muted);
  box-shadow: 0 1px 0 var(--border);
}

/* Compact density */
.density-compact .msg-bubble { padding: 6px 10px; font-size: .83rem; }
.density-compact .chat-messages { gap: 3px; }
.density-compact .session-item { padding: 5px 8px; }
.density-compact .chat-header { padding: 6px 12px; }

/* Account page styles */
.settings-h1 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 6px;
}
.profile-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 24px;
}
.profile-avatar {
  width: 64px; height: 64px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  flex-shrink: 0;
}
.profile-info h2 { font-size: 1.1rem; font-weight: 700; }
.profile-info p  { font-size: .82rem; color: var(--muted); }

.pref-section { margin-bottom: 28px; }
.pref-section-title {
  font-size: .8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.toggle-row:last-child { border-bottom: none; }
.toggle-row-label strong { display: block; font-size: .85rem; }
.toggle-row-label span { font-size: .75rem; color: var(--muted); }

/* Toggle switch */
.toggle-switch {
  position: relative;
  width: 40px; height: 22px;
  flex-shrink: 0;
}
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-thumb {
  position: absolute; inset: 0;
  background: var(--border);
  border-radius: 22px;
  cursor: pointer;
  transition: background .2s;
}
.toggle-thumb::after {
  content: '';
  position: absolute;
  width: 16px; height: 16px;
  left: 3px; top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform .2s;
}
.toggle-switch input:checked + .toggle-thumb { background: var(--primary); }
.toggle-switch input:checked + .toggle-thumb::after { transform: translateX(18px); }

.pref-row-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
@media (max-width: 600px) { .pref-row-3 { grid-template-columns: 1fr; } }

/* ══════════════════════════════════════════════════════════════════════════
   UI POLISH
   ══════════════════════════════════════════════════════════════════════════ */

/* Base font bump */
body { font-size: 15px; }

/* Sidebar — slightly off-white / darker in dark mode */
.app-sidebar {
  background: #f8fafc;
  border-right-color: #e2e8f0;
}
[data-theme="dark"] .app-sidebar { background: #111827; border-right-color: #1f2937; }

.sidebar-header { background: inherit; }

/* Sidebar logo text bump */
.sidebar-logo-text h2 { font-size: 1rem; }

/* Session items — subtler, tighter */
.session-item {
  border-radius: 9px;
  font-size: .84rem;
}
.session-item:hover { background: rgba(37,99,235,.07); color: var(--text); }
.session-item.active {
  background: var(--primary-light);
  color: var(--primary);
}
[data-theme="dark"] .session-item:hover { background: rgba(255,255,255,.06); }

/* Chat header — slightly more padding + subtle bottom shadow */
.chat-header {
  padding: 12px 20px;
  box-shadow: 0 1px 0 var(--border);
  gap: 12px;
}

/* Model select — slightly larger text */
.model-selector select,
#model-select { font-size: .88rem; }

/* Chat messages — more breathing room */
.chat-messages { padding: 24px 20px; gap: 10px; }

/* Messages fill the full width of .chat-messages (no fixed cap) — was
   860px, keeping bubbles narrow even after .chat-messages itself was
   widened to fill the screen. */

/* Avatar — bigger, gradient for AI */
.msg-avatar {
  width: 32px; height: 32px;
  font-size: .72rem;
  box-shadow: 0 2px 6px rgba(0,0,0,.15);
}
.msg-ai .msg-avatar {
  background: linear-gradient(135deg, var(--primary) 0%, hsl(var(--primary-h) var(--primary-s) calc(var(--primary-l) - 15%)) 100%);
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .02em;
}
.msg-user .msg-avatar {
  background: var(--primary);
}

/* AI bubble — clean card look */
.msg-ai .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
  font-size: .9rem;
  line-height: 1.7;
}
[data-theme="dark"] .msg-ai .msg-bubble {
  background: #1e293b;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
}

/* User bubble — follows active colour theme */
.msg-user .msg-bubble {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
  color: #fff;
  border-top-right-radius: 4px;
  box-shadow: 0 3px 10px color-mix(in srgb, var(--primary) 40%, transparent);
  font-size: .9rem;
  line-height: 1.7;
}
/* For themes where the dark-mode primary is light enough to need dark text */
html[data-color-theme="whatsapp"][data-theme="dark"] .msg-user .msg-bubble,
html[data-color-theme="graphite"][data-theme="dark"] .msg-user .msg-bubble {
  color: #0f172a;
}
html[data-color-theme="whatsapp"][data-theme="dark"] .msg-user .msg-bubble a,
html[data-color-theme="graphite"][data-theme="dark"] .msg-user .msg-bubble a {
  color: #1e3a5f;
}
html[data-color-theme="whatsapp"][data-theme="dark"] .msg-user .msg-avatar,
html[data-color-theme="graphite"][data-theme="dark"] .msg-user .msg-avatar {
  color: #0f172a;
}

/* Message bubble font sizes */
.msg-bubble { padding: 12px 16px; border-radius: 14px; }

/* Meta row */
.msg-meta { gap: 10px; padding: 2px 6px; }
.msg-time  { font-size: .72rem; }
.copy-btn  { font-size: .75rem; }

/* Code copy button — match new bubble padding */

/* ── Chat input area — redesigned ───────────────────────────────────────── */
.chat-input-wrap {
  padding: 12px 20px 16px;
}

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 18px;
  padding: 0;
  box-shadow: 0 2px 16px rgba(0,0,0,.08);
  overflow: hidden;
  transition: border-color .15s, box-shadow .15s;
}
.chat-input-row:focus-within {
  border-color: var(--primary);
  box-shadow: 0 2px 16px rgba(0,0,0,.06), 0 0 0 3px rgba(37,99,235,.1);
}
[data-theme="dark"] .chat-input-row {
  box-shadow: 0 2px 16px rgba(0,0,0,.3);
}

/* Attach button — left side, inside the box */
.attach-btn {
  align-self: flex-end;
  padding: 11px 10px 14px 14px;
  border-radius: 0;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  transition: color .15s;
}
.attach-btn:hover { color: var(--primary); background: none; }

/* Textarea — inside the box, no external border */
#chat-input {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font-size: .92rem;
  font-family: inherit;
  color: var(--text);
  outline: none;
  max-height: 180px;
  min-height: unset;
  overflow-y: auto;
  line-height: 1.5;
  padding: 13px 8px;
  vertical-align: middle;
  box-shadow: none;
  display: block;
}
#chat-input::placeholder { color: var(--muted); }
#chat-input:focus { border-color: transparent; box-shadow: none; }

/* Send button — right side, inside the box */
.send-btn {
  align-self: flex-end;
  margin: 7px 8px 7px 4px;
  width: 36px;
  height: 36px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(37,99,235,.4);
  transition: box-shadow .15s, transform .1s, background .15s;
}
.send-btn:hover   { box-shadow: 0 4px 14px rgba(37,99,235,.5); transform: scale(1.05); }
.send-btn:active  { transform: scale(.94); }
.send-btn:disabled { opacity: .35; cursor: not-allowed; box-shadow: none; transform: none; }
.chat-input-row.streaming .send-btn {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  box-shadow: 0 2px 8px rgba(239,68,68,.4);
}
.chat-input-row.streaming .send-btn:hover {
  box-shadow: 0 4px 14px rgba(239,68,68,.5);
}

/* Footer row below input */
.input-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 7px;
  padding: 0 4px;
}
.char-counter {
  font-size: .7rem;
  color: var(--border);
  font-variant-numeric: tabular-nums;
  min-width: 20px;
}
.chat-hint { font-size: .72rem; color: var(--muted); }

/* Welcome screen */
.welcome-screen h2 { font-size: 1.5rem; font-weight: 700; }
.welcome-screen p  { font-size: .92rem; }
.chip { font-size: .83rem; padding: 8px 16px; }

/* ── Typing dots (●●●) ──────────────────────────────────────────────────── */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 2px;
}
.typing-dots span {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--primary);
  opacity: 0.5;
  animation: tdot 1.3s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes tdot {
  0%, 70%, 100% { transform: translateY(0) scale(.8); opacity: .4; }
  35%           { transform: translateY(-5px) scale(1); opacity: 1; }
}

/* Override old cursor blink — keep it subtle */
.cursor-blink {
  font-weight: 400;
  font-size: .9em;
  color: var(--primary);
  opacity: .8;
}

/* Sidebar new-chat button */
.sidebar-new-chat { padding: 10px 12px 6px; }
.btn-primary.btn-full.btn-sm {
  padding: 14px 14px;
  font-size: .84rem;
  border-radius: 9px;
}

/* Model badge */
.model-badge {
  font-size: .68rem;
  padding: 2px 8px;
}

/* Admin sidebar polish */
.admin-sidebar { background: #0f172a; }
.admin-nav-link { font-size: .84rem; }
.admin-main h1 { font-size: 1.4rem; }

/* Settings pages — .settings-main is intentionally unpadded here; see the
   comment by its base rule above. (Previously this duplicated padding on
   top of account.php's own inner wrapper div, stacking to ~68px per side.) */

/* Auth cards */
.auth-card { padding: 40px 36px; }
.auth-logo h1 { font-size: 1.4rem; }

/* Buttons — slightly larger text */
.btn { font-size: .86rem; }
.btn-sm { font-size: .8rem; }


/* =========================================
   MODERN UI/UX UPGRADES (2026 EDITION)
   ========================================= */

/* 1. Adaptive Theme Engine & OLED — moved to assets/css/tokens.css */

/* 2. Glassmorphism & Depth */
body {
  background-image: var(--bg-mesh);
  background-attachment: fixed;
}

.app-sidebar,
.chat-header,
.chat-input-wrap,
.sys-prompt-bar {
  background: var(--surface-glass) !important;
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-color: var(--border-glass) !important;
}

/* Floating effect for input */
.chat-input-row {
  background: var(--surface-glass) !important;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 8px 32px rgba(0,0,0,0.06) !important;
}
[data-theme="dark"] .chat-input-row,
[data-theme="oled"] .chat-input-row {
  box-shadow: 0 8px 32px rgba(0,0,0,0.4) !important;
}

/* 3. Micro-Animations */
@keyframes slideUpFade {
  from { opacity: 0; transform: translateY(15px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg {
  animation: slideUpFade 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

/* Upgraded button bounce */
.btn, .send-btn, .session-item {
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) !important;
}

/* 4. Skeleton Loading */
@keyframes pulse-glow {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 0.8; }
}

.skeleton-msg {
  display: flex;
  gap: 10px;
  width: 100%;
  margin-bottom: 16px;
  animation: pulse-glow 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
  pointer-events: none;
}
/* Mirror .msg-user's real alignment (app.css ~line 595) so the "your
   question" skeleton row sits on the right like the message it stands in
   for, instead of stretching full-width on the left like every other row. */
.skeleton-msg.user {
  align-self: flex-end;
  flex-direction: row-reverse;
  max-width: 92%;
}
.skeleton-msg .msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
}
.skeleton-msg .msg-bubble {
  height: 70px;
  /* Match the real bubbles' tucked corner (assets/css/dashboard.css):
     AI = top-left square, user = top-right square. */
  border-radius: var(--r-lg, 20px);
  border-top-left-radius: var(--r-xs, 6px);
  background: linear-gradient(90deg, var(--surface) 0%, var(--border) 50%, var(--surface) 100%);
  background-size: 200% 100%;
}
.skeleton-msg.user .msg-bubble {
  border-top-left-radius: var(--r-lg, 20px);
  border-top-right-radius: var(--r-xs, 6px);
}

/* 5. Overlay Mode (Floating Chat Interface) */
.overlay-mode .app-shell {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 440px;
  height: 80vh;
  max-height: 850px;
  border-radius: 24px;
  box-shadow: 0 24px 48px rgba(0,0,0,0.25), 0 0 0 1px var(--border-glass);
  overflow: hidden;
  z-index: 9999;
  transform-origin: bottom right;
  animation: slideUpFade 0.4s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.overlay-mode .app-sidebar {
  position: absolute;
  left: -260px;
  z-index: 50;
  height: 100%;
  box-shadow: 20px 0 40px rgba(0,0,0,0.1);
}
.overlay-mode .app-sidebar.open {
  left: 0;
}
.overlay-mode .menu-toggle {
  display: flex !important;
}

@media (max-width: 768px) {
  .overlay-mode .app-shell {
    bottom: 0; right: 0; width: 100%; height: 100dvh; border-radius: 0;
  }
}


/* =========================================
   AUTOMATION & MACRO TOOLS (MAGIC UI)
   ========================================= */

/* Magic Toolbar (Text Highlight) */
.magic-toolbar {
  position: absolute;
  z-index: 1000;
  display: flex;
  gap: 4px;
  padding: 6px;
  background: var(--surface-glass, rgba(255,255,255,0.8));
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-glass, rgba(0,0,0,0.1));
  border-radius: 12px;
  box-shadow: 0 10px 32px rgba(0,0,0,0.15), 0 0 0 1px var(--border-glass);
  transform: translateY(-10px);
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.magic-toolbar.active {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.magic-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.15s ease;
}

.magic-btn:hover {
  background: var(--primary-light);
  color: var(--primary);
  border-color: var(--primary);
  transform: translateY(-2px);
}

/* Slash Command Menu */
.slash-menu {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 0;
  width: 320px;
  max-height: 250px;
  overflow-y: auto;
  background: var(--surface-glass, rgba(255,255,255,0.85));
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid var(--border-glass, rgba(0,0,0,0.1));
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.15);
  display: none;
  flex-direction: column;
  padding: 6px;
  z-index: 100;
}

.slash-menu.active {
  display: flex;
  animation: slideUpFade 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.slash-item {
  display: flex;
  flex-direction: column;
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
  border: none;
  background: transparent;
  text-align: left;
  transition: background 0.1s;
}

.slash-item.selected, .slash-item:hover {
  background: var(--primary-light);
}

.slash-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--primary);
  display: flex;
  justify-content: space-between;
}

.slash-desc {
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

/* Active Agent Chip */
.active-agent-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 20px;
  margin-bottom: 8px;
  box-shadow: 0 4px 12px rgba(37,99,235,0.3);
  animation: pulse-glow 2s infinite;
}

.active-agent-chip .cancel-agent {
  background: rgba(0,0,0,0.2);
  border: none;
  color: #fff;
  border-radius: 50%;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.6rem;
}


/* =========================================
   V3: ARTIFACTS CANVAS & SPLIT SCREEN
   ========================================= */

.app-shell {
  display: flex;
  width: 100vw;
  height: 100vh; /* or 100dvh */
  overflow: hidden;
  transition: all 0.3s ease;
}

.chat-container {
  flex: 1;
  min-width: 300px;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
}

.artifacts-panel {
  width: 0;
  opacity: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width 0.3s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s ease;
  overflow: hidden;
}

.artifacts-panel.open {
  width: 50vw;
  max-width: 800px;
  opacity: 1;
}

.artifacts-header {
  height: 60px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface-glass);
  backdrop-filter: blur(12px);
}

.artifacts-content {
  flex: 1;
  overflow: auto;
  position: relative;
  background: #fff; /* Always white for proper web/document rendering */
}

.artifacts-content iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.artifacts-content .mermaid-render {
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100%;
  background: var(--surface);
}

/* Document Editor */
.document-editor {
  width: 100%;
  height: 100%;
  padding: 40px;
  outline: none;
  font-family: system-ui, sans-serif;
  font-size: 1.1rem;
  line-height: 1.8;
  color: #000;
  max-width: 800px;
  margin: 0 auto;
}

@media (max-width: 768px) {
  .artifacts-panel.open {
    position: fixed;
    top: 0; right: 0; bottom: 0;
    width: 100vw;
    z-index: 1000;
  }
}


/* =========================================
   MOBILE RESPONSIVENESS & AI ENHANCER
   ========================================= */

@media (max-width: 768px) {
    .app-shell {
        flex-direction: column;
    }

    /* Hamburger shows */
    .hamburger-btn {
        display: flex !important;
        align-items: center;
        justify-content: center;
    }

    /* Sidebar becomes off-canvas drawer */
    .app-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100dvh;
        width: 280px;
        max-width: 85vw;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
        box-shadow: none;
    }

    .app-sidebar.mobile-open {
        transform: translateX(0);
        box-shadow: 4px 0 24px rgba(0,0,0,0.3);
    }

    /* Chat Container adjusts */
    .chat-container {
        width: 100vw;
        height: 100dvh; /* Use dynamic viewport height for mobile browsers */
    }

    /* Header & Messages padding tightened */
    .chat-header {
        padding: 10px 16px;
    }

    .chat-messages {
        padding: 16px 12px;
        padding-bottom: 120px; /* Safe space for bottom input */
    }

    .msg {
        padding: 16px 12px;
        gap: 12px;
    }

    .msg-avatar {
        width: 28px;
        height: 28px;
        font-size: 0.75rem;
    }

    /* Input area fixed at bottom with iOS safe area support */
    .chat-input-wrap {
        padding: 8px 12px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        border-radius: 16px 16px 0 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
    }

    /* Hide complex model selector on very small screens, show abbreviated */
    .model-selector select {
        max-width: 120px;
        font-size: 0.8rem;
    }

    .header-actions .btn:not(.model-selector) {
        padding: 6px;
    }
    .header-actions .btn span {
        display: none; /* Hide text on buttons like 'Export PDF' */
    }
}

/* Magic Wand Glow */
.enhance-prompt-btn:hover {
    background: var(--primary-light);
    transform: scale(1.05);
}


/* =========================================
   INPUT ROW REFACTORING & ALIGNMENT
   ========================================= */
.chat-input-row {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    padding: 12px 16px !important;
    border-radius: 24px !important;
    background: var(--surface-glass) !important;
    backdrop-filter: blur(24px) !important;
    -webkit-backdrop-filter: blur(24px) !important;
    border: 1px solid var(--border) !important;
    box-shadow: 0 8px 32px rgba(0,0,0,0.08) !important;
    max-width: 800px;
    margin: 0 auto;
    width: 100%;
}

#chat-input {
    width: 100% !important;
    min-height: 44px !important;
    /* No max-height here (was 250px !important) — _taFit()'s JS auto-grow
       (dashboard.js) sets its own inline height/overflow-y against a
       dynamic cap (desktop: half the viewport, min 260px). A fixed 250px
       cap here silently clipped the textarea below that once content grew
       past it, while the JS's overflow-y logic — comparing against its own,
       larger cap — hadn't yet decided to turn scrolling on. Net effect: text
       got visually cut off with no scrollbar once it grew past ~4 lines. */
    border: none !important;
    background: transparent !important;
    padding: 0 4px !important;
    margin: 0 !important;
    resize: none !important;
    font-size: 1.05rem !important;
    line-height: 1.6 !important;
    color: var(--text) !important;
    font-family: inherit !important;
}

#chat-input:focus {
    outline: none !important;
    box-shadow: none !important;
}

.chat-input-toolbar {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    width: 100% !important;
}

.chat-input-toolbar .toolbar-group {
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
}

.tool-icon-btn {
    width: 36px !important;
    height: 36px !important;
    padding: 0 !important;
    border-radius: 50% !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: transparent !important;
    border: 1px solid transparent !important;
    color: var(--muted) !important;
    cursor: pointer !important;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1) !important;
}

.tool-icon-btn:hover {
    background: var(--surface) !important;
    border-color: var(--border) !important;
    color: var(--text) !important;
    transform: translateY(-2px) !important;
}

.tool-icon-btn.active {
    color: var(--danger) !important;
    background: rgba(239, 68, 68, 0.1) !important;
    border-color: rgba(239, 68, 68, 0.3) !important;
}

.send-btn-modern {
    width: 36px !important;
    height: 36px !important;
    border-radius: 50% !important;
    padding: 0 !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: var(--primary) !important;
    color: #fff !important;
    border: none !important;
    cursor: pointer !important;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1) !important;
}

.send-btn-modern:hover {
    transform: translateY(-2px) scale(1.05) !important;
    box-shadow: 0 4px 16px var(--primary-light) !important;
    background: var(--primary-hover) !important;
}


/* =========================================
   LAYOUT OVERFLOW FIXES (app-main)
   ========================================= */
body, html {
    margin: 0; padding: 0;
    width: 100vw; height: 100vh;
    overflow: hidden; /* Stop global scrolling */
    background: var(--bg);
}

.app-shell {
    display: flex;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

.app-sidebar {
    width: 260px;
    min-width: 260px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    transition: transform 0.3s ease;
    z-index: 50;
}

.app-main {
    flex: 1;
    min-width: 0; /* CRITICAL: prevents flex child from expanding beyond parent */
    max-width: calc(100vw - 260px); /* Force width constraint */
    display: flex;
    flex-direction: column;
    height: 100vh;
    position: relative;
    background: var(--bg);
    /* overflow-x managed per element */
}

/* Ensure messages don't break the layout */
.msg-content {
    word-break: break-word;
    overflow-wrap: anywhere;
    max-width: 100%;
    overflow-x: auto;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    width: 100%;
}

/* =========================================
   NEW SIDEBAR & USER DROPDOWN STYLES
   ========================================= */
.sidebar-header { padding: 16px; }
.sidebar-nav-group { padding: 0 12px; display: flex; flex-direction: column; gap: 4px; margin-bottom: 16px; }
.sidebar-nav-item {
    display: flex; align-items: center; gap: 10px; padding: 10px 12px;
    color: var(--text); text-decoration: none; border-radius: 8px;
    font-size: 0.9rem; font-weight: 500; transition: all 0.2s;
}
.sidebar-nav-item:hover, .sidebar-nav-item.active { background: var(--surface-hover); color: var(--primary); }
.sidebar-nav-item svg { width: 18px; height: 18px; color: var(--muted); }
.sidebar-nav-item:hover svg, .sidebar-nav-item.active svg { color: var(--primary); }

.sidebar-body { flex: 1; overflow-y: auto; padding: 0 12px 12px 12px; }
.nav-label { font-size: 0.75rem; text-transform: uppercase; font-weight: 700; color: var(--muted); margin: 12px 0 8px 12px; letter-spacing: 0.5px; }

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border);
    position: relative;
}

.user-profile-btn {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 10px; background: transparent; border: none; border-radius: 10px;
    cursor: pointer; transition: background 0.2s; text-align: left;
}
.user-profile-btn:hover { background: var(--surface-hover); }
.user-profile-btn .avatar {
    width: 32px; height: 32px; border-radius: 8px; background: var(--primary);
    color: white; display: flex; align-items: center; justify-content: center; font-weight: bold;
}
.user-profile-btn .info { flex: 1; overflow: hidden; }
.user-profile-btn .name { display: block; font-size: 0.9rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-profile-btn .role { display: block; font-size: 0.75rem; color: var(--muted); text-transform: capitalize; }
.user-profile-btn .chevron { width: 16px; height: 16px; color: var(--muted); transition: transform 0.2s; }
.user-profile-btn.open .chevron { transform: rotate(180deg); }

/* The Modern Popup Dropdown */
.user-menu-dropdown {
    position: absolute; bottom: calc(100% + 10px); left: 12px; width: calc(100% - 24px);
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2); padding: 8px;
    display: flex; flex-direction: column; gap: 4px;
    opacity: 0; visibility: hidden; transform: translateY(10px);
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    z-index: 100;
}
.user-menu-dropdown.show { opacity: 1; visibility: visible; transform: translateY(0); }
.user-menu-dropdown a {
    display: flex; align-items: center; gap: 10px; padding: 10px 12px;
    color: var(--text); text-decoration: none; border-radius: 8px;
    font-size: 0.9rem; font-weight: 500; transition: background 0.2s;
}
.user-menu-dropdown a:hover { background: var(--surface-hover); }
.user-menu-dropdown a.danger { color: var(--danger); }
.user-menu-dropdown a.danger:hover { background: rgba(239,68,68,0.1); }
.user-menu-dropdown hr { border: none; border-top: 1px solid var(--border); margin: 4px 0; }

/* Hide old sf-nav just in case */
.sf-nav { display: none !important; }

/* Remove any accidental red borders from debugging */
* { outline: none !important; }

@media (max-width: 768px) {
    .app-main { max-width: 100vw; }
}


/* =========================================
   FLEXBOX OVERFLOW FIXES (app-main)
   ========================================= */
body, html {
    margin: 0; padding: 0;
    width: 100vw; height: 100vh;
    overflow: hidden !important; 
    background: var(--bg);
}

.app-shell {
    display: flex;
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    overflow: hidden;
}

.app-sidebar {
    width: 260px;
    min-width: 260px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    z-index: 50;
    height: 100vh;
}

.app-main {
    flex: 1 1 auto;
    min-width: 0; /* CRITICAL: prevents flex child from overflowing */
    max-width: calc(100vw - 260px); /* Force strict width constraint */
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg);
    position: relative;
    /* overflow-x managed per element */
}

@media (max-width: 768px) {
    .app-main { max-width: 100vw; }
}

/* Ensure long text, URLs, or markdown doesn't break boundaries */
.msg-content, .msg-bubble, .markdown-body, pre {
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: anywhere;
    max-width: 100%;
}
.msg-content pre {
    overflow-x: auto;
    max-width: 100%;
}

.chat-messages-wrap {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden !important;
    width: 100%;
    position: relative;
}

/* Clean up any red/dashed debug borders */
* { outline: none !important; }
.debug, .layout-debug, .sf-nav { display: none !important; }

/* =========================================
   SETTINGS & MANAGEMENT MODAL
   ========================================= */
.mng-modal-overlay {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.5); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    display: flex; align-items: center; justify-content: center;
    z-index: 9999;
    opacity: 0; visibility: hidden; pointer-events: none;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.mng-modal-overlay.show { opacity: 1; visibility: visible; pointer-events: auto; }

.mng-modal-content {
    background: var(--surface); border: 1px solid var(--border);
    width: 90%; max-width: 700px; border-radius: 24px;
    box-shadow: 0 24px 80px rgba(0,0,0,0.4);
    transform: scale(0.95) translateY(20px); transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    overflow: hidden;
}
.mng-modal-overlay.show .mng-modal-content { transform: scale(1) translateY(0); }

.mng-modal-header {
    padding: 24px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
}
.mng-modal-header h2 { margin: 0; font-size: 1.4rem; color: var(--text); font-weight: 700; }

.mng-modal-body { padding: 24px; background: var(--bg); }

.mng-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px;
}

.mng-card {
    display: flex; align-items: flex-start; gap: 16px; padding: 20px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
    text-decoration: none; color: inherit; transition: all 0.2s;
}
.mng-card:hover {
    transform: translateY(-4px); box-shadow: 0 12px 24px rgba(0,0,0,0.1); border-color: var(--primary);
}
.mng-icon {
    width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.mng-text h3 { margin: 0 0 4px 0; font-size: 1.1rem; font-weight: 600; color: var(--text); }
.mng-text p { margin: 0; font-size: 0.85rem; color: var(--muted); line-height: 1.4; }

/* ═══════════════════════════════════════════════════════════════
   MB AI WEB v1 — THEME POLISH
   ═══════════════════════════════════════════════════════════════ */

/* ── Settings Sidebar ───────────────────────────────────────── */

@media (max-width: 768px) {
  /* ── Full height using dynamic viewport (shrinks when keyboard opens) ── */
  .app-shell {
    height: 100dvh !important;
    min-height: 0;
  }

  .app-main {
    height: 100dvh !important;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  /* Chat messages fill available space above composer */
  .chat-messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px 6px !important;
    gap: 6px;
  }

  /* Composer always visible at bottom, never hidden behind keyboard */
  .chat-input-wrap {
    flex-shrink: 0 !important;
    padding: 8px 8px max(8px, env(safe-area-inset-bottom)) !important;
    position: relative;
    z-index: 10;
  }

  .composer-card {
    border-radius: 14px !important;
    padding: 8px 8px 6px !important;
  }

  #chat-input {
    font-size: 16px !important; /* Prevents iOS auto-zoom on focus */
    max-height: 100px;
  }

  /* ── Chat layout: use full width ── */
  .msg {
    max-width: 100% !important;
    width: 100% !important;
    gap: 6px !important;
  }

  /* Hide avatar on AI messages to gain space */
  .msg-ai .msg-avatar { display: none; }

  .msg-ai .msg-body { width: 100%; max-width: 100%; }
  .msg-ai .msg-bubble {
    width: 100%;
    max-width: 100%;
    border-radius: 10px !important;
    padding: 10px 12px;
    font-size: .88rem;
  }

  .msg-user .msg-avatar { width: 24px; height: 24px; font-size: .6rem; }
  .msg-user .msg-bubble {
    max-width: calc(100vw - 60px);
    padding: 9px 11px;
    font-size: .88rem;
  }

  /* Wrap code to prevent overflow */
  .code-block-wrap { max-width: 100%; overflow: hidden; }
  .code-block-wrap pre { overflow-x: hidden; }
  .code-block-wrap pre code {
    white-space: pre-wrap !important;
    word-break: break-all !important;
    overflow-wrap: break-word !important;
    min-width: 0 !important;
    font-size: .75rem;
    line-height: 1.55;
  }
  .msg-bubble pre code {
    white-space: pre-wrap !important;
    word-break: break-all !important;
    overflow-wrap: break-word !important;
  }
  .msg-bubble { overflow-wrap: break-word; word-break: break-word; }

  /* Code header smaller */
  .code-header { padding: 5px 8px 5px 10px; }
  .code-lang { font-size: .65rem; }
  .code-copy-btn { padding: 2px 8px; font-size: .68rem; }

  /* Hide label text on tool buttons to save space */
  .composer-tool-btn .btn-label-hide-mobile { display: none; }
}

/* ═══════════════════════════════════════════════════════════════════
   PHASE 18 — QUICK WINS: REASONING UI, FOLLOW-UPS
   ═══════════════════════════════════════════════════════════════════ */

/* Reasoning <think> blocks */
.think-block {
  border: 1px solid #c4b5fd;
  border-left: 3px solid #7c3aed;
  border-radius: 8px;
  margin: 8px 0;
  background: #faf5ff;
  overflow: hidden;
  font-size: .82rem;
}
[data-theme="dark"] .think-block { background: #1e1b2e; border-color: #4c1d95; border-left-color: #7c3aed; }
.think-summary {
  cursor: pointer;
  padding: 7px 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  color: #6d28d9;
  font-weight: 600;
  font-size: .78rem;
  user-select: none;
  list-style: none;
}
[data-theme="dark"] .think-summary { color: #a78bfa; }
.think-summary::-webkit-details-marker { display: none; }
.think-content {
  padding: 10px 14px;
  border-top: 1px solid #c4b5fd;
  color: var(--muted);
  white-space: pre-wrap;
  font-family: monospace;
  font-size: .77rem;
  line-height: 1.6;
  max-height: 280px;
  overflow-y: auto;
}
[data-theme="dark"] .think-content { border-top-color: #4c1d95; }

/* Follow-up suggestion chips */
.followup-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.followup-label { font-size: .7rem; color: var(--muted); font-weight: 600; flex-basis: 100%; }
.followup-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: .75rem;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  transition: background .15s, color .15s, border-color .15s;
  text-align: left;
}
.followup-chip:hover { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Web-search source citations */
.citation-strip { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; align-items: center; }
.citation-label {
  font-size: .68rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--muted); margin-right: 2px;
}
.citation-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--primary-light); border: 1px solid var(--border); border-radius: 20px;
  padding: 3px 10px 3px 4px; font-size: .72rem; color: var(--text);
  text-decoration: none; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.citation-chip:hover { background: var(--primary); color: #fff; border-color: var(--primary); }
.citation-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; border-radius: 50%; flex-shrink: 0;
  background: var(--primary); color: #fff; font-size: .64rem; font-weight: 700;
}
.citation-chip:hover .citation-num { background: #fff; color: var(--primary); }

/* ═══════════════════════════════════════════════════════════════════
   PHASE 19 — PERSONAS & FOLDERS
   ═══════════════════════════════════════════════════════════════════ */

/* Persona picker near sys-prompt bar */
.persona-select-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.persona-select-wrap select {
  flex: 1;
  font-size: .76rem;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}
.persona-save-btn {
  font-size: .72rem;
  padding: 4px 9px;
  white-space: nowrap;
}

/* Folder items in sidebar */
.folder-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px 5px 12px;
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border);
}
.folder-item:hover { background: var(--surface); color: var(--text); }
.folder-item.open { color: var(--primary); }
.folder-caret { transition: transform .2s; }
.folder-item.open .folder-caret { transform: rotate(90deg); }
.folder-sessions { display: none; }
.folder-sessions.open { display: block; }
.folder-session-indent { padding-left: 24px; }

/* New folder button */
.folder-add-btn {
  font-size: .72rem;
  color: var(--muted);
  padding: 3px 10px 3px 14px;
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  background: none;
  border: none;
  font-family: inherit;
  width: 100%;
  text-align: left;
}
.folder-add-btn:hover { color: var(--primary); }

/* ═══════════════════════════════════════════════════════════════════
   PHASE 20 — WEB GROUNDING
   ═══════════════════════════════════════════════════════════════════ */

.url-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 20px;
  font-size: .73rem;
  color: #1d4ed8;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
[data-theme="dark"] .url-chip { background: #1e3a5f; border-color: #2563eb; color: #93c5fd; }
.url-chip .url-chip-del { cursor: pointer; color: var(--muted); flex-shrink: 0; background: none; border: none; padding: 0; line-height: 1; }
.url-chips-row { display: flex; flex-wrap: wrap; gap: 4px; padding: 4px 10px; }

/* ═══════════════════════════════════════════════════════════════════
   PHASE 21 — MEMORY
   ═══════════════════════════════════════════════════════════════════ */

.memory-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: .68rem;
  padding: 2px 8px;
  border-radius: 20px;
  background: #fef3c7;
  color: #92400e;
  font-weight: 600;
  border: 1px solid #fcd34d;
}
[data-theme="dark"] .memory-badge { background: #3b2a00; color: #fbbf24; border-color: #78350f; }

/* ═══════════════════════════════════════════════════════════════════
   PHASE 22 — 2FA & SECURITY
   ═══════════════════════════════════════════════════════════════════ */

.totp-qr-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.totp-qr-wrap img { border-radius: 8px; border: 2px solid var(--border); }
.totp-secret-box {
  font-family: monospace;
  font-size: .85rem;
  letter-spacing: .08em;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 14px;
  text-align: center;
  user-select: all;
}
.session-device-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: .82rem;
}
.session-device-row:last-child { border-bottom: none; }
.session-current-badge {
  font-size: .65rem;
  background: #dcfce7;
  color: #166534;
  padding: 2px 7px;
  border-radius: 20px;
  font-weight: 700;
}

/* ═══════════════════════════════════════════════════════════════════
   PHASE 23 — ANALYTICS
   ═══════════════════════════════════════════════════════════════════ */

.analytics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.analytics-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
}
.analytics-card .ac-value {
  font-size: 2rem;
  font-weight: 800;
  color: var(--text);
  line-height: 1;
}
.analytics-card .ac-label {
  font-size: .75rem;
  color: var(--muted);
  margin-top: 4px;
}
.analytics-card .ac-delta {
  font-size: .72rem;
  margin-top: 6px;
  font-weight: 600;
}
.ac-delta.up { color: #16a34a; }
.ac-delta.down { color: #dc2626; }
.chart-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
}
.chart-wrap h3 { font-size: .85rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 16px; }
.chart-bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  font-size: .78rem;
}
.chart-bar-label { min-width: 80px; color: var(--muted); text-align: right; }
.chart-bar-track { flex: 1; height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; }
.chart-bar-fill  { height: 100%; background: var(--primary); border-radius: 4px; transition: width .4s; }
.chart-bar-val   { min-width: 40px; color: var(--text); font-weight: 600; }

/* ═══════════════════════════════════════════════════════════════════
   PHASE 24 — COMMAND PALETTE
   ═══════════════════════════════════════════════════════════════════ */

#cmd-palette {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9000;
  background: rgba(0,0,0,.5);
  align-items: flex-start;
  justify-content: center;
  padding-top: 80px;
}
#cmd-palette.open { display: flex; }
#cmd-palette-box {
  width: 100%;
  max-width: 560px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 24px 48px rgba(0,0,0,.25);
  overflow: hidden;
}
#cmd-palette-input {
  width: 100%;
  padding: 14px 16px;
  font-size: .95rem;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  outline: none;
  font-family: inherit;
}
#cmd-palette-results {
  max-height: 360px;
  overflow-y: auto;
}
.cmd-result {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  cursor: pointer;
  font-size: .84rem;
  transition: background .1s;
}
.cmd-result:hover, .cmd-result.selected { background: var(--primary); color: #fff; }
.cmd-result:hover .cmd-sub, .cmd-result.selected .cmd-sub { color: rgba(255,255,255,.7); }
.cmd-result-icon { width: 28px; height: 28px; border-radius: 7px; background: var(--border); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.cmd-result:hover .cmd-result-icon, .cmd-result.selected .cmd-result-icon { background: rgba(255,255,255,.2); }
.cmd-main { flex: 1; font-weight: 500; }
.cmd-sub { font-size: .72rem; color: var(--muted); }
.cmd-hint { font-size: .7rem; color: var(--muted); }
.cmd-section-header { font-size: .68rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; padding: 8px 16px 4px; }

/* ═══════════════════════════════════════════════════════════════════
   PHASE 25 — CODE ARTIFACTS (SANDBOX PREVIEW)
   ═══════════════════════════════════════════════════════════════════ */

.code-run-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  font-size: .72rem;
  font-weight: 600;
  background: #16a34a;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s;
}
.code-run-btn:hover { background: #15803d; }
#artifact-modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 8000;
  background: rgba(0,0,0,.6);
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#artifact-modal.open { display: flex; }
#artifact-box {
  width: 100%;
  max-width: 900px;
  height: 80vh;
  background: var(--surface);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 48px rgba(0,0,0,.3);
}
#artifact-header {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
#artifact-header h3 { margin: 0; font-size: .88rem; flex: 1; }
#artifact-iframe {
  flex: 1;
  border: none;
  background: #fff;
}

/* ═══════════════════════════════════════════════════════════════════
   PHASE 26 — ACCESSIBILITY
   ═══════════════════════════════════════════════════════════════════ */

/* Better focus rings */
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ═══════════════════════════════════════════════════════════════════
   PHASE 29 — CREDITS & MONETIZATION
   ═══════════════════════════════════════════════════════════════════ */

.credit-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: .7rem;
  padding: 3px 9px;
  border-radius: 20px;
  background: #fef3c7;
  color: #78350f;
  font-weight: 700;
  border: 1px solid #fde68a;
  cursor: default;
}
[data-theme="dark"] .credit-badge { background: #3b2000; color: #fcd34d; border-color: #92400e; }
.credit-badge.low { background: #fee2e2; color: #991b1b; border-color: #fca5a5; }
[data-theme="dark"] .credit-badge.low { background: #3b0000; color: #f87171; border-color: #7f1d1d; }
.plan-card {
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  text-align: center;
  transition: border-color .2s;
}
.plan-card.featured { border-color: var(--primary); }
.plan-card .plan-price { font-size: 2.2rem; font-weight: 800; }
.plan-card .plan-name  { font-size: 1rem; font-weight: 700; margin-bottom: 8px; }
.plan-card .plan-credits { font-size: .82rem; color: var(--muted); margin-bottom: 16px; }

/* ── Billing / admin components (Phase 1-8) ──────────────────────────────────
   Shared classes used by admin billing/invoices/taxes/currencies pages and the
   account billing tab. Built on the existing design tokens so they theme
   automatically. */
.admin-content { display: block; }
.admin-page-header { margin-bottom: 24px; }
.admin-page-header h1 { margin: 0 0 4px; }
.admin-page-sub { font-size: .85rem; color: var(--muted); margin: 0; line-height: 1.5; }

.admin-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; overflow: hidden;
}
.admin-card-header {
  font-size: .78rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted);
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.admin-card-body { padding: 18px; }

.admin-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.admin-table thead th {
  text-align: left; font-size: .68rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted);
  padding: 11px 16px; border-bottom: 2px solid var(--border); white-space: nowrap;
}
.admin-table tbody td { padding: 11px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.admin-table tbody tr:last-child td { border-bottom: none; }
.admin-table tbody tr:hover { background: color-mix(in srgb, var(--primary) 4%, transparent); }

/* Form controls (Bootstrap-style names used by the billing UI) */
.form-control {
  width: 100%; padding: 8px 11px; font-size: .86rem; font-family: inherit;
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px;
  transition: border-color .15s, box-shadow .15s; box-sizing: border-box;
}
.form-control:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}
.form-control-sm { padding: 5px 8px; font-size: .8rem; }
.form-label { display: block; font-size: .8rem; font-weight: 600; color: var(--text); margin-bottom: 5px; }
.form-hint  { display: block; font-size: .74rem; color: var(--muted); margin-top: 4px; }

.settings-section-title {
  font-size: .68rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .08em; color: var(--muted); margin: 0 0 12px;
}
