/* ═══════════════════════════════════════════════════════
   CROWN UNCENSORED AI v3.1 — Multi-Purpose AI Platform
   Homepage · General AI · Coding AI · Character AI · Image AI
   Self-Hosted Image Gen (ComfyUI + A1111) · Mobile-First
   ═══════════════════════════════════════════════════════ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-primary: #020617;
  --bg-secondary: #0f172a;
  --bg-tertiary: #1e293b;
  --bg-hover: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #d946ef;
  --accent2: #8b5cf6;
  --accent-hover: #c026d3;
  --accent-glow: rgba(217, 70, 239, 0.2);
  --border: #1e293b;
  --border-light: #334155;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --info: #3b82f6;
  --code-bg: #0d1117;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --sidebar-w: 280px;
  --topbar-h: 52px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

#app { height: 100%; display: flex; flex-direction: column; }

/* Scrollbar */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ══════════════════════════════════════════════════════
   HOMEPAGE
   ══════════════════════════════════════════════════════ */
.home {
  height: 100%; display: flex; flex-direction: column;
  overflow-y: auto; overflow-x: hidden;
  background: var(--bg-primary);
}

.home-hdr {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; border-bottom: 1px solid var(--border);
  background: var(--bg-secondary); flex-shrink: 0;
  position: sticky; top: 0; z-index: 10;
  backdrop-filter: blur(12px);
}
.home-logo { display: flex; align-items: center; gap: 12px; }
.crown-ic {
  width: 42px; height: 42px;
  background: linear-gradient(135deg, #d946ef, #8b5cf6, #6366f1);
  border-radius: 13px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; flex-shrink: 0;
  box-shadow: 0 0 24px var(--accent-glow);
  animation: crownPulse 3s ease-in-out infinite;
}
@keyframes crownPulse {
  0%,100% { box-shadow: 0 0 16px var(--accent-glow); }
  50% { box-shadow: 0 0 32px var(--accent-glow), 0 0 60px rgba(139,92,246,0.15); }
}
.logo-text {
  font-size: 16px; font-weight: 800;
  background: linear-gradient(135deg, #d946ef, #a78bfa);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text; line-height: 1.2;
}
.logo-sub { font-size: 10px; color: var(--text-muted); letter-spacing: 1.5px; text-transform: uppercase; display: block; }

.hbtn {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm); cursor: pointer;
  color: var(--text-secondary);
  background: var(--bg-tertiary); border: 1px solid var(--border);
  font-size: 15px; transition: all 0.2s;
}
.hbtn:hover { border-color: var(--accent); color: var(--accent); }

.home-main { flex: 1; padding: 30px 20px 40px; max-width: 900px; margin: 0 auto; width: 100%; }

.home-hero {
  text-align: center; margin-bottom: 36px;
}
.hero-crown {
  font-size: 64px; margin-bottom: 16px;
  animation: heroFloat 4s ease-in-out infinite;
  display: inline-block;
}
@keyframes heroFloat { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
.home-hero h2 {
  font-size: 28px; font-weight: 800; margin-bottom: 10px;
  background: linear-gradient(135deg, #f1f5f9, #cbd5e1);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.home-hero p { color: var(--text-muted); font-size: 15px; max-width: 520px; margin: 0 auto 16px; line-height: 1.7; }
.free-pill {
  display: inline-flex; align-items: center; gap: 8px;
  background: rgba(34,197,94,0.1); border: 1px solid rgba(34,197,94,0.3);
  color: var(--success); padding: 8px 18px; border-radius: 24px;
  font-size: 13px; font-weight: 600;
}

/* Mode Cards Grid */
.mode-grid {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 16px; margin-bottom: 32px;
}
.mode-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px; cursor: pointer;
  transition: all 0.3s ease;
  position: relative; overflow: hidden;
}
.mode-card::before {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(135deg, transparent 50%, rgba(217,70,239,0.03));
  opacity: 0; transition: opacity 0.3s;
}
.mode-card:hover {
  border-color: var(--accent); transform: translateY(-4px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.4), 0 0 30px var(--accent-glow);
}
.mode-card:hover::before { opacity: 1; }
.mode-card:active { transform: translateY(-2px) scale(0.99); }
.mc-icon {
  width: 52px; height: 52px; border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; color: white; margin-bottom: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}
.mode-card h3 { font-size: 17px; font-weight: 700; margin-bottom: 8px; color: var(--text-primary); }
.mode-card p { font-size: 13px; color: var(--text-muted); line-height: 1.6; margin-bottom: 12px; }
.mc-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.mc-tags span {
  padding: 3px 10px; border-radius: 12px;
  background: var(--bg-tertiary); border: 1px solid var(--border);
  font-size: 10px; font-weight: 600; color: var(--text-secondary);
}
.mc-go {
  font-size: 13px; font-weight: 600; color: var(--accent);
  display: flex; align-items: center; gap: 6px;
  transition: gap 0.2s;
}
.mode-card:hover .mc-go { gap: 10px; }

/* Home Stats */
.home-stats {
  display: flex; justify-content: center; gap: 24px; flex-wrap: wrap;
}
.hs {
  display: flex; align-items: center; gap: 8px;
  color: var(--text-muted); font-size: 13px;
}
.hs i { color: var(--accent); font-size: 14px; }
.hs strong { color: var(--text-primary); font-weight: 700; }

/* ══════════════════════════════════════════════════════
   APP LAYOUT (Chat pages: General + Coding + CharChat)
   ══════════════════════════════════════════════════════ */
.app-layout { display: flex; height: 100%; width: 100%; overflow: hidden; position: relative; }

/* Sidebar Overlay (mobile) */
.sb-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.5); backdrop-filter: blur(4px);
  z-index: 49; opacity: 0; transition: opacity 0.3s;
}
.sb-overlay.vis { display: block; opacity: 1; }

/* ─── Sidebar ─── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0; z-index: 50;
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
  height: 100%;
}
.sb-logo {
  padding: 14px 16px;
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.crown-ic-sm {
  width: 34px; height: 34px;
  background: linear-gradient(135deg, #d946ef, #8b5cf6, #6366f1);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
  box-shadow: 0 0 16px var(--accent-glow);
}
.sb-title {
  font-size: 13px; font-weight: 700;
  background: linear-gradient(135deg, #d946ef, #a78bfa);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
  line-height: 1.3;
}

.new-chat-btn {
  margin: 8px 10px; padding: 10px;
  border: 1.5px dashed var(--border-light);
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer; color: var(--text-secondary);
  font-size: 13px; font-weight: 500;
  transition: all 0.2s; background: transparent;
}
.new-chat-btn:hover { border-color: var(--accent); color: var(--accent); background: rgba(217,70,239,0.05); }

.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; }
.chat-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  cursor: pointer; transition: all 0.15s; margin-bottom: 2px;
}
.chat-item:hover { background: var(--bg-tertiary); }
.chat-item.active { background: var(--bg-tertiary); border: 1px solid var(--border-light); }
.ci-ic { color: var(--text-muted); font-size: 13px; flex-shrink: 0; }
.ci-t { flex: 1; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-secondary); }
.chat-item.active .ci-t { color: var(--text-primary); }
.ci-del {
  opacity: 0; color: var(--text-muted); font-size: 11px; padding: 5px;
  border-radius: 4px; transition: all 0.15s; cursor: pointer; flex-shrink: 0;
  background: transparent; border: none;
}
.chat-item:hover .ci-del { opacity: 1; }
.ci-del:hover { color: var(--error); background: rgba(239,68,68,0.1); }

.sb-footer {
  padding: 8px 10px; border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 2px; flex-shrink: 0;
}
.sf-btn {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: var(--radius-sm);
  cursor: pointer; color: var(--text-secondary);
  font-size: 13px; transition: all 0.15s;
  background: transparent; border: none; width: 100%; text-align: left;
}
.sf-btn:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.sf-btn i { width: 16px; text-align: center; }

/* ─── Main Area ─── */
.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100%; }

/* ─── Topbar ─── */
.topbar {
  height: var(--topbar-h);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  padding: 0 8px; gap: 6px; flex-shrink: 0;
}
.tb-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm); cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.15s; background: transparent; border: none;
  font-size: 15px; flex-shrink: 0;
}
.tb-btn:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.tb-btn.active { color: var(--accent); background: rgba(217,70,239,0.1); }

.tb-title {
  font-size: 14px; font-weight: 700;
  display: flex; align-items: center; gap: 8px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tb-spacer { flex: 1; }

.model-sel {
  display: flex; align-items: center; gap: 6px;
  background: var(--bg-tertiary); padding: 5px 10px;
  border-radius: var(--radius-sm); cursor: pointer;
  font-size: 12px; font-weight: 600;
  transition: all 0.15s; border: 1px solid transparent;
  position: relative; min-width: 0;
}
.model-sel:hover { border-color: var(--border-light); }
.dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.dot.free { background: var(--success); }
.dot.groq { background: var(--warning); }
.dot.xai { background: var(--info); }
.mname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-primary); }

/* Model Dropdown */
.model-dd {
  position: fixed; top: 0; left: 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius); padding: 6px;
  min-width: 300px; max-width: 90vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  z-index: 200; display: none;
  max-height: 70vh; overflow-y: auto;
}
.model-dd.open { display: block; }
.mdd-sec {
  padding: 8px 10px 4px;
  font-size: 10px; font-weight: 700;
  color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px;
  display: flex; align-items: center; gap: 8px;
}
.mdd-badge {
  display: inline-block; font-size: 9px; font-weight: 700;
  padding: 1px 6px; border-radius: 4px; text-transform: uppercase;
}
.badge-free { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-groq { background: rgba(245,158,11,0.15); color: var(--warning); }
.badge-xai { background: rgba(59,130,246,0.15); color: var(--info); }
.mdd-item {
  padding: 8px 12px; border-radius: var(--radius-sm);
  cursor: pointer; transition: all 0.12s;
}
.mdd-item:hover { background: var(--bg-tertiary); }
.mdd-item.active { background: var(--bg-tertiary); border-left: 2px solid var(--accent); }
.mdd-nm { font-size: 13px; font-weight: 600; color: var(--text-primary); }
.mdd-ds { font-size: 11px; color: var(--text-muted); margin-top: 1px; }

/* ─── Content Area ─── */
.content-area { flex: 1; display: flex; overflow: hidden; min-height: 0; }

/* ─── Chat Panel ─── */
.chat-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.msgs-wrap {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Welcome Screen (in chat) */
.welcome-chat {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  min-height: 100%; padding: 30px 20px; text-align: center;
}
.wc-icon {
  width: 72px; height: 72px;
  background: linear-gradient(135deg, #d946ef, #8b5cf6, #6366f1);
  border-radius: 22px;
  display: flex; align-items: center; justify-content: center;
  font-size: 36px; margin-bottom: 20px;
  box-shadow: 0 0 40px var(--accent-glow);
  animation: heroFloat 4s ease-in-out infinite;
}
.welcome-chat h3 {
  font-size: 22px; font-weight: 800; margin-bottom: 8px;
  background: linear-gradient(135deg, #f1f5f9, #94a3b8);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
}
.welcome-chat p {
  color: var(--text-muted); font-size: 14px; max-width: 440px;
  margin-bottom: 24px; line-height: 1.6;
}
.wc-grid {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 10px; max-width: 520px; width: 100%;
}
.wc-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px;
  cursor: pointer; transition: all 0.2s; text-align: left;
  font-size: 13px; font-weight: 500; color: var(--text-secondary);
  line-height: 1.5;
}
.wc-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,0.3); }
.wc-card:active { transform: scale(0.98); }

/* Messages */
.msg { padding: 4px 16px; max-width: 860px; margin: 0 auto; width: 100%; }
.msg-in { display: flex; gap: 12px; padding: 10px 0; }
.msg-av {
  width: 30px; height: 30px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; flex-shrink: 0; margin-top: 2px;
}
.msg-av.user { background: linear-gradient(135deg, #3b82f6, #6366f1); color: white; }
.msg-av.assistant { background: linear-gradient(135deg, #d946ef, #8b5cf6); box-shadow: 0 0 10px var(--accent-glow); }
.msg-body { flex: 1; min-width: 0; }
.msg-ct { line-height: 1.7; font-size: 14px; word-wrap: break-word; overflow-wrap: break-word; }
.msg-ct p { margin-bottom: 8px; }
.msg-ct p:last-child { margin-bottom: 0; }
.msg-ct ul, .msg-ct ol { margin: 6px 0; padding-left: 22px; }
.msg-ct li { margin-bottom: 3px; }
.msg-ct h1,.msg-ct h2,.msg-ct h3,.msg-ct h4 { font-weight: 700; margin: 14px 0 6px; }
.msg-ct h1 { font-size: 19px; } .msg-ct h2 { font-size: 16px; } .msg-ct h3 { font-size: 14px; }
.msg-ct strong { font-weight: 700; color: #e2e8f0; }
.msg-ct em { color: #c4b5fd; }
.msg-ct a { color: var(--accent); text-decoration: underline; }
.msg-ct blockquote {
  border-left: 3px solid var(--accent); padding: 6px 14px;
  margin: 8px 0; background: rgba(217,70,239,0.04);
  border-radius: 0 8px 8px 0; color: var(--text-secondary);
}
.msg-ct table { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.msg-ct th, .msg-ct td { border: 1px solid var(--border-light); padding: 6px 10px; text-align: left; }
.msg-ct th { background: var(--bg-tertiary); font-weight: 600; }

/* Code blocks */
.msg-ct pre {
  position: relative; margin: 10px 0; border-radius: var(--radius);
  overflow: hidden; background: var(--code-bg) !important;
  border: 1px solid var(--border);
}
.code-hdr {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; background: rgba(255,255,255,0.03);
  border-bottom: 1px solid var(--border);
  font-size: 11px; color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
  flex-wrap: wrap; gap: 4px;
}
.code-acts { display: flex; gap: 4px; flex-wrap: wrap; }
.cbtn {
  padding: 3px 8px; border-radius: 5px;
  cursor: pointer; font-size: 10px; font-weight: 600;
  transition: all 0.15s; background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text-secondary);
  display: inline-flex; align-items: center; gap: 3px;
  white-space: nowrap;
}
.cbtn:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.prev-b:hover { border-color: var(--accent); color: var(--accent); }
.dep-b:hover { border-color: var(--success); color: var(--success); }

.msg-ct pre code {
  display: block; padding: 14px; overflow-x: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 12.5px; line-height: 1.55;
  background: transparent !important;
  -webkit-overflow-scrolling: touch;
}
.msg-ct :not(pre) > code {
  background: var(--bg-tertiary); padding: 2px 6px;
  border-radius: 4px; font-family: 'JetBrains Mono', monospace;
  font-size: 12px; color: #e879f9; border: 1px solid var(--border);
  word-break: break-all;
}

/* Message actions */
.msg-acts { display: flex; gap: 4px; margin-top: 6px; }
.ma {
  padding: 3px 7px; border-radius: 5px;
  cursor: pointer; font-size: 11px; color: var(--text-muted);
  transition: all 0.15s; background: transparent; border: none;
  display: flex; align-items: center; gap: 3px;
}
.ma:hover { background: var(--bg-tertiary); color: var(--text-primary); }

/* Typing indicator */
.typing { display: flex; gap: 4px; padding: 6px 0; }
.typing span {
  width: 6px; height: 6px; background: var(--accent);
  border-radius: 50%; animation: tBounce 1.4s ease-in-out infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes tBounce {
  0%,60%,100% { transform: translateY(0); opacity: 0.3; }
  30% { transform: translateY(-7px); opacity: 1; }
}

/* ─── Input Area ─── */
.input-area {
  padding: 10px 16px;
  padding-bottom: calc(10px + var(--safe-bottom));
  max-width: 860px; margin: 0 auto; width: 100%;
  flex-shrink: 0;
}
.input-box {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden; transition: border-color 0.2s, box-shadow 0.2s;
}
.input-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.input-top { display: flex; align-items: flex-end; padding: 10px 12px; gap: 8px; }
#inp {
  flex: 1; background: transparent; border: none;
  color: var(--text-primary); font-size: 14px;
  font-family: 'Inter', sans-serif; line-height: 1.5;
  resize: none; min-height: 22px; max-height: 160px;
  outline: none; -webkit-appearance: none;
}
#inp::placeholder { color: var(--text-muted); }

.send-btn {
  width: 34px; height: 34px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  background: linear-gradient(135deg, #d946ef, #8b5cf6);
  border: none; color: white; font-size: 14px;
  transition: all 0.2s; box-shadow: 0 0 14px var(--accent-glow);
  flex-shrink: 0;
}
.send-btn:hover { transform: scale(1.05); }
.send-btn:active { transform: scale(0.95); }
.send-btn.stop { background: linear-gradient(135deg, #ef4444, #dc2626); box-shadow: 0 0 14px rgba(239,68,68,0.25); }

.input-btm {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; border-top: 1px solid var(--border);
  gap: 4px; flex-wrap: wrap;
}
.inp-feats { display: flex; gap: 1px; }
.ifb {
  padding: 5px 7px; border-radius: 5px;
  cursor: pointer; color: var(--text-muted);
  font-size: 13px; transition: all 0.15s;
  background: transparent; border: none;
}
.ifb:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.inp-info {
  font-size: 10px; color: var(--text-muted);
  display: flex; align-items: center; gap: 4px; white-space: nowrap;
}

/* ─── Preview Panel (Coding Mode) ─── */
.preview-panel {
  width: 0; overflow: hidden;
  border-left: 1px solid var(--border);
  background: var(--bg-secondary);
  display: flex; flex-direction: column;
  transition: width 0.3s ease;
}
.preview-panel.open { width: 50%; }
.prev-hdr {
  height: var(--topbar-h);
  display: flex; align-items: center;
  padding: 0 12px; gap: 8px;
  border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.prev-ttl {
  font-size: 13px; font-weight: 600; flex: 1;
  display: flex; align-items: center; gap: 6px;
}
.prev-tabs { display: flex; gap: 2px; }
.ptab {
  padding: 5px 10px; border-radius: 5px;
  cursor: pointer; font-size: 11px; font-weight: 600;
  color: var(--text-muted); transition: all 0.15s;
  background: transparent; border: none;
}
.ptab:hover { color: var(--text-primary); background: var(--bg-tertiary); }
.ptab.active { color: var(--accent); background: rgba(217,70,239,0.1); }
.prev-x {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px; cursor: pointer; color: var(--text-muted);
  transition: all 0.15s; background: transparent; border: none;
}
.prev-x:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.prev-body { flex: 1; overflow: hidden; background: white; position: relative; }
.prev-body iframe { width: 100%; height: 100%; border: none; background: white; }
.prev-code {
  width: 100%; height: 100%; overflow: auto;
  background: var(--code-bg); padding: 14px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px; line-height: 1.55;
  white-space: pre-wrap; color: var(--text-primary);
  display: none;
}

/* ══════════════════════════════════════════════════════
   CHARACTER PAGE
   ══════════════════════════════════════════════════════ */
.char-page {
  height: 100%; display: flex; flex-direction: column;
  overflow: hidden;
}
.char-content {
  flex: 1; overflow-y: auto; padding: 20px;
}
.create-char-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 14px;
  background: linear-gradient(135deg, rgba(217,70,239,0.1), rgba(139,92,246,0.1));
  border: 2px dashed var(--accent);
  border-radius: var(--radius-lg);
  color: var(--accent); font-size: 15px; font-weight: 600;
  cursor: pointer; transition: all 0.2s; margin-bottom: 20px;
}
.create-char-btn:hover {
  background: linear-gradient(135deg, rgba(217,70,239,0.15), rgba(139,92,246,0.15));
  transform: translateY(-2px); box-shadow: 0 8px 24px var(--accent-glow);
}

.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.char-card {
  display: flex; align-items: center; gap: 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  cursor: pointer; transition: all 0.2s; position: relative;
}
.char-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,0.3); }
.cc-avatar {
  width: 48px; height: 48px; border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.cc-info { flex: 1; min-width: 0; }
.cc-info h4 { font-size: 14px; font-weight: 700; margin-bottom: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cc-info p { font-size: 12px; color: var(--text-muted); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.cc-msgs { font-size: 10px; color: var(--text-muted); margin-top: 4px; display: block; }
.cc-del {
  position: absolute; top: 8px; right: 8px;
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px; cursor: pointer; color: var(--text-muted);
  background: transparent; border: none;
  opacity: 0; transition: all 0.15s;
}
.char-card:hover .cc-del { opacity: 1; }
.cc-del:hover { background: rgba(239,68,68,0.1); color: var(--error); }

.char-empty {
  text-align: center; padding: 40px 20px; color: var(--text-muted);
}
.ce-icon { font-size: 56px; margin-bottom: 16px; }
.char-empty h3 { font-size: 20px; font-weight: 700; color: var(--text-secondary); margin-bottom: 8px; }
.char-empty p { font-size: 14px; line-height: 1.6; max-width: 400px; margin: 0 auto; }

/* Character Chat Header */
.char-hdr-info {
  display: flex; align-items: center; gap: 10px; min-width: 0;
}
.char-hdr-av {
  width: 32px; height: 32px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; flex-shrink: 0;
}
.char-hdr-name { font-size: 14px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.char-hdr-status { font-size: 10px; color: var(--success); }

/* Character Create Modal — Emoji Picker */
.emoji-pick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.ep {
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; border-radius: 10px;
  background: var(--bg-tertiary); border: 2px solid transparent;
  cursor: pointer; transition: all 0.15s;
}
.ep:hover { background: var(--bg-hover); transform: scale(1.1); }
.ep.sel { border-color: var(--accent); background: rgba(217,70,239,0.1); box-shadow: 0 0 12px var(--accent-glow); }

/* ══════════════════════════════════════════════════════
   IMAGE & FILE GENERATION PAGE
   ══════════════════════════════════════════════════════ */
.img-page {
  height: 100%; display: flex; flex-direction: column;
  overflow: hidden;
}
.img-content {
  flex: 1; overflow-y: auto; padding: 20px;
}
.img-gen-panel {
  max-width: 700px; margin: 0 auto;
}
.ig-section { margin-bottom: 16px; }
.ig-row { display: flex; gap: 12px; }
.ig-half { flex: 1; min-width: 0; }

.fg-label {
  display: block; font-size: 12px; font-weight: 600;
  color: var(--text-secondary); margin-bottom: 6px;
}
.fg-input {
  width: 100%; padding: 9px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  color: var(--text-primary); font-size: 13px;
  font-family: 'Inter', sans-serif;
  transition: border-color 0.2s; outline: none;
  -webkit-appearance: none;
}
.fg-input:focus { border-color: var(--accent); }
.fg-input::placeholder { color: var(--text-muted); }
.fg-ta { min-height: 80px; resize: vertical; }
.fg-hint { font-size: 10px; color: var(--text-muted); margin-top: 4px; }
.fg-hint a { color: var(--accent); }

select.fg-input {
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

.btn-gen {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 14px;
  background: linear-gradient(135deg, #d946ef, #a21caf);
  border: none; border-radius: var(--radius);
  color: white; font-size: 15px; font-weight: 700;
  cursor: pointer; transition: all 0.2s;
  box-shadow: 0 4px 20px rgba(217,70,239,0.3);
  margin-bottom: 16px;
}
.btn-gen:hover { transform: translateY(-2px); box-shadow: 0 8px 30px rgba(217,70,239,0.4); }
.btn-gen:active { transform: scale(0.98); }

.img-loading {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  padding: 30px; color: var(--text-muted); font-size: 14px;
}
.spinner {
  width: 24px; height: 24px;
  border: 3px solid var(--border-light);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.img-result { margin-bottom: 20px; }
.img-res-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.img-res-card img {
  width: 100%; display: block;
  max-height: 500px; object-fit: contain;
  background: #111;
}
.img-res-actions {
  display: flex; gap: 8px; padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.img-res-prompt {
  padding: 0 16px 12px;
  font-size: 12px; color: var(--text-muted);
  line-height: 1.5;
}
.img-err { color: var(--error); text-align: center; padding: 20px; font-size: 14px; }

.ig-divider {
  display: flex; align-items: center; gap: 12px;
  margin: 24px 0 16px; max-width: 700px; margin-left: auto; margin-right: auto;
}
.ig-divider::before, .ig-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}
.ig-divider span { font-size: 12px; font-weight: 600; color: var(--text-muted); white-space: nowrap; }

.img-history {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px; max-width: 700px; margin: 0 auto;
}
.ih-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden; cursor: pointer;
  transition: all 0.2s;
}
.ih-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,0.3); }
.ih-card img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; background: #111; }
.ih-info { padding: 8px 10px; }
.ih-prompt { font-size: 11px; color: var(--text-secondary); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ih-meta { font-size: 9px; color: var(--text-muted); margin-top: 2px; display: block; }

/* ══════════════════════════════════════════════════════
   IMAGE GENERATION v3.1 — Self-Hosted Backend Tabs
   ══════════════════════════════════════════════════════ */

/* Backend Selector Tabs (Free Cloud / A1111 / ComfyUI) */
.img-tabs {
  display: flex; gap: 4px; margin-bottom: 20px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius); padding: 4px;
}
.img-tab {
  flex: 1; padding: 10px 8px;
  text-align: center; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 12px; font-weight: 600;
  color: var(--text-muted); transition: all 0.2s;
  background: transparent; border: none;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  white-space: nowrap;
}
.img-tab:hover { color: var(--text-secondary); background: var(--bg-tertiary); }
.img-tab.active {
  color: white;
  background: linear-gradient(135deg, #d946ef, #8b5cf6);
  box-shadow: 0 2px 12px var(--accent-glow);
}
.img-tab i { font-size: 13px; }

/* Panel container */
.img-panel { display: none; }
.img-panel.active { display: block; }

/* Server Connection UI */
.local-conn {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.local-conn h4 {
  font-size: 13px; font-weight: 700; margin-bottom: 10px;
  color: var(--text-primary);
  display: flex; align-items: center; gap: 8px;
}
.local-conn h4 i { color: var(--accent); }
.lc-row {
  display: flex; gap: 8px; align-items: center;
  margin-bottom: 8px;
}
.lc-row .fg-input { flex: 1; }
.btn-conn {
  padding: 9px 16px;
  background: linear-gradient(135deg, #d946ef, #8b5cf6);
  border: none; border-radius: var(--radius-sm);
  color: white; font-size: 12px; font-weight: 600;
  cursor: pointer; transition: all 0.2s;
  white-space: nowrap; flex-shrink: 0;
  display: flex; align-items: center; gap: 6px;
}
.btn-conn:hover { transform: translateY(-1px); box-shadow: 0 4px 16px var(--accent-glow); }
.btn-conn:active { transform: scale(0.97); }
.btn-conn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.lc-status {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px; font-weight: 600; padding: 4px 10px;
  border-radius: 12px;
}
.lc-status.connecting { color: var(--warning); background: rgba(245,158,11,0.1); }
.lc-status.connected { color: var(--success); background: rgba(34,197,94,0.1); }
.lc-status.failed { color: var(--error); background: rgba(239,68,68,0.1); }
.lc-status i { font-size: 8px; }

.lc-hint {
  font-size: 10px; color: var(--text-muted); margin-top: 6px; line-height: 1.5;
}
.lc-hint code {
  background: var(--bg-tertiary); padding: 1px 5px;
  border-radius: 3px; font-family: 'JetBrains Mono', monospace;
  font-size: 9px; color: var(--accent);
}

/* Mode Tabs (txt2img / img2img / inpaint / upscale) */
.mode-tabs {
  display: flex; gap: 3px; margin-bottom: 14px;
  background: var(--bg-tertiary); border-radius: var(--radius-sm);
  padding: 3px;
}
.mt {
  flex: 1; padding: 7px 6px; text-align: center;
  border-radius: 6px; cursor: pointer;
  font-size: 11px; font-weight: 600;
  color: var(--text-muted); transition: all 0.15s;
  background: transparent; border: none;
  white-space: nowrap;
}
.mt:hover { color: var(--text-secondary); }
.mt.active {
  color: var(--text-primary);
  background: var(--bg-secondary);
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

/* Uncensored Badge */
.uncen-badge {
  display: inline-flex; align-items: center; gap: 5px;
  background: rgba(239,68,68,0.1); border: 1px solid rgba(239,68,68,0.3);
  color: #f87171; padding: 3px 10px; border-radius: 12px;
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.5px;
}
.uncen-badge i { font-size: 9px; }

/* Upload Zone */
.upload-zone {
  border: 2px dashed var(--border-light);
  border-radius: var(--radius);
  padding: 24px 16px; text-align: center;
  cursor: pointer; transition: all 0.2s;
  background: var(--bg-tertiary);
  position: relative; overflow: hidden;
}
.upload-zone:hover {
  border-color: var(--accent);
  background: rgba(217,70,239,0.03);
}
.upload-zone.has-image { padding: 8px; }
.upload-zone i { font-size: 28px; color: var(--text-muted); margin-bottom: 8px; display: block; }
.upload-zone span { font-size: 12px; color: var(--text-muted); display: block; }
.upload-zone img {
  max-width: 100%; max-height: 200px; border-radius: var(--radius-sm);
  object-fit: contain; display: block; margin: 0 auto;
}
.upload-zone .uz-remove {
  position: absolute; top: 6px; right: 6px;
  width: 24px; height: 24px; border-radius: 50%;
  background: rgba(239,68,68,0.85); color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; cursor: pointer; border: none;
  opacity: 0; transition: opacity 0.2s;
}
.upload-zone:hover .uz-remove { opacity: 1; }

/* 1/3 width columns */
.ig-third { flex: 1; min-width: 0; }
.ig-row-3 { display: flex; gap: 10px; }

/* Range Slider */
.fg-range {
  width: 100%; height: 4px; -webkit-appearance: none;
  background: var(--bg-hover); border-radius: 2px;
  outline: none; margin: 6px 0;
  cursor: pointer;
}
.fg-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px; border-radius: 50%;
  background: linear-gradient(135deg, #d946ef, #8b5cf6);
  box-shadow: 0 0 8px var(--accent-glow);
  cursor: pointer;
}
.fg-range::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: linear-gradient(135deg, #d946ef, #8b5cf6);
  box-shadow: 0 0 8px var(--accent-glow);
  cursor: pointer; border: none;
}
.range-val {
  display: inline-block; font-size: 12px; font-weight: 700;
  color: var(--accent); min-width: 32px; text-align: right;
}

/* Progress Bar */
.gen-progress {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 16px;
  display: none;
}
.gen-progress.active { display: block; }
.gp-bar {
  width: 100%; height: 8px;
  background: var(--bg-tertiary);
  border-radius: 4px; overflow: hidden;
  margin-bottom: 8px;
}
.gp-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, #d946ef, #8b5cf6, #6366f1);
  border-radius: 4px;
  transition: width 0.3s ease;
  animation: progressShimmer 2s linear infinite;
  background-size: 200% 100%;
}
@keyframes progressShimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.gp-text {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--text-muted);
}
.gp-cancel {
  padding: 4px 12px; border-radius: 6px;
  background: rgba(239,68,68,0.1); border: 1px solid rgba(239,68,68,0.3);
  color: #f87171; font-size: 10px; font-weight: 600;
  cursor: pointer; transition: all 0.15s;
}
.gp-cancel:hover { background: rgba(239,68,68,0.2); }

/* Source Badges (on image results & history) */
.img-src-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 8px;
  font-size: 9px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.3px;
}
.img-src-badge.free { background: rgba(34,197,94,0.1); color: var(--success); }
.img-src-badge.a1111 { background: rgba(59,130,246,0.1); color: var(--info); }
.img-src-badge.comfyui { background: rgba(245,158,11,0.1); color: var(--warning); }

/* LoRA insert buttons */
.lora-list { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.lora-btn {
  padding: 3px 8px; border-radius: 6px;
  background: var(--bg-tertiary); border: 1px solid var(--border);
  color: var(--text-secondary); font-size: 10px; font-weight: 600;
  cursor: pointer; transition: all 0.15s;
  white-space: nowrap; max-width: 180px;
  overflow: hidden; text-overflow: ellipsis;
}
.lora-btn:hover { border-color: var(--accent); color: var(--accent); background: rgba(217,70,239,0.05); }

/* Batch output grid */
.batch-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px; margin-top: 12px;
}
.batch-grid img {
  width: 100%; aspect-ratio: 1; object-fit: contain;
  background: #111; border-radius: var(--radius-sm);
  border: 1px solid var(--border); cursor: pointer;
  transition: all 0.2s;
}
.batch-grid img:hover { border-color: var(--accent); transform: scale(1.02); }

/* Use as Input button */
.btn-use-input {
  padding: 6px 12px; border-radius: var(--radius-sm);
  background: rgba(59,130,246,0.1); border: 1px solid rgba(59,130,246,0.3);
  color: var(--info); font-size: 11px; font-weight: 600;
  cursor: pointer; transition: all 0.15s;
  display: inline-flex; align-items: center; gap: 5px;
}
.btn-use-input:hover { background: rgba(59,130,246,0.2); }

/* ControlNet section */
.controlnet-section {
  background: var(--bg-tertiary); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px; margin-top: 8px;
}
.controlnet-section h5 {
  font-size: 11px; font-weight: 700; color: var(--accent);
  margin-bottom: 8px; display: flex; align-items: center; gap: 6px;
}

/* ══════════════════════════════════════════════════════
   MODALS
   ══════════════════════════════════════════════════════ */
.modal-bg {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6); backdrop-filter: blur(6px);
  z-index: 200; display: none;
  align-items: center; justify-content: center;
  padding: 20px;
}
.modal-bg.open { display: flex; }

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border-light);
  border-radius: 18px; width: 100%;
  max-width: 520px; max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 40px 100px rgba(0,0,0,0.5);
  animation: modalIn 0.25s ease;
}
@keyframes modalIn { from { transform: translateY(20px) scale(0.97); opacity: 0; } }

.modal-hdr {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--bg-secondary); z-index: 1;
}
.modal-hdr h3 { font-size: 16px; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.modal-x {
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px; cursor: pointer; color: var(--text-muted);
  transition: all 0.15s; background: transparent; border: none;
}
.modal-x:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.modal-body { padding: 20px; }

.fg { margin-bottom: 16px; }

/* Free notice in settings */
.free-notice {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 14px; margin-bottom: 20px;
  background: rgba(34,197,94,0.08);
  border: 1px solid rgba(34,197,94,0.25);
  border-radius: var(--radius); color: var(--success);
  font-size: 13px;
}
.free-notice i { font-size: 18px; margin-top: 2px; flex-shrink: 0; }

/* Buttons */
.btn-primary {
  padding: 9px 20px;
  background: linear-gradient(135deg, #d946ef, #8b5cf6);
  border: none; border-radius: var(--radius-sm);
  color: white; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: all 0.2s;
  display: inline-flex; align-items: center; gap: 6px;
  text-decoration: none;
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 0 20px var(--accent-glow); }
.btn-primary:active { transform: scale(0.97); }
.btn-sec {
  padding: 9px 20px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  color: var(--text-primary); font-size: 13px; font-weight: 500;
  cursor: pointer; transition: all 0.2s;
  display: inline-flex; align-items: center; gap: 6px;
}
.btn-sec:hover { background: var(--bg-hover); }

/* Deploy options */
.dep-opts { display: flex; flex-direction: column; gap: 8px; }
.dep-opt {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; background: var(--bg-tertiary);
  border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer; transition: all 0.2s;
}
.dep-opt:hover { border-color: var(--accent); background: rgba(217,70,239,0.05); }
.dep-opt:active { transform: scale(0.98); }
.dep-opt i {
  font-size: 20px; width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 9px; background: rgba(217,70,239,0.1); color: var(--accent);
  flex-shrink: 0;
}
.dep-name { font-size: 13px; font-weight: 600; }
.dep-desc { font-size: 11px; color: var(--text-muted); margin-top: 1px; }

/* Toast */
.toast-box {
  position: fixed; top: 16px; right: 16px;
  z-index: 300; display: flex; flex-direction: column; gap: 6px;
  max-width: calc(100vw - 32px);
}
.toast {
  padding: 10px 16px; border-radius: var(--radius);
  font-size: 12px; font-weight: 500;
  display: flex; align-items: center; gap: 8px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
  animation: tSlide 0.3s ease; max-width: 340px;
  word-break: break-word;
}
.toast.success { background: #065f46; border: 1px solid #10b981; color: #6ee7b7; }
.toast.error { background: #7f1d1d; border: 1px solid #ef4444; color: #fca5a5; }
.toast.info { background: #1e3a5f; border: 1px solid #3b82f6; color: #93c5fd; }
@keyframes tSlide { from { transform: translateX(100%); opacity: 0; } }

/* ═══════════════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
   ═══════════════════════════════════════════════════ */

/* Tablets */
@media (max-width: 1024px) {
  .preview-panel.open { width: 45%; }
  .mode-grid { grid-template-columns: repeat(2, 1fr); gap: 14px; }
  .ig-row-3 { flex-wrap: wrap; }
  .ig-row-3 .ig-third { min-width: calc(50% - 6px); }
}

/* Mobile */
@media (max-width: 768px) {
  :root { --sidebar-w: 280px; --topbar-h: 48px; }

  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: 4px 0 20px rgba(0,0,0,0.3);
  }
  .sidebar.open { transform: translateX(0); }

  .preview-panel.open {
    position: fixed; inset: 0; width: 100% !important;
    z-index: 48; border: none;
  }

  .topbar { padding: 0 6px; gap: 4px; }
  .model-sel { padding: 4px 8px; font-size: 11px; }
  .mname { max-width: 100px; }
  .tb-title { font-size: 12px; }

  /* Home */
  .home-main { padding: 20px 14px 30px; }
  .home-hero h2 { font-size: 22px; }
  .home-hero p { font-size: 13px; }
  .hero-crown { font-size: 50px; }
  .free-pill { font-size: 11px; padding: 6px 14px; }
  .mode-grid { grid-template-columns: 1fr; gap: 12px; }
  .mode-card { padding: 18px; }
  .mc-icon { width: 44px; height: 44px; font-size: 18px; }
  .mode-card h3 { font-size: 15px; }
  .home-stats { gap: 16px; }
  .hs { font-size: 11px; }

  /* Chat */
  .msg { padding: 3px 12px; }
  .msg-in { gap: 8px; padding: 8px 0; }
  .msg-av { width: 26px; height: 26px; font-size: 11px; }
  .msg-ct { font-size: 13.5px; line-height: 1.65; }
  .msg-ct pre code { font-size: 11.5px; padding: 10px; }

  .input-area { padding: 8px 10px; padding-bottom: calc(8px + var(--safe-bottom)); }
  .input-top { padding: 8px 10px; gap: 6px; }
  #inp { font-size: 16px; } /* Prevent iOS zoom */

  .welcome-chat { padding: 20px 16px; }
  .wc-icon { width: 60px; height: 60px; font-size: 28px; border-radius: 18px; }
  .welcome-chat h3 { font-size: 18px; }
  .welcome-chat p { font-size: 13px; }
  .wc-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .wc-card { padding: 12px; font-size: 12px; }

  /* Characters */
  .char-content { padding: 14px; }
  .char-grid { grid-template-columns: 1fr; gap: 10px; }
  .char-hdr-name { max-width: 120px; }

  /* Image */
  .img-content { padding: 14px; }
  .ig-row { flex-direction: column; gap: 0; }
  .ig-row-3 { flex-direction: column; gap: 0; }
  .img-history { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .img-tabs { flex-wrap: wrap; }
  .img-tab { font-size: 11px; padding: 8px 6px; gap: 4px; }
  .img-tab i { font-size: 11px; }
  .mode-tabs { flex-wrap: wrap; }
  .mt { font-size: 10px; padding: 6px 4px; }
  .local-conn { padding: 12px; }
  .lc-row { flex-direction: column; }
  .btn-conn { width: 100%; justify-content: center; }
  .upload-zone { padding: 18px 12px; }
  .batch-grid { grid-template-columns: repeat(2, 1fr); gap: 6px; }

  /* Modal */
  .model-dd { max-width: calc(100vw - 20px); }
  .modal { margin: 10px; border-radius: 14px; }
  .toast-box { top: 10px; right: 10px; left: 10px; }
  .toast { max-width: 100%; }
}

/* Small phones */
@media (max-width: 400px) {
  .wc-grid { grid-template-columns: 1fr; gap: 6px; }
  .mname { max-width: 80px; }
  .code-hdr { padding: 4px 8px; }
  .cbtn { font-size: 9px; padding: 2px 6px; }
  .home-hdr { padding: 10px 12px; }
  .logo-text { font-size: 14px; }
  .crown-ic { width: 36px; height: 36px; font-size: 17px; }
  .mc-tags span { font-size: 9px; padding: 2px 7px; }
  .char-card { padding: 12px; }
  .cc-avatar { width: 40px; height: 40px; font-size: 20px; }
  .img-history { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  .img-tab { font-size: 10px; gap: 3px; padding: 7px 4px; }
  .mt { font-size: 9px; }
  .lora-btn { font-size: 9px; max-width: 140px; }
  .ep { width: 34px; height: 34px; font-size: 17px; }
}

/* Landscape phones */
@media (max-height: 500px) and (max-width: 900px) {
  .home-hero { margin-bottom: 20px; }
  .hero-crown { font-size: 40px; margin-bottom: 8px; }
  .home-hero h2 { font-size: 18px; }
  .mode-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .mode-card { padding: 14px; }
  .mc-icon { width: 36px; height: 36px; font-size: 16px; margin-bottom: 8px; }
  .wc-icon { width: 48px; height: 48px; font-size: 22px; margin-bottom: 12px; }
}
