:root {
  --purple-900: #241857;
  --purple-700: #3D2A7D;
  --purple-600: #4A2E8C;
  --purple-100: #EFEAFB;
  --gold-500: #F0A93B;
  --gold-600: #D98F1E;
  --gold-100: #FDF1DC;
  --coral-500: #FF7A50;
  --green-600: #2FA36B;
  --cream-50: #FBF8F2;
  --ink-900: #221B3A;
  --ink-600: #5B5470;
  --ink-400: #8B84A0;
  --line: #E7E1F3;
  --white: #FFFFFF;
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shadow-card: 0 2px 10px rgba(36, 24, 87, 0.08);
  --shadow-pop: 0 12px 32px rgba(36, 24, 87, 0.18);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--cream-50);
  color: var(--ink-900);
  font-family: 'Sarabun', sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-family: 'Noto Serif Thai', serif;
  font-weight: 600;
  margin: 0;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--cream-50);
  position: relative;
}

/* ---------- Header ---------- */
.app-header {
  background: linear-gradient(135deg, var(--purple-700) 0%, var(--purple-900) 65%);
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}
.app-header::after {
  content: "";
  position: absolute;
  top: -60%;
  right: -10%;
  width: 220px;
  height: 220px;
  background: radial-gradient(circle, rgba(240,169,59,0.35) 0%, rgba(240,169,59,0) 70%);
  pointer-events: none;
}
.header-inner {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
}
.header-icon {
  border-radius: 10px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}
.header-text { flex: 1; min-width: 0; }
.header-text h1 {
  color: var(--white);
  font-size: 17px;
  line-height: 1.3;
}
.header-text p {
  color: var(--gold-100);
  font-size: 12.5px;
  margin: 2px 0 0;
  opacity: 0.85;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  background: rgba(255,255,255,0.12);
  border: none;
  color: var(--white);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: rgba(255,255,255,0.22); }

/* ---------- Chat area ---------- */
.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 12px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  text-align: center;
  max-width: 480px;
  margin: 8px auto 0;
  padding: 8px 8px 4px;
}
.empty-icon {
  border-radius: 20px;
  margin-bottom: 14px;
  box-shadow: var(--shadow-card);
}
.empty-state h2 {
  font-size: 19px;
  color: var(--purple-900);
  margin-bottom: 8px;
}
.empty-state p {
  font-size: 14px;
  color: var(--ink-600);
  line-height: 1.7;
  margin: 0 0 18px;
}
.suggested-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.catalog-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px dashed var(--purple-600);
  color: var(--purple-700);
  font-family: 'Sarabun', sans-serif;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
  margin-bottom: 16px;
  transition: all 0.15s ease;
}
.catalog-link:hover { background: var(--purple-100); }
.chip {
  font-family: 'Sarabun', sans-serif;
  font-size: 13.5px;
  color: var(--purple-700);
  background: var(--purple-100);
  border: 1px solid transparent;
  padding: 9px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: left;
}
.chip:hover { background: var(--white); border-color: var(--purple-600); }

/* ---------- Thread & bubbles ---------- */
.thread {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 620px;
  margin: 0 auto;
}

.msg-user {
  align-self: flex-end;
  max-width: 82%;
  background: var(--gold-100);
  color: var(--ink-900);
  padding: 11px 15px;
  border-radius: 16px 16px 4px 16px;
  font-size: 15px;
  line-height: 1.6;
}

.msg-ai {
  align-self: flex-start;
  max-width: 92%;
  background: var(--white);
  border: 1px solid var(--line);
  border-left: 3px solid var(--purple-600);
  border-radius: 4px 16px 16px 16px;
  padding: 14px 16px;
  box-shadow: var(--shadow-card);
}
.msg-ai .answer-text {
  font-size: 15px;
  line-height: 1.75;
  color: var(--ink-900);
  white-space: pre-wrap;
}
.msg-ai.not-found .answer-text { color: var(--ink-600); font-style: italic; }

.msg-ai .thinking {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink-400);
  font-size: 14px;
}
.dot-flash { display:inline-flex; gap:3px; }
.dot-flash span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--purple-600);
  animation: dotflash 1.1s infinite ease-in-out;
}
.dot-flash span:nth-child(2) { animation-delay: 0.15s; }
.dot-flash span:nth-child(3) { animation-delay: 0.3s; }
@keyframes dotflash {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}

.citations {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.cite-chip {
  font-size: 11.5px;
  background: var(--purple-100);
  color: var(--purple-700);
  padding: 4px 10px;
  border-radius: 999px;
  line-height: 1.5;
}
.cite-chip.ocr-flag {
  background: var(--gold-100);
  color: var(--gold-600);
}

.source-toggle {
  margin-top: 10px;
  background: none;
  border: none;
  color: var(--purple-600);
  font-family: 'Sarabun', sans-serif;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 0;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.source-toggle:hover { color: var(--purple-900); }
.source-toggle svg { transition: transform 0.15s ease; }
.source-toggle.open svg { transform: rotate(180deg); }

.source-quote {
  margin-top: 8px;
  background: var(--cream-50);
  border-left: 2px solid var(--gold-500);
  border-radius: 0 8px 8px 0;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--ink-600);
  line-height: 1.65;
  white-space: pre-wrap;
  display: none;
}
.source-quote.visible { display: block; }
.source-quote .quote-meta {
  display: block;
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--ink-400);
}

.msg-error {
  align-self: center;
  font-size: 13.5px;
  color: #A32D2D;
  background: #FCEBEB;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
}

/* ---------- Composer ---------- */
.composer {
  flex-shrink: 0;
  padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--cream-50) 60%, rgba(251,248,242,0));
}
.composer-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 6px 6px 6px 16px;
  box-shadow: var(--shadow-card);
  max-width: 620px;
  margin: 0 auto;
}
.composer-form textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-family: 'Sarabun', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  max-height: 120px;
  padding: 8px 0;
  background: transparent;
  color: var(--ink-900);
}
.composer-form textarea::placeholder { color: var(--ink-400); }
.send-btn {
  flex-shrink: 0;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--gold-500);
  color: var(--purple-900);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
}
.send-btn:hover { background: var(--gold-600); }
.send-btn:active { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.5; cursor: default; }

.disclaimer {
  max-width: 620px;
  margin: 8px auto 0;
  font-size: 11px;
  color: var(--ink-400);
  text-align: center;
  line-height: 1.5;
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(36,24,87,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.modal-overlay[hidden] { display: none; }
.modal {
  background: var(--white);
  border-radius: var(--radius-lg);
  padding: 24px;
  max-width: 420px;
  width: 100%;
  box-shadow: var(--shadow-pop);
}
.modal h2 { font-size: 18px; color: var(--purple-900); margin-bottom: 10px; }
.modal p { font-size: 14px; color: var(--ink-600); line-height: 1.7; }
.modal ul { margin: 8px 0 14px; padding-left: 20px; font-size: 14px; color: var(--ink-900); line-height: 1.9; }
.modal-note { font-size: 12.5px; color: var(--ink-400); }
.modal-link {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--purple-700);
  font-family: 'Sarabun', sans-serif;
  font-size: 13.5px;
  font-weight: 600;
  padding: 8px 0 4px;
  cursor: pointer;
}
.modal-link:hover { color: var(--purple-900); }
.modal-close {
  margin-top: 6px;
  width: 100%;
  background: var(--purple-700);
  color: var(--white);
  border: none;
  border-radius: var(--radius-sm);
  padding: 11px;
  font-family: 'Sarabun', sans-serif;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
}
.modal-close:hover { background: var(--purple-900); }

/* ---------- Catalog modal ---------- */
.modal-catalog {
  max-width: 560px;
  max-height: 82vh;
  display: flex;
  flex-direction: column;
}
.modal-subtext {
  font-size: 13px;
  color: var(--ink-600);
  margin: 0 0 12px;
}
.catalog-search {
  width: 100%;
  font-family: 'Sarabun', sans-serif;
  font-size: 14px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  outline: none;
  color: var(--ink-900);
  flex-shrink: 0;
}
.catalog-search:focus { border-color: var(--purple-600); }
.catalog-list {
  overflow-y: auto;
  flex: 1;
  margin: 0 -8px;
  padding: 0 8px;
}
.catalog-group-title {
  font-family: 'Sarabun', sans-serif;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--purple-700);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 16px 0 6px;
}
.catalog-group-title:first-child { margin-top: 0; }
.catalog-row {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--line);
}
.catalog-item {
  display: block;
  flex: 1 1 auto;
  min-width: 0;
  text-align: left;
  background: none;
  border: none;
  padding: 10px 4px;
  cursor: pointer;
  font-family: 'Sarabun', sans-serif;
  color: var(--ink-900);
  transition: background 0.12s ease;
}
.catalog-item:hover { background: var(--purple-100); }
.catalog-fulltext-btn {
  flex-shrink: 0;
  width: 38px;
  border: none;
  background: none;
  color: var(--purple-600);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s ease, color 0.12s ease;
}
.catalog-fulltext-btn:hover { background: var(--gold-100); color: var(--gold-600); }
.catalog-item-title { font-size: 13.5px; line-height: 1.5; overflow-wrap: break-word; }
.catalog-item-meta {
  display: block;
  font-size: 11.5px;
  color: var(--ink-400);
  margin-top: 3px;
}
.catalog-item-meta .ocr-note { color: var(--gold-600); }
.catalog-loading, .catalog-empty {
  text-align: center;
  color: var(--ink-400);
  font-size: 13px;
  padding: 24px 0;
}

/* ---------- Full-text modal ---------- */
.modal-fulltext {
  max-width: 640px;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
}
.modal-fulltext #fullTextMeta {
  font-size: 12.5px;
  color: var(--purple-700);
  font-weight: 600;
  margin: -6px 0 10px;
}
.modal-fulltext #fullTextMeta .ocr-note { color: var(--gold-600); font-weight: 500; }
.fulltext-body {
  overflow-y: auto;
  flex: 1;
  background: var(--cream-50);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 16px;
  font-family: 'Sarabun', sans-serif;
  font-size: 13.5px;
  line-height: 1.85;
  color: var(--ink-900);
  white-space: pre-wrap;
  margin-bottom: 14px;
}

/* ---------- Responsive ---------- */
@media (min-width: 761px) {
  .app-shell {
    margin-top: 24px;
    margin-bottom: 24px;
    height: calc(100vh - 48px);
    border-radius: 22px;
    overflow: hidden;
    box-shadow: var(--shadow-pop);
  }
}

@media (prefers-reduced-motion: reduce) {
  .dot-flash span { animation: none; }
}
