:root {
  --bg: #ffffff;
  --panel: #f7f7f8;
  --panel-2: #ececf1;
  --text: #202123;
  --muted: #6b6c70;
  --border: #e4e4e7;
  --accent: #111827;
  --accent-text: #ffffff;
  --user: #f1f1f3;
  --danger: #c2413b;
  --shadow: 0 18px 50px rgba(0,0,0,.12);
  --sidebar-width: 272px;
}

:root[data-theme="dark"] {
  --bg: #212121;
  --panel: #171717;
  --panel-2: #2f2f2f;
  --text: #ececec;
  --muted: #a1a1aa;
  --border: #3d3d3d;
  --accent: #f4f4f5;
  --accent-text: #18181b;
  --user: #2f2f2f;
  --danger: #f87171;
  --shadow: 0 20px 60px rgba(0,0,0,.45);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif; }
button, input, textarea, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.app-page { height: 100vh; overflow: hidden; }
.app-shell { display: flex; width: 100%; height: 100vh; background: var(--bg); }
.sidebar { width: var(--sidebar-width); min-width: var(--sidebar-width); background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 12px; z-index: 20; }
.sidebar-top { display: flex; gap: 8px; align-items: center; }
.new-chat-btn { flex: 1; border: 1px solid var(--border); background: var(--bg); border-radius: 12px; padding: 11px 13px; text-align: left; cursor: pointer; font-weight: 600; }
.new-chat-btn:hover, .sidebar-action:hover, .conversation-item:hover { background: var(--panel-2); }
.conversation-list { flex: 1; overflow-y: auto; padding: 14px 0; }
.empty-list { color: var(--muted); font-size: 13px; text-align: center; padding: 30px 8px; }
.conversation-item { display: flex; align-items: center; border-radius: 10px; margin-bottom: 3px; position: relative; }
.conversation-item.active { background: var(--panel-2); }
.conversation-open { flex: 1; min-width: 0; display: flex; align-items: center; gap: 9px; border: 0; background: transparent; padding: 10px; cursor: pointer; text-align: left; }
.conversation-icon { color: var(--muted); }
.conversation-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conversation-delete { opacity: 0; border: 0; background: transparent; cursor: pointer; padding: 7px 10px; color: var(--muted); font-size: 18px; }
.conversation-item:hover .conversation-delete { opacity: 1; }
.conversation-delete:hover { color: var(--danger); }
.sidebar-bottom { border-top: 1px solid var(--border); padding-top: 9px; display: grid; gap: 3px; }
.sidebar-action { border: 0; background: transparent; border-radius: 10px; text-decoration: none; padding: 10px; text-align: left; cursor: pointer; display: block; font-size: 14px; }
.sidebar-overlay { display: none; }

.main-panel { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
.topbar { height: 58px; min-height: 58px; border-bottom: 1px solid transparent; display: flex; align-items: center; justify-content: center; padding: 8px 18px; position: relative; }
.mode-switch { display: flex; gap: 4px; background: var(--panel); padding: 4px; border-radius: 12px; }
.mode-btn { border: 0; background: transparent; padding: 7px 14px; border-radius: 9px; cursor: pointer; color: var(--muted); font-weight: 600; font-size: 14px; }
.mode-btn.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.model-pill { position: absolute; right: 20px; color: var(--muted); font-size: 12px; border: 1px solid var(--border); padding: 5px 9px; border-radius: 999px; }
.icon-btn { width: 38px; height: 38px; border: 0; background: transparent; border-radius: 10px; cursor: pointer; font-size: 22px; }
.icon-btn:hover { background: var(--panel-2); }
.mobile-only { display: none; }

.message-area { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.welcome { width: min(760px, calc(100% - 32px)); margin: 0 auto; min-height: calc(100vh - 225px); display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 50px 0; }
.welcome-logo, .brand-mark { width: 48px; height: 48px; border-radius: 16px; display: grid; place-items: center; background: var(--accent); color: var(--accent-text); font-weight: 800; letter-spacing: -.5px; }
.welcome h1 { font-size: clamp(28px, 4vw, 42px); margin: 18px 0 8px; letter-spacing: -1.3px; }
.welcome p { margin: 0; color: var(--muted); }
.suggestion-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; width: min(650px, 100%); margin-top: 32px; }
.suggestion { min-height: 58px; border: 1px solid var(--border); border-radius: 14px; background: var(--bg); text-align: left; padding: 13px 15px; cursor: pointer; }
.suggestion:hover { background: var(--panel); }
.messages { width: 100%; padding: 16px 0 40px; }
.message-row { width: min(820px, calc(100% - 34px)); margin: 0 auto; display: grid; grid-template-columns: 32px minmax(0, 1fr); gap: 14px; padding: 19px 0; }
.avatar { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; font-weight: 700; font-size: 12px; background: var(--accent); color: var(--accent-text); }
.message-row.user .avatar { background: var(--panel-2); color: var(--text); }
.message-content { min-width: 0; padding-top: 3px; line-height: 1.72; font-size: 15px; }
.message-row.user .message-content { display: flex; justify-content: flex-end; }
.message-row.user .message-body { background: var(--user); border-radius: 18px 18px 5px 18px; padding: 10px 14px; max-width: min(680px, 88%); }
.message-body h1, .message-body h2, .message-body h3 { line-height: 1.3; margin: 18px 0 8px; }
.message-body h1 { font-size: 22px; }
.message-body h2 { font-size: 19px; }
.message-body h3 { font-size: 17px; }
.message-body blockquote { border-left: 3px solid var(--border); margin: 10px 0; padding: 4px 12px; color: var(--muted); }
.md-list { padding-left: 8px; }
.inline-code { background: var(--panel-2); padding: 2px 5px; border-radius: 5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }
pre { background: #111827; color: #e5e7eb; border-radius: 12px; overflow: auto; margin: 14px 0; }
pre code { display: block; padding: 14px 16px 16px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; white-space: pre; }
.code-head { height: 38px; display: flex; align-items: center; justify-content: space-between; padding: 0 12px; background: rgba(255,255,255,.07); color: #aab1bd; font-size: 12px; }
.copy-code { border: 0; background: transparent; color: #d1d5db; cursor: pointer; }
.message-actions { margin-top: 8px; opacity: 0; transition: opacity .15s ease; }
.message-row:hover .message-actions { opacity: 1; }
.message-actions button { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 3px 0; font-size: 12px; }
.typing { display: inline-flex; gap: 4px; padding-top: 8px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 70%, 100% { transform: translateY(0); opacity: .4; } 35% { transform: translateY(-5px); opacity: 1; } }
.message-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); padding: 10px 12px; border-radius: 10px; }
.generated-image-card { max-width: 690px; border: 1px solid var(--border); border-radius: 16px; overflow: hidden; background: var(--panel); }
.generated-image-card img { display: block; width: 100%; height: auto; background: var(--panel-2); }
.image-tools { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 13px; color: var(--muted); font-size: 12px; }
.image-tools a { color: var(--text); text-decoration: none; font-weight: 600; }
.image-loading { width: min(520px, 100%); height: 230px; border: 1px solid var(--border); border-radius: 16px; display: flex; flex-direction: column; align-items: center; justify-content: center; overflow: hidden; position: relative; gap: 6px; }
.image-loading span { font-size: 12px; color: var(--muted); }
.image-shimmer { position: absolute; inset: 0; background: linear-gradient(110deg, var(--panel) 30%, var(--panel-2) 45%, var(--panel) 60%); background-size: 220% 100%; animation: shimmer 1.5s infinite linear; opacity: .7; }
.image-loading strong, .image-loading span { z-index: 1; }
@keyframes shimmer { to { background-position-x: -220%; } }

.composer-wrap { flex: 0 0 auto; width: min(820px, calc(100% - 34px)); margin: 0 auto; padding: 8px 0 14px; background: linear-gradient(180deg, transparent 0, var(--bg) 24%); }
.image-options { display: flex; gap: 8px; padding: 0 4px 8px; }
.image-options select { border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 10px; padding: 7px 10px; font-size: 12px; }
.composer { border: 1px solid var(--border); background: var(--bg); border-radius: 24px; padding: 10px 10px 10px 16px; display: flex; align-items: flex-end; gap: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.07); }
.composer textarea { flex: 1; resize: none; border: 0; outline: 0; background: transparent; color: var(--text); line-height: 1.5; min-height: 27px; max-height: 180px; padding: 3px 0; }
.composer textarea::placeholder { color: var(--muted); }
.send-btn { width: 34px; height: 34px; flex: 0 0 34px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text); cursor: pointer; font-size: 20px; font-weight: 700; }
.send-btn:disabled { opacity: .45; cursor: not-allowed; }
.composer-note { text-align: center; color: var(--muted); font-size: 11px; padding-top: 7px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 100; display: grid; place-items: center; padding: 18px; }
.modal-card { width: min(620px, 100%); max-height: 90vh; overflow-y: auto; background: var(--bg); border: 1px solid var(--border); border-radius: 18px; box-shadow: var(--shadow); padding: 20px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; }
.modal form, .auth-card form { display: grid; gap: 14px; margin-top: 18px; }
label { display: grid; gap: 7px; font-size: 13px; font-weight: 600; }
input, textarea, select { border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 10px; padding: 10px 11px; outline: none; }
input:focus, textarea:focus, select:focus { border-color: color-mix(in srgb, var(--text) 50%, var(--border)); box-shadow: 0 0 0 3px color-mix(in srgb, var(--text) 8%, transparent); }
.settings-actions { display: flex; justify-content: flex-end; gap: 9px; }
.primary-btn, .secondary-btn { border-radius: 10px; padding: 10px 14px; cursor: pointer; border: 1px solid var(--border); font-weight: 650; }
.primary-btn { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.secondary-btn { background: var(--bg); color: var(--text); }
.full { width: 100%; }
.status-text { min-height: 20px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.success { color: #16803a; }
.error-text { color: var(--danger); }
.toast { position: fixed; left: 50%; bottom: 94px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; z-index: 200; box-shadow: var(--shadow); font-size: 13px; }
.toast.error { background: #991b1b; color: #fff; }

.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 22px; background: var(--panel); }
.auth-card { width: min(420px, 100%); background: var(--bg); border: 1px solid var(--border); border-radius: 20px; padding: 28px; box-shadow: var(--shadow); }
.auth-card .brand-mark { margin: 0 auto 18px; }
.auth-card h1 { text-align: center; margin: 0 0 7px; font-size: 25px; }
.auth-card > p { text-align: center; margin: 0; }
.setup-card { width: min(500px, 100%); }
.alert { white-space: pre-line; border-radius: 10px; padding: 10px 12px; margin-top: 15px; font-size: 13px; }
.alert.error { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }
.requirements { color: var(--muted); font-size: 12px; text-align: center; margin-top: 18px; }

@media (max-width: 820px) {
  .mobile-only { display: inline-grid; place-items: center; }
  .sidebar { position: fixed; inset: 0 auto 0 0; transform: translateX(-105%); transition: transform .2s ease; box-shadow: var(--shadow); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.38); z-index: 15; }
  .sidebar-overlay.show { display: block; }
  .topbar { justify-content: center; }
  .topbar > .mobile-only { position: absolute; left: 10px; }
  .model-pill { display: none; }
  .welcome { min-height: calc(100vh - 215px); }
}

@media (max-width: 560px) {
  .suggestion-grid { grid-template-columns: 1fr; }
  .suggestion { min-height: 50px; }
  .message-row { width: calc(100% - 22px); grid-template-columns: 28px minmax(0, 1fr); gap: 10px; padding: 15px 0; }
  .avatar { width: 27px; height: 27px; border-radius: 8px; }
  .composer-wrap { width: calc(100% - 18px); padding-bottom: max(9px, env(safe-area-inset-bottom)); }
  .composer { border-radius: 20px; }
  .composer-note { display: none; }
  .image-options { overflow-x: auto; }
  .image-options select { min-width: max-content; }
  .settings-actions { flex-direction: column-reverse; }
  .settings-actions button { width: 100%; }
  .auth-card { padding: 22px; }
}
