:root {
  --brand-orange: #ff8724;
  --brand-red: #ec0000;
  --brand-red-dark: #c40000;
  --bg: #f7f7f8;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #6b6b6b;
  --border: #e5e5e5;
  --bubble-bot: #ffffff;
  --bubble-user: #ff8724;
  --bubble-user-text: #ffffff;
  --radius: 18px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f10;
    --surface: #1c1c1e;
    --text: #f5f5f5;
    --text-muted: #9a9a9a;
    --border: #2a2a2c;
    --bubble-bot: #1c1c1e;
    --bubble-user: #ff8724;
    --bubble-user-text: #1a1a1a;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }

  /* Dark red text is unreadable on the dark background; orange passes there. */
  .citation-chip {
    color: var(--brand-orange);
  }

  .source-body a {
    color: var(--brand-orange);
  }
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: "Noto Sans Thai", "Sarabun", -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg);
}

.header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--brand-orange);
  color: #fff;
  flex-shrink: 0;
}

.logo {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: #fff;
  padding: 2px;
}

.header-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.header-text h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.subtitle {
  font-size: 12px;
  opacity: 0.9;
}

.lang-toggle {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.4);
  color: #fff;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.lang-toggle:hover {
  background: rgba(255, 255, 255, 0.3);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  display: flex;
  flex-direction: column;
  max-width: 85%;
  animation: fadein 0.2s ease;
}

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

.msg.user {
  align-self: flex-end;
}

.msg.bot {
  align-self: flex-start;
}

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.bot .bubble {
  background: var(--bubble-bot);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
}

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

.citations {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.citation-chip {
  font: inherit;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 10px;
  background: rgba(255, 135, 36, 0.12);
  color: var(--brand-red-dark);
  border: 1px solid rgba(255, 135, 36, 0.3);
}

button.citation-chip {
  cursor: pointer;
  text-align: left;
}

button.citation-chip:hover,
button.citation-chip[aria-expanded="true"] {
  border-color: var(--brand-orange);
  background: rgba(255, 135, 36, 0.22);
}

button.citation-chip:focus-visible {
  outline: 2px solid var(--brand-orange);
  outline-offset: 1px;
}

.source-panel {
  margin-top: 6px;
  padding: 10px 12px;
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid rgba(255, 135, 36, 0.4);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  line-height: 1.55;
}

.source-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.source-close {
  flex-shrink: 0;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
}

.source-body p {
  margin: 6px 0;
}

.source-body .source-h {
  font-weight: 600;
  margin-top: 10px;
}

.source-body ol,
.source-body ul {
  margin: 4px 0;
  padding-left: 20px;
}

.source-body a {
  color: var(--brand-red);
  word-break: break-all;
}

.feedback {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.feedback button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
}

.feedback button:hover {
  border-color: var(--brand-orange);
  color: var(--brand-orange);
}

.feedback button.selected {
  border-color: var(--brand-orange);
  color: var(--brand-orange);
  background: rgba(255, 135, 36, 0.12);
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 16px 8px;
  flex-shrink: 0;
}

.suggestions:empty {
  display: none;
}

.suggestion-chip {
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
}

.suggestion-chip:hover {
  border-color: var(--brand-orange);
  color: var(--brand-orange);
}

.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  align-items: flex-end;
}

.input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px 14px;
  font-size: 15px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  max-height: 120px;
  line-height: 1.4;
}

.input:focus {
  outline: none;
  border-color: var(--brand-orange);
}

.send {
  background: var(--brand-red);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.send:hover {
  background: var(--brand-red-dark);
}

.send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.disclaimer {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  padding: 0 16px 10px;
  margin: 0;
  flex-shrink: 0;
}

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
}

.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: blink 1.2s infinite;
}

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

@keyframes blink {
  0%, 60%, 100% { opacity: 0.3; }
  30% { opacity: 1; }
}
