/* ==============================================
   MOONCRUST-DEV — PREMIUM UI DESIGN SYSTEM
   ============================================== */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&family=JetBrains+Mono:wght@400;500;600&display=swap');

:root {
  --bg: #212121;
  --bg-panel: #171717;
  --bg-card: #2f2f2f;
  --bg-hover: #3a3a3a;
  --bg-input: #2f2f2f;

  --border: #383838;
  --border-glow: #444444;
  --border-focus: #888888;

  --purple: #ffffff;
  --indigo: #ffffff;
  --pink: #ffffff;
  --cyan: #ffffff;
  --emerald: #10b981;
  --grad: #ffffff;
  --grad-btn: #ffffff;
  --glow: rgba(255,255,255,0.05);

  --text: #ececec;
  --text2: #b4b4b4;
  --text3: #888888;

  --font: 'Inter', system-ui, sans-serif;
  --mono: 'JetBrains Mono', monospace;
  --sidebar-w: 260px;
  --radius: 8px;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  font-size: 15px;
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  min-height: -webkit-fill-available;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
}

::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.08); border-radius: 99px; }
::selection { background: rgba(139,92,246,0.3); }

/* LAYOUT */
.app {
  position: relative;
  display: flex;
  height: 100vh;
  height: 100dvh;
  min-height: -webkit-fill-available;
  width: 100%;
  overflow: hidden;
}

/* ================== SIDEBAR ================== */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: rgba(23, 23, 23, 0.98);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: max(20px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  gap: 14px;
  transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  z-index: 200;
  will-change: transform;
  overscroll-behavior: contain;
}

.sidebar__brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
}

.sidebar__brand-main {
  display: flex;
  flex-direction: column;
}

.sidebar__close {
  display: none;
  background: transparent;
  border: none;
  color: var(--text2);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.sidebar__close:hover,
.sidebar__close:active {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.brand-logo { display: none; }

@keyframes pulse {
  0%,100% { opacity:0.6; transform:scale(0.9); }
  50% { opacity:1; transform:scale(1.1); }
}

.brand-logo__icon { font-size: 24px; position: relative; z-index: 1; }

.brand-name {
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 6px;
}

.badge-tag {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 2px 6px;
  border-radius: 4px;
  background: #ffffff;
  color: #000000;
  text-transform: uppercase;
}

.btn-new {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px;
  background: #ffffff;
  border: 1px solid #ffffff;
  border-radius: var(--radius);
  color: #000000;
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-new:hover {
  background: #e5e5e5;
  transform: translateY(-1px);
}

.btn-sidebar-act {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px;
  margin-top: 6px;
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius);
  color: var(--text2);
  font-weight: 500;
  font-size: 12.5px;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-sidebar-act:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.2);
}

.sidebar__history {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding-right: 2px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.hist-empty {
  padding: 24px 12px;
  text-align: center;
  color: var(--text3);
  font-size: 12.5px;
  font-style: italic;
}

.hist-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 13px;
  color: var(--text2);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  gap: 6px;
  min-height: 42px;
  user-select: none;
  border: 1px solid transparent;
  white-space: nowrap;
}
.hist-item:hover { background: rgba(255,255,255,0.06); color: #fff; }
.hist-item.active { background: var(--glow); color: #fff; border-color: rgba(255,255,255,0.12); }
.hist-item.editing { background: rgba(255, 255, 255, 0.08); border-color: rgba(255, 255, 255, 0.2); }

.hist-item__main {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.hist-item__icon {
  flex-shrink: 0;
  color: var(--text3);
}
.hist-item.active .hist-item__icon {
  color: #fff;
}

.hist-item__label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  flex: 1;
}

.hist-item__actions {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.hist-item__btn {
  background: transparent;
  border: none;
  color: var(--text3);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  transition: all 0.15s;
  padding: 0;
}
.hist-item__btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
}
.hist-item__del:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.15);
}

.hist-item__input-wrap {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 6px;
}

.hist-item__input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border-focus);
  color: #fff;
  padding: 6px 10px;
  font-size: 13px;
  border-radius: 6px;
  outline: none;
  font-family: inherit;
}
.hist-item__input:focus {
  border-color: #fff;
}

.hist-item__save {
  color: var(--emerald) !important;
  font-weight: 700;
  font-size: 14px;
}
.hist-item__save:hover {
  background: rgba(16, 185, 129, 0.18) !important;
}

.hist-item__cancel {
  color: #ef4444 !important;
  font-size: 14px;
}
.hist-item__cancel:hover {
  background: rgba(239, 68, 68, 0.18) !important;
}

.sidebar__footer {
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.model-select-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
  border-radius: 8px;
  position: relative;
}

.model-select {
  background: transparent;
  border: none;
  color: var(--text2);
  font-size: 12px;
  font-weight: 600;
  width: 100%;
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}
.model-select option {
  background: var(--bg-panel);
  color: var(--text);
}

.btn-clear-cache {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 8px 10px;
  margin-top: 8px;
  background: transparent;
  border: 1px dashed rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  color: var(--text3);
  font-size: 11.5px;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-clear-cache:hover,
.btn-clear-cache:active {
  background: rgba(239, 68, 68, 0.1);
  border-color: rgba(239, 68, 68, 0.35);
  color: #f87171;
}

/* ================== MAIN ================== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: -webkit-fill-available;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

/* Mobile Topbar */
.topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  background: #171717;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 20;
  gap: 8px;
}

.topbar__menu {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background 0.2s;
  flex-shrink: 0;
}
.topbar__menu:active {
  background: rgba(255, 255, 255, 0.1);
}

.topbar__title-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  padding: 0 4px;
}

.topbar__title {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.5px;
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
  color: #ffffff;
}
.topbar__chat-text {
  color: var(--text) !important;
  font-size: 13.5px;
  font-weight: 600;
}

.topbar__actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.topbar__act {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #fff;
  cursor: pointer;
  width: 38px;
  height: 38px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}
.topbar__act:active {
  background: rgba(255, 255, 255, 0.15);
  transform: scale(0.96);
}

/* ================== WELCOME ================== */
.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 36px 20px;
  text-align: center;
  position: relative;
  overflow-y: auto;
  animation: fadeUp 0.4s var(--ease);
}

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

.welcome__hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 520px;
  width: 100%;
}

.welcome__title {
  font-size: clamp(30px, 5vw, 42px);
  font-weight: 800;
  letter-spacing: -0.5px;
  margin-bottom: 12px;
  color: #ffffff;
  line-height: 1.25;
}



/* ================== CHAT ================== */
.chat {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  display: none;
  scroll-behavior: smooth;
}
.chat.show { display: block; }

.chat__messages {
  max-width: 780px;
  margin: 0 auto;
  padding: 24px 20px calc(110px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 22px;
}

/* Message Row */
.msg {
  display: flex;
  gap: 14px;
  animation: msgIn 0.35s var(--ease);
}

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

.msg__avatar {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 16px;
  font-weight: 800;
}
.msg.user .msg__avatar { background: #3a3a3a; color: #fff; }
.msg.ai .msg__avatar {
  background: #ffffff;
  color: #000000;
  font-size: 18px;
}

.msg__body { flex: 1; min-width: 0; }

.msg__head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
}
.msg__name { font-size: 13px; font-weight: 700; }
.msg__time { font-size: 11px; color: var(--text3); }

.msg__text {
  font-size: 14.5px;
  line-height: 1.75;
  color: #e2e8f0;
  word-wrap: break-word;
}

.msg.user .msg__text { color: #f1f5f9; }

/* Markdown Styles */
.msg__text p { margin-bottom: 12px; }
.msg__text p:last-child { margin-bottom: 0; }
.msg__text h1,.msg__text h2,.msg__text h3 { color: #fff; font-weight: 700; margin: 18px 0 8px; }
.msg__text h3 { font-size: 16px; }
.msg__text ul,.msg__text ol { margin: 8px 0 12px 20px; }
.msg__text li { margin-bottom: 4px; }
.msg__text strong { color: #fff; }
.msg__text em { color: var(--purple); }

.msg__text blockquote {
  border-left: 3px solid var(--purple);
  background: rgba(139,92,246,0.08);
  padding: 10px 16px;
  border-radius: 0 10px 10px 0;
  margin: 12px 0;
  color: var(--text2);
}

.msg__text code:not(pre code) {
  font-family: var(--mono);
  font-size: 12.5px;
  background: rgba(139,92,246,0.12);
  color: #c4b5fd;
  padding: 2px 7px;
  border-radius: 5px;
}

.msg__text table { width:100%; border-collapse:collapse; margin:14px 0; font-size:13px; }
.msg__text th { text-align:left; padding:8px 12px; border-bottom:2px solid rgba(139,92,246,0.2); font-weight:700; color:#fff; }
.msg__text td { padding:8px 12px; border-bottom:1px solid var(--border); color:var(--text2); }

/* Code Block Wrapper */
.code-wrap {
  margin: 14px 0;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.08);
  background: #0d1117;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}

.code-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 14px;
  background: #161b22;
  border-bottom: 1px solid rgba(255,255,255,0.05);
}

.code-lang {
  font-family: var(--mono);
  font-size: 10.5px;
  font-weight: 600;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.code-btns { display: flex; gap: 5px; }

.code-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 5px;
  padding: 3px 9px;
  font-size: 11px;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.15s;
}
.code-btn:hover { background: rgba(255,255,255,0.14); color: #fff; }

.code-btn--run {
  background: rgba(16,185,129,0.15);
  border-color: rgba(16,185,129,0.3);
  color: #34d399;
}
.code-btn--run:hover { background: rgba(16,185,129,0.3); color: #fff; }

.code-wrap pre {
  margin: 0;
  padding: 16px;
  overflow-x: auto;
  background: transparent !important;
}

.code-wrap code {
  font-family: var(--mono) !important;
  font-size: 13px !important;
  line-height: 1.65 !important;
}

/* Typing Indicator */
.typing {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px 0;
}
.typing__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #ffffff;
  animation: bounce 1.4s infinite ease-in-out;
}
.typing__dot:nth-child(2) { animation-delay: 0.2s; }
.typing__dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%,80%,100% { transform:translateY(0); opacity:0.3; }
  40% { transform:translateY(-8px); opacity:1; }
}

/* Copy feedback */
.msg__actions {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity 0.2s;
}
.msg:hover .msg__actions { opacity: 1; }

.msg-act-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px 10px;
  border-radius: 6px;
  border: none;
  background: rgba(255,255,255,0.04);
  color: var(--text3);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s;
  gap: 4px;
}
.msg-act-btn:hover { background: rgba(255,255,255,0.1); color: #fff; }

.msg-act-btn.speaking {
  color: #38bdf8;
  background: rgba(56, 189, 248, 0.15);
  border: 1px solid rgba(56, 189, 248, 0.3);
}

.msg-act-btn--stop {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.25);
  color: #f87171;
  opacity: 1;
}
.msg-act-btn--stop:hover { background: rgba(239, 68, 68, 0.25); color: #fff; }

/* ================== COMPOSER ================== */
.composer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px max(16px, env(safe-area-inset-right)) max(12px, calc(8px + env(safe-area-inset-bottom))) max(16px, env(safe-area-inset-left));
  background: linear-gradient(to top, var(--bg) 75%, transparent);
  z-index: 10;
  pointer-events: none;
}

.composer__box, .composer__preview, .composer__doc {
  pointer-events: auto;
}

.composer__box {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border: 1.5px solid var(--border-glow);
  border-radius: 16px;
  padding: 8px 10px 8px 20px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  transition: all 0.2s;
}
.composer__box:focus-within {
  border-color: var(--border-focus);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

.composer__box textarea {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: #fff;
  font-family: var(--font);
  font-size: 14.5px;
  line-height: 1.5;
  resize: none;
  max-height: 180px;
  padding: 6px 0;
}
.composer__box textarea::placeholder { color: var(--text3); }

.composer__send {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: none;
  background: #ffffff;
  color: #000000;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
  box-shadow: none;
}
.composer__send:hover {
  background: #e5e5e5;
}

/* Upload Attach Button */
.composer__attach {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
  color: var(--text3);
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}
.composer__attach:hover {
  color: #fff;
  background: rgba(255,255,255,0.08);
}

/* Microphone Voice Button */
.composer__mic {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
  border: none;
  background: transparent;
  color: var(--text3);
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}
.composer__mic:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
}
.composer__mic.listening {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.15);
  animation: pulseMic 1.2s infinite;
}

@keyframes pulseMic {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
  70% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Image Preview Strip */
.composer__preview {
  display: none;
  max-width: 780px;
  margin: 0 auto 8px;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  align-items: center;
  gap: 10px;
  animation: fadeUp 0.25s var(--ease);
}
.composer__preview.show { display: flex; }

.composer__preview img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.composer__preview-remove {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  margin-left: auto;
}
.composer__preview-remove:hover {
  background: rgba(239, 68, 68, 0.25);
  color: #fff;
}

/* User message with image */
.msg__image {
  max-width: 240px;
  max-height: 240px;
  border-radius: 10px;
  margin-top: 8px;
  border: 1px solid var(--border);
  object-fit: cover;
  cursor: pointer;
  transition: transform 0.2s;
}
.msg__image:hover { transform: scale(1.02); }

.composer__hint {
  text-align: center;
  font-size: 11px;
  color: var(--text3);
  margin-top: 8px;
  max-width: 780px;
  margin-left: auto;
  margin-right: auto;
}

.composer__hint kbd {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: var(--mono);
  font-size: 10px;
}

/* Document Preview & Active Chip */
.composer__doc {
  max-width: 780px;
  margin: 0 auto 8px;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  animation: fadeUp 0.25s var(--ease);
}

.composer__doc-icon {
  color: var(--text2);
  flex-shrink: 0;
}

.composer__doc-info {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.composer__doc-name {
  color: #fff;
  font-size: 13.5px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.composer__doc-size {
  color: var(--text3);
  font-size: 12px;
  flex-shrink: 0;
}

.composer__doc-remove {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  margin-left: auto;
  flex-shrink: 0;
}

.composer__doc-remove:hover {
  background: rgba(239, 68, 68, 0.25);
  color: #fff;
}

.composer__doc-remove:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

/* User message document chip */
.msg__doc-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--text2);
  max-width: 100%;
}

.msg__doc-chip svg {
  flex-shrink: 0;
}

.msg__doc-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ================== PREVIEW MODAL ================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 500;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.modal-overlay.show { display: flex; }

.modal-frame {
  width: 100%;
  max-width: 900px;
  height: 80vh;
  background: #0f1422;
  border: 1px solid var(--border-glow);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
  animation: modalPop 0.25s var(--ease);
}

@keyframes modalPop {
  from { opacity:0; transform:scale(0.95); }
  to { opacity:1; transform:scale(1); }
}

.modal-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 700;
}

.modal-bar button {
  background: none;
  border: none;
  color: var(--text3);
  font-size: 20px;
  cursor: pointer;
}
.modal-bar button:hover { color: #fff; }

.modal-frame iframe {
  flex: 1;
  width: 100%;
  border: none;
  background: #fff;
}

/* ================== RESPONSIVE ================== */

/* --- Mobile (up to 768px - Phones 9:16, 20:9, foldables, iOS, Android) --- */
@media (max-width: 768px) {
  html { font-size: 14px; }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(86vw, 320px);
    min-width: min(86vw, 320px);
    transform: translate3d(-100%, 0, 0);
    box-shadow: 16px 0 48px rgba(0, 0, 0, 0.75);
    z-index: 200 !important;
    background: #18181b !important;
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
  }
  .sidebar.open { transform: translate3d(0, 0, 0) !important; }

  .topbar { display: flex; }

  .welcome {
    padding: max(20px, env(safe-area-inset-top)) 16px calc(90px + env(safe-area-inset-bottom));
    justify-content: center;
    padding-top: max(20px, 8vh);
  }
  .welcome__title { font-size: clamp(26px, 7vw, 34px); margin-bottom: 8px; }

  .chat__messages { 
    padding: 14px 14px calc(95px + env(safe-area-inset-bottom));
    gap: 16px;
  }

  .msg { gap: 10px; }
  .msg__avatar { width: 32px; height: 32px; border-radius: 8px; font-size: 15px; }
  .msg__text { font-size: 14.5px; line-height: 1.65; }
  .msg__name { font-size: 12px; }

  .code-wrap {
    max-width: 100%;
    margin: 10px 0;
    border-radius: 10px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .code-wrap pre { padding: 10px 12px; }
  .code-wrap code { font-size: 12px !important; }
  .code-bar { padding: 6px 12px; font-size: 12px; }
  .code-btn { padding: 4px 8px; font-size: 11px; min-height: 30px; }

  .composer {
    padding: 6px max(10px, env(safe-area-inset-right)) max(10px, calc(6px + env(safe-area-inset-bottom))) max(10px, env(safe-area-inset-left));
  }
  .composer__box {
    padding: 4px 6px 4px 12px;
    border-radius: 16px;
    gap: 6px;
  }
  /* CRITICAL: 16px font-size prevents iOS Safari from auto-zooming! */
  .composer__box textarea {
    font-size: 16px !important;
    padding: 8px 0;
  }
  .composer__attach,
  .composer__mic {
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    border-radius: 10px;
  }
  .composer__attach:active,
  .composer__mic:active {
    background: rgba(255,255,255,0.12);
  }
  .composer__send {
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    border-radius: 10px;
  }
  .composer__send:active {
    transform: scale(0.95);
  }

  /* Accessible Touch Targets (Apple & Google Guidelines: min 44x44) */
  .sidebar__close { display: inline-flex; width: 38px; height: 38px; }
  .topbar__menu { width: 42px; height: 42px; }
  .topbar__act { width: 38px; height: 38px; }
  .btn-new { padding: 12px; min-height: 46px; font-size: 14px; }
  .btn-sidebar-act { padding: 10px 12px; min-height: 42px; font-size: 13px; }
  .hist-item { padding: 8px 10px; min-height: 44px; font-size: 13px; }
  .hist-item__actions { display: flex !important; opacity: 1 !important; }
  .hist-item__btn {
    width: 36px;
    height: 36px;
    color: var(--text2);
  }
  .hist-item__btn:active { background: rgba(255, 255, 255, 0.16); color: #fff; }
  .hist-item__del:active { color: #ef4444; background: rgba(239, 68, 68, 0.2); }
  .model-select-wrapper { padding: 10px 12px; min-height: 46px; }
  .model-select { font-size: 16px !important; }

  /* Message actions permanently visible & touch-friendly on mobile */
  .msg__actions {
    opacity: 1 !important;
    gap: 8px;
    margin-top: 10px;
  }
  .msg-act-btn {
    padding: 6px 12px;
    font-size: 12px;
    min-height: 34px;
    border-radius: 8px;
  }
  .msg-act-btn:active {
    background: rgba(255,255,255,0.15);
    transform: scale(0.97);
  }

  /* Full screen preview modal on mobile */
  .modal-overlay { padding: 0; }
  .modal-frame {
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
    border: none;
  }

  /* Lock screen responsive */
  .lock-box {
    padding: 28px 20px;
    border-radius: 20px;
    width: min(92%, 360px);
  }
  .lock-box h2 { font-size: 1.3rem; }
  .lock-box input {
    font-size: 16px !important; /* Prevents iOS auto-zoom */
    padding: 12px 14px;
    letter-spacing: 4px;
  }
  .lock-box button {
    min-height: 46px;
    font-size: 1rem;
  }
}

/* --- Small phones (up to 380px) --- */
@media (max-width: 380px) {
  html { font-size: 13px; }

  .sidebar { width: 260px; min-width: 260px; }
  .welcome__title { font-size: 22px; }
  .chat__messages { padding: 10px 8px 110px; }
  .composer { padding: 6px 8px 10px; }
  .composer__box { padding: 5px 6px 5px 12px; }
  .lock-box { padding: 24px 16px; width: 95%; }
}

/* --- Tablet landscape (769px - 1024px) --- */
@media (min-width: 769px) and (max-width: 1024px) {
  :root { --sidebar-w: 220px; }

  .welcome__title { font-size: 36px; }
  .chat__messages { max-width: 680px; }
  .composer__box { max-width: 680px; }
}

/* --- Large screens / TV (1400px+) --- */
@media (min-width: 1400px) {
  html { font-size: 16px; }
  :root { --sidebar-w: 300px; }

  .chat__messages { max-width: 900px; }
  .composer__box { max-width: 900px; }
  .welcome__title { font-size: 52px; }
  .msg__text { font-size: 15.5px; }
  .code-wrap code { font-size: 14px !important; }
}

/* --- Keyboard open fix for mobile --- */
@media (max-height: 450px) and (max-width: 768px) {
  .welcome { display: none; }
  .composer { padding-bottom: 6px; }
  .chat__messages { padding-bottom: 80px; }
}

/* Mobile overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 150;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sidebar-overlay.show { display: block; }

/* --- Touch devices: no hover capability --- */
@media (hover: none) {
  .hist-item__actions { display: flex !important; opacity: 1 !important; }
  .hist-item__btn { display: inline-flex !important; }
  .msg__actions { opacity: 1; }
  .hist-item:active { background: rgba(255,255,255,0.08); }
  .msg-act-btn:active { background: rgba(255,255,255,0.12); color: #fff; }
}

/* ================== BRAND & WELCOME (v2.5) ================== */
/* Moon yellow from the crescent in the logo: the one accent, used for the focus ring. */
:root { --moon: #e8d38a; }

:focus-visible {
  outline: 2px solid var(--moon);
  outline-offset: 2px;
}
.model-select-wrapper:focus-within {
  outline: 2px solid var(--moon);
  outline-offset: 2px;
  border-radius: var(--radius);
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex: none;
}
.brand-name { gap: 10px; }

.welcome__logo {
  width: 104px;
  height: 104px;
  border-radius: 50%;
  margin-bottom: 18px;
}

@media (max-width: 768px) {
  .welcome__logo { width: 84px; height: 84px; margin-bottom: 14px; }
}
.topbar__title-wrap { gap: 8px; }
@media (max-width: 768px) {
  .composer__doc-remove { width: 40px; height: 40px; }
}
/* Keep the placeholder on one line on narrow phones instead of wrapping under the box. */
.composer__box textarea::placeholder {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Message avatars: round like profile pictures. Mooncrust gets the moon from the logo. */
.msg__avatar { border-radius: 50%; overflow: hidden; }
.msg.user .msg__avatar { background: #3a3a3a; color: #9a9a9a; }
.msg.user .msg__avatar svg { width: 100%; height: 100%; }
.msg.ai .msg__avatar { background: #2b2840; color: var(--moon); }

/* Welcome is one casual line under the logo, so it stays calmer than a hero headline. */
.welcome__title {
  font-size: clamp(24px, 3vw, 32px);
  font-weight: 700;
  letter-spacing: -0.3px;
  margin-bottom: 0;
}

/* ================== SHARE (v2.6) ================== */
.sidebar-link {
  display: block;
  margin-top: 10px;
  padding: 6px 4px;
  font-size: 12px;
  color: var(--text2);
  text-align: center;
  text-decoration: none;
}
.sidebar-link:hover { color: var(--text); text-decoration: underline; }

.share-dialog {
  width: min(460px, calc(100vw - 32px));
  margin: auto;
  padding: 0;
  background: #262626;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.share-dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
.share-dialog__inner { padding: 20px; }
.share-dialog__title { font-size: 17px; font-weight: 700; margin-bottom: 8px; }
.share-dialog__note { font-size: 13.5px; line-height: 1.55; color: var(--text2); margin-bottom: 16px; }
.share-dialog__row { display: flex; gap: 8px; }
.share-dialog__link {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font: 13px var(--mono);
  padding: 0 12px;
  min-height: 44px;
}
.share-dialog__actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.share-dialog__btn {
  min-height: 44px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font: 600 13.5px var(--font);
  cursor: pointer;
  white-space: nowrap;
}
.share-dialog__btn:hover { background: var(--bg-hover); }
.share-dialog__btn--primary { background: #fff; color: #111; border-color: #fff; }
.share-dialog__btn--primary:hover { background: #e6e6e6; }
.share-dialog__status { min-height: 18px; margin-top: 10px; font-size: 12.5px; color: var(--moon); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  background: #f2f2f2;
  color: #111;
  border-radius: 10px;
  font-size: 13.5px;
  line-height: 1.4;
  text-align: center;
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

@media (max-width: 480px) {
  .share-dialog__row { flex-direction: column; }
}
