:root {
  --bg: #0b0c0e;
  --surface: #17181b;
  --surface-2: #202226;
  --border: rgba(255, 255, 255, 0.08);
  --text: #ededf0;
  --text-dim: #9a9ca3;
  --accent: #3ea88f;
  --accent-text: #062521;
  --danger: #e5695f;
  --radius-lg: 20px;
  --radius-md: 14px;
  --font: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f7f5;
    --surface: #ffffff;
    --surface-2: #eef0ee;
    --border: rgba(0, 0, 0, 0.08);
    --text: #1c1d1f;
    --text-dim: #6b6d72;
    --accent: #2f8d76;
    --accent-text: #ffffff;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  position: relative;
}

/* ---------- Topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar-title {
  font-size: 0.92rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.topbar-spacer {
  flex: 1;
}

/* ---------- Drawer ---------- */

.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.drawer-overlay.visible { opacity: 1; }

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 260px;
  max-width: 82vw;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding: 10px;
  gap: 2px;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
}

.drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 14px;
}

.drawer-title {
  font-size: 0.95rem;
  font-weight: 600;
}

.drawer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--text-dim);
  background: transparent;
  border: none;
  border-radius: 12px;
  padding: 11px 12px;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}

.drawer-item svg { flex-shrink: 0; opacity: 0.8; }

.drawer-item:hover { background: var(--surface-2); color: var(--text); }

.drawer-item.active {
  background: var(--surface-2);
  color: var(--text);
}

/* ---------- Views ---------- */

.view {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  min-width: 0;
}

.view.active { display: flex; }

.view-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
  padding: 20px 16px 40px;
}

/* ---------- Thread ---------- */

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
  max-width: 280px;
  padding: 32px;
}

.empty-state svg {
  color: var(--text-dim);
  opacity: 0.6;
  margin-bottom: 14px;
}

.empty-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 6px;
}

.empty-sub {
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0;
}

/* ---------- Messages ---------- */

.msg-row {
  display: flex;
  animation: rise 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

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

.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.bubble {
  max-width: 78%;
  border-radius: var(--radius-lg);
  padding: 12px 14px;
}

.bubble.user {
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 6px;
}

.bubble.assistant {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
  min-width: 180px;
}

.bubble-thumb {
  display: block;
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: 12px;
  margin-bottom: 8px;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.bubble-text {
  font-size: 0.9rem;
  line-height: 1.45;
  white-space: pre-wrap;
  margin: 0;
}

.bubble-meta {
  font-size: 0.7rem;
  opacity: 0.65;
  margin-top: 6px;
}

/* ---------- Typing indicator ---------- */

.typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 2px;
}

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: bounce 1.1s infinite ease-in-out;
}

.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ---------- Result image ---------- */

.result-img {
  display: block;
  width: 100%;
  border-radius: var(--radius-md);
  margin-bottom: 8px;
  background: var(--surface-2);
}

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

.download-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 12px;
  cursor: pointer;
  text-decoration: none;
  transition: transform 0.15s cubic-bezier(0.16, 1, 0.3, 1), background 0.15s;
}

.download-btn:hover { background: var(--surface); }
.download-btn:active { transform: scale(0.96); }

/* ---------- Error bubble ---------- */

.bubble-error {
  font-size: 0.85rem;
  color: var(--danger);
  margin: 0;
  line-height: 1.5;
}

/* ---------- Composer ---------- */

.composer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
}

.composer-inner {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ratio-picker {
  display: flex;
  gap: 6px;
  align-self: flex-start;
}

.ratio-btn {
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 5px 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.ratio-btn:hover {
  color: var(--text);
}

.ratio-btn.active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}

.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 8px 6px 6px;
  max-width: 260px;
}

.attach-chip img {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 8px;
}

.attach-name {
  font-size: 0.78rem;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.attach-remove {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: none;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-dim);
  cursor: pointer;
  flex-shrink: 0;
}

.attach-remove:hover { color: var(--text); }

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 6px 6px 6px 10px;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.15s cubic-bezier(0.16, 1, 0.3, 1);
}

.icon-btn:hover { color: var(--text); background: var(--surface-2); }
.icon-btn:active { transform: scale(0.94); }

.prompt-input {
  flex: 1;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  max-height: 120px;
  padding: 8px 0;
  line-height: 1.4;
}

.prompt-input::placeholder { color: var(--text-dim); }

.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  transition: transform 0.15s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.15s;
}

.send-btn:not(:disabled):hover { transform: translateY(-1px); }
.send-btn:not(:disabled):active { transform: scale(0.94); }

.send-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  background: var(--surface-2);
  color: var(--text-dim);
}

/* ---------- Scrollbar ---------- */

.thread::-webkit-scrollbar { width: 6px; }
.thread::-webkit-scrollbar-thumb {
  background: var(--surface-2);
  border-radius: 3px;
}

@media (max-width: 480px) {
  .bubble { max-width: 86%; }
}

/* ---------- History ---------- */

.history-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

@media (min-width: 560px) {
  .history-grid { grid-template-columns: repeat(3, 1fr); }
}

.history-item {
  position: relative;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--surface-2);
  aspect-ratio: 1 / 1;
  cursor: pointer;
  border: 1px solid var(--border);
}

.history-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.2s ease;
}

.history-item:hover img { transform: scale(1.04); }

.history-item .history-date {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 6px 8px;
  font-size: 0.68rem;
  color: #fff;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.65), transparent);
}

/* History preview modal */

.preview-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.preview-overlay.visible { opacity: 1; }

.preview-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 14px;
  max-width: 440px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
}

.preview-card img {
  width: 100%;
  border-radius: var(--radius-md);
  display: block;
  margin-bottom: 12px;
  background: var(--surface-2);
}

.preview-prompt {
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--text-dim);
  margin: 0 0 12px;
  white-space: pre-wrap;
}

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

/* ---------- API doc ---------- */

.api-doc {
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  font-size: 0.9rem;
  line-height: 1.6;
  min-width: 0;
}

.api-doc h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0 0 6px;
}

.api-lead {
  color: var(--text-dim);
  margin: 0 0 28px;
}

.api-doc h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 28px 0 10px;
}

.api-doc p {
  color: var(--text-dim);
  margin: 0 0 10px;
}

.api-doc code {
  font-family: 'SF Mono', 'JetBrains Mono', monospace;
  font-size: 0.82em;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 5px;
  color: var(--text);
}

.code-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 0 0 4px;
}

.code-block code {
  background: none;
  padding: 0;
  font-family: 'SF Mono', 'JetBrains Mono', monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  white-space: pre;
  color: var(--text);
}

.params-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin-bottom: 4px;
}

.params-table th {
  text-align: left;
  color: var(--text-dim);
  font-weight: 500;
  padding: 6px 10px 6px 0;
  border-bottom: 1px solid var(--border);
}

.params-table td {
  padding: 8px 10px 8px 0;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.api-notes {
  color: var(--text-dim);
  padding-left: 18px;
  margin: 0;
}

.api-notes li { margin-bottom: 6px; }
