:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-glass: rgba(255, 255, 255, 0.86);
  --surface-soft: #eef1f7;
  --border: rgba(15, 23, 42, 0.08);
  --text: #14171f;
  --text-sub: #5f6777;
  --accent: #2563eb;
  --accent-bg: rgba(37, 99, 235, 0.1);
  --user-bubble: #2563eb;
  --user-text: #ffffff;
  --danger: #dc2626;
  --success: #16a34a;
  --code-bg: #161821;
  --code-text: #eef2ff;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --surface: #1a1d26;
    --surface-glass: rgba(22, 25, 33, 0.88);
    --surface-soft: #242936;
    --border: rgba(255, 255, 255, 0.09);
    --text: #f3f5f9;
    --text-sub: #9ca3b4;
    --accent: #3b82f6;
    --accent-bg: rgba(59, 130, 246, 0.18);
    --user-bubble: #2563eb;
    --user-text: #ffffff;
    --danger: #ef4444;
    --success: #22c55e;
    --code-bg: #0b0d12;
    --code-text: #e2e8f0;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  }
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "SF Pro Text", "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

/* Login View */
.login-view {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 32px 24px;
  text-align: center;
  box-shadow: var(--shadow);
}

.login-logo {
  width: 72px;
  height: 72px;
  border-radius: 18px;
  margin-bottom: 12px;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.22);
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 22px;
  font-weight: 700;
}

.login-sub {
  margin: 0 0 22px;
  font-size: 13.5px;
  color: var(--text-sub);
}

#login-form input,
.form-input {
  width: 100%;
  height: 44px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  padding: 0 14px;
  font-size: 16px; /* Prevents iOS auto-zoom */
  outline: none;
  margin-bottom: 10px;
}

#login-form input:focus,
.form-input:focus {
  border-color: var(--accent);
  background: var(--surface);
}

.login-error {
  color: var(--danger);
  font-size: 13.5px;
  margin-bottom: 12px;
}

.primary-btn {
  width: 100%;
  height: 48px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:active {
  opacity: 0.86;
  transform: scale(0.99);
}

/* App Layout */
.app-view {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg);
}

/* Topbar */
.topbar {
  flex-shrink: 0;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--surface-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-pill-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 36px;
  padding: 0 11px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}

.icon-pill-btn.accent-soft {
  background: var(--accent-bg);
  color: var(--accent);
  border-color: transparent;
}

.dot-badge {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 2px var(--surface);
}

.circle-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.model-segmented {
  display: inline-flex;
  padding: 3px;
  border-radius: 18px;
  background: var(--surface-soft);
  border: 1px solid var(--border);
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-size: 13px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 15px;
  cursor: pointer;
  transition: all 0.16s ease;
}

.seg-btn.active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.09);
}

/* Banners */
.net-banner {
  background: #f59e0b;
  color: #fff;
  font-size: 12.5px;
  font-weight: 500;
  text-align: center;
  padding: 6px 12px;
}

.pwa-tip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
  background: var(--accent-bg);
  color: var(--text);
  font-size: 12.5px;
  border-bottom: 1px solid var(--border);
}

.pwa-tip button {
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-size: 18px;
  padding: 0 4px;
  cursor: pointer;
}

/* Chat Scroll Area */
.chat-scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 12px 20px;
}

.welcome-box {
  margin: 22px auto 12px;
  max-width: 520px;
  text-align: center;
  padding: 20px 14px;
}

.welcome-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
}

.welcome-desc {
  font-size: 13.5px;
  color: var(--text-sub);
  margin-bottom: 20px;
  line-height: 1.6;
}

.starter-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 390px) {
  .starter-grid {
    grid-template-columns: 1fr;
  }
}

.starter-chip {
  text-align: left;
  padding: 12px 14px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.02);
}

.starter-chip:active {
  background: var(--surface-soft);
}

/* Messages */
.messages-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 100%;
}

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

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

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

.provider-tag {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 8px;
  background: var(--accent-bg);
  color: var(--accent);
  font-weight: 600;
  font-size: 11px;
}

.msg-bubble {
  max-width: 92%;
  padding: 12px 15px;
  border-radius: 18px;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.msg-row.user .msg-bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 5px;
  max-width: 85%;
  white-space: pre-wrap;
}

.msg-row.assistant .msg-bubble {
  width: 100%;
  max-width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-top-left-radius: 6px;
  box-shadow: var(--shadow);
}

/* Uploaded attachments on user message */
.user-atts {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 6px;
  max-width: 88%;
}

.user-att-img {
  width: 110px;
  height: 110px;
  object-fit: cover;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
}

.user-att-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  text-decoration: none;
}

/* Live Running Task Status Card */
.task-status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
  border-radius: 12px;
  background: var(--surface-soft);
  font-size: 13px;
}

.task-status-left {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-weight: 500;
}

.spinner {
  width: 15px;
  height: 15px;
  border: 2.2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.stop-inline-btn {
  border: 1px solid rgba(220, 38, 38, 0.3);
  background: rgba(220, 38, 38, 0.09);
  color: var(--danger);
  border-radius: 10px;
  padding: 5px 11px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

.error-box {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(220, 38, 38, 0.08);
  border: 1px solid rgba(220, 38, 38, 0.22);
  color: var(--danger);
  font-size: 14px;
}

/* Generated Output File Cards */
.output-files-wrap {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.output-card {
  border: 1px solid var(--border);
  background: var(--surface-soft);
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.output-preview-img {
  max-width: 100%;
  max-height: 320px;
  object-fit: contain;
  border-radius: 10px;
  background: var(--surface);
}

.output-info-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.file-ext-badge {
  min-width: 44px;
  height: 44px;
  padding: 0 8px;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.output-meta {
  flex: 1;
  min-width: 0;
}

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

.output-size {
  font-size: 12px;
  color: var(--text-sub);
}

.output-actions {
  display: flex;
  gap: 8px;
}

.dl-btn,
.share-btn {
  flex: 1;
  height: 40px;
  border-radius: 11px;
  font-size: 13.5px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  text-decoration: none;
  cursor: pointer;
}

.dl-btn {
  background: var(--accent);
  color: #fff;
  border: none;
}

.share-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

/* 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: 17.5px; }
.md-body h3 { font-size: 16px; }
.md-body ul, .md-body ol {
  margin: 6px 0 10px;
  padding-left: 22px;
}
.md-body li {
  margin-bottom: 4px;
}
.md-body blockquote {
  margin: 8px 0;
  padding: 6px 12px;
  border-left: 3.5px solid var(--accent);
  background: var(--surface-soft);
  border-radius: 0 8px 8px 0;
  color: var(--text-sub);
}
.md-body code.inline-code {
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--surface-soft);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13.5px;
}
.md-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}
.md-body a {
  color: var(--accent);
  text-decoration: underline;
}

.code-block-wrap {
  margin: 10px 0;
  border-radius: 12px;
  overflow: hidden;
  background: var(--code-bg);
  color: var(--code-text);
}

.code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.07);
  font-size: 12px;
  color: #94a3b8;
}

.copy-code-btn {
  border: none;
  background: rgba(255, 255, 255, 0.12);
  color: #f8fafc;
  border-radius: 7px;
  padding: 3px 9px;
  font-size: 12px;
  cursor: pointer;
}

.code-block-wrap pre {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  line-height: 1.5;
}

.table-scroll {
  overflow-x: auto;
  margin: 10px 0;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.md-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

.md-body th, .md-body td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.md-body th {
  background: var(--surface-soft);
  font-weight: 600;
}

.msg-footer-actions {
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.copy-msg-btn {
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-size: 12px;
  padding: 4px 6px;
  cursor: pointer;
}

/* Composer */
.composer-wrap {
  flex-shrink: 0;
  background: var(--surface-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(var(--safe-bottom) + 8px);
}

.pending-files {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 2px 10px;
}

.pending-chip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  flex-shrink: 0;
  max-width: 210px;
}

.pending-thumb {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  object-fit: cover;
}

.pending-chip-info {
  min-width: 0;
  font-size: 12.5px;
}

.pending-chip-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.pending-chip-sub {
  font-size: 11px;
  color: var(--text-sub);
}

.pending-remove {
  border: none;
  background: var(--surface-soft);
  color: var(--text-sub);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
}

.composer-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.attach-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
}

#prompt-input {
  flex: 1;
  min-height: 42px;
  max-height: 132px;
  padding: 10px 14px;
  border-radius: 21px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 16px; /* Prevents iOS zoom */
  line-height: 1.4;
  resize: none;
  outline: none;
  font-family: inherit;
}

#prompt-input:focus {
  border-color: var(--accent);
}

.send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
}

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

.send-btn:disabled {
  opacity: 0.45;
}

/* History Drawer & Bottom Sheet & Modal */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.42);
  z-index: 40;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(82vw, 320px);
  background: var(--surface);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  box-shadow: var(--shadow);
}

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

.drawer-header h2 {
  margin: 0;
  font-size: 17px;
}

.sessions-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.session-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 11px 12px;
  border-radius: 14px;
  cursor: pointer;
  border: 1px solid transparent;
}

.session-item.active {
  background: var(--accent-bg);
  border-color: rgba(37, 99, 235, 0.25);
}

.session-item-main {
  flex: 1;
  min-width: 0;
}

.session-item-title {
  font-size: 14.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item-sub {
  font-size: 12px;
  color: var(--text-sub);
  margin-top: 2px;
}

.session-del-btn {
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-size: 17px;
  padding: 4px 8px;
  cursor: pointer;
}

/* Bottom Sheet */
.bottom-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-width: 520px;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 14px 14px calc(var(--safe-bottom) + 14px);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: var(--shadow);
}

.sheet-title {
  text-align: center;
  font-size: 13px;
  color: var(--text-sub);
  padding-bottom: 4px;
}

.sheet-item {
  width: 100%;
  height: 50px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.sheet-item.cancel {
  margin-top: 4px;
  background: var(--surface);
  color: var(--text-sub);
}

/* Settings Modal */
.modal-wrap {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  max-height: 86dvh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 22px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.test-result {
  padding: 9px 11px;
  border-radius: 10px;
  font-size: 12.5px;
  margin-bottom: 10px;
  word-break: break-word;
}
.test-result.ok {
  background: rgba(22, 163, 74, 0.12);
  color: var(--success);
}
.test-result.err {
  background: rgba(220, 38, 38, 0.1);
  color: var(--danger);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
}

.guide-box {
  background: var(--surface-soft);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 13.5px;
  margin-bottom: 14px;
}

.guide-box ol {
  margin: 8px 0 0;
  padding-left: 20px;
}

.admin-details {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  margin-bottom: 14px;
  font-size: 13.5px;
}

.admin-details summary {
  cursor: pointer;
  font-weight: 600;
}

.small-muted {
  font-size: 12.5px;
  color: var(--text-sub);
  margin: 8px 0;
}

.secondary-btn {
  width: 100%;
  height: 42px;
  border-radius: 12px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
}

.danger-outline-btn {
  width: 100%;
  height: 44px;
  border-radius: 12px;
  border: 1px solid rgba(220, 38, 38, 0.35);
  background: transparent;
  color: var(--danger);
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  top: calc(var(--safe-top) + 58px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.9);
  color: #fff;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 13.5px;
  font-weight: 500;
  z-index: 90;
  pointer-events: none;
}
