/* ─── Reset & Base ───────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:      #000000;
  --surface: #1c1c1e;
  --card:    #2c2c2e;
  --border:  #38383a;
  --text:    #ffffff;
  --text2:   rgba(235, 235, 245, 0.6);
  --text3:   rgba(235, 235, 245, 0.3);
  --accent:  #0a84ff;
  --green:   #30d158;
  --red:     #ff453a;
  --orange:  #ff9f0a;
  --radius:  18px;
  --font:    -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'SF Pro Display', system-ui, sans-serif;
  --mono:    'SF Mono', 'Fira Code', Menlo, Consolas, monospace;
}

html {
  height: 100%;
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body {
  height: 100%;
  font-family: var(--font);
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button, input, textarea, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  background: none;
  border: none;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

button { cursor: pointer; -webkit-tap-highlight-color: transparent; }

/* ─── Overlay ────────────────────────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
.overlay.visible {
  opacity: 1;
  pointer-events: all;
}

/* ─── Icon buttons ───────────────────────────────────────────────────────── */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: var(--text);
  flex-shrink: 0;
  transition: background 0.15s;
}
.icon-btn:active { background: rgba(255,255,255,0.1); }

/* ─── Status dots ────────────────────────────────────────────────────────── */
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-green  { background: var(--green); }
.dot-red    { background: var(--red); }
.dot-orange { background: var(--orange); }
.dot-gray   { background: var(--text3); }

/* ─── History Sidebar ────────────────────────────────────────────────────── */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 82%;
  max-width: 360px;
  background: var(--surface);
  z-index: 200;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
.sidebar.open { transform: translateX(0); }

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 56px;
  padding: 0 12px 0 8px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
}

.sidebar-new {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  justify-content: center;
  transition: opacity 0.15s;
}
.new-chat-btn:active { opacity: 0.8; }

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  -webkit-overflow-scrolling: touch;
}

/* Date group headers */
.history-group-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 12px 16px 6px;
}

/* Chat item with swipe-to-delete */
.history-item-wrap {
  position: relative;
  overflow: hidden;
}
.history-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 11px 16px;
  cursor: pointer;
  transition: background 0.15s, transform 0.25s ease;
  position: relative;
  background: var(--surface);
}
.history-item.active { background: var(--card); }
.history-item:active  { background: rgba(255,255,255,0.07); }

.history-item-title {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-item-meta {
  font-size: 12px;
  color: var(--text2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Swipe delete reveal button */
.history-delete-btn {
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 72px;
  background: var(--red);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  transform: translateX(100%);
  transition: transform 0.25s ease;
}
.history-item-wrap.swiped .history-item { transform: translateX(-72px); }
.history-item-wrap.swiped .history-delete-btn { transform: translateX(0); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.clear-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--red);
  font-size: 15px;
  padding: 8px 0;
}

/* ─── Main App ───────────────────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  position: relative;
  z-index: 1;
}

/* ─── Header ─────────────────────────────────────────────────────────────── */
.header {
  display: flex;
  align-items: center;
  height: 56px;
  padding: 0 8px;
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  gap: 4px;
}
.header-title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

/* ─── Model Bar ──────────────────────────────────────────────────────────── */
.model-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
}
.model-bar:active { background: var(--card); }

.model-bar-text {
  flex: 1;
  font-size: 14px;
  color: var(--text2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chevron-down { color: var(--text3); flex-shrink: 0; }

/* ─── Chat Area ──────────────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

/* Date separator */
.date-separator {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 12px 0;
  color: var(--text3);
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.date-separator::before,
.date-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* Message rows */
.msg {
  display: flex;
  flex-direction: column;
  max-width: 100%;
  margin: 2px 0;
}
.msg-user { align-items: flex-end; }
.msg-ai   { align-items: flex-start; }

.bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  font-size: 15px;
  word-break: break-word;
}
.msg-user .bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.msg-ai .bubble {
  background: var(--card);
  color: var(--text);
  border-bottom-left-radius: 5px;
}

/* Markdown in AI bubbles */
.bubble p { margin: 0 0 0.5em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; }
.bubble ul, .bubble ol { padding-left: 1.4em; margin: 0.3em 0; }
.bubble li { margin: 0.15em 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 {
  font-weight: 700; margin: 0.6em 0 0.3em;
  line-height: 1.25;
}
.bubble h1 { font-size: 1.2em; }
.bubble h2 { font-size: 1.1em; }
.bubble h3 { font-size: 1em; }
.bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 10px;
  color: var(--text2);
  margin: 0.4em 0;
}
.bubble pre {
  background: #000;
  border-radius: 10px;
  padding: 12px;
  overflow-x: auto;
  margin: 0.5em 0;
}
.bubble code {
  font-family: var(--mono);
  font-size: 13px;
  background: rgba(0,0,0,0.35);
  border-radius: 4px;
  padding: 1px 5px;
}
.bubble pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: 13px;
  color: #e0e0e0;
}
.bubble a { color: var(--accent); text-decoration: none; }
.bubble a:active { opacity: 0.7; }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 0.6em 0; }
.bubble table { border-collapse: collapse; width: 100%; font-size: 13px; }
.bubble th, .bubble td {
  border: 1px solid var(--border);
  padding: 5px 8px;
  text-align: left;
}
.bubble th { background: rgba(255,255,255,0.07); font-weight: 600; }

/* Typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 10px 14px;
  background: var(--card);
  border-radius: var(--radius);
  border-bottom-left-radius: 5px;
  width: fit-content;
}
.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text2);
  animation: blink 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; }
  40%           { opacity: 1; }
}

/* Empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text3);
  gap: 10px;
  text-align: center;
  padding: 24px;
}
.empty-state svg { opacity: 0.3; }
.empty-state-title { font-size: 17px; font-weight: 600; color: var(--text2); }
.empty-state-sub   { font-size: 14px; }

/* ─── Input Area ─────────────────────────────────────────────────────────── */
.input-wrap {
  flex-shrink: 0;
  padding: 8px 12px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border-radius: 22px;
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--border);
}
.msg-input {
  flex: 1;
  background: none;
  color: var(--text);
  font-size: 16px; /* CRITICAL: prevents iOS zoom */
  line-height: 1.45;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0;
}
.msg-input::placeholder { color: var(--text3); }

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s, transform 0.1s;
}
.send-btn:disabled { opacity: 0.35; }
.send-btn:not(:disabled):active { transform: scale(0.9); }

/* Stop button (shown while streaming) */
.send-btn.stop-mode { background: var(--red); }

/* Attach image button */
.attach-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: var(--text2);
  flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.attach-btn:active { background: rgba(255,255,255,0.08); }
.attach-btn.has-images { color: var(--accent); }

/* Image preview strip above input */
.img-preview-list {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 0 0 8px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.img-preview-list:empty { display: none; }
.img-preview-list::-webkit-scrollbar { display: none; }

.img-preview-item {
  position: relative;
  flex-shrink: 0;
  width: 64px;
  height: 64px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--card);
}
.img-preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.img-preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0,0,0,0.65);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

/* Images inside chat bubbles */
.bubble-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}
.bubble-img {
  width: 100%;
  max-width: 240px;
  max-height: 200px;
  object-fit: cover;
  border-radius: 10px;
  display: block;
  cursor: zoom-in;
  -webkit-tap-highlight-color: transparent;
}
/* Single image takes full bubble width */
.bubble-images:has(> :only-child) .bubble-img { max-width: 100%; }

/* Text inside user bubble when images are present */
.bubble-text {
  white-space: pre-wrap;
  word-break: break-word;
  margin-top: 2px;
}

/* ─── Settings Bottom Sheet ──────────────────────────────────────────────── */
.settings-sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  z-index: 300;
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.25, 0.8, 0.25, 1);
  display: flex;
  flex-direction: column;
  max-height: 92vh;
  padding-bottom: env(safe-area-inset-bottom);
}
.settings-sheet.open { transform: translateY(0); }

.sheet-handle {
  width: 40px;
  height: 5px;
  background: var(--border);
  border-radius: 3px;
  margin: 10px auto 0;
  flex-shrink: 0;
}
.sheet-header {
  display: flex;
  align-items: center;
  height: 52px;
  padding: 0 8px 0 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sheet-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
}
.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.settings-section-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text2);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 8px;
}

/* Provider list */
.providers-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.provider-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
}
.provider-info {
  flex: 1;
  min-width: 0;
}
.provider-name {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.provider-addr {
  font-size: 12px;
  color: var(--text2);
  margin-top: 2px;
}

.add-provider-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--accent);
  font-size: 15px;
  font-weight: 500;
  padding: 10px 0;
}

/* Default model select */
.default-model-select {
  width: 100%;
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 15px;
  border: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='rgba(235,235,245,0.6)' stroke-width='2.5' stroke-linecap='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 16px;
  padding-right: 36px;
}

/* ─── Modals ─────────────────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  background: rgba(0,0,0,0.6);
}
.modal.open {
  opacity: 1;
  pointer-events: all;
}
.modal-card {
  background: var(--surface);
  border-radius: 18px;
  width: 100%;
  max-width: 380px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  padding: 16px 12px 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
}
.modal-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1;
}
.modal-footer {
  display: flex;
  gap: 10px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-footer .btn-primary { margin-left: auto; }

/* Form elements */
.form-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text2);
  margin-top: 4px;
}
.form-input {
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 13px;
  font-size: 15px;
  border: 1px solid transparent;
  width: 100%;
  transition: border-color 0.15s;
}
.form-input:focus { border-color: var(--accent); }
.form-select {
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='rgba(235,235,245,0.6)' stroke-width='2.5' stroke-linecap='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
  padding-right: 32px;
}
.modal-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
}
.test-result {
  font-size: 13px;
  font-weight: 500;
}
.test-result.ok  { color: var(--green); }
.test-result.err { color: var(--red); }

/* Buttons */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  padding: 10px 20px;
  font-size: 15px;
  font-weight: 600;
  transition: opacity 0.15s;
}
.btn-primary:active { opacity: 0.8; }

.btn-secondary {
  background: var(--card);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 500;
  transition: opacity 0.15s;
}
.btn-secondary:active { opacity: 0.8; }

.btn-danger {
  background: rgba(255, 69, 58, 0.15);
  color: var(--red);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 600;
  transition: opacity 0.15s;
}
.btn-danger:active { opacity: 0.8; }

/* Model picker modal */
.model-modal-card { max-height: 80vh; }
.model-list { gap: 0; padding: 8px 0; }

.model-provider-group { margin-bottom: 4px; }
.model-provider-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.model-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  cursor: pointer;
  transition: background 0.12s;
}
.model-item:active { background: rgba(255,255,255,0.07); }
.model-item.selected { background: rgba(10, 132, 255, 0.12); }
.model-item-name {
  flex: 1;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-item-check {
  color: var(--accent);
  flex-shrink: 0;
}
.model-loading {
  padding: 24px 20px;
  color: var(--text2);
  font-size: 15px;
  text-align: center;
}
.model-error {
  padding: 24px 20px;
  color: var(--red);
  font-size: 14px;
  text-align: center;
}
.model-empty {
  padding: 24px 20px;
  color: var(--text2);
  font-size: 14px;
  text-align: center;
}

/* Confirm dialog */
.confirm-card { max-width: 300px; }
.confirm-message {
  font-size: 15px;
  text-align: center;
  line-height: 1.5;
  padding: 8px 0;
}
.confirm-footer { justify-content: center; }

/* ─── Image Lightbox ─────────────────────────────────────────────────────── */
.img-lightbox {
  position: fixed;
  inset: 0;
  z-index: 600;
  background: rgba(0, 0, 0, 0.96);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.img-lightbox.open {
  opacity: 1;
  pointer-events: all;
}
.img-lightbox-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
  touch-action: pinch-zoom;
  user-select: none;
}
.img-lightbox-close {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 10px);
  right: 16px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.15);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  -webkit-tap-highlight-color: transparent;
}

/* ─── HTML Preview Panel ────────────────────────────────────────────────── */
.html-preview-panel {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  transform: translateX(100%);
  transition: transform 0.32s cubic-bezier(0.25, 0.8, 0.25, 1);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
.html-preview-panel.open { transform: translateX(0); }

.preview-header {
  display: flex;
  align-items: center;
  height: 52px;
  padding: 0 8px;
  gap: 4px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.preview-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  text-align: center;
}

/* Mobile / Desktop toggle */
.preview-device-toggle {
  display: flex;
  background: var(--card);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}
.preview-device-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 26px;
  border-radius: 6px;
  color: var(--text2);
  transition: background 0.15s, color 0.15s;
}
.preview-device-btn.active {
  background: var(--accent);
  color: #fff;
}

/* Preview iframe area */
.preview-body {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow: auto;
  background: #e5e5ea; /* grey surround for desktop mode */
  -webkit-overflow-scrolling: touch;
}
.preview-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: #fff;
  flex-shrink: 0;
}

/* Desktop mode: fixed 1280px width centered */
.preview-body.desktop-mode {
  padding: 16px;
}
.preview-body.desktop-mode .preview-iframe {
  width: 1280px;
  max-width: 100%;
  height: auto;
  min-height: 100%;
  border-radius: 8px;
  box-shadow: 0 4px 32px rgba(0,0,0,0.3);
}

/* Copy feedback */
.preview-copy-done {
  color: var(--green) !important;
}

/* ─── Code block preview button ──────────────────────────────────────────── */
.preview-btn-wrap {
  margin-top: -4px;
  margin-bottom: 6px;
}
.code-preview-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: rgba(10, 132, 255, 0.12);
  border-radius: 8px;
  padding: 5px 11px;
  transition: background 0.15s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.code-preview-btn:active { opacity: 0.7; }

/* ─── AI bubble copy button ─────────────────────────────────────────────── */
.bubble-actions {
  display: flex;
  align-items: center;
  padding: 1px 4px;
  margin-top: 1px;
}
.bubble-copy-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text3);
  padding: 3px 7px;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.bubble-copy-btn:active { background: rgba(255,255,255,0.08); }
.bubble-copy-btn.copied { color: var(--green); }

/* ─── Document file chip preview strip ──────────────────────────────────── */
.doc-preview-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.doc-preview-list:empty { display: none; }

.doc-preview-item {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--card);
  border-radius: 8px;
  padding: 5px 8px;
  font-size: 13px;
  color: var(--text);
  max-width: 200px;
}
.doc-preview-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.doc-preview-remove {
  background: none;
  border: none;
  color: var(--text2);
  font-size: 16px;
  cursor: pointer;
  padding: 0 2px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

/* File chips inside user message bubbles */
.bubble-files {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}
.bubble-file-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(255,255,255,0.08);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  max-width: 180px;
}
.file-chip-icon { flex-shrink: 0; }
.file-chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Doc attach button active state */
.doc-attach-btn.has-files { color: var(--accent); }

/* ─── Scrollbar (minimal) ────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 3px; height: 3px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ─── Landscape adjustments ──────────────────────────────────────────────── */
@media (orientation: landscape) and (max-height: 500px) {
  .header    { height: 44px; }
  .model-bar { height: 36px; }
  .chat-area { padding: 10px 12px; }
  .bubble    { font-size: 14px; }
  .settings-sheet { max-height: 98vh; }
}

/* ─── Reduced motion ─────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
  .typing-dot { opacity: 0.6; }
}

/* ─── Web Search: toggle button ─────────────────────────────────────────── */
.search-btn {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 50%;
  color: var(--text2); flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.search-btn:active { background: rgba(255,255,255,0.08); }
.search-btn.search-active { color: var(--accent); background: rgba(10,132,255,0.12); }

/* --- Voice / Mic button --- */
.mic-btn {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 50%;
  color: var(--text2); flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.mic-btn:active { background: rgba(255,255,255,0.08); }
.mic-btn.recording {
  color: var(--red);
  background: rgba(255,69,58,0.12);
  animation: mic-pulse 1.2s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,69,58,0.4); }
  50%       { box-shadow: 0 0 0 8px rgba(255,69,58,0); }
}

/* ─── Web Search: iOS-style toggle switch ───────────────────────────────── */
.ws-toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0; border-bottom: 1px solid var(--border); margin-bottom: 4px;
}
.ws-toggle-label { font-size: 15px; color: var(--text); }
.toggle-switch {
  position: relative; display: inline-block; flex-shrink: 0;
  -webkit-tap-highlight-color: transparent; cursor: pointer;
}
.toggle-switch input { position: absolute; opacity: 0; width: 0; height: 0; pointer-events: none; }
.toggle-track {
  display: block; width: 51px; height: 31px; background: var(--border);
  border-radius: 16px; position: relative; transition: background 0.2s ease;
}
.toggle-switch input:checked ~ .toggle-track { background: var(--accent); }
.toggle-thumb {
  position: absolute; top: 2px; left: 2px; width: 27px; height: 27px;
  border-radius: 50%; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,0.35);
  transition: transform 0.2s cubic-bezier(0.25,0.8,0.25,1);
}
.toggle-switch input:checked ~ .toggle-track .toggle-thumb { transform: translateX(20px); }

/* ─── Web Search: collapsible config panel ──────────────────────────────── */
.ws-config {
  display: flex; flex-direction: column; gap: 10px;
  overflow: hidden; max-height: 400px; opacity: 1;
  transition: max-height 0.3s cubic-bezier(0.25,0.8,0.25,1), opacity 0.25s ease;
}
.ws-config--hidden { max-height: 0; opacity: 0; pointer-events: none; }
.ws-range {
  width: 100%; height: 4px; -webkit-appearance: none; appearance: none;
  background: var(--border); border-radius: 2px; cursor: pointer; margin: 4px 0; padding: 0;
}
.ws-range::-webkit-slider-thumb {
  -webkit-appearance: none; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 1px 4px rgba(0,0,0,0.4); cursor: pointer;
}
.ws-range::-moz-range-thumb {
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent);
  border: none; box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

/* ─── Web Search: results card ──────────────────────────────────────────── */
.search-results-card {
  width: 100%; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; overflow: hidden; margin: 4px 0 8px; flex-shrink: 0;
}
.search-results-header {
  display: flex; align-items: center; gap: 8px; padding: 10px 14px;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  transition: background 0.12s; user-select: none;
}
.search-results-header:active { background: rgba(255,255,255,0.05); }
.search-results-icon { font-size: 15px; flex-shrink: 0; line-height: 1; }
.search-results-title { flex: 1; font-size: 13px; font-weight: 600; color: var(--text2); }
.search-results-count { font-size: 12px; color: var(--text2); font-weight: 400; }
.search-results-chevron { color: var(--text2); transition: transform 0.2s ease; flex-shrink: 0; }
.search-results-card.expanded .search-results-chevron { transform: rotate(180deg); }
.search-results-divider { height: 1px; background: var(--border); }
.search-results-body {
  overflow: hidden; max-height: 0;
  transition: max-height 0.3s cubic-bezier(0.25,0.8,0.25,1);
}
.search-results-card.expanded .search-results-body { max-height: var(--results-body-height, 600px); }
.search-result-item { padding: 10px 14px; border-bottom: 1px solid var(--border); }
.search-result-item:last-child { border-bottom: none; }
.search-result-num { font-size: 11px; font-weight: 600; color: var(--text2); margin-bottom: 2px; }
.search-result-title {
  font-size: 14px; font-weight: 500; color: var(--text); margin-bottom: 2px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.search-result-meta {
  font-size: 12px; color: var(--accent); margin-bottom: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.search-result-snippet {
  font-size: 13px; color: var(--text2); line-height: 1.45;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* ─── Web Search: toast notification ────────────────────────────────────── */
.search-toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom) + 80px);
  left: 50%; transform: translateX(-50%) translateY(8px);
  background: var(--card); color: var(--red);
  border: 1px solid rgba(255,69,58,0.3); border-radius: 10px;
  padding: 9px 16px; font-size: 13px; font-weight: 500;
  z-index: 999; opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  white-space: nowrap; max-width: calc(100vw - 32px); text-align: center;
}
.search-toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }
