:root {
  --ink: #17211d;
  --muted: #5c6661;
  --soft: #f3f1eb;
  --paper: #ffffff;
  --page: #fbfaf7;
  --line: #dedfd9;
  --blue: #294f86;
  --blue-soft: #edf3fb;
  --gold: #9a7132;
  --gold-soft: #f8f1e4;
  --green: #2f7259;
  --warn: #a56328;
  --shadow: 0 20px 60px rgba(28, 37, 33, .12);
  font-family: "Noto Sans Thai", "Leelawadee UI", Tahoma, sans-serif;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--page);
  font-size: 16px;
}

body.overlay-open { overflow: hidden; }
button, textarea { font: inherit; }
button { color: inherit; }
button:focus-visible,
textarea:focus-visible,
a:focus-visible { outline: 3px solid rgba(41, 79, 134, .3); outline-offset: 2px; }
.hidden { display: none !important; }

.app-shell {
  display: grid;
  grid-template-rows: 76px minmax(0, 1fr);
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.app-header {
  position: relative;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-width: 0;
  padding: 0 clamp(18px, 3vw, 42px);
  border-bottom: 1px solid var(--line);
  background: rgba(251, 250, 247, .96);
  backdrop-filter: blur(14px);
}

.brand,
.header-actions,
.header-button,
.system-state,
.evidence-modal-heading,
.modal-close,
.source-button,
.source-meta,
.detail-actions,
.loading-title { display: flex; align-items: center; }

.brand { min-width: 0; gap: 12px; }
.brand-mark,
.welcome-mark {
  display: grid;
  place-items: center;
  background: transparent;
}
.brand-mark img,
.welcome-mark img { display: block; width: 100%; height: 100%; object-fit: contain; filter: drop-shadow(0 6px 9px rgba(154, 113, 50, .18)); }
.brand-mark { flex: 0 0 42px; width: 42px; height: 42px; }
.brand-copy { min-width: 0; }
.brand-copy strong { display: block; font-size: 17px; font-weight: 700; line-height: 1.2; }
.brand-copy small { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }

.header-actions { flex: 0 0 auto; gap: 8px; }
.header-button {
  gap: 8px;
  min-height: 44px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.header-button:hover { border-color: var(--line); background: var(--paper); }
.header-button svg,
.send-button svg,
.book-symbol svg,
.source-button svg,
.evidence-tab svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.header-button svg { width: 20px; height: 20px; }
.system-state {
  gap: 8px;
  min-height: 40px;
  margin-left: 4px;
  padding: 0 11px;
  border-radius: 999px;
  color: var(--muted);
  background: var(--soft);
  font-size: 13px;
}
.health-dot { width: 9px; height: 9px; border-radius: 50%; background: #9ca39f; }
.health-dot.ok { background: var(--green); box-shadow: 0 0 0 4px rgba(47, 114, 89, .13); }
.health-dot.warn { background: var(--warn); box-shadow: 0 0 0 4px rgba(165, 99, 40, .12); }

.chat-panel {
  position: relative;
  display: flex;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
  background: var(--page);
}
.provider-banner {
  flex: 0 0 auto;
  padding: 10px 20px;
  border-bottom: 1px solid #ead8b8;
  color: #71502e;
  background: #fff7e8;
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  scrollbar-gutter: stable;
  padding: 52px max(22px, calc((100% - 880px) / 2)) 190px;
}
.app-shell:not(.has-conversation) .messages {
  display: grid;
  place-items: center;
  overflow: hidden;
  padding-bottom: 210px;
}
.welcome { width: min(700px, 100%); text-align: center; }
.welcome-mark {
  width: 58px;
  height: 58px;
  margin: 0 auto 24px;
  border-radius: 18px;
  font-size: 30px;
}
.welcome h1 {
  margin: 0;
  font-size: clamp(36px, 4vw, 52px);
  font-weight: 750;
  line-height: 1.22;
  letter-spacing: -.7px;
}
.welcome p {
  max-width: 650px;
  margin: 16px auto 0;
  color: var(--muted);
  font-size: 19px;
  line-height: 1.75;
}

.message {
  width: min(880px, 100%);
  margin: 0 auto 42px;
  animation: rise .22s ease both;
}
@keyframes rise { from { opacity: 0; transform: translateY(7px); } }
.message.user { display: flex; justify-content: flex-end; }
.message.user .message-body {
  max-width: min(680px, 88%);
  padding: 13px 18px;
  border-radius: 20px 6px 20px 20px;
  color: #26302c;
  background: #eceeea;
  font-size: 17px;
  line-height: 1.7;
  white-space: pre-wrap;
}
.message.assistant .message-body { max-width: 820px; }
.answer-label {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 10px;
  color: var(--gold);
  font-size: 14px;
  font-weight: 700;
}
.answer-label::before {
  content: "";
  display: block;
  width: 28px;
  height: 28px;
  background: url("./logo-diri.png?v=1") center / contain no-repeat;
  filter: drop-shadow(0 4px 6px rgba(154, 113, 50, .16));
}
.answer-text {
  margin: 0;
  color: var(--ink);
  font-size: 20px;
  line-height: 1.9;
  white-space: pre-wrap;
}
.answer-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 15px;
  color: var(--green);
  font-size: 14px;
}
.answer-state::before { content: "✓"; font-weight: 800; }
.answer-state.caution { color: var(--warn); }
.answer-state.caution::before { content: "!"; }
.sources-label {
  margin: 24px 0 10px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
}
.source-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
.source-button {
  gap: 10px;
  max-width: 100%;
  min-height: 48px;
  padding: 9px 14px;
  border: 1px solid #cdd9e9;
  border-radius: 14px;
  color: var(--blue);
  background: var(--blue-soft);
  cursor: pointer;
  text-align: left;
  transition: transform .15s ease, border-color .15s ease, background .15s ease;
}
.source-button:hover { transform: translateY(-1px); border-color: #95adca; background: #e5eef9; }
.source-button svg { flex: 0 0 22px; width: 22px; height: 22px; }
.source-button-copy { min-width: 0; }
.source-button strong,
.source-button small { display: block; }
.source-button strong { overflow: hidden; text-overflow: ellipsis; font-size: 15px; line-height: 1.35; white-space: nowrap; }
.source-button small { margin-top: 2px; color: #5d7190; font-size: 12px; }

.loading-card {
  max-width: 540px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--paper);
  box-shadow: 0 8px 28px rgba(28, 37, 33, .05);
}
.loading-title { gap: 10px; font-size: 16px; font-weight: 600; }
.spinner { width: 18px; height: 18px; border: 2px solid #ccd2ce; border-top-color: var(--blue); border-radius: 50%; animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-steps { margin-top: 7px; color: var(--muted); font-size: 13px; }

.composer-wrap {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
  padding: 28px max(22px, calc((100% - 880px) / 2)) 18px;
  background: linear-gradient(0deg, var(--page) 74%, rgba(251, 250, 247, 0));
}
.app-shell:not(.has-conversation) .composer-wrap {
  top: calc(50% + 74px);
  right: auto;
  bottom: auto;
  left: 50%;
  width: min(880px, calc(100% - 44px));
  padding: 0;
  transform: translateX(-50%);
  background: transparent;
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 11px 11px 11px 18px;
  border: 1px solid #cbd0cc;
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 0 12px 36px rgba(28, 37, 33, .11);
}
.composer:focus-within { border-color: #7794b9; box-shadow: 0 15px 44px rgba(41, 79, 134, .14); }
.app-shell:not(.has-conversation) .composer { min-height: 82px; padding: 16px 14px 14px 22px; border-radius: 24px; }
.composer textarea {
  width: 100%;
  min-width: 0;
  max-height: 170px;
  padding: 8px 0;
  resize: none;
  border: 0;
  outline: 0;
  color: var(--ink);
  background: transparent;
  font-size: 18px;
  line-height: 1.6;
}
.composer textarea::placeholder { color: #818984; }
.send-button {
  display: grid;
  place-items: center;
  flex: 0 0 48px;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 15px;
  color: #fff;
  background: var(--blue);
  cursor: pointer;
}
.send-button:hover { background: #1f426f; }
.send-button:disabled { cursor: wait; opacity: .5; }
.send-button svg { width: 23px; height: 23px; }
.composer-note { margin: 9px 0 0; color: #858d88; font-size: 12px; text-align: center; }
.app-shell:not(.has-conversation) .composer-note { display: none; }

.sessions-drawer {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 70;
  display: flex;
  width: min(420px, 92vw);
  padding: 28px 22px 18px;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: var(--paper);
  box-shadow: var(--shadow);
  transform: translateX(-105%);
  transition: transform .22s ease;
}
.sessions-drawer.open { transform: translateX(0); }
.drawer-header { display: flex; align-items: center; justify-content: space-between; gap: 18px; padding: 0 4px 22px; border-bottom: 1px solid var(--line); }
.drawer-header span { color: var(--muted); font-size: 13px; }
.drawer-header h2 { margin: 3px 0 0; font-size: 26px; line-height: 1.25; }
.close-button {
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  color: var(--muted);
  background: var(--soft);
  cursor: pointer;
  font-size: 28px;
}
.close-button:hover { color: var(--ink); background: #e8e7e1; }
.session-list { min-height: 0; padding-top: 14px; overflow-y: auto; }
.session-item {
  position: relative;
  display: flex;
  align-items: stretch;
  width: 100%;
  min-height: 66px;
  margin: 3px 0;
  padding: 0 48px 0 0;
  border-radius: 14px;
}
.session-item:hover { background: var(--soft); }
.session-item.active { color: var(--blue); background: var(--blue-soft); box-shadow: inset 4px 0 var(--blue); }
.session-open {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  cursor: pointer;
  text-align: left;
}
.session-title { display: block; overflow: hidden; text-overflow: ellipsis; font-size: 16px; font-weight: 600; white-space: nowrap; }
.session-meta { display: block; margin-top: 4px; color: var(--muted); font-size: 13px; }
.session-delete { position: absolute; top: 14px; right: 12px; width: 34px; height: 34px; border: 0; border-radius: 9px; color: var(--muted); background: transparent; cursor: pointer; font-size: 20px; }
.session-delete:hover { color: #93452f; background: #faebe7; }
.drawer-backdrop { display: none; position: fixed; inset: 0; z-index: 60; background: rgba(23, 33, 29, .35); backdrop-filter: blur(2px); }
.drawer-backdrop.open { display: block; }

.evidence-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  color: var(--ink);
  background: var(--page);
  animation: modalIn .2s ease both;
}
@keyframes modalIn { from { opacity: 0; transform: scale(.985); } }
.evidence-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-width: 0;
  padding: 18px clamp(20px, 4vw, 56px);
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.evidence-modal-heading { min-width: 0; gap: 15px; }
.book-symbol {
  display: grid;
  place-items: center;
  flex: 0 0 50px;
  width: 50px;
  height: 50px;
  border-radius: 15px;
  color: var(--blue);
  background: var(--blue-soft);
}
.book-symbol svg { width: 28px; height: 28px; }
.evidence-modal-heading > div { min-width: 0; }
.modal-kicker { display: block; color: var(--muted); font-size: 13px; font-weight: 600; }
.evidence-modal-heading h2 { overflow: hidden; margin: 3px 0 0; text-overflow: ellipsis; font-size: clamp(20px, 2.2vw, 28px); line-height: 1.3; white-space: nowrap; }
.modal-close {
  flex: 0 0 auto;
  gap: 10px;
  min-height: 46px;
  padding: 0 10px 0 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--paper);
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
}
.modal-close:hover { background: var(--soft); }
.modal-close b { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--soft); font-size: 22px; font-weight: 400; }
.evidence-tabs {
  display: flex;
  gap: 9px;
  padding: 13px clamp(20px, 4vw, 56px);
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}
.evidence-tab {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  max-width: 360px;
  min-height: 44px;
  padding: 8px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--muted);
  background: var(--paper);
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
}
.evidence-tab:hover { color: var(--blue); border-color: #aac0da; }
.evidence-tab.active { color: var(--blue); border-color: #aac0da; background: var(--blue-soft); font-weight: 700; }
.evidence-tab svg { flex: 0 0 19px; width: 19px; height: 19px; }
.evidence-tab span { overflow: hidden; text-overflow: ellipsis; }
.evidence-detail { min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.evidence-detail-inner { width: min(1040px, 100%); margin: 0 auto; padding: clamp(28px, 4vw, 52px) 24px 70px; }
.source-heading { margin-bottom: 28px; }
.source-heading h3 { margin: 0; font-size: clamp(27px, 3vw, 38px); line-height: 1.35; }
.source-heading p { margin: 10px 0 0; color: var(--muted); font-size: 17px; line-height: 1.7; }
.source-meta { flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.meta-pill { padding: 7px 11px; border-radius: 999px; color: #53605a; background: var(--soft); font-size: 14px; }
.meta-pill.strong { color: #795823; background: var(--gold-soft); font-weight: 700; }
.evidence-visual { display: grid; place-items: center; width: 100%; min-height: 320px; max-height: 68vh; margin: 0 auto 34px; overflow: hidden; border: 1px solid var(--line); border-radius: 18px; background: #efeee9; }
.evidence-visual img { display: block; width: 100%; height: 100%; max-height: 68vh; object-fit: contain; }
.source-excerpt { padding: clamp(24px, 4vw, 40px); border-left: 5px solid var(--gold); border-radius: 0 18px 18px 0; background: var(--paper); box-shadow: 0 10px 36px rgba(28, 37, 33, .06); }
.source-excerpt h4 { margin: 0 0 14px; color: var(--gold); font-size: 16px; }
.source-excerpt blockquote { margin: 0; font-size: 19px; line-height: 1.95; white-space: pre-wrap; }
.detail-actions { flex-wrap: wrap; gap: 10px; margin-top: 26px; }
.detail-actions a,
.detail-actions button {
  display: inline-flex;
  align-items: center;
  min-height: 46px;
  padding: 0 15px;
  border: 1px solid #cdd9e9;
  border-radius: 12px;
  color: var(--blue);
  background: var(--blue-soft);
  text-decoration: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
}
.detail-actions a:hover,
.detail-actions button:hover { border-color: #95adca; background: #e5eef9; }
.technical-details { margin-top: 30px; color: var(--muted); font-size: 14px; }
.technical-details summary { cursor: pointer; font-weight: 600; }
.technical-details dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 8px 16px; padding: 14px; border-radius: 12px; background: var(--soft); }
.technical-details dt { font-weight: 700; }
.technical-details dd { margin: 0; overflow-wrap: anywhere; }

@media (max-width: 760px) {
  .app-shell { grid-template-rows: 66px minmax(0, 1fr); }
  .app-header { gap: 8px; padding: 0 12px; }
  .brand { gap: 8px; }
  .brand-mark { flex-basis: 38px; width: 38px; height: 38px; }
  .brand-copy strong { font-size: 15px; }
  .brand-copy small,
  .system-state span:last-child { display: none; }
  .system-state { min-height: 38px; margin: 0; padding: 0 10px; }
  .header-actions { gap: 2px; }
  .header-button { min-height: 40px; padding: 0 8px; font-size: 13px; }
  .header-button svg { width: 18px; height: 18px; }
  .messages { padding: 34px 16px 170px; }
  .app-shell:not(.has-conversation) .messages { padding: 0 20px 195px; }
  .welcome-mark { width: 50px; height: 50px; margin-bottom: 18px; }
  .welcome h1 { font-size: 34px; }
  .welcome p { margin-top: 12px; font-size: 17px; line-height: 1.7; }
  .message { margin-bottom: 34px; }
  .message.user .message-body { max-width: 92%; font-size: 16px; }
  .answer-text { font-size: 18px; line-height: 1.85; }
  .source-button { width: 100%; }
  .source-button strong { white-space: normal; }
  .composer-wrap { padding: 24px 12px 10px; }
  .app-shell:not(.has-conversation) .composer-wrap { top: calc(50% + 76px); width: calc(100% - 24px); }
  .composer { gap: 8px; padding: 8px 8px 8px 14px; border-radius: 18px; }
  .app-shell:not(.has-conversation) .composer { min-height: 72px; padding: 11px 10px 10px 17px; border-radius: 20px; }
  .composer textarea { font-size: 17px; }
  .send-button { flex-basis: 44px; width: 44px; height: 44px; border-radius: 14px; }
  .composer-note { font-size: 11px; }
  .evidence-modal-header { gap: 12px; padding: 12px 14px; }
  .book-symbol { flex-basis: 42px; width: 42px; height: 42px; border-radius: 13px; }
  .book-symbol svg { width: 24px; height: 24px; }
  .modal-kicker { font-size: 11px; }
  .evidence-modal-heading h2 { font-size: 18px; }
  .modal-close { min-height: 42px; padding: 0 7px; }
  .modal-close span { display: none; }
  .evidence-tabs { padding: 10px 14px; }
  .evidence-detail-inner { padding: 27px 16px 54px; }
  .source-heading h3 { font-size: 26px; }
  .source-heading p { font-size: 16px; }
  .evidence-visual { min-height: 220px; max-height: 55vh; border-radius: 14px; }
  .evidence-visual img { max-height: 55vh; }
  .source-excerpt { padding: 22px 19px; }
  .source-excerpt blockquote { font-size: 17px; line-height: 1.9; }
  .technical-details dl { grid-template-columns: 1fr; gap: 4px; }
  .technical-details dd { margin-bottom: 8px; }
}

@media (max-width: 430px) {
  .header-button span { display: none; }
  .header-button { width: 40px; justify-content: center; padding: 0; }
  .system-state { width: 34px; min-height: 34px; justify-content: center; padding: 0; }
  .brand-copy strong { font-size: 14px; }
  .welcome h1 { font-size: 30px; }
  .welcome p { font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
