:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sidebar-bg: #f0f2f6;
  --border: #e3e6ed;
  --text: #181c25;
  --text-sub: #646e82;
  --primary: #2563eb;
  --primary-soft: #eff4ff;
  --zz-gold: #d97706;
  --zz-bg: #fffbeb;
  --zz-border: #fcd34d;
  --danger: #dc2626;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea {
  font-family: inherit;
  font-size: 16px; /* Prevents iOS Safari auto-zoom on input focus */
}

.hidden {
  display: none !important;
}

/* ===== Login Screen ===== */
.login-screen {
  width: 100%;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(145deg, #eef2ff 0%, #f8fafc 55%, #fef3c7 100%);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 22px;
  padding: 32px 26px;
  box-shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
}

.login-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 22px;
}

.login-logo img {
  width: 44px;
  height: 44px;
  border-radius: 12px;
}

.login-logo h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 700;
}

.login-logo p {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--text-sub);
}

.form-field {
  margin-bottom: 15px;
}

.form-field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sub);
  margin-bottom: 6px;
}

.form-field input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #f9fafb;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

.form-field input:focus {
  border-color: var(--primary);
  background: #fff;
}

.login-btn {
  width: 100%;
  padding: 13px;
  margin-top: 6px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.login-btn:active {
  transform: scale(0.99);
}

.login-error {
  margin-top: 12px;
  font-size: 13px;
  color: var(--danger);
  text-align: center;
}

/* ===== Main App Shell ===== */
.app-shell {
  display: flex;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

/* ===== Left Sidebar (Projects + Conversations) ===== */
.sidebar {
  width: 288px;
  min-width: 288px;
  height: 100%;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
  transition: margin-left 0.24s cubic-bezier(0.22, 1, 0.36, 1), transform 0.24s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 40;
}

.sidebar.collapsed-desktop {
  margin-left: -288px;
}

.sidebar-top {
  padding: 14px 14px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.new-project-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.new-project-btn:active {
  background: #f3f4f6;
}

.project-tree {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 10px 16px;
}

.project-group {
  margin-bottom: 6px;
  border-radius: 11px;
}

.project-header {
  display: flex;
  align-items: center;
  padding: 8px 8px;
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  gap: 6px;
}

.project-header:hover {
  background: rgba(0, 0, 0, 0.04);
}

.project-header.active-proj {
  background: rgba(37, 99, 235, 0.07);
}

.proj-arrow {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--text-sub);
  transition: transform 0.18s ease;
}

.project-group.is-collapsed .proj-arrow {
  transform: rotate(-90deg);
}

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

.proj-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

.icon-mini-btn {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  color: var(--text-sub);
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  cursor: pointer;
}

.icon-mini-btn:hover, .icon-mini-btn:active {
  background: rgba(0, 0, 0, 0.07);
  color: var(--text);
}

.conv-list {
  padding-left: 20px;
  margin-top: 2px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.project-group.is-collapsed .conv-list {
  display: none;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13.5px;
  color: #374151;
}

.conv-item:hover {
  background: rgba(0, 0, 0, 0.04);
}

.conv-item.active {
  background: var(--panel);
  color: var(--primary);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.conv-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-del-btn {
  opacity: 0.55;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: var(--text-sub);
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
}

.conv-del-btn:hover {
  opacity: 1;
  color: var(--danger);
}

.sidebar-footer {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-meta {
  min-width: 0;
}

.user-meta .uname {
  font-size: 13.5px;
  font-weight: 600;
}

.user-meta .ubadge {
  font-size: 11px;
  color: var(--text-sub);
}

.logout-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text-sub);
  font-size: 12.5px;
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
}

/* ===== Main Workspace Area ===== */
.main-pane {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.topbar {
  padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 14px) 8px 14px;
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  z-index: 20;
}

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

.nav-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.breadcrumb {
  min-width: 0;
}

.breadcrumb .bc-proj {
  font-size: 11.5px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.breadcrumb .bc-conv {
  font-size: 14.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.files-drawer-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

/* ===== Chat Stream ===== */
.chat-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px 20px;
}

.chat-inner {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-welcome {
  text-align: center;
  padding: 56px 20px 24px;
  color: var(--text-sub);
}

.empty-welcome h2 {
  margin: 0 0 8px;
  font-size: 20px;
  color: var(--text);
}

.empty-welcome p {
  margin: 0 auto;
  max-width: 420px;
  font-size: 14px;
  line-height: 1.6;
}

.msg-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

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

.msg-row.assistant {
  align-items: flex-start;
}

.msg-bubble {
  max-width: 92%;
  padding: 12px 15px;
  border-radius: 16px;
  line-height: 1.65;
  font-size: 15px;
  word-break: break-word;
}

.msg-row.user .msg-bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}

.msg-row.assistant .msg-bubble {
  width: 100%;
  max-width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.03);
}

.msg-meta-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 11.5px;
  color: var(--text-sub);
}

.badge-model {
  padding: 2px 7px;
  border-radius: 6px;
  background: #eef2ff;
  color: #3730a3;
  font-weight: 600;
}

.badge-zz {
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--zz-bg);
  border: 1px solid var(--zz-border);
  color: var(--zz-gold);
  font-weight: 600;
}

.live-status-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  margin-bottom: 8px;
  border-radius: 999px;
  background: #f1f5f9;
  color: #475569;
  font-size: 12.5px;
}

.spinner-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary);
  animation: pulseDot 1.1s infinite ease-in-out;
}

@keyframes pulseDot {
  0%, 100% { transform: scale(0.75); opacity: 0.5; }
  50% { transform: scale(1.2); opacity: 1; }
}

/* Markdown Typography inside Assistant Bubble */
.md-body p { margin: 0 0 10px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4 {
  margin: 14px 0 8px;
  line-height: 1.35;
}
.md-body h1 { font-size: 19px; }
.md-body h2 { font-size: 17px; }
.md-body h3 { font-size: 15.5px; }
.md-body ul, .md-body ol {
  margin: 6px 0 10px;
  padding-left: 22px;
}
.md-body li { margin-bottom: 4px; }
.md-body pre {
  background: #0f172a;
  color: #f8fafc;
  padding: 12px 14px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}
.md-body code {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: rgba(15, 23, 42, 0.06);
  padding: 2px 5px;
  border-radius: 5px;
}
.md-body pre code {
  background: transparent;
  padding: 0;
}
.md-body blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 4px solid var(--primary);
  background: #f8fafc;
  border-radius: 0 8px 8px 0;
  color: #334155;
}
.md-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}
.md-body th, .md-body td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  text-align: left;
}
.md-body th {
  background: #f8fafc;
}
.zz-source-callout {
  margin: 8px 0;
  padding: 8px 12px;
  background: var(--zz-bg);
  border: 1px solid var(--zz-border);
  border-radius: 10px;
  color: #92400e;
  font-size: 13px;
  font-weight: 600;
}

/* File Cards in Messages */
.msg-files-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.file-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 11px;
  border-radius: 10px;
  background: #f8fafc;
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 12.5px;
  text-decoration: none;
  cursor: pointer;
}

.msg-row.user .file-chip {
  background: rgba(255, 255, 255, 0.18);
  border-color: rgba(255, 255, 255, 0.32);
  color: #fff;
}

.inline-img-preview {
  max-width: 260px;
  max-height: 220px;
  border-radius: 10px;
  border: 1px solid var(--border);
  margin-top: 6px;
  cursor: pointer;
  object-fit: contain;
  background: #fff;
}

/* Ask Question Interactive Card */
.question-card {
  margin-top: 10px;
  padding: 12px;
  border-radius: 12px;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
}
.question-card h4 {
  margin: 0 0 8px;
  font-size: 14px;
  color: #1e40af;
}
.q-opt-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 12px;
  margin-top: 6px;
  border-radius: 9px;
  border: 1px solid #93c5fd;
  background: #fff;
  color: var(--text);
  font-size: 13.5px;
  cursor: pointer;
}
.q-opt-btn:active {
  background: #dbeafe;
}

/* ===== Bottom Composer + 4-Stop Model Slider ===== */
.composer-wrap {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 10px calc(var(--safe-right) + 12px) calc(var(--safe-bottom) + 10px) 12px;
  z-index: 20;
}

.composer-inner {
  max-width: 820px;
  margin: 0 auto;
}

/* 4-Grid Model Slider */
.model-slider-box {
  margin-bottom: 8px;
  background: #f3f4f8;
  border-radius: 12px;
  padding: 5px;
  position: relative;
}

.model-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  position: relative;
  z-index: 2;
}

.model-cell {
  text-align: center;
  padding: 6px 2px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-sub);
  border-radius: 9px;
  cursor: pointer;
  user-select: none;
  transition: color 0.18s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-cell.active {
  background: var(--panel);
  color: var(--primary);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.1);
}

.model-range-overlay {
  width: 100%;
  margin: 4px 0 0;
  accent-color: var(--primary);
  height: 18px;
  cursor: pointer;
  display: block;
}

.model-hint-line {
  font-size: 11.5px;
  color: var(--text-sub);
  text-align: center;
  margin-top: 2px;
}

/* Staged Upload Bar */
.staged-files-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.staged-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 9px;
  background: var(--primary-soft);
  border: 1px solid #bfdbfe;
  font-size: 12.5px;
  color: #1d4ed8;
  white-space: nowrap;
}

.staged-remove {
  border: none;
  background: transparent;
  color: #1d4ed8;
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
}

/* Action Toolbar + Input Row */
.action-pills-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  overflow-x: auto;
}

.tool-pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #f8fafc;
  color: #334155;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.tool-pill-btn:active {
  background: #e2e8f0;
}

.tool-pill-btn.zz-btn.active {
  background: var(--zz-bg);
  border-color: #f59e0b;
  color: #b45309;
  box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.18);
}

.input-box-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.prompt-textarea {
  flex: 1;
  min-height: 44px;
  max-height: 150px;
  padding: 10px 13px;
  border-radius: 13px;
  border: 1px solid var(--border);
  background: #f9fafb;
  resize: none;
  outline: none;
  line-height: 1.45;
}

.prompt-textarea:focus {
  border-color: var(--primary);
  background: #fff;
}

.send-btn {
  height: 44px;
  min-width: 58px;
  padding: 0 15px;
  border: none;
  border-radius: 13px;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  font-size: 14.5px;
  cursor: pointer;
  flex-shrink: 0;
}

.send-btn.stop-mode {
  background: var(--danger);
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ===== Slide-over Files Drawer & Modal ===== */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.36);
  z-index: 35;
}

.files-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(360px, 88vw);
  height: 100dvh;
  background: var(--panel);
  border-left: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  box-shadow: -8px 0 28px rgba(15, 23, 42, 0.12);
}

.drawer-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.drawer-head h3 {
  margin: 0;
  font-size: 16px;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.drawer-file-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: #f9fafb;
}

.df-info {
  min-width: 0;
  flex: 1;
  cursor: pointer;
}

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

.df-sub {
  font-size: 11.5px;
  color: var(--text-sub);
  margin-top: 2px;
}

.df-btns {
  display: flex;
  gap: 4px;
}

.preview-modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.72);
  z-index: 80;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 12px) 12px calc(var(--safe-bottom) + 12px);
}

.preview-box {
  flex: 1;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
  background: var(--panel);
  border-radius: 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.preview-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.preview-content {
  flex: 1;
  overflow: auto;
  background: #f1f5f9;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-content iframe, .preview-content img, .preview-content video {
  width: 100%;
  height: 100%;
  border: none;
  object-fit: contain;
}

/* Mobile Responsive (< 820px: iPhone & iPad Split View) */
@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-102%);
    margin-left: 0 !important;
    box-shadow: 8px 0 28px rgba(15, 23, 42, 0.16);
  }
  .sidebar.open-mobile {
    transform: translateX(0);
  }
  .model-cell {
    font-size: 11.5px;
    padding: 6px 1px;
  }
}
