:root {
  --bg: #faf8f3;
  --bg-2: #f5f2ea;
  --surface: #ffffff;
  --surface-2: #f7f4ec;
  --surface-hover: #efebe1;
  --line: #e6e0d2;
  --line-2: #d6cebb;
  --text: #1a1814;
  --text-dim: #5d574c;
  --text-muted: #948c7c;
  --accent: #b45309;
  --accent-soft: #b4530912;
  --accent-line: #d97706;
  --user: #155e75;
  --danger: #b91c1c;
  --radius: 6px;
  --radius-sm: 3px;
  --shadow-sm: 0 1px 2px rgba(20, 14, 0, 0.04);
  --shadow: 0 4px 24px rgba(40, 30, 10, 0.08);
  --serif: 'Iowan Old Style', 'Palatino', 'Cambria', Georgia, serif;
  --sans: ui-sans-serif, -apple-system, 'SF Pro Text', system-ui, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'JetBrains Mono', Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  background-image:
    radial-gradient(1200px 600px at 90% -10%, #fcf1de 0%, transparent 60%),
    radial-gradient(800px 400px at -10% 110%, #f0e9d8 0%, transparent 50%);
}

button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
dialog { border: 0; padding: 0; background: transparent; color: inherit; }

.app {
  display: grid;
  grid-template-columns: 268px 1fr;
  height: 100vh;
  transition: grid-template-columns 0.22s ease;
}
.app.sidebar-collapsed { grid-template-columns: 0 1fr; }

/* ── sidebar ──────────────────────────────────────────────── */
.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: var(--bg-2);
  padding: 14px 12px 10px;
  overflow: hidden;
  min-width: 0;
}
.app.sidebar-collapsed .sidebar { padding: 0; border-right: 0; }

.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 6px 14px;
}
.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-family: var(--serif);
}
.brand-mark {
  font-size: 20px;
  color: var(--accent);
  transform: translateY(2px);
  font-family: var(--sans);
}
.brand-name {
  font-size: 21px;
  font-style: italic;
  letter-spacing: -0.01em;
}

.icon-only {
  width: 28px; height: 28px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}
.icon-only:hover { color: var(--text); background: var(--surface-hover); }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text);
  text-align: left;
  box-shadow: var(--shadow-sm);
  transition: background .12s ease, border-color .12s ease;
}
.new-chat-btn:hover { background: var(--surface-hover); border-color: var(--line-2); }
.new-chat-btn .plus {
  font-size: 16px;
  color: var(--accent);
  font-weight: 600;
}

.chat-list-label {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 18px 8px 6px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
  margin: 0 -2px;
}
.chat-list-empty {
  padding: 12px 10px;
  font-size: 12.5px;
  color: var(--text-muted);
  font-style: italic;
  font-family: var(--serif);
}

.chat-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .1s ease;
}
.chat-item:hover { background: var(--surface); }
.chat-item.is-active {
  background: var(--surface);
  border-color: var(--line);
}
.chat-item-title {
  flex: 1;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 6px;
}
.chat-item-delete {
  opacity: 0;
  width: 22px; height: 22px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  border-radius: 2px;
  font-size: 14px;
  flex-shrink: 0;
  transition: opacity .12s ease, color .12s ease, background .12s ease;
}
.chat-item:hover .chat-item-delete,
.chat-item.is-active .chat-item-delete { opacity: 1; }
.chat-item-delete:hover { background: var(--surface-hover); color: var(--danger); }

.sidebar-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 6px 4px;
  border-top: 1px solid var(--line);
  margin-top: 8px;
}
.model-tag {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 3px 7px;
  border: 1px solid var(--line);
  border-radius: 2px;
  background: var(--surface);
}
.ghost-btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  transition: color .12s ease, background .12s ease, border-color .12s ease;
}
.ghost-btn:hover { color: var(--danger); background: var(--surface); border-color: var(--line); }

/* ── chat surface ─────────────────────────────────────────── */
.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  min-width: 0;
}

.chat-head {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(250, 248, 243, 0.85);
  backdrop-filter: blur(6px);
}
.sidebar-show {
  font-size: 22px;
  display: none;
}
.app.sidebar-collapsed .sidebar-show { display: inline-flex; }

.chat-title {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-size: 19px;
  font-weight: 400;
  letter-spacing: -0.01em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-meta {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* ── stream ───────────────────────────────────────────────── */
.stream {
  overflow-y: auto;
  padding: 28px 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}
.stream-inner {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
}

.welcome {
  margin: auto auto 60px;
  text-align: center;
  max-width: 520px;
  padding: 40px 20px;
}
.welcome-mark {
  font-size: 42px;
  color: var(--accent);
  margin-bottom: 18px;
}
.welcome h2 {
  margin: 0 0 12px;
  font-family: var(--serif);
  font-style: italic;
  font-size: 26px;
  font-weight: 400;
  color: var(--text);
}
.welcome p {
  font-family: var(--serif);
  font-size: 15.5px;
  line-height: 1.55;
  color: var(--text-dim);
  margin: 0 auto 28px;
  max-width: 420px;
}
.welcome-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}
.suggestion {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 12.5px;
  color: var(--text-dim);
  font-family: var(--serif);
  font-style: italic;
  transition: border-color .12s ease, color .12s ease, background .12s ease;
}
.suggestion:hover {
  border-color: var(--accent-line);
  color: var(--accent);
  background: var(--accent-soft);
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}
.msg-role {
  font-family: var(--mono);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}
.msg.is-user .msg-role { color: var(--user); }
.msg.is-assistant .msg-role { color: var(--accent); }

.msg-text {
  font-family: var(--serif);
  font-size: 15.5px;
  line-height: 1.55;
  color: var(--text);
  white-space: pre-wrap;
}
.msg.is-pending .msg-text {
  color: var(--text-muted);
  font-style: italic;
}

.msg-attachments {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.msg-attachments .thumb { width: 60px; height: 60px; }

.msg-images {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 10px;
}

/* ── images / generation ──────────────────────────────────── */
.gen-frame {
  position: relative;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 1;
  cursor: zoom-in;
}
.gen-frame img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  transition: opacity .35s ease, filter .8s ease;
}
.gen-frame .placeholder-img {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--serif);
  font-style: italic;
  font-size: 14px;
  color: var(--text-muted);
}

/* shimmer base layer */
.gen-frame.is-pending .placeholder-img,
.gen-frame.is-pending::before {
  background: linear-gradient(110deg, var(--surface-2) 30%, var(--surface-hover) 50%, var(--surface-2) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.6s infinite linear;
}
@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

/* scan-line overlay during generation (visible while pending) */
.gen-frame.is-pending::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg,
      transparent 0%,
      rgba(180, 83, 9, 0.0) 35%,
      rgba(180, 83, 9, 0.18) 50%,
      rgba(180, 83, 9, 0.0) 65%,
      transparent 100%
    );
  background-size: 100% 200%;
  animation: scan 2.2s ease-in-out infinite;
  pointer-events: none;
  mix-blend-mode: multiply;
}
@keyframes scan {
  0%   { background-position: 0 -100%; opacity: 0.9; }
  50%  { background-position: 0 100%; opacity: 0.9; }
  100% { background-position: 0 -100%; opacity: 0.9; }
}

/* progressive partial: blur fading away */
.gen-frame img.is-partial {
  filter: blur(8px) saturate(0.95);
}
.gen-frame img.is-partial-2 {
  filter: blur(4px) saturate(0.98);
}
.gen-frame img.is-partial-3 {
  filter: blur(1.5px);
}
.gen-frame.is-final img { filter: none; }

/* progress dot */
.gen-progress {
  position: absolute;
  top: 10px; left: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
  box-shadow: var(--shadow-sm);
  z-index: 2;
}
.gen-progress::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse-dot 1.2s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: 0.4; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1); }
}
.gen-frame:not(.is-pending) .gen-progress { display: none; }

.gen-actions {
  position: absolute;
  bottom: 10px;
  right: 10px;
  display: flex;
  gap: 6px;
  opacity: 0;
  transition: opacity .15s ease;
  z-index: 2;
}
.gen-frame:hover .gen-actions { opacity: 1; }
.gen-frame.is-pending .gen-actions { display: none; }
.gen-action {
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 5px 9px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.gen-action:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

.thumb {
  position: relative;
  width: 64px; height: 64px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
  border: 1px solid var(--line);
  flex-shrink: 0;
}
.thumb img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.thumb-remove {
  position: absolute;
  top: 2px; right: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 0;
  background: rgba(26, 24, 20, 0.85);
  color: #fff;
  font-size: 11px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.thumb-remove:hover { background: var(--danger); }

/* ── composer ─────────────────────────────────────────────── */
.composer {
  border-top: 1px solid var(--line);
  background: rgba(250, 248, 243, 0.92);
  backdrop-filter: blur(6px);
  padding: 14px 24px 22px;
}
.composer { position: relative; }
.composer-row {
  max-width: 760px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 10px 10px 10px 12px;
  box-shadow: var(--shadow-sm);
  transition: border-color .12s ease, box-shadow .12s ease;
}
.composer-row:focus-within {
  border-color: var(--accent-line);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  min-width: 32px;
  padding: 0 8px;
  border-radius: 999px;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: background .12s ease, color .12s ease, border-color .12s ease;
  gap: 6px;
}
.composer-icon-btn:hover { color: var(--text); background: var(--surface-hover); }

#prompt {
  border: 0;
  outline: none;
  background: transparent;
  resize: none;
  padding: 6px 4px;
  max-height: 240px;
  min-height: 22px;
  font-family: var(--serif);
  font-size: 15.5px;
  line-height: 1.45;
  color: var(--text);
  align-self: center;
}
#prompt::placeholder {
  color: var(--text-muted);
  font-style: italic;
}

.composer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.settings-btn {
  border: 1px solid var(--line);
  padding: 0 12px 0 10px;
}
.settings-btn:hover { border-color: var(--line-2); }
.settings-summary {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.settings-btn.is-customized {
  border-color: var(--accent-line);
  color: var(--accent);
}
.settings-btn.is-customized .settings-summary { color: var(--accent); }

/* ── settings popover ─────────────────────────────────────────── */
.settings-pop {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 24px;
  width: 360px;
  max-width: calc(100vw - 28px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: 14px;
  animation: pop-in .14s ease-out;
}
.settings-pop[hidden] { display: none; }
@keyframes pop-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.settings-section { display: flex; flex-direction: column; gap: 8px; }
.settings-label {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.settings-hint {
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--text-muted);
  letter-spacing: 0.04em;
  height: 12px;
}

/* aspect ratio grid */
.aspect-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.aspect-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 4px 6px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color .12s ease, background .12s ease, color .12s ease;
  min-height: 56px;
}
.aspect-btn:hover { border-color: var(--line-2); background: var(--surface-2); color: var(--text); }
.aspect-btn.is-active {
  border-color: var(--accent-line);
  background: var(--accent-soft);
  color: var(--accent);
}
.aspect-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 28px;
  width: 100%;
}
.aspect-rect {
  display: block;
  width: calc(var(--w) * 1px);
  height: calc(var(--h) * 1px);
  background: var(--text-dim);
  border-radius: 2px;
  transition: background .12s ease;
}
.aspect-btn:hover .aspect-rect { background: var(--text); }
.aspect-btn.is-active .aspect-rect { background: var(--accent); }
.aspect-auto {
  font-size: 16px;
  color: var(--text-dim);
  line-height: 1;
}
.aspect-btn:hover .aspect-auto { color: var(--text); }
.aspect-btn.is-active .aspect-auto { color: var(--accent); }
.aspect-label { font-size: 9.5px; }

/* pill rows */
.pill-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.pill {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: border-color .12s ease, background .12s ease, color .12s ease;
}
.pill:hover { border-color: var(--line-2); color: var(--text); background: var(--surface-2); }
.pill.is-active {
  border-color: var(--accent-line);
  background: var(--accent-soft);
  color: var(--accent);
}

.send-btn {
  width: 32px; height: 32px;
  border: 0;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .12s ease, transform .08s ease;
}
.send-btn:hover { background: var(--accent); }
.send-btn:active { transform: translateY(1px); }
.send-btn:disabled {
  background: var(--line-2);
  cursor: not-allowed;
  color: var(--text-muted);
}
.send-btn.is-busy svg { display: none; }
.send-btn.is-busy::after {
  content: '';
  width: 12px; height: 12px;
  border: 1.5px solid var(--bg);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.attachments {
  max-width: 760px;
  margin: 0 auto 8px;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ── toast ────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--serif);
  box-shadow: var(--shadow);
  max-width: 480px;
  z-index: 100;
}

/* ── lightbox ─────────────────────────────────────────────── */
.lightbox::backdrop { background: rgba(20, 14, 0, 0.7); }
.lightbox {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: var(--radius);
  overflow: hidden;
}
.lightbox img {
  display: block;
  max-width: 92vw;
  max-height: 92vh;
}
.lightbox-close {
  position: fixed;
  top: 18px; right: 18px;
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, 0.95);
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  box-shadow: var(--shadow);
}

/* ── responsive ───────────────────────────────────────────── */
@media (max-width: 760px) {
  .app { grid-template-columns: 0 1fr; }
  .app:not(.sidebar-collapsed) {
    grid-template-columns: 1fr 0;
  }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 280px;
    z-index: 50;
    box-shadow: var(--shadow);
  }
  .app.sidebar-collapsed .sidebar { display: none; }
  .composer-options { display: none; }
  .composer-row { grid-template-columns: auto 1fr auto; }
  .stream { padding: 16px 14px; }
  .composer { padding: 10px 14px 14px; }
}
