:root {
  --bg: #fafaf7;
  --surface: #ffffff;
  --text: #1a1a1a;
  --muted: #6b6b6b;
  --accent: #1f7a4a;
  --accent-soft: #e8f3ec;
  --user-bubble: #1f7a4a;
  --user-text: #ffffff;
  --assistant-bubble: #ffffff;
  --border: #e3e3e0;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 1px 4px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --surface: #242424;
    --text: #f0f0ee;
    --muted: #a0a0a0;
    --accent: #4ea677;
    --accent-soft: #1f3a2c;
    --user-bubble: #4ea677;
    --user-text: #0d1f15;
    --assistant-bubble: #2a2a2a;
    --border: #3a3a3a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 1px 4px rgba(0, 0, 0, 0.2);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "SF Pro Text", system-ui, sans-serif;
  overscroll-behavior-y: contain;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.logo { font-weight: 600; font-size: 18px; }

#new-chat {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
}

main {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.welcome {
  background: var(--accent-soft);
  border-radius: 12px;
  padding: 16px;
  color: var(--text);
}

.welcome p { margin: 0 0 8px; }

.welcome ul {
  margin: 0;
  padding-left: 20px;
  color: var(--muted);
  font-size: 14px;
}

.welcome code {
  font-size: 12px;
  background: var(--surface);
  padding: 2px 4px;
  border-radius: 4px;
}

.msg {
  max-width: 90%;
  padding: 12px 16px;
  border-radius: 16px;
  box-shadow: var(--shadow);
  word-wrap: break-word;
}

.msg.user {
  background: var(--user-bubble);
  color: var(--user-text);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.msg.user img {
  max-width: 100%;
  border-radius: 8px;
  margin-bottom: 8px;
}

.msg.assistant {
  background: var(--assistant-bubble);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.msg.assistant > h1:first-child,
.msg.assistant > h2:first-child { margin-top: 0; }

.msg.assistant h1 { font-size: 22px; margin: 16px 0 8px; }
.msg.assistant h2 { font-size: 18px; margin: 16px 0 6px; color: var(--accent); }
.msg.assistant h3 { font-size: 16px; margin: 12px 0 4px; }
.msg.assistant ul, .msg.assistant ol { padding-left: 22px; margin: 4px 0 8px; }
.msg.assistant li { margin: 2px 0; }
.msg.assistant table {
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 14px;
  width: 100%;
}
.msg.assistant th, .msg.assistant td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: left;
  vertical-align: top;
}
.msg.assistant th { background: var(--accent-soft); }
.msg.assistant blockquote {
  border-left: 3px solid var(--accent);
  margin: 12px 0;
  padding: 4px 12px;
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
}
.msg.assistant code {
  font-size: 13px;
  background: var(--bg);
  padding: 2px 4px;
  border-radius: 4px;
}
.msg.assistant pre {
  background: var(--bg);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}
.msg.assistant hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

.msg.error {
  background: #fce8e8;
  color: #8a1a1a;
  align-self: flex-start;
  border: 1px solid #f5b5b5;
}

.thinking {
  align-self: flex-start;
  color: var(--muted);
  font-size: 14px;
  font-style: italic;
  padding: 8px 16px;
}
.thinking::after {
  content: '';
  display: inline-block;
  width: 12px;
  text-align: left;
  animation: dots 1.4s infinite;
}
@keyframes dots {
  0%, 20% { content: ''; }
  40% { content: '.'; }
  60% { content: '..'; }
  80%, 100% { content: '...'; }
}

#composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  user-select: none;
  flex: 0 0 auto;
}

#input {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 14px;
  font: inherit;
  background: var(--bg);
  color: var(--text);
  min-height: 40px;
  max-height: 160px;
  outline: none;
}

#input:focus { border-color: var(--accent); }

#send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  font-size: 18px;
  cursor: pointer;
  flex: 0 0 auto;
}

#send:disabled { opacity: 0.4; cursor: not-allowed; }

#image-preview {
  position: fixed;
  bottom: 76px;
  left: 12px;
  right: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow);
}
#preview-img {
  height: 56px;
  width: 56px;
  object-fit: cover;
  border-radius: 6px;
}
#clear-image {
  margin-left: auto;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--muted);
}

dialog#login {
  border: none;
  border-radius: 16px;
  padding: 24px;
  background: var(--surface);
  color: var(--text);
  max-width: 320px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}
dialog#login::backdrop { background: rgba(0, 0, 0, 0.4); }
dialog#login h2 { margin: 0 0 8px; }
dialog#login p { margin: 0 0 16px; color: var(--muted); }
dialog#login input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: var(--bg);
  color: var(--text);
  margin-bottom: 12px;
}
dialog#login button {
  width: 100%;
  padding: 10px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
