:root {
  --bg: #ffffff;
  --bg-alt: #f6f8fa;
  --bg-elev: #ffffff;
  --bg-input: #ffffff;
  --text: #24292f;
  --text-muted: #656d76;
  --border: #d0d7de;
  --accent: #0969da;
  --accent-hover: #0550ae;
  --danger: #cf222e;
  --success: #1a7f37;
  --warning: #9a6700;
  --radius: 8px;
}

[data-theme="dark"] {
  --bg: #0e1117;
  --bg-alt: #161b22;
  --bg-elev: #1c2330;
  --bg-input: #111827;
  --text: #e6edf3;
  --text-muted: #9da7b3;
  --border: #30363d;
  --accent: #4c9aff;
  --accent-hover: #79b8ff;
  --danger: #f85149;
  --success: #3fb950;
  --warning: #d29922;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background: var(--bg); color: var(--text); }

.app { display: flex; height: 100vh; overflow: hidden; }

/* Sidebar */
.sidebar { width: 280px; background: var(--bg-alt); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px; overflow-y: auto; }
.sidebar-header h1 { margin: 0 0 4px; font-size: 1.25rem; }
.tagline { font-size: 0.75rem; color: var(--text-muted); }
.toggle-row { display: flex; align-items: center; gap: 8px; margin: 12px 0; font-size: 0.9rem; cursor: pointer; }
.nav-group { margin: 12px 0; }
.nav-label { font-size: 0.75rem; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.5px; margin-bottom: 6px; display: block; }
.radio-group { display: flex; flex-direction: column; gap: 6px; }
.radio { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: var(--radius); cursor: pointer; font-size: 0.9rem; }
.radio:hover { background: var(--bg-elev); }
.radio.active { background: var(--bg-elev); font-weight: 600; }
.radio input { accent-color: var(--accent); }
.agent-status { margin-top: auto; font-size: 0.8rem; color: var(--text-muted); }
.agent-status .status-item { margin: 4px 0; padding: 4px 0; border-top: 1px solid var(--border); }
.sidebar-footer { margin-top: 12px; font-size: 0.7rem; color: var(--text-muted); text-align: center; }

/* Theme customization */
.theme-panel { padding: 0 0 8px; }
.theme-panel input[type="color"] { width: 100%; height: 32px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg-input); cursor: pointer; }

/* Main */
.main { flex: 1; overflow-y: auto; padding: 24px 32px; }
h2 { margin-top: 0; font-size: 1.5rem; }
.caption { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 16px; }

/* Panels / Forms */
.panel { background: var(--bg-alt); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
label { display: block; margin: 12px 0 4px; font-size: 0.85rem; font-weight: 600; }
input[type="text"], input[type="email"], input[type="password"], textarea, select { width: 100%; padding: 8px 10px; background: var(--bg-input); color: var(--text); border: 1px solid var(--border); border-radius: 6px; font-size: 0.9rem; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.range-val { font-size: 0.85rem; color: var(--text-muted); }
.check-row { display: flex; align-items: center; gap: 8px; cursor: pointer; margin: 8px 0; }
.check-row input { accent-color: var(--accent); }

/* Grid */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

/* Buttons */
.btn { padding: 8px 16px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); cursor: pointer; font-size: 0.9rem; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.secondary { background: var(--bg-alt); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 0.8rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-row { display: flex; gap: 12px; margin: 16px 0; }
.auth-panel { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; margin: 12px 0; background: var(--bg-elev); }
.auth-panel input { margin-bottom: 8px; }
.auth-panel .btn { width: 100%; margin-top: 6px; }

/* Progress */
.progress { margin: 12px 0; }
.progress-bar { height: 8px; background: var(--bg-elev); border-radius: 4px; overflow: hidden; border: 1px solid var(--border); }
.progress-bar > div { height: 100%; background: var(--accent); width: 0%; transition: width 0.3s ease; }
.progress span { font-size: 0.85rem; color: var(--text-muted); margin-top: 4px; display: block; }
.progress-bar.indeterminate > div {
  width: 40% !important;
  transition: none;
  animation: progress-indeterminate 1.2s ease-in-out infinite;
}
@keyframes progress-indeterminate {
  0% { margin-left: -40%; }
  100% { margin-left: 100%; }
}

/* Alerts */
.alert { padding: 10px 12px; border-radius: var(--radius); border: 1px solid var(--border); margin: 12px 0; font-size: 0.9rem; }
.alert.success { border-color: var(--success); color: var(--success); background: rgba(26,127,55,0.08); }
.alert.error { border-color: var(--danger); color: var(--danger); background: rgba(207,34,46,0.08); }
.alert.warning { border-color: var(--warning); color: var(--warning); background: rgba(154,103,0,0.08); }

/* Results */
.result { background: var(--bg-alt); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin: 16px 0; }
.result pre { background: var(--bg-elev); padding: 12px; border-radius: 6px; overflow-x: auto; }
.result img { max-width: 100%; border-radius: 6px; display: block; margin: 8px auto; }

/* Chat */
.chat-messages { display: flex; flex-direction: column; gap: 10px; max-height: 60vh; overflow-y: auto; padding: 12px; background: var(--bg-alt); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 8px; }
.chat-bubble { padding: 10px 14px; border-radius: 12px; max-width: 90%; font-size: 0.9rem; line-height: 1.4; }
.chat-bubble.user { align-self: flex-end; background: var(--accent); color: #fff; }
.chat-bubble.assistant { align-self: flex-start; background: var(--bg-elev); border: 1px solid var(--border); }
.chat-bubble.system { align-self: center; font-style: italic; color: var(--text-muted); font-size: 0.8rem; background: transparent; border: none; }
.chat-input-row { display: flex; gap: 8px; }
.chat-input-row input { flex: 1; }

/* Utility */
.hidden { display: none !important; }
.thumb-grid { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.thumb-grid img { width: 80px; height: 80px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }

/* Image gallery */
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 14px; margin-top: 16px; }
.gallery-item { margin: 0; cursor: pointer; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--bg-elev); transition: transform 0.12s ease, box-shadow 0.12s ease; }
.gallery-item:hover { transform: translateY(-2px); box-shadow: 0 4px 14px rgba(0,0,0,0.18); border-color: var(--accent); }
.gallery-item img { width: 100%; aspect-ratio: 1 / 1; object-fit: cover; display: block; }
.gallery-item figcaption { padding: 6px 8px; font-size: 0.75rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Image lightbox / regenerate modal */
.image-modal { position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center; }
.image-modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.65); }
.image-modal-body { position: relative; background: var(--bg-alt); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; width: min(560px, 92vw); max-height: 92vh; overflow-y: auto; z-index: 1; }
.image-modal-body img { width: 100%; max-height: 50vh; object-fit: contain; border-radius: 6px; background: var(--bg-elev); }
.image-modal-body label { display: block; margin: 12px 0 4px; font-size: 0.85rem; }
.image-modal-body textarea { width: 100%; }
.image-modal-name { font-size: 0.8rem; color: var(--text-muted); margin-top: 6px; word-break: break-all; }
.image-modal-close { position: absolute; top: 8px; right: 12px; background: transparent; border: none; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--text-muted); }
.image-modal-close:hover { color: var(--text); }

/* Expander style */
.expander { border: 1px solid var(--border); border-radius: var(--radius); margin: 8px 0; overflow: hidden; }
.expander-header { padding: 10px 12px; background: var(--bg-elev); cursor: pointer; font-weight: 600; font-size: 0.9rem; display: flex; justify-content: space-between; align-items: center; }
.expander-body { padding: 12px; background: var(--bg-alt); font-size: 0.9rem; }
.expander-body p, .expander-body li { margin: 6px 0; }
.expander-body ul { padding-left: 18px; }

/* Markdown content inside results */
.markdown-body h1, .markdown-body h2, .markdown-body h3 { margin-top: 16px; margin-bottom: 8px; }
.markdown-body p { margin: 8px 0; line-height: 1.6; }
.markdown-body code { background: var(--bg-elev); padding: 2px 6px; border-radius: 4px; font-size: 0.85rem; }
.markdown-body pre { background: var(--bg-elev); padding: 12px; border-radius: 6px; overflow-x: auto; }
.markdown-body blockquote { border-left: 4px solid var(--border); margin: 0; padding-left: 12px; color: var(--text-muted); }
.markdown-body table { border-collapse: collapse; width: 100%; margin: 8px 0; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 6px 10px; }
.markdown-body th { background: var(--bg-elev); }

/* Config cards */
.config-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; background: var(--bg-alt); }
.config-card h4 { margin: 0 0 8px; font-size: 1rem; }
.config-card .fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.config-card .actions { display: flex; gap: 8px; margin-top: 10px; }

/* Mobile top bar + hamburger + overlay (hidden on desktop) */
.mobile-topbar { display: none; align-items: center; gap: 12px; height: 56px; padding: 0 16px; background: var(--bg-alt); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50; }
.mobile-title { font-size: 1.05rem; font-weight: 600; }
.hamburger { display: inline-flex; flex-direction: column; justify-content: center; gap: 5px; width: 40px; height: 40px; padding: 8px; background: transparent; border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.hamburger span { display: block; height: 2px; width: 100%; background: var(--text); border-radius: 2px; transition: transform 0.25s ease, opacity 0.2s ease; }
.app.nav-open .hamburger span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.app.nav-open .hamburger span:nth-child(2) { opacity: 0; }
.app.nav-open .hamburger span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }
.sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 40; opacity: 0; transition: opacity 0.25s ease; }
.sidebar-overlay.show { opacity: 1; }

@media (max-width: 768px) {
  .mobile-topbar { display: flex; }
  .app { height: auto; min-height: 100vh; overflow: visible; }

  /* Off-canvas sidebar drawer */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: 82%; max-width: 320px;
    z-index: 45; transform: translateX(-100%); transition: transform 0.28s ease;
    box-shadow: 2px 0 16px rgba(0,0,0,0.25);
  }
  .app.nav-open .sidebar { transform: translateX(0); }
  .app.nav-open .sidebar-overlay { display: block; }

  .main { padding: 16px; width: 100%; overflow-y: visible; }
  h2 { font-size: 1.25rem; }

  /* Stack form columns and button rows */
  .grid-2 { grid-template-columns: 1fr; }
  .config-card .fields { grid-template-columns: 1fr; }
  .btn-row { flex-direction: column; }
  .btn-row .btn { width: 100%; }
  .chat-actions { flex-wrap: wrap; }

  /* Larger touch targets + prevent iOS zoom on focus (>=16px) */
  input[type="text"], input[type="email"], input[type="password"], textarea, select { font-size: 16px; padding: 12px; }
  .btn { padding: 12px 16px; font-size: 1rem; }
  .btn.small { padding: 8px 12px; font-size: 0.85rem; }
  .radio { padding: 10px 8px; }
  .chat-bubble { max-width: 100%; }
  .chat-messages { max-height: 55vh; }

  /* Keep chat send button from shrinking */
  .chat-input-row { gap: 6px; }
  .chat-input-row .btn { width: auto; white-space: nowrap; }
}

@media (max-width: 400px) {
  .main { padding: 12px; }
  .mobile-title { font-size: 0.95rem; }
}
