@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

:root {
  /* Colors based on the Vercel/Linear premium dark mode aesthetic */
  --page-bg: #0e0e0e; /* The very outer background */
  --viewer-bg: #171717; /* Left canvas background */
  --chat-bg: #212121; /* Right sidebar background */
  --bubble-bg: #2a2a2a; /* User prompt bubble & input background */
  
  --text-primary: #ededed;
  --text-secondary: #8c8c8c;
  --text-tertiary: #5c5c5c;
  
  --accent: #ffffff;
  --accent-faint: rgba(255, 255, 255, 0.1);
  
  --radius-app: 16px;
  --radius-bubble: 16px;
  --radius-input: 24px;
  
  --font-main: 'Inter', -apple-system, sans-serif;
  --transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; width: 100vw; height: 100vh; overflow: hidden;
  background: var(--page-bg); color: var(--text-primary);
  font-family: var(--font-main); -webkit-font-smoothing: antialiased;
  display: flex; align-items: center; justify-content: center;
}

button, input, textarea, select {
  font-family: inherit; font-size: inherit; color: inherit;
}

/* Focus rings: keep them for keyboard users (a11y), suppress on mouse clicks.
   :focus-visible fires only when the browser detects a non-pointer focus
   (Tab key, screen reader, etc), so clicked buttons no longer keep a halo
   behind them after you click. */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.3);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ── App Shell (The Floating Container) ── */
.app-shell {
  width: calc(100vw - 32px);
  height: calc(100vh - 32px);
  max-width: 1600px;
  background: var(--chat-bg);
  border-radius: var(--radius-app);
  box-shadow: 0 40px 80px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.05);
  display: flex; flex-direction: row; overflow: hidden;
}

/* ── LEFT: Viewer Panel ── */
.viewer-panel {
  flex: 1;
  background: var(--viewer-bg);
  display: flex; flex-direction: column;
  position: relative;
}

.viewer-header {
  height: 64px; padding: 0 24px;
  display: flex; align-items: center; gap: 12px;
  z-index: 10;
}
/* The HTML `hidden` attribute must win over class display rules (e.g.
   .viewer-title { display:flex }), or feature-flagged elements still show. */
[hidden] { display: none !important; }

/* Promote-to-showcase modal (admin) */
.promote-modes { display: flex; gap: 18px; margin: 12px 0; font-size: 0.9rem; color: var(--text-secondary); }
.promote-modes label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
#promote-new-fields { display: flex; flex-direction: column; gap: 10px; }
#promote-new-fields input, #promote-new-fields textarea, #promote-existing-fields select {
  width: 100%; padding: 10px 12px; border-radius: 8px; background: var(--bubble-bg);
  border: 1px solid rgba(255,255,255,0.08); color: var(--text-primary); font-size: 0.9rem;
  font-family: inherit; resize: vertical;
}
.promote-actions { display: flex; gap: 12px; margin-top: 18px; }
.promote-actions button { padding: 10px 16px; border-radius: 8px; border: none; cursor: pointer; font-weight: 500; font-size: 0.9rem; background: var(--bubble-bg); color: var(--text-primary); }
.promote-actions .btn-primary { background: var(--text-primary); color: var(--chat-bg); }
.lightbox-promote { margin-top: 12px; }

/* Change-password modal */
.change-pw-fields { display: flex; flex-direction: column; gap: 10px; margin: 16px 24px 4px; }
#change-pw-backdrop .promote-actions { margin: 12px 24px 24px; }
.change-pw-fields input {
  width: 100%; padding: 10px 12px; border-radius: 8px; background: var(--bubble-bg);
  border: 1px solid rgba(255,255,255,0.08); color: var(--text-primary); font-size: 0.9rem;
  font-family: inherit; box-sizing: border-box;
}
.recharge-note.is-error { color: #f87171; }
.recharge-note.is-success { color: #4ade80; }
.lightbox-promote button { width: 100%; padding: 10px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.08); background: var(--bubble-bg); color: var(--text-primary); cursor: pointer; font-size: 0.9rem; }
.viewer-title {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.9rem; font-weight: 500; color: var(--text-primary);
  cursor: pointer; padding: 6px 12px; border-radius: 6px;
  background: transparent; border: none;
}
.viewer-title:hover { background: rgba(255,255,255,0.05); }
.viewer-title svg { width: 14px; height: 14px; color: var(--text-secondary); }

.brand-icon {
  width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
}
.brand-icon svg { width: 100%; height: 100%; display: block; }

.image-frame {
  flex: 1; display: flex; align-items: center; justify-content: center;
  position: relative; padding: 40px;
}

.empty-state { display: none; }
.image-frame.is-empty .empty-state {
  display: flex; flex-direction: column; align-items: center; gap: 16px; opacity: 0.5;
}
.empty-state p { margin: 0; font-size: 0.95rem; color: var(--text-secondary); }

#generated-image {
  max-width: 100%; max-height: 100%; object-fit: contain;
  /* Matching the exact screenshot: no borders on the image, just a clean drop */
  box-shadow: 0 20px 40px rgba(0,0,0,0.4);
  border-radius: 4px;
  animation: fadeIn 0.5s ease;
}

.loading-overlay {
  position: absolute; inset: 0; background: rgba(23,23,23,0.7); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 20;
}
.loading-overlay[hidden] { display: none !important; }
.spinner {
  width: 32px; height: 32px; border: 2px solid var(--text-tertiary);
  border-top-color: var(--text-primary); border-radius: 50%; animation: spin 1s linear infinite;
}

@keyframes fadeIn { from { opacity: 0; transform: scale(0.98); } to { opacity: 1; transform: scale(1); } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── RIGHT: Chat Panel ── */
.chat-panel {
  width: 420px; min-width: 420px;
  background: var(--chat-bg);
  display: flex; flex-direction: column;
}

.chat-header {
  height: 64px; padding: 0 24px;
  display: flex; align-items: center; justify-content: space-between;
}
.chat-header span { font-size: 0.85rem; color: var(--text-secondary); }

.user-bar { display: flex; gap: 12px; font-size: 0.8rem; }
.user-bar button { background: none; border: none; cursor: pointer; color: var(--text-secondary); padding: 0; }
.user-bar button:hover { color: var(--text-primary); }
.points-chip { color: var(--text-primary); }

/* Chat History (Scrollable Area) */
.chat-history {
  flex: 1; overflow-y: auto; padding: 0 24px 24px 24px;
  display: flex; flex-direction: column; gap: 24px;
}
/* Hide scrollbar completely for pure aesthetic */
.chat-history::-webkit-scrollbar { width: 0; height: 0; }

.user-bubble {
  background: var(--bubble-bg); padding: 16px 20px; border-radius: var(--radius-bubble);
  font-size: 0.9rem; line-height: 1.5; color: var(--text-secondary);
  align-self: flex-end; width: 100%; white-space: pre-wrap;
}

.ai-bubble {
  display: flex; flex-direction: column; gap: 12px; font-size: 0.85rem; color: var(--text-secondary); line-height: 1.6;
}
.ai-step { display: flex; align-items: center; gap: 12px; color: var(--text-primary); }
.ai-step svg { width: 14px; height: 14px; color: var(--text-secondary); }
.ai-step.is-loading svg { animation: spin 1s linear infinite; }
.ai-step.is-error { color: #f87171; }
.ai-text { margin-top: 8px; }

/* Result thumbnail inside a completed AI bubble */
.ai-thumb {
  margin-top: 12px; width: 120px; height: 120px; object-fit: cover;
  border-radius: 10px; box-shadow: 0 8px 20px rgba(0,0,0,0.4);
  border: 1px solid rgba(255,255,255,0.08); cursor: pointer;
  transition: var(--transition);
}
.ai-thumb:hover { transform: scale(1.03); border-color: rgba(255,255,255,0.2); }

/* Multi-variant grid inside an AI bubble (n>1). Two columns: 2 variants → 1×2,
   3 → 2×2 with one empty cell, 4 → 2×2. */
.ai-variant-grid {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  max-width: 260px;
}
.ai-variant-thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 10px;
  box-shadow: 0 8px 20px rgba(0,0,0,0.4);
  border: 1px solid rgba(255,255,255,0.08);
  cursor: pointer;
  transition: var(--transition);
}
.ai-variant-thumb:hover { transform: scale(1.03); border-color: rgba(255,255,255,0.2); }
/* Active = the thumb whose image is currently on the main canvas. Applies to
   both single-result .ai-thumb and multi-variant .ai-variant-thumb so the user
   can spot which past result they're editing. */
.ai-variant-thumb.is-active,
.ai-thumb.is-active {
  border-color: var(--accent, #ffffff);
  box-shadow: 0 0 0 2px var(--accent, #ffffff), 0 8px 20px rgba(0,0,0,0.4);
}

/* Edit · Regenerate icon-only actions beneath a finished turn. Pulled up tight
   against the result so they read as controls for that turn. */
.turn-actions {
  display: flex; gap: 4px; margin-top: -6px; margin-left: -4px;
}
.turn-action-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 8px; cursor: pointer;
  background: transparent; border: none;
  color: var(--text-tertiary); transition: var(--transition); padding: 0;
}
.turn-action-btn:hover {
  background: rgba(255,255,255,0.08); color: var(--text-primary);
}
.turn-action-btn svg { width: 15px; height: 15px; pointer-events: none; }

.msg-metrics {
  font-size: 0.75rem; color: var(--text-tertiary); display: flex; gap: 12px; margin-top: 8px;
}
.msg-metrics strong { color: var(--text-secondary); font-weight: normal; margin-left: 4px; }

/* ── BOTTOM: Chat Input Box ── */
.chat-input-area {
  padding: 0 24px 24px 24px;
  position: relative;
}

.chat-input-container {
  background: var(--bubble-bg);
  border-radius: var(--radius-input);
  display: flex; flex-direction: column;
  padding: 8px; border: 1px solid transparent;
  transition: var(--transition);
}
.chat-input-container:focus-within {
  border-color: rgba(255,255,255,0.1); background: #2f2f2f;
}

#prompt {
  background: transparent; border: none; color: var(--text-primary);
  font-size: 0.9rem; line-height: 1.5; resize: none; width: 100%;
  padding: 12px 16px; outline: none;
  /* Auto-grows from min-height up to max-height (then scrolls) — JS sets height. */
  min-height: 60px; max-height: 200px; height: 60px; overflow-y: auto;
}
#prompt::placeholder { color: var(--text-tertiary); }

.input-toolbar {
  display: flex; align-items: center; justify-content: space-between; padding: 4px 8px;
}

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

.icon-btn {
  width: 36px; height: 36px; border-radius: 50%; border: none; background: transparent;
  color: var(--text-secondary); display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: var(--transition); position: relative;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); color: var(--text-primary); }
.icon-btn svg { width: 18px; height: 18px; pointer-events: none; }

.submit-btn {
  width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--text-secondary);
  color: var(--chat-bg); display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: var(--transition);
}
.submit-btn:hover:not(:disabled) { background: var(--text-primary); transform: scale(1.05); }
.submit-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.submit-btn svg { width: 16px; height: 16px; fill: currentColor; pointer-events: none; }
/* In-flight: the send button becomes a stop button (red). */
.submit-btn.is-stopping { background: #ef4444; color: #fff; }
.submit-btn.is-stopping:hover { background: #dc2626; transform: scale(1.05); }
.submit-btn.is-stopping svg { width: 14px; height: 14px; }

/* ── Hidden Popovers (Templates, Settings, Edits) ── */
.popover-menu {
  display: none; position: absolute; bottom: 100%; left: 0; right: 0;
  background: var(--chat-bg); border-radius: var(--radius-app);
  box-shadow: 0 -10px 40px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.1);
  padding: 20px; margin-bottom: 12px; z-index: 100;
  flex-direction: column; gap: 16px;
}
.popover-menu.is-open { display: flex; animation: slideUp 0.2s cubic-bezier(0.16,1,0.3,1); }

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

.popover-header { display: flex; justify-content: space-between; align-items: center; font-size: 0.9rem; font-weight: 500; }
.popover-close { background: none; border: none; color: var(--text-secondary); cursor: pointer; font-size: 1.2rem; }
.popover-close:hover { color: var(--text-primary); }

.field-group { display: flex; flex-direction: column; gap: 8px; }
.field-group label { font-size: 0.75rem; color: var(--text-secondary); }
.field-group select, .field-group textarea, .field-group input {
  background: var(--bubble-bg); border: 1px solid rgba(255,255,255,0.05); color: var(--text-primary);
  border-radius: 8px; padding: 10px; font-size: 0.85rem; outline: none; width: 100%;
}
.field-group textarea { resize: vertical; }
.field-group select:focus, .field-group textarea:focus { border-color: rgba(255,255,255,0.2); }

.btn-primary {
  background: var(--text-primary); color: var(--chat-bg); border: none;
  border-radius: 8px; padding: 10px; font-weight: 500; cursor: pointer;
}
.btn-primary:hover { opacity: 0.9; }

/* Hidden logically */
.visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

#status-text { font-size: 0.75rem; color: var(--text-secondary); text-align: center; margin: 12px 0 0; }

/* ── Main Tab Switching ── */
.main-tab-panel { display: none; height: 100%; width: 100%; }
.main-tab-panel.tab-active { display: flex; }

/* ── GALLERY TAB ── */
#tab-gallery { display: none; padding: 40px; overflow-y: auto; background: var(--viewer-bg); width: 100%; flex-direction: column; }
#tab-gallery.tab-active { display: flex; }
.gallery-header { display: flex; justify-content: space-between; align-items: flex-end; max-width: 1400px; margin: 0 auto 32px auto; width: 100%; padding-bottom: 16px; border-bottom: 1px solid rgba(255,255,255,0.1); }
.gallery-header h2 { font-size: 1.5rem; font-weight: 500; margin: 0; }
.gallery-header button { background: var(--bubble-bg); border: none; color: var(--text-primary); padding: 8px 16px; border-radius: 8px; cursor: pointer; }

.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 20px; max-width: 1400px; margin: 0 auto; width: 100%; }
.gallery-card { background: var(--chat-bg); border: 1px solid rgba(255,255,255,0.05); border-radius: 12px; overflow: hidden; cursor: pointer; transition: var(--transition); }
.gallery-card:hover { border-color: rgba(255,255,255,0.2); transform: translateY(-4px); }
.gallery-thumb-wrap { width: 100%; aspect-ratio: 1; background: var(--bubble-bg); }
.gallery-thumb { width: 100%; height: 100%; object-fit: cover; }
.gallery-thumb-empty { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: var(--text-tertiary); font-size: 0.8rem; }
.gallery-card-body { padding: 16px; }
.gallery-prompt { font-size: 0.85rem; color: var(--text-primary); margin: 0 0 8px 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.gallery-meta { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--text-secondary); }
.gallery-status-ok { color: var(--text-primary); }
#gallery-load-more { display: block; margin: 40px auto; background: transparent; border: 1px solid rgba(255,255,255,0.1); color: var(--text-primary); padding: 10px 24px; border-radius: 8px; cursor: pointer; }

/* ── LIGHTBOX ── */
.lightbox-backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.8); z-index: 200; align-items: center; justify-content: center; padding: 40px; }
.lightbox-backdrop.open { display: flex; }
.lightbox { background: var(--chat-bg); border: 1px solid rgba(255,255,255,0.1); border-radius: var(--radius-app); max-width: 1000px; width: 100%; height: 80vh; display: flex; overflow: hidden; }
.lightbox-img-area { flex: 1; background: var(--viewer-bg); display: flex; align-items: center; justify-content: center; padding: 24px; position: relative; }

/* Lightbox carousel controls — only shown when there are multiple variants. */
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 44px; height: 44px; border-radius: 50%;
  background: rgba(0,0,0,0.55); color: #fff; border: 1px solid rgba(255,255,255,0.1);
  font-size: 28px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.lb-nav:hover { background: rgba(0,0,0,0.8); }
.lb-prev { left: 16px; }
.lb-next { right: 16px; }
.lb-counter {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  font-size: 0.85rem; color: rgba(255,255,255,0.85);
  background: rgba(0,0,0,0.55); padding: 4px 10px; border-radius: 12px;
}

/* Gallery card badge for grouped variants (×2 / ×3 / ×4). */
.gallery-thumb-wrap { position: relative; }
.gallery-variant-badge {
  position: absolute; top: 8px; right: 8px;
  background: rgba(0,0,0,0.65); color: #fff;
  font-size: 0.75rem; font-weight: 500;
  padding: 2px 8px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.1);
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }
.lightbox-sidebar { width: 340px; padding: 32px; display: flex; flex-direction: column; background: var(--chat-bg); border-left: 1px solid rgba(255,255,255,0.05); overflow-y: auto; min-height: 0; }
.lightbox-prompt { font-size: 0.95rem; color: var(--text-primary); margin: 0 0 24px 0; line-height: 1.5; }
.lightbox-meta { font-size: 0.85rem; color: var(--text-secondary); margin-bottom: auto; line-height: 1.8;}
.lightbox-actions { display: flex; flex-direction: column; gap: 12px; margin-top: 24px; }
.lightbox-actions button { padding: 12px; border-radius: 8px; border: none; cursor: pointer; font-weight: 500; font-size: 0.9rem; }
.lightbox-actions .btn-primary { background: var(--text-primary); color: var(--chat-bg); }
.lightbox-actions button:not(.btn-primary) { background: var(--bubble-bg); color: var(--text-primary); border: 1px solid rgba(255,255,255,0.05); }

/* ── POINT LEDGER ── */
.username-btn {
  background: none; border: none; color: var(--text-secondary); cursor: pointer;
  padding: 4px 8px; font-size: 0.8rem;
}
.username-btn strong {
  color: var(--text-primary); font-weight: 600;
  transition: var(--transition);
}
.username-btn:hover strong { text-decoration: underline; text-underline-offset: 3px; }

.ledger-backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.7); z-index: 300; align-items: center; justify-content: center; padding: 40px; }
.ledger-backdrop.open { display: flex; animation: fadeIn 0.2s ease; }
.ledger-modal {
  background: var(--chat-bg); border: 1px solid rgba(255,255,255,0.1); border-radius: var(--radius-app);
  width: 100%; max-width: 460px; max-height: 80vh; display: flex; flex-direction: column;
  box-shadow: 0 40px 80px rgba(0,0,0,0.5);
}
.ledger-header { display: flex; justify-content: space-between; align-items: flex-start; padding: 24px 24px 16px; border-bottom: 1px solid rgba(255,255,255,0.08); }
.ledger-title { font-size: 1.1rem; font-weight: 600; color: var(--text-primary); margin: 0; }
.ledger-balance-line { font-size: 0.8rem; color: var(--text-secondary); margin: 6px 0 0; }
.ledger-balance-line span { color: var(--text-primary); font-weight: 600; }
.ledger-close { background: none; border: none; color: var(--text-secondary); cursor: pointer; font-size: 1.4rem; line-height: 1; }
.ledger-close:hover { color: var(--text-primary); }
.ledger-list { overflow-y: auto; padding: 8px 24px 24px; }
.ledger-empty { color: var(--text-secondary); font-size: 0.85rem; text-align: center; padding: 24px 0; }
.ledger-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 14px 0; border-bottom: 1px solid rgba(255,255,255,0.05); }
.ledger-row:last-child { border-bottom: none; }
.ledger-row-main { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.ledger-reason { font-size: 0.9rem; color: var(--text-primary); }
.ledger-time { font-size: 0.72rem; color: var(--text-tertiary); }
.ledger-delta { font-size: 0.95rem; font-weight: 600; white-space: nowrap; }
.ledger-delta.is-credit { color: #4ade80; }
.ledger-delta.is-debit { color: #f87171; }

/* ── Legacy token compatibility (login.html / admin.html) ──────────────────────
   These pages were authored against an earlier token set. styles.css was later
   rewritten for the workspace and dropped those names, leaving the auth/admin
   pages unstyled (transparent cards, missing borders). Map the old names onto the
   current dark theme so both pages render correctly and consistently. */
:root {
  --line: rgba(255, 255, 255, 0.1);
  --panel: var(--chat-bg);
  --muted: var(--text-secondary);
  --text: var(--text-primary);
  --radius: var(--radius-app);
  --shadow: 0 40px 80px rgba(0, 0, 0, 0.5);
  --font: var(--font-main);
}

.page-shell {
  position: relative;
  width: 100vw; height: 100vh;
  overflow-y: auto;
  display: flex; flex-direction: column;
}
/* Decorative blurred glows behind the auth/admin content. */
.ambient { position: fixed; width: 480px; height: 480px; border-radius: 50%; filter: blur(120px); opacity: 0.18; pointer-events: none; z-index: 0; }
.ambient-left { top: -160px; left: -120px; background: #ffffff; opacity: 0.06; }
.ambient-right { bottom: -180px; right: -120px; background: #ffffff; opacity: 0.05; }
.page-shell > *:not(.ambient) { position: relative; z-index: 1; }

.eyebrow { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.18em; color: var(--muted); margin: 0 0 8px; }
.field-label { font-size: 0.78rem; color: var(--muted); }

/* ── Main tab active state (Generate / Gallery header buttons) ──────────────────
   Linear/Vercel-style underline indicator. The pseudo-element is positioned
   absolutely against the button so a transparent 2px reservation on the
   inactive state prevents a 2px height-jiggle when switching tabs. */
.main-tab-btn {
  position: relative;
  background: transparent !important; /* override the .viewer-title:hover bg */
  color: var(--text-secondary);
  text-decoration: none; /* the community entry is an <a>; kill the default underline */
  transition: color 0.15s ease;
}
.main-tab-btn::after {
  content: "";
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: -6px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 2px;
  opacity: 0;
  transform: scaleX(0.6);
  transform-origin: center;
  transition: opacity 0.15s ease, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.main-tab-btn:hover { color: var(--text-primary); }
.main-tab-btn.tab-active {
  color: var(--text-primary);
  background: transparent;
}
.main-tab-btn.tab-active::after {
  opacity: 1;
  transform: scaleX(1);
}

/* ── Canvas download button (floats over a generated image) ────────────────── */
.canvas-actions { position: absolute; top: 16px; right: 16px; z-index: 15; display: flex; gap: 8px; }
.image-frame.is-empty .canvas-actions { display: none; }
.canvas-action-btn {
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.12);
  background: rgba(0,0,0,0.45); backdrop-filter: blur(8px); color: var(--text-primary);
  display: flex; align-items: center; justify-content: center; cursor: pointer; transition: var(--transition);
}
.canvas-action-btn:hover { background: rgba(0,0,0,0.7); border-color: rgba(255,255,255,0.3); }
.canvas-action-btn svg { width: 18px; height: 18px; pointer-events: none; }

/* ── Recharge modal (reuses ledger backdrop) ───────────────────────────────── */
.recharge-note { font-size: 0.8rem; color: var(--text-secondary); margin: 0 24px 8px; line-height: 1.5; }
.recharge-contact { font-size: 0.85rem; color: var(--text-primary); font-weight: 500; margin: 0 24px 12px; padding: 10px 12px; background: var(--bubble-bg); border-radius: 8px; word-break: break-all; }
.recharge-qr-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; margin: 0 24px 14px; }
.recharge-qr-wrap img { width: 180px; height: 180px; object-fit: contain; border-radius: 10px; background: #fff; padding: 8px; }
.recharge-qr-tip { font-size: 0.78rem; color: var(--text-secondary); margin: 0; }
.plan-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; padding: 8px 24px 24px; }
.plan-card {
  border: 1px solid var(--line); border-radius: 12px; padding: 16px 14px; text-align: center;
  cursor: pointer; transition: var(--transition); background: var(--bubble-bg);
}
.plan-card:hover { border-color: rgba(255,255,255,0.3); transform: translateY(-2px); }
.plan-points { font-size: 1.2rem; font-weight: 700; color: var(--text-primary); }
.plan-price { font-size: 0.85rem; color: var(--text-secondary); margin-top: 4px; }
.plan-badge { display: inline-block; margin-top: 8px; font-size: 0.68rem; color: #4ade80; }

/* ── Recharge: method switch (微信 / 闲鱼兑换码) + redeem box ─────────────────── */
.recharge-methods { display: flex; gap: 8px; margin: 4px 24px 12px; }
.recharge-method-btn {
  flex: 1; padding: 9px 12px; font-size: 0.85rem; cursor: pointer;
  color: var(--text-secondary); background: var(--bubble-bg);
  border: 1px solid var(--line); border-radius: 9px; transition: var(--transition);
}
.recharge-method-btn:hover { border-color: rgba(255,255,255,0.3); }
.recharge-method-btn.is-active {
  color: var(--text-primary); font-weight: 600;
  border-color: var(--accent, #4ade80);
  box-shadow: inset 0 0 0 1px var(--accent, #4ade80);
}
.redeem-box { display: flex; gap: 8px; margin: 0 24px 10px; }
.redeem-box input {
  flex: 1; min-width: 0; padding: 10px 12px; font-size: 0.9rem; letter-spacing: 0.06em;
  color: var(--text-primary); background: var(--bubble-bg);
  border: 1px solid var(--line); border-radius: 9px; text-transform: uppercase;
}
.redeem-box input:focus { outline: none; border-color: var(--accent, #4ade80); }
.redeem-box .btn-primary { white-space: nowrap; }
.xianyu-buy-btn { display: block; margin: 0 24px 12px; text-align: center; text-decoration: none; }

/* ── Language switcher ──────────────────────────────────────────────────────
   Tiny pill button in the top bar. Stays unobtrusive but discoverable. */
.lang-switch-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-secondary, var(--text, #ededed));
  font: inherit;
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: var(--transition, all 0.15s);
  white-space: nowrap;
}
.lang-switch-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-primary, var(--text, #ededed));
  border-color: rgba(255, 255, 255, 0.2);
}

/* ═══════════════════════════════════════════════════════════════════════════
   RESPONSIVE / MOBILE ADAPTATION
   Added 2026-05-28. Pure additive layer — desktop styles above are untouched.
   Two breakpoints:
     ≤900px : tablet / landscape phone — main shell becomes vertical
     ≤600px : portrait phone — tighten paddings, fonts, full-width modals
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── ≤900px : tablet & landscape phone ──────────────────────────────────── */
@media (max-width: 900px) {
  /* Let the page scroll on small screens — desktop's locked viewport
     hides content the moment it exceeds 100vh. */
  html, body {
    height: auto;
    min-height: 100vh;
    overflow: auto;
    display: block;
  }

  /* Main app shell: stop being a horizontal split, stack vertically.
     Drop the floating-card chrome since margins waste precious width. */
  .app-shell {
    width: 100%;
    height: auto;
    min-height: 100vh;
    flex-direction: column;
    border-radius: 0;
    box-shadow: none;
  }

  /* Viewer (image preview) — give it a sensible height instead of flex:1
     fighting with the chat panel below. */
  .viewer-panel {
    flex: none;
    min-height: 50vh;
  }
  .image-frame { padding: 20px; }

  /* Chat panel: full width, natural height. */
  .chat-panel {
    width: 100%;
    min-width: 0;
    flex: none;
  }
  .chat-history { max-height: 50vh; }

  /* Gallery — shrink the huge outer padding. */
  #tab-gallery { padding: 24px 16px; }
  .gallery-header { margin-bottom: 20px; }
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; }

  /* Lightbox: stop being a side-by-side modal — stack image then meta. */
  .lightbox-backdrop { padding: 16px; }
  .lightbox {
    flex-direction: column;
    height: auto;
    max-height: 90vh;
    overflow-y: auto;
  }
  .lightbox-img-area { padding: 16px; min-height: 40vh; }
  .lightbox-sidebar {
    width: 100%;
    padding: 20px;
    border-left: none;
    border-top: 1px solid rgba(255,255,255,0.05);
  }

  /* Ledger / recharge modal — let the backdrop padding stop eating the screen. */
  .ledger-backdrop { padding: 16px; }
}

/* ── ≤600px : portrait phone — tighten everything ───────────────────────── */
@media (max-width: 600px) {
  /* Headers: drop horizontal padding so titles fit. */
  .chat-header { padding: 0 16px; height: 56px; }

  /* The viewer header packs brand + workspace title + Gallery + user-bar
     (username / points / recharge / [admin] / logout). On a 375px screen
     that's 6–7 items — guaranteed to crush. Let it wrap onto two rows
     and shrink each element. */
  .viewer-header {
    flex-wrap: wrap;
    height: auto;
    min-height: 56px;
    padding: 10px 12px;
    gap: 8px;
    row-gap: 6px;
  }
  .viewer-title {
    font-size: 0.8rem;
    padding: 5px 8px;
    gap: 6px;
  }
  /* Hide the dropdown chevron next to the workspace title on phone —
     it's decorative and adds clutter. */
  .viewer-header .viewer-title[data-tab="generate"] svg { display: none; }

  /* User-bar lives in a flex container with margin-left:auto (set inline
     in index.html). On phone we want it to take a full row beneath the
     tabs rather than try to fit alongside. */
  .viewer-header > div:last-child {
    margin-left: 0 !important;
    width: 100%;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 6px !important;
  }
  .user-bar {
    font-size: 0.72rem;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .username-btn { padding: 2px 4px; font-size: 0.72rem; }
  .points-chip { font-size: 0.72rem; }

  /* Brand icon: keep it but slightly smaller. */
  .brand-icon { width: 18px; height: 18px; font-size: 9px; }

  /* Chat content area: smaller side padding. */
  .chat-history { padding: 0 16px 16px; gap: 18px; }
  .chat-input-area { padding: 0 12px 16px; }

  /* Bubble & text sizing — slightly larger because phone reading distance. */
  .user-bubble { font-size: 0.92rem; padding: 14px 16px; }
  .ai-bubble { font-size: 0.88rem; }
  #prompt { font-size: 1rem; padding: 10px 12px; min-height: 56px; height: 56px; }
  /* Prevents iOS Safari from auto-zooming when focusing a <16px input. */

  /* Touch targets — bump from 36px toward WCAG 44px minimum. */
  .icon-btn,
  .submit-btn { width: 40px; height: 40px; }

  /* Popover (templates / settings panel inside chat). */
  .popover-menu { padding: 16px; border-radius: 12px; }

  /* Gallery cards — 2 columns on phone instead of cramped auto-fill. */
  #tab-gallery { padding: 16px 12px; }
  .gallery-header { flex-direction: column; align-items: stretch; gap: 12px; }
  .gallery-header h2 { font-size: 1.25rem; }
  .gallery-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .gallery-card-body { padding: 12px; }
  .gallery-prompt { font-size: 0.8rem; }

  /* Ledger modal — fill screen on phone, drop generous padding. */
  .ledger-backdrop { padding: 8px; }
  .ledger-modal { max-height: 90vh; }
  .ledger-header { padding: 18px 18px 12px; }
  .ledger-list { padding: 8px 18px 18px; }

  /* Recharge plan grid — 2 cols on phone (was auto-fill ~120px ≈ 2 anyway). */
  .plan-grid { grid-template-columns: repeat(2, 1fr); padding: 8px 18px 18px; }

  /* Lightbox — even tighter on portrait phones. */
  .lightbox-backdrop { padding: 8px; }
  .lightbox-sidebar { padding: 16px; }
  .lightbox-prompt { font-size: 0.9rem; margin-bottom: 16px; }

  /* Canvas (download) action button — keep visible but not overbearing. */
  .canvas-actions { top: 10px; right: 10px; }

  /* ── Auth / admin pages (login.html, admin.html) ── */
  .page-shell { width: 100%; }
  .ambient { width: 280px; height: 280px; filter: blur(80px); }
}

/* ── Admin console (admin.html inline styles) — phone overrides ─────────── */
@media (max-width: 600px) {
  .admin-layout { padding: 20px 14px; }
  .admin-header { flex-direction: column; align-items: stretch; gap: 14px; margin-bottom: 20px; }
  .admin-header h1 { font-size: 1.5rem; }
  .search-bar { flex-direction: column; }

  /* Wide users table can't shrink — let it scroll horizontally rather
     than crushing every column. Wrap in a scroll container if you want
     a hint shadow, but raw overflow works for now. */
  #users-table { display: block; overflow-x: auto; white-space: nowrap; }

  /* Modals: stop using fixed 360/640px widths. */
  .modal { width: 100% !important; max-width: 100% !important; padding: 22px 18px; }
  #gens-modal { padding-top: 20px !important; }
  #gens-modal .modal { max-height: 85vh; }
  .generations-table { font-size: 0.72rem !important; }
  .generations-table th,
  .generations-table td { padding: 6px 8px; }

  /* Toast positioning — center bottom on phone. */
  #toast { left: 16px; right: 16px; bottom: 16px; text-align: center; }
}
/* ── TEMPLATE GALLERY MODAL ──────────────────────────────────────────── */
.tg-modal {
  background: var(--chat-bg);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius-app);
  box-shadow: 0 40px 80px rgba(0,0,0,0.5);
  width: 100%;
  max-width: 860px;
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.tg-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}
.tg-modal-title { font-size: 1rem; font-weight: 600; color: var(--text-primary); }
.tg-count { font-size: 0.78rem; color: var(--text-secondary); }
.tg-search-wrap { padding: 12px 24px 0; flex-shrink: 0; }
#tg-search {
  width: 100%;
  padding: 8px 14px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 0.875rem;
  outline: none;
  box-sizing: border-box;
}
#tg-search:focus { border-color: rgba(255,255,255,0.3); }
#tg-search::placeholder { color: var(--text-tertiary); }
.tg-filters {
  display: flex;
  gap: 6px;
  padding: 12px 24px 8px;
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}
.tg-filters::-webkit-scrollbar { display: none; }
.tg-filter-btn {
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 20px;
  padding: 5px 14px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.tg-filter-btn:hover { border-color: rgba(255,255,255,0.3); color: var(--text-primary); }
.tg-filter-btn.active { background: var(--accent); border-color: var(--accent); color: #111; font-weight: 500; }
.tg-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  padding: 8px 24px 24px;
  overflow-y: auto;
  flex: 1;
}
.tg-card {
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 10px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  transition: border-color 0.15s;
}
.tg-card:hover { border-color: rgba(255,255,255,0.25); }
.tg-card-body { flex: 1; }
.tg-tag-badge {
  display: inline-block;
  font-size: 0.68rem;
  padding: 2px 8px;
  background: rgba(255,255,255,0.1);
  color: var(--text-secondary);
  border-radius: 10px;
  margin-bottom: 6px;
}
.tg-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 6px;
}
.tg-preview {
  font-size: 0.73rem;
  color: var(--text-secondary);
  margin: 0;
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tg-card-footer { display: flex; gap: 6px; }
.tg-use-btn {
  flex: 1;
  font-size: 0.78rem;
  padding: 7px 0;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: #111;
  cursor: pointer;
  font-weight: 500;
  transition: opacity 0.15s;
}
.tg-use-btn:hover { opacity: 0.82; }
.tg-copy-btn {
  font-size: 0.78rem;
  padding: 7px 12px;
  background: transparent;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 6px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.tg-copy-btn:hover { color: var(--text-primary); border-color: rgba(255,255,255,0.3); }
.tg-empty {
  grid-column: 1/-1;
  text-align: center;
  color: var(--text-secondary);
  padding: 48px 0;
  font-size: 0.875rem;
}
@media (max-width: 768px) {
  .tg-modal { max-height: 90vh; border-radius: 16px 16px 0 0; align-self: flex-end; }
  .tg-grid { grid-template-columns: repeat(2, 1fr); }
  .tg-header, .tg-search-wrap, .tg-filters, .tg-grid { padding-left: 16px; padding-right: 16px; }
}
@media (max-width: 420px) {
  .tg-grid { grid-template-columns: 1fr; }
}
