/* ============================================================
   Tipitaka Advisor — Design System
   แนวคิด: คัมภีร์ใบลาน — สงบ ขรึม น่าเชื่อถือ
   ไม่ใช่ดีไซน์ chatbot ทั่วไป (ไม่มี bubble โค้งมน ฟ้า-ขาว)
   ============================================================ */

:root {
  /* Palette */
  --paper: #F2EDE1;        /* พื้นกระดาษใบลาน */
  --paper-deep: #E8E0CC;   /* ส่วนเข้มกว่าเล็กน้อย เช่น input area */
  --ink: #3A2E22;          /* ตัวอักษรหลัก น้ำตาลเข้มอมดำ */
  --teak: #5C3A2E;         /* น้ำตาลไม้สัก accent หลัก */
  --gilt: #A8843C;         /* ทองหม่น accent รอง / highlight */
  --robe-green: #3D4A3D;   /* เขียวจีวร ใช้กับสถานะ/ลิงก์ */
  --hairline: #C9BCA0;     /* เส้นแบ่งบาง ๆ */
  --citation-bg: #E3D9BE;  /* พื้นหลังกล่องอ้างอิง */

  --font-display: 'Noto Serif Thai', serif;
  --font-body: 'Noto Sans Thai', sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  height: 100%;
}

.page {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 760px;
  margin: 0 auto;
}

/* ---------- Header ---------- */
.site-header {
  padding: 28px 20px 20px;
  border-bottom: 1px solid var(--hairline);
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 14px;
}

.mark {
  font-size: 28px;
  color: var(--gilt);
  line-height: 1;
}

.back-link {
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.15s ease;
}
.back-link:hover { transform: scale(1.1); }

.header-text h1 {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 600;
  margin: 0;
  color: var(--teak);
}

.subtitle {
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--ink);
  opacity: 0.65;
}

/* ---------- Chat area ---------- */
.chat-area {
  flex: 1;
  padding: 24px 20px 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.welcome-block {
  padding: 8px 4px 20px;
  border-bottom: 1px dashed var(--hairline);
}

.welcome-lede {
  font-family: var(--font-display);
  font-size: 19px;
  font-weight: 600;
  color: var(--teak);
  margin: 0 0 8px;
}

.welcome-text {
  font-size: 15px;
  line-height: 1.75;
  margin: 0 0 16px;
  opacity: 0.85;
}

.example-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  font-family: var(--font-body);
  font-size: 13px;
  padding: 8px 14px;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  border-radius: 3px;
  border: 1px solid var(--hairline);
  background: var(--paper-deep);
  color: var(--teak);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.chip:hover { background: var(--citation-bg); border-color: var(--gilt); }

/* ---------- Message blocks ---------- */
.msg {
  max-width: 100%;
}

.msg-question {
  font-family: var(--font-body);
  font-size: 15px;
  font-weight: 500;
  color: var(--teak);
  padding-left: 14px;
  border-left: 3px solid var(--gilt);
}

.msg-answer {
  font-family: var(--font-display);
  font-size: 16px;
  line-height: 1.85;
  white-space: pre-wrap;
}

.thought-note {
  font-family: var(--font-body);
  font-size: 12px;
  color: var(--robe-green);
  opacity: 0.7;
  font-style: italic;
  margin-bottom: 4px;
}

.msg-answer.loading {
  font-family: var(--font-body);
  font-size: 14px;
  opacity: 0.6;
  font-style: italic;
}

.citation-box {
  margin-top: 12px;
  padding: 10px 14px;
  background: var(--citation-bg);
  border-radius: 3px;
  font-family: var(--font-body);
  font-size: 12.5px;
  color: var(--robe-green);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.citation-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border: 1px solid var(--robe-green);
  border-radius: 2px;
  opacity: 0.85;
}

.error-text {
  font-family: var(--font-body);
  font-size: 14px;
  color: #8B3A2E;
}

/* ---------- Composer ---------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 20px;
  background: var(--paper-deep);
  border-top: 1px solid var(--hairline);
}

.composer-input {
  flex: 1;
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.5;
  padding: 12px 14px;
  border: 1px solid var(--hairline);
  border-radius: 4px;
  background: var(--paper);
  color: var(--ink);
  resize: none;
  max-height: 140px;
}
.composer-input:focus {
  outline: 2px solid var(--gilt);
  outline-offset: 1px;
}

.send-btn {
  flex-shrink: 0;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 4px;
  background: var(--teak);
  color: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease;
}
.send-btn:hover { background: var(--gilt); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Footer ---------- */
.site-footer {
  padding: 10px 20px 18px;
  text-align: center;
}
.site-footer p {
  font-size: 11.5px;
  color: var(--ink);
  opacity: 0.55;
  margin: 0;
}

/* ---------- Accessibility ---------- */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

button:focus-visible, .composer-input:focus-visible {
  outline: 2px solid var(--gilt);
  outline-offset: 2px;
}

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
  .header-text h1 { font-size: 19px; }
  .msg-answer { font-size: 15px; }
}

/* AI answer markdown */
.answer-body.markdown-rendered {
  line-height: 1.85;
}

.answer-body.markdown-rendered h1,
.answer-body.markdown-rendered h2,
.answer-body.markdown-rendered h3 {
  margin: 18px 0 10px;
  color: #4b2f22;
  line-height: 1.45;
}

.answer-body.markdown-rendered h1 { font-size: 1.35rem; }
.answer-body.markdown-rendered h2 { font-size: 1.22rem; }
.answer-body.markdown-rendered h3 { font-size: 1.12rem; }

.answer-body.markdown-rendered p {
  margin: 8px 0;
}

.answer-body.markdown-rendered strong {
  font-weight: 700;
  color: #4b2f22;
}

.answer-body.markdown-rendered .md-link {
  color: #9a6a1f !important;
  font-weight: 700;
  text-decoration: underline !important;
  word-break: break-word;
}

.answer-body.markdown-rendered .md-bullet,
.answer-body.markdown-rendered .md-numbered {
  margin: 8px 0;
  line-height: 1.75;
}

.answer-body.markdown-rendered blockquote {
  margin: 12px 0;
  padding: 8px 14px;
  border-left: 3px solid rgba(181, 137, 54, .55);
  background: rgba(181, 137, 54, .08);
}

.answer-body.markdown-rendered hr {
  border: 0;
  border-top: 1px solid rgba(181, 137, 54, .28);
  margin: 18px 0;
}

/* Clickable links inside AI answers */
.msg-answer .answer-link {
  color: #9a6a1f !important;
  font-weight: 700;
  text-decoration: underline !important;
  word-break: break-word;
}

.recent-question-btn {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  font: inherit;
  color: inherit;
  cursor: pointer;
  padding: 0;
}

.recent-reader {
  margin: 28px auto;
  padding: 28px;
  max-width: 720px;
  border: 1px solid rgba(181, 137, 54, .28);
  border-radius: 24px;
  background: rgba(255, 252, 244, .72);
}

.recent-reader h3 {
  color: #9a6a1f;
  margin: 0 0 12px;
}

.reader-question {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 28px;
  color: #4b2f22;
}

.reader-answer {
  line-height: 1.85;
  color: #4b2f22;
}

.reader-answer h1,
.reader-answer h2,
.reader-answer h3 {
  margin: 18px 0 12px;
  color: #4b2f22;
}

.reader-answer a {
  color: #9a6a1f;
  font-weight: 700;
  text-decoration: underline;
  word-break: break-word;
}

.reader-muted {
  color: #8a7a6a;
}

.reader-chat-btn {
  display: inline-block;
  margin-top: 24px;
  padding: 12px 22px;
  border-radius: 999px;
  background: #b98b2e;
  color: white;
  text-decoration: none;
  font-weight: 700;
}

/* Fix index recent questions layout */
#recentLog,
.recent-log,
.recent-section {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin-left: auto !important;
  margin-right: auto !important;
  overflow: visible !important;
}

#recentLogList,
.recent-log-list {
  width: 100% !important;
  max-height: none !important;
  height: auto !important;
  overflow: visible !important;
  padding: 0 !important;
  margin: 18px 0 0 !important;
  list-style: none !important;
}

#recentLogList li,
.recent-log-list li {
  list-style: none !important;
  margin: 0 0 14px !important;
}

#recentLogList button,
.recent-log-list button,
.recent-question-btn {
  width: 100% !important;
  display: block !important;
  text-align: left !important;
  padding: 18px 22px !important;
  border-radius: 16px !important;
  border: 1px solid rgba(181, 137, 54, .35) !important;
  background: rgba(255, 252, 244, .85) !important;
  color: #4b2f22 !important;
  font: inherit !important;
  cursor: pointer !important;
}

#recentLogList button:hover,
.recent-log-list button:hover,
.recent-question-btn:hover {
  border-color: rgba(181, 137, 54, .85) !important;
  box-shadow: 0 4px 14px rgba(75, 47, 34, .08) !important;
}

#recentQuestionReader[hidden],
.recent-reader[hidden] {
  display: none !important;
}

#recentQuestionReader,
.recent-reader {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin: 32px auto !important;
  padding: 28px !important;
  border-radius: 24px !important;
  text-align: left !important;
}

@media (max-width: 640px) {
  #recentLog,
  .recent-log,
  .recent-section,
  #recentQuestionReader,
  .recent-reader {
    width: calc(100vw - 28px) !important;
  }

  #recentLogList button,
  .recent-log-list button,
  .recent-question-btn {
    padding: 16px 18px !important;
    font-size: 1rem !important;
  }
}

/* Final fix: recent list scroll + reader hidden until selected */
#recentQuestionReader[hidden],
.recent-reader[hidden] {
  display: none !important;
}

#recentLog,
.recent-log,
.recent-section {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin-left: auto !important;
  margin-right: auto !important;
}

#recentLogList,
.recent-log-list {
  width: 100% !important;
  max-height: 360px !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  padding: 0 8px 0 0 !important;
  margin: 18px 0 0 !important;
  list-style: none !important;
}

#recentLogList li,
.recent-log-list li {
  list-style: none !important;
  margin: 0 0 14px !important;
}

#recentLogList button,
.recent-log-list button,
.recent-question-btn {
  width: 100% !important;
  display: block !important;
  text-align: left !important;
  padding: 18px 22px !important;
  border-radius: 16px !important;
  border: 1px solid rgba(181, 137, 54, .35) !important;
  background: rgba(255, 252, 244, .85) !important;
  color: #4b2f22 !important;
  font: inherit !important;
  cursor: pointer !important;
}

#recentQuestionReader,
.recent-reader {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin: 32px auto !important;
  padding: 28px !important;
  border: 1px solid rgba(181, 137, 54, .28) !important;
  border-radius: 24px !important;
  background: rgba(255, 252, 244, .72) !important;
  text-align: left !important;
}

.reader-question {
  font-size: 1.25rem !important;
  font-weight: 700 !important;
  margin-bottom: 28px !important;
  color: #4b2f22 !important;
}

.reader-answer {
  line-height: 1.85 !important;
  color: #4b2f22 !important;
}

.reader-answer a {
  color: #9a6a1f !important;
  font-weight: 700 !important;
  text-decoration: underline !important;
  word-break: break-word !important;
}

@media (max-width: 640px) {
  #recentLogList,
  .recent-log-list {
    max-height: 320px !important;
  }
}

/* Hard override: recent reader/list */
#recentQuestionReader,
.recent-reader {
  display: none !important;
}

#recentQuestionReader.is-open,
.recent-reader.is-open {
  display: block !important;
}

#recentLog,
.recent-log,
.recent-section {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin: 0 auto !important;
}

#recentLogList,
.recent-log-list {
  width: 100% !important;
  max-height: 360px !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  padding: 0 8px 0 0 !important;
  margin: 18px 0 0 !important;
  list-style: none !important;
}

#recentLogList li,
.recent-log-list li {
  list-style: none !important;
  margin: 0 0 14px !important;
}

#recentLogList button,
.recent-log-list button,
.recent-question-btn {
  width: 100% !important;
  display: block !important;
  text-align: left !important;
  padding: 18px 22px !important;
  border-radius: 16px !important;
  border: 1px solid rgba(181, 137, 54, .35) !important;
  background: rgba(255, 252, 244, .9) !important;
  color: #4b2f22 !important;
  font: inherit !important;
  cursor: pointer !important;
}

#recentQuestionReader.is-open,
.recent-reader.is-open {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin: 32px auto !important;
  padding: 28px !important;
  border: 1px solid rgba(181, 137, 54, .28) !important;
  border-radius: 24px !important;
  background: rgba(255, 252, 244, .72) !important;
  text-align: left !important;
}

.reader-question {
  font-size: 1.25rem !important;
  font-weight: 700 !important;
  margin-bottom: 28px !important;
  color: #4b2f22 !important;
}

.reader-answer {
  line-height: 1.85 !important;
  color: #4b2f22 !important;
}

.reader-answer a {
  color: #9a6a1f !important;
  font-weight: 700 !important;
  text-decoration: underline !important;
  word-break: break-word !important;
}

#recentQuestionReader,
.recent-reader {
  display: none !important;
}

#recentQuestionReader.is-open,
.recent-reader.is-open {
  display: block !important;
}

#recentLog,
.recent-log,
.recent-section {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin-left: auto !important;
  margin-right: auto !important;
}

#recentLogList {
  max-height: 360px !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  list-style: none !important;
  padding: 0 8px 0 0 !important;
  margin: 18px 0 0 !important;
}

#recentLogList li {
  list-style: none !important;
  margin: 0 0 14px !important;
}

.recent-question-btn {
  width: 100% !important;
  display: block !important;
  text-align: left !important;
  padding: 18px 22px !important;
  border-radius: 16px !important;
  border: 1px solid rgba(181, 137, 54, .35) !important;
  background: rgba(255, 252, 244, .9) !important;
  color: #4b2f22 !important;
  font: inherit !important;
  cursor: pointer !important;
}

#recentQuestionReader.is-open,
.recent-reader.is-open {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin: 32px auto !important;
  padding: 28px !important;
  border-radius: 24px !important;
  border: 1px solid rgba(181, 137, 54, .28) !important;
  background: rgba(255, 252, 244, .76) !important;
  text-align: left !important;
}

.reader-question {
  font-size: 1.25rem !important;
  font-weight: 700 !important;
  color: #4b2f22 !important;
  margin-bottom: 24px !important;
}

.reader-answer {
  line-height: 1.85 !important;
  color: #4b2f22 !important;
}

.reader-answer a {
  color: #9a6a1f !important;
  font-weight: 700 !important;
  text-decoration: underline !important;
  word-break: break-word !important;
}

/* Force recent answers on index */
#recentLog,
.recent-log,
.recent-section {
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin-left: auto !important;
  margin-right: auto !important;
}

#recentLogList,
.recent-log-list {
  width: 100% !important;
  max-height: 360px !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  padding: 0 8px 0 0 !important;
  margin: 18px 0 0 !important;
  list-style: none !important;
}

#recentLogList li,
.recent-log-list li,
.force-question-li {
  list-style: none !important;
  margin: 0 0 14px !important;
}

.force-question-btn {
  width: 100% !important;
  display: block !important;
  text-align: left !important;
  padding: 18px 22px !important;
  border-radius: 16px !important;
  border: 1px solid rgba(181, 137, 54, .35) !important;
  background: rgba(255, 252, 244, .9) !important;
  color: #4b2f22 !important;
  font: inherit !important;
  cursor: pointer !important;
}

#recentQuestionReaderForce,
.force-recent-reader {
  display: none !important;
}

#recentQuestionReaderForce.is-open,
.force-recent-reader.is-open {
  display: block !important;
  width: min(720px, calc(100vw - 32px)) !important;
  max-width: 720px !important;
  margin: 32px auto !important;
  padding: 28px !important;
  border-radius: 24px !important;
  border: 1px solid rgba(181, 137, 54, .28) !important;
  background: rgba(255, 252, 244, .78) !important;
  text-align: left !important;
}

.force-reader-question {
  font-size: 1.25rem !important;
  font-weight: 700 !important;
  color: #4b2f22 !important;
  margin-bottom: 28px !important;
}

.force-reader-answer {
  line-height: 1.85 !important;
  color: #4b2f22 !important;
}

.force-reader-answer h1,
.force-reader-answer h2,
.force-reader-answer h3 {
  color: #4b2f22 !important;
  margin: 18px 0 12px !important;
}

.force-reader-answer a {
  color: #9a6a1f !important;
  font-weight: 700 !important;
  text-decoration: underline !important;
  word-break: break-word !important;
}

.force-reader-chat-btn {
  display: inline-block !important;
  margin-top: 24px !important;
  padding: 12px 22px !important;
  border-radius: 999px !important;
  background: #b98b2e !important;
  color: white !important;
  text-decoration: none !important;
  font-weight: 700 !important;
}

.force-question-btn.is-active {
  border-color: rgba(185, 139, 46, .95) !important;
  box-shadow: 0 0 0 2px rgba(185, 139, 46, .18) !important;
  background: rgba(255, 248, 231, .98) !important;
  font-weight: 700 !important;
}

/* Spin restored dhamma wheel spokes */
@keyframes nookWheelSpin {
  from {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

#wheelRestoreSvg {
  animation: nookWheelSpin 28s linear infinite !important;
  transform-origin: center center !important;
  will-change: transform !important;
}

.wheel-wrap:hover #wheelRestoreSvg {
  animation-duration: 12s !important;
}
