/* ============================================================
   XR Chat — stylesheet
   Two themes driven by [data-theme] on <html>.
   ============================================================ */

:root {
  --bg:            #ffffff;
  --bg-elev:       #f6f7f9;
  --bg-sunk:       #eceef2;
  --sidebar:       #f2f3f6;
  --text:          #12151a;
  --text-dim:      #5b6472;
  --text-faint:    #8b95a4;
  --border:        #e2e5ea;
  --accent:        #4f46e5;
  --accent-soft:   #eef2ff;
  --accent-text:   #ffffff;
  --bubble-user:   #4f46e5;
  --bubble-ai:     #f6f7f9;
  --danger:        #dc2626;
  --ok:            #16a34a;
  --code-bg:       #1e2430;
  --code-text:     #e6edf3;
  --radius:        14px;
  --shadow:        0 1px 3px rgba(16,24,40,.08), 0 8px 24px rgba(16,24,40,.06);
  --sidebar-w:     268px;
}

[data-theme="dark"] {
  --bg:            #0b0d10;
  --bg-elev:       #14181d;
  --bg-sunk:       #1b2027;
  --sidebar:       #101317;
  --text:          #e8ecf1;
  --text-dim:      #9aa5b4;
  --text-faint:    #6b7688;
  --border:        #232932;
  --accent:        #6366f1;
  --accent-soft:   #1c1f3a;
  --accent-text:   #ffffff;
  --bubble-user:   #4f46e5;
  --bubble-ai:     #14181d;
  --danger:        #f87171;
  --ok:            #4ade80;
  --code-bg:       #0d1117;
  --code-text:     #e6edf3;
  --shadow:        0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Noto Sans", "Noto Sans Bengali", "Noto Sans Arabic",
               "Noto Sans Devanagari", "Hiragino Sans", "Microsoft YaHei",
               Helvetica, Arial, sans-serif;
  font-size: 15.5px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

svg {
  width: 20px; height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

.app { display: flex; height: 100dvh; overflow: hidden; }

/* ─────────────────────── Sidebar ─────────────────────── */

.sidebar {
  width: var(--sidebar-w);
  flex: none;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 12px;
  z-index: 40;
}

.sidebar-head { display: flex; align-items: center; justify-content: space-between; }

.logo { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -.01em; }

.logo-mark {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 13px; font-weight: 700; letter-spacing: .02em;
}

.sidebar-close { display: none; }

.new-chat {
  display: flex; align-items: center; gap: 9px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg);
  color: var(--text);
  font: inherit; font-weight: 550;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.new-chat:hover { background: var(--accent-soft); border-color: var(--accent); }

.history-label {
  font-size: 11.5px; font-weight: 650;
  text-transform: uppercase; letter-spacing: .07em;
  color: var(--text-faint);
  padding: 4px 6px 0;
}

.history { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; min-height: 0; }

.history-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border-radius: 9px;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  border: none; background: none; font-family: inherit;
  text-align: left; width: 100%;
}
.history-item:hover { background: var(--bg-sunk); color: var(--text); }
.history-item.active { background: var(--accent-soft); color: var(--text); font-weight: 550; }
.history-item .label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-item .del { opacity: 0; padding: 2px; border-radius: 5px; color: var(--text-faint); border: none; background: none; cursor: pointer; }
.history-item:hover .del { opacity: 1; }
.history-item .del:hover { color: var(--danger); }
.history-item .del svg { width: 15px; height: 15px; }

.sidebar-foot { border-top: 1px solid var(--border); padding-top: 10px; display: flex; flex-direction: column; gap: 2px; }

.ghost-btn {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 10px;
  border: none; border-radius: 9px;
  background: none; color: var(--text-dim);
  font: inherit; font-size: 14px;
  cursor: pointer; text-align: left;
}
.ghost-btn:hover { background: var(--bg-sunk); color: var(--text); }
.ghost-btn.danger:hover { color: var(--danger); }
.ghost-btn svg { width: 17px; height: 17px; }

.scrim {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 35;
}
.scrim.show { display: block; }

/* ─────────────────────── Main column ─────────────────────── */

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 18px;
  border-bottom: 1px solid var(--border);
  flex: none;
}

.icon-btn {
  display: none;
  padding: 7px;
  border: none; border-radius: 9px;
  background: none; color: var(--text-dim);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-sunk); color: var(--text); }

.topbar-title {
  flex: 1;
  font-size: 14.5px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Model picker in the top bar */
.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.model-picker {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg-elev);
  color: var(--text-dim);
  cursor: pointer;
  transition: border-color .15s, color .15s;
  min-width: 0;
}
.model-picker:hover { border-color: var(--accent); color: var(--text); }
.model-picker:focus-within { border-color: var(--accent); }
.model-picker[hidden] { display: none; }
.model-picker svg { width: 15px; height: 15px; }

.model-picker select {
  border: none; outline: none;
  background: none; color: inherit;
  font: inherit; font-size: 13px; font-weight: 550;
  cursor: pointer;
  max-width: 170px;
  text-overflow: ellipsis;
}
/* The native dropdown list is painted by the OS, so its options need an
   explicit background or they render white-on-white in dark mode. */
.model-picker select option { background: var(--bg-elev); color: var(--text); }

.status { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-faint); }
.status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); }
.status.online .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.status.offline .dot { background: var(--danger); }

/* ─────────────────────── Thread ─────────────────────── */

.thread { flex: 1; overflow-y: auto; padding: 26px 18px 8px; scroll-behavior: smooth; }

.welcome { max-width: 760px; margin: 8vh auto 0; text-align: center; padding: 0 8px; }

.welcome-mark {
  width: 54px; height: 54px; margin: 0 auto 20px;
  display: grid; place-items: center;
  border-radius: 16px;
  background: var(--accent); color: var(--accent-text);
  font-weight: 700; font-size: 19px;
}

.welcome h1 { font-size: 27px; font-weight: 650; letter-spacing: -.02em; margin: 0 0 6px; }
.welcome-sub { color: var(--text-dim); margin: 0 0 30px; }

.starters { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }

.starter {
  padding: 13px 15px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elev);
  color: var(--text-dim);
  font: inherit; font-size: 14px;
  cursor: pointer;
  transition: border-color .15s, color .15s, transform .12s;
}
.starter:hover { border-color: var(--accent); color: var(--text); transform: translateY(-1px); }

.msg { max-width: 780px; margin: 0 auto 22px; display: flex; gap: 13px; }
.msg.user { flex-direction: row-reverse; }

.avatar {
  width: 30px; height: 30px; flex: none;
  border-radius: 9px;
  display: grid; place-items: center;
  font-size: 11px; font-weight: 700;
  background: var(--accent); color: var(--accent-text);
}
.msg.user .avatar { background: var(--bg-sunk); color: var(--text-dim); }

.bubble-wrap { min-width: 0; max-width: 100%; }

.bubble {
  padding: 11px 15px;
  border-radius: var(--radius);
  background: var(--bubble-ai);
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--bubble-user);
  border-color: transparent;
  color: #fff;
}

.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin-bottom: 4px; }
.bubble h1, .bubble h2, .bubble h3 { margin: 14px 0 8px; font-size: 1.05em; font-weight: 650; }
.bubble h1:first-child, .bubble h2:first-child, .bubble h3:first-child { margin-top: 0; }
.bubble a { color: var(--accent); }
.msg.user .bubble a { color: #fff; text-decoration: underline; }

.bubble code {
  background: var(--bg-sunk);
  padding: 2px 5px;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .89em;
}
.msg.user .bubble code { background: rgba(255,255,255,.18); }

.bubble pre {
  background: var(--code-bg);
  color: var(--code-text);
  padding: 13px 15px;
  border-radius: 10px;
  overflow-x: auto;
  margin: 0 0 10px;
}
.bubble pre code { background: none; padding: 0; font-size: 13px; line-height: 1.55; }

/* Shown under a reply that stopped at the token ceiling. */
.cut-note {
  margin-top: 7px;
  padding: 7px 11px;
  border-radius: 9px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  color: var(--text-dim);
  font-size: 12.5px;
}

/* Transient notice in the thread (model switched, etc.) */
.thread-note {
  max-width: 780px;
  margin: 0 auto 18px;
  text-align: center;
}

.msg-actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-actions { opacity: 1; }
.msg.user .msg-actions { justify-content: flex-end; }

.act {
  display: flex; align-items: center; gap: 5px;
  padding: 4px 8px;
  border: none; border-radius: 7px;
  background: none; color: var(--text-faint);
  font: inherit; font-size: 12px;
  cursor: pointer;
}
.act:hover { background: var(--bg-sunk); color: var(--text); }
.act svg { width: 14px; height: 14px; }

/* Typing indicator */
.typing { display: flex; gap: 4px; padding: 4px 2px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-faint);
  animation: blink 1.3s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

/* Blinking caret while streaming */
.cursor::after {
  content: "";
  display: inline-block;
  width: 7px; height: 1.05em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--accent);
  animation: caret 1s steps(2) infinite;
}
@keyframes caret { 50% { opacity: 0; } }

.error-note {
  max-width: 780px; margin: 0 auto 20px;
  padding: 11px 15px;
  border: 1px solid var(--danger);
  border-radius: 11px;
  background: color-mix(in srgb, var(--danger) 9%, transparent);
  color: var(--danger);
  font-size: 14px;
}

/* ─────────────────────── Composer ─────────────────────── */

.composer-wrap { flex: none; padding: 8px 18px 14px; background: var(--bg); }

.composer {
  max-width: 780px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 8px 8px 16px;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--bg-elev);
  box-shadow: var(--shadow);
  transition: border-color .15s;
}
.composer:focus-within { border-color: var(--accent); }

.composer textarea {
  flex: 1;
  border: none; outline: none; resize: none;
  background: none; color: var(--text);
  font: inherit; line-height: 1.55;
  padding: 8px 0;
  max-height: 200px;
}
.composer textarea::placeholder { color: var(--text-faint); }

.send {
  width: 36px; height: 36px; flex: none;
  display: grid; place-items: center;
  border: none; border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  cursor: pointer;
  transition: opacity .15s, transform .12s;
}
.send:hover:not(:disabled) { transform: scale(1.06); }
.send:disabled { opacity: .35; cursor: not-allowed; }
.send svg { width: 17px; height: 17px; }
.send[hidden] { display: none; }   /* beats display:grid above */
.send.stop { background: var(--danger); }
.send.stop svg { fill: currentColor; stroke: none; }

.disclaimer { max-width: 780px; margin: 8px auto 0; text-align: center; font-size: 11.5px; color: var(--text-faint); }

/* ─────────────────────── Scrollbars ─────────────────────── */

.thread::-webkit-scrollbar, .history::-webkit-scrollbar { width: 9px; }
.thread::-webkit-scrollbar-thumb, .history::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 5px;
  border: 2px solid transparent; background-clip: content-box;
}
.thread::-webkit-scrollbar-thumb:hover { background: var(--text-faint); background-clip: content-box; }

/* ─────────────────────── Mobile ─────────────────────── */

@media (max-width: 860px) {
  /* The chat title and the model name cannot both fit on a phone;
     the model matters more, so the title steps aside. */
  .topbar-title { display: none; }
  .model-picker select { max-width: 120px; }
  .status .status-text { display: none; }

  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .sidebar-close, .icon-btn { display: grid; }
  .starters { grid-template-columns: 1fr; }
  .welcome { margin-top: 5vh; }
  .welcome h1 { font-size: 22px; }
  .thread { padding: 18px 12px 8px; }
  .composer-wrap { padding: 8px 12px 12px; }
  body { font-size: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
  .thread { scroll-behavior: auto; }
}
