:root {
  --bg-primary: #0a0e17;
  --bg-secondary: #0f172a;
  --bg-card: rgba(18, 24, 39, 0.75);
  --bg-card-hover: rgba(26, 34, 53, 0.85);
  --border-gold: rgba(229, 184, 105, 0.25);
  --border-subtle: rgba(255, 255, 255, 0.08);

  --gold-primary: #e5b869;
  --gold-light: #f3d699;
  --gold-dark: #b38938;
  --gold-glow: rgba(229, 184, 105, 0.3);

  --emerald-success: #10b981;
  --red-danger: #ef4444;
  --amber-warn: #f59e0b;

  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-dim: #64748b;

  --font-thai: 'Prompt', 'Sarabun', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-en: 'Plus Jakarta Sans', sans-serif;
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --transition: all 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

[hidden] { display: none !important; }

body {
  background-color: var(--bg-primary);
  background-image:
    radial-gradient(circle at 15% 20%, rgba(229, 184, 105, 0.07) 0%, transparent 40%),
    radial-gradient(circle at 85% 70%, rgba(30, 58, 138, 0.15) 0%, transparent 45%);
  color: var(--text-main);
  font-family: var(--font-thai);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* Glassmorphism */
.glass-panel {
  background: var(--bg-card);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  transition: var(--transition);
}

/* Header & Navigation */
header {
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(10, 14, 23, 0.85);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 1rem 2rem;
}

.header-container {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.logo-group {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo-icon {
  width: 44px;
  height: 44px;
  background: linear-gradient(135deg, var(--gold-dark), var(--gold-primary));
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 20px var(--gold-glow);
  font-size: 22px;
}

.logo-text h1 {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(90deg, #ffffff, var(--gold-primary));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.logo-text span {
  font-size: 0.75rem;
  color: var(--gold-light);
  letter-spacing: 0.06em;
  display: block;
}

.nav-tabs {
  display: flex;
  gap: 8px;
  background: rgba(15, 23, 42, 0.6);
  padding: 4px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}

.tab-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  font-family: var(--font-thai);
  font-size: 0.95rem;
  padding: 8px 18px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: center;
  gap: 8px;
}

.tab-btn.active {
  background: linear-gradient(135deg, rgba(229, 184, 105, 0.2), rgba(229, 184, 105, 0.05));
  color: var(--gold-light);
  border-color: var(--border-gold);
  font-weight: 600;
}

.tab-btn:hover:not(.active) {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.04);
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 0.8rem;
  background: rgba(16, 185, 129, 0.1);
  border: 1px solid rgba(16, 185, 129, 0.3);
  color: var(--emerald-success);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 10px currentColor;
  animation: pulse 2s infinite;
}

.status-badge.warn,
.status-badge.checking {
  background: rgba(245, 158, 11, 0.1);
  border-color: rgba(245, 158, 11, 0.35);
  color: #fbbf24;
}

.status-badge.error {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.45);
  color: #f87171;
}

.status-badge.error .status-dot { animation: none; }

/* โฟกัสจากคีย์บอร์ดต้องมองเห็นได้เสมอ (เมาส์คลิกไม่แสดงกรอบ) */
:focus-visible {
  outline: 2px solid var(--gold-light);
  outline-offset: 2px;
}

:focus:not(:focus-visible) { outline: none; }

mark {
  background: rgba(229, 184, 105, 0.35);
  color: #fff;
  border-radius: 2px;
  padding: 0 1px;
}

@keyframes pulse {
  0% { transform: scale(0.95); opacity: 0.7; }
  50% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0.7; }
}

/* Layout */
/* ทั้งสองแท็บกว้างเท่ากัน — สลับแท็บแล้วเลย์เอาต์ไม่กระโดด */
main {
  max-width: 1100px;
  margin: 2rem auto;
  padding: 0 2rem;
}

.tab-content { display: none; }

.tab-content.active {
  display: block;
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel-header {
  padding: 1.5rem 1.75rem;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-main);
}

.panel-title svg { color: var(--gold-primary); }

.panel-note {
  font-size: 0.8rem;
  color: var(--gold-light);
}

.panel-body { padding: 1.75rem; }

/* Ask form */
.ask-panel { width: 100%; }

.field-hint {
  margin-top: 6px;
  font-size: 0.82rem;
  color: #fbbf24;
}

/* Model picker */
.model-picker { margin-top: 1rem; }
.model-picker .kb-select { width: 100%; max-width: 320px; }
.model-picker .field-hint { color: var(--text-muted); }

/* Sample prompt chips */
.sample-prompts-wrap {
  margin-bottom: 1.25rem;
}

.sample-prompt-title {
  display: block;
  font-size: 0.82rem;
  color: var(--gold-light);
  margin-bottom: 8px;
  font-weight: 500;
}

.sample-prompt-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip-btn {
  background: rgba(229, 184, 105, 0.08);
  border: 1px solid rgba(229, 184, 105, 0.25);
  border-radius: 20px;
  color: #f1f5f9;
  font-family: var(--font-thai);
  font-size: 0.8rem;
  padding: 6px 14px;
  cursor: pointer;
  transition: var(--transition);
  display: inline-flex;
  align-items: center;
}

.chip-btn.active {
  background: rgba(229, 184, 105, 0.25);
  border-color: var(--gold-primary);
  color: #fff;
}

.chip-btn:hover {
  background: rgba(229, 184, 105, 0.2);
  border-color: var(--gold-primary);
  transform: translateY(-1px);
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(229, 184, 105, 0.15);
}

.form-label {
  display: block;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text-muted);
}

textarea.legal-input {
  width: 100%;
  min-height: 120px;
  background: rgba(10, 14, 23, 0.6);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  color: var(--text-main);
  font-family: var(--font-thai);
  font-size: 1rem;
  line-height: 1.6;
  resize: vertical;
  transition: var(--transition);
}

textarea.legal-input:focus {
  border-color: var(--gold-primary);
  box-shadow: 0 0 15px rgba(229, 184, 105, 0.2);
}

.ask-actions {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 12px;
  margin-top: 1rem;
}

.hint {
  margin-top: 1rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  line-height: 1.7;
}

.hint strong { color: var(--text-muted); }

.btn-primary,
.btn-secondary {
  width: 100%;
  padding: 14px;
  border-radius: var(--radius-md);
  font-family: var(--font-thai);
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: var(--transition);
}

.btn-primary {
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-dark));
  border: none;
  color: #0b0f19;
  box-shadow: 0 4px 20px rgba(229, 184, 105, 0.3);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border-gold);
  color: var(--gold-light);
}

.btn-primary:hover:not(:disabled),
.btn-secondary:hover:not(:disabled) { transform: translateY(-2px); }

.btn-primary:disabled,
.btn-secondary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary[data-busy="1"],
.btn-secondary[data-busy="1"] { cursor: wait; opacity: 0.75; }

/* Results */
.result-area {
  margin: 1.5rem 0 0;
}

.debug-detail {
  margin-bottom: 1.5rem;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.debug-detail pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin-top: 6px;
  font-family: Consolas, monospace;
}

/* ผลค้นหา / ตัวบทที่ AI อ่าน: คอลัมน์เดียว อ่านตัวบทยาวได้ง่าย */
.statute-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.relevance-tag {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 4px;
  border: 1px solid transparent;
}

.relevance-tag.high { color: #34d399; border-color: rgba(16, 185, 129, 0.4); }
.relevance-tag.mid { color: #fbbf24; border-color: rgba(245, 158, 11, 0.4); }
.relevance-tag.low { color: var(--text-dim); border-color: var(--border-subtle); }

.statute-content.collapsed {
  max-height: 9.5rem;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(to bottom, #000 60%, transparent);
  mask-image: linear-gradient(to bottom, #000 60%, transparent);
}

.expand-btn {
  align-self: flex-start;
  margin-top: 6px;
  background: none;
  border: none;
  color: var(--gold-light);
  font-family: var(--font-thai);
  font-size: 0.82rem;
  cursor: pointer;
  padding: 2px 0;
}

.result-block { margin-bottom: 1.5rem; }

article.result-block { padding: 1.25rem 1.5rem; }

.result-label {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--gold-light);
  margin-bottom: 10px;
  cursor: default;
}

details > summary.result-label { cursor: pointer; }

.answer-text {
  white-space: pre-line;
  font-size: 1.05rem;
  line-height: 1.8;
  color: #ffffff;
}

.model-tag,
.rank-tag {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-dim);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 1px 6px;
  margin-left: 6px;
}

.rank-tag { margin-left: auto; }

.verification-banner {
  padding: 1rem 1.5rem;
  border-radius: var(--radius-md);
  margin-bottom: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid transparent;
}

.banner-verified {
  background: rgba(16, 185, 129, 0.12);
  border-color: rgba(16, 185, 129, 0.35);
  color: #34d399;
}

.banner-abstain {
  background: rgba(245, 158, 11, 0.12);
  border-color: rgba(245, 158, 11, 0.35);
  color: #fbbf24;
}

.banner-warning {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.35);
  color: #f87171;
}

.banner-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.banner-icon { font-size: 1.5rem; font-weight: bold; }
.banner-title { font-weight: 700; font-size: 1rem; }
.banner-sub { font-size: 0.82rem; opacity: 0.9; margin-top: 2px; }

.score-pill {
  font-size: 1.2rem;
  font-weight: 800;
  font-family: var(--font-en);
  padding: 4px 12px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.3);
  white-space: nowrap;
}

.error-detail {
  font-family: Consolas, monospace;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 1.5rem;
  word-break: break-word;
}

.result-block {
  margin-top: 1.5rem;
  padding: 1.5rem;
}

.result-label {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--gold-light);
  margin-bottom: 0.85rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.answer-text {
  font-size: 1rem;
  line-height: 1.85;
  color: #f8fafc;
  white-space: pre-wrap;
  word-break: break-word;
}

.citation-card {
  background: rgba(10, 14, 23, 0.55);
  border: 1px solid var(--border-subtle);
  border-left: 3px solid var(--emerald-success);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  margin-bottom: 10px;
}

.citation-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.citation-pill {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  background: rgba(16, 185, 129, 0.12);
  border: 1px solid rgba(16, 185, 129, 0.35);
  border-radius: 6px;
  color: #34d399;
  font-size: 0.85rem;
  font-weight: 600;
}

.citation-page {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.citation-quote {
  margin: 10px 0 6px;
  font-size: 0.95rem;
  line-height: 1.7;
  color: #f8fafc;
  font-style: italic;
}

details > summary {
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--gold-light);
  margin-top: 4px;
}

details .statute-content { margin-top: 8px; }

.rejected-block {
  background: rgba(239, 68, 68, 0.06);
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.rejected-block .result-label { color: #f87171; }
.rejected-block ul { padding-left: 18px; }
.rejected-quote { font-style: italic; color: var(--text-dim); }

/* Knowledge Base */
.filter-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.filter-row-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding-top: 7px;
  white-space: nowrap;
}

.book-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 1rem;
}

.book-filters.sub {
  padding-left: 12px;
  border-left: 2px solid var(--border-gold);
}

.book-filters.sub .book-filter-btn { font-size: 0.8rem; padding: 4px 12px; }

.book-filter-btn small {
  font-size: 0.7rem;
  color: var(--text-dim);
  margin-left: 4px;
}

.book-filter-btn {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  font-family: var(--font-thai);
  font-size: 0.85rem;
  padding: 6px 14px;
  border-radius: 20px;
  cursor: pointer;
  transition: var(--transition);
}

.book-filter-btn:hover {
  background: rgba(229, 184, 105, 0.12);
  border-color: var(--border-gold);
  color: var(--gold-light);
}

.book-filter-btn.active {
  background: linear-gradient(135deg, rgba(229, 184, 105, 0.25), rgba(229, 184, 105, 0.08));
  border-color: var(--gold-primary);
  color: #ffffff;
  font-weight: 600;
}

.kb-toolbar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: rgba(15, 23, 42, 0.5);
  padding: 16px 20px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  margin-bottom: 1.25rem;
}

.kb-search-row {
  width: 100%;
}

.search-box-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.search-box-wrap .search-icon {
  position: absolute;
  left: 16px;
  color: var(--text-dim);
  pointer-events: none;
}

.search-box-wrap .search-box {
  width: 100%;
  padding-left: 46px;
  padding-right: 40px;
}

.clear-search-btn {
  position: absolute;
  right: 14px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.35rem;
  cursor: pointer;
  line-height: 1;
  padding: 2px 6px;
  transition: var(--transition);
}

.clear-search-btn:hover {
  color: var(--text-main);
}

.kb-controls-row {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.control-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.kb-select {
  background: rgba(10, 14, 23, 0.85);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  font-family: var(--font-thai);
  font-size: 0.85rem;
  padding: 7px 12px;
  cursor: pointer;
  transition: var(--transition);
}

.kb-select:focus {
  border-color: var(--gold-primary);
  box-shadow: 0 0 12px var(--gold-glow);
}

.btn-text-action {
  background: transparent;
  border: 1px dashed var(--gold-dark);
  color: var(--gold-light);
  font-family: var(--font-thai);
  font-size: 0.8rem;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition);
}

.btn-text-action:hover {
  background: rgba(229, 184, 105, 0.15);
  border-style: solid;
}

.count-badge {
  font-size: 0.85rem;
  color: var(--gold-light);
  white-space: nowrap;
  margin-left: auto;
}

.search-box {
  width: 100%;
  padding: 14px 20px;
  background: rgba(10, 14, 23, 0.6);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  color: var(--text-main);
  font-family: var(--font-thai);
  font-size: 1rem;
}

.search-box:focus {
  border-color: var(--gold-primary);
}

.statute-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: 20px;
  margin-top: 1.25rem;
  align-items: start;   /* การ์ดสูงตามเนื้อหา ไม่ยืดให้เท่ากัน */
}

.statute-card {
  padding: 1.25rem 1.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  transition: var(--transition);
  display: flex;
  flex-direction: column;
}

.statute-card:hover { border-color: var(--border-gold); }

.statute-card.repealed { opacity: 0.6; }

.statute-card-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.statute-badge {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(229, 184, 105, 0.1);
  color: var(--gold-light);
}

.statute-thai-badge {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(59, 130, 246, 0.12);
  color: #93c5fd;
  border: 1px solid rgba(59, 130, 246, 0.25);
}

.repealed-badge {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(239, 68, 68, 0.12);
  color: #f87171;
}

.copy-sec-btn {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 0.75rem;
  color: var(--text-dim);
  cursor: pointer;
  transition: var(--transition);
}

.copy-sec-btn:hover {
  background: rgba(229, 184, 105, 0.15);
  border-color: var(--gold-primary);
  color: var(--gold-light);
}

.copy-sec-btn.copied {
  background: rgba(16, 185, 129, 0.15);
  border-color: var(--emerald-success);
  color: var(--emerald-success);
}

.statute-sec {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-main);
}

.statute-title {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 10px;
}

/* Authentic Thai Legal Code Book Formatting */
.statute-content {
  font-size: 0.95rem;
  color: #e2e8f0;
  line-height: 1.85;
  background: rgba(8, 12, 22, 0.5);
  padding: 14px 18px;
  border-radius: 6px;
  border-left: 3.5px solid var(--gold-primary);
  flex: 1;
}

.authentic-book-format {
  font-family: 'Sarabun', 'Prompt', -apple-system, sans-serif;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.25);
}

.statute-para {
  text-indent: 2.5rem;
  text-align: justify;
  text-justify: inter-cluster;
  margin-bottom: 0.75rem;
  line-height: 1.85;
  letter-spacing: 0.01em;
}

.statute-para:last-child {
  margin-bottom: 0;
}

.statute-subclause {
  text-align: justify;
  text-justify: inter-cluster;
  margin-bottom: 0.55rem;
  line-height: 1.85;
  letter-spacing: 0.01em;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.statute-subclause:last-child {
  margin-bottom: 0;
}

.statute-subclause.level-1 {
  padding-left: 1.5rem;
}

.statute-subclause.level-2 {
  padding-left: 3rem;
}

.clause-num {
  font-weight: 600;
  color: var(--gold-light);
  white-space: nowrap;
  flex-shrink: 0;
}

.clause-body {
  color: #cbd5e1;
  flex: 1;
}

.statute-repealed-notice {
  padding: 10px 14px;
  background: rgba(239, 68, 68, 0.08);
  border-radius: 6px;
  color: #fca5a5;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.repealed-tag {
  font-weight: 700;
  color: #ef4444;
}

.statute-source {
  margin-top: 8px;
  font-size: 0.72rem;
  color: var(--text-dim);
}

.load-more-wrap {
  text-align: center;
  margin-top: 2rem;
}

.load-more-wrap .btn-secondary {
  max-width: 260px;
  margin: 0 auto;
}

footer {
  text-align: center;
  padding: 2rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  border-top: 1px solid var(--border-subtle);
  margin-top: 3rem;
}

/* Responsive */
@media (max-width: 760px) {
  /* header กะทัดรัด เพื่อให้ช่องพิมพ์คำถามอยู่ในจอแรก */
  header { padding: 0.6rem 1rem; }
  .header-container { gap: 8px; }
  .logo-icon { width: 34px; height: 34px; font-size: 17px; }
  .logo-text h1 { font-size: 1.05rem; }
  .logo-text span { display: none; }
  .nav-tabs { order: 3; width: 100%; }
  .tab-btn { flex: 1; justify-content: center; padding: 6px 8px; font-size: 0.85rem; }
  .status-badge { font-size: 0.72rem; padding: 4px 10px; }

  main { padding: 0 1rem; margin-top: 1rem; }
  .panel-header { padding: 1rem 1.1rem; }
  .panel-body { padding: 1.1rem; }

  /* ชิปตัวอย่างเลื่อนแนวนอนแถวเดียว */
  .sample-prompt-chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 6px;
    scrollbar-width: thin;
  }
  .chip-btn { flex: 0 0 auto; white-space: nowrap; }

  textarea.legal-input { min-height: 96px; }
  .ask-actions { grid-template-columns: 1fr; }
  .statute-grid { grid-template-columns: 1fr; }

  /* แถบควบคุมในคลัง: ห้ามล้นจอแนวนอน */
  .kb-toolbar { padding: 12px; }
  .kb-controls-row { flex-direction: column; align-items: stretch; gap: 10px; }
  .control-group { flex-wrap: wrap; min-width: 0; }
  .kb-select { width: 100%; min-width: 0; }
  .count-badge { margin-left: 0; white-space: normal; }
  .filter-row { flex-direction: column; gap: 4px; }
  .filter-row-label { padding-top: 0; }
  .book-filters.sub { padding-left: 8px; }
  .statute-subclause.level-1 { padding-left: 0.75rem; }
  .statute-subclause.level-2 { padding-left: 1.5rem; }
  .statute-para { text-indent: 1.5rem; }
  .verification-banner { flex-wrap: wrap; padding: 0.85rem 1rem; }
}

/* ---------- ความคิดเห็นผู้ใช้ ---------- */
.fb-only { display: none !important; }
body.feedback-on .fb-only { display: revert !important; }
body.feedback-on .copy-sec-btn.fb-only { display: inline-flex !important; }
body.feedback-on .feedback-box.fb-only,
body.feedback-on .footer-actions.fb-only { display: block !important; }

/* ช่องกับดักบอท — ซ่อนจากผู้ใช้และโปรแกรมอ่านหน้าจอ */
.fb-trap { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }

.feedback-box { padding: 1.25rem 1.5rem; }
.fb-question { font-weight: 600; margin-bottom: 0.75rem; }
.fb-votes { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.fb-vote.active { border-color: var(--gold-primary); color: var(--gold-light); background: rgba(229, 184, 105, 0.12); }

.feedback-form { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 1rem; }
.feedback-form .btn-primary { align-self: flex-start; width: auto; padding: 12px 22px; }
.fb-textarea { min-height: 90px; }
.fb-input {
  width: 100%;
  background: rgba(10, 14, 23, 0.6);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.65rem 0.9rem;
  color: var(--text-main);
  font-family: var(--font-thai);
  font-size: 1rem;
  transition: var(--transition);
}
.fb-input:focus { border-color: var(--gold-primary); box-shadow: 0 0 15px rgba(229, 184, 105, 0.2); }
.fb-privacy { font-size: 0.8rem; color: var(--text-dim); line-height: 1.6; }
.fb-msg { font-size: 0.9rem; min-height: 1.2em; color: var(--text-muted); }
.fb-msg.ok { color: var(--emerald-success); }
.fb-msg.error { color: var(--red-danger); }

.report-btn { color: var(--text-dim); }
.report-btn:hover { color: var(--amber-warn); border-color: var(--amber-warn); }

.footer-actions { margin-top: 0.75rem; }
.footer-review-btn { display: inline-flex; width: auto; padding: 0.5rem 1.1rem; font-size: 0.9rem; }

.feedback-dialog {
  margin: auto;
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  background: var(--bg-secondary);
  color: var(--text-main);
  border: 1px solid var(--border-gold);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  font-family: var(--font-thai);
}
.feedback-dialog::backdrop { background: rgba(5, 8, 15, 0.75); }
.feedback-dialog .feedback-form { margin-top: 0; }
.dialog-title { font-size: 1.2rem; color: var(--gold-light); margin-bottom: 0.25rem; }
.dialog-section { color: var(--text-muted); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.75rem; margin-top: 0.5rem; }
.dialog-actions button { width: auto; padding: 12px 22px; }

.star-field { border: 0; }
/* ดาวเรียง 5→1 ใน HTML แล้วกลับด้านด้วย row-reverse เพื่อให้ hover/เลือกแล้วติดดาวทางซ้ายทั้งหมด */
.stars { display: inline-flex; flex-direction: row-reverse; gap: 0.25rem; }
.stars input { position: absolute; opacity: 0; pointer-events: none; }
.stars label { font-size: 2rem; line-height: 1; color: var(--text-dim); cursor: pointer; transition: var(--transition); }
.stars label:hover,
.stars label:hover ~ label,
.stars input:checked ~ label { color: var(--gold-primary); }
.stars input:focus-visible + label { outline: 2px solid var(--gold-primary); outline-offset: 2px; border-radius: 4px; }

@media (max-width: 760px) {
  .feedback-box { padding: 1rem; }
  .feedback-dialog { padding: 1.1rem; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions button { width: 100%; justify-content: center; }
}

/* ---------- แชตติวกฎหมาย ---------- */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.chat-layout {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 16px;
  height: calc(100dvh - 170px);
  min-height: 520px;
}

.chat-sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  min-height: 0;
}

.new-chat-btn { padding: 10px 14px; font-size: 0.95rem; }
.sidebar-title { font-size: 0.78rem; font-weight: 600; color: var(--text-dim); letter-spacing: 0.04em; margin-top: 6px; }
.sidebar-note { font-size: 0.72rem; color: var(--text-dim); line-height: 1.5; }

.chat-list {
  list-style: none;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scrollbar-width: thin;
}

.chat-item {
  display: flex;
  align-items: stretch;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
}
.chat-item:hover { background: rgba(255, 255, 255, 0.04); }
.chat-item.active { background: rgba(229, 184, 105, 0.1); border-color: var(--border-gold); }

.chat-open {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: 0;
  color: var(--text-main);
  font-family: var(--font-thai);
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.chat-item-title { font-size: 0.86rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item-time { font-size: 0.7rem; color: var(--text-dim); }

.chat-delete {
  background: none;
  border: 0;
  color: var(--text-dim);
  font-family: var(--font-thai);
  font-size: 0.8rem;
  padding: 0 10px;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.chat-delete:hover, .chat-delete.confirm { color: #f87171; }
.chat-empty { font-size: 0.82rem; color: var(--text-dim); padding: 8px 10px; }

.chat-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border-subtle);
}
.chat-title {
  flex: 1;
  min-width: 0;
  font-size: 1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-header .model-picker { margin-top: 0; }
.chat-header .model-picker .kb-select { max-width: 200px; padding-top: 6px; padding-bottom: 6px; }
.model-desc { font-size: 0.75rem; color: var(--text-dim); padding: 6px 18px 0; text-align: right; }
.model-desc:empty { display: none; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  cursor: pointer;
  transition: var(--transition);
}
.icon-btn:hover:not(:disabled) { color: var(--gold-light); border-color: var(--border-gold); }
.icon-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.chat-list-toggle { display: none; }

.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scrollbar-width: thin;
}

.chat-welcome { margin: auto; max-width: 640px; text-align: center; padding: 1rem 0; }
.welcome-icon {
  width: 56px; height: 56px; margin: 0 auto 12px;
  display: flex; align-items: center; justify-content: center; font-size: 28px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--gold-dark), var(--gold-primary));
  box-shadow: 0 0 24px var(--gold-glow);
}
.welcome-title { font-size: 1.25rem; font-weight: 600; color: var(--gold-light); margin-bottom: 6px; }
.welcome-sub { font-size: 0.88rem; color: var(--text-muted); line-height: 1.7; margin-bottom: 18px; }
.welcome-chips { justify-content: center; }

.msg { display: flex; gap: 10px; align-items: flex-start; max-width: 100%; }
.msg-user { justify-content: flex-end; }
.avatar {
  width: 32px; height: 32px; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  border-radius: 10px; font-size: 16px;
  background: linear-gradient(135deg, var(--gold-dark), var(--gold-primary));
}
.bubble { border-radius: 16px; line-height: 1.75; min-width: 0; }
.user-bubble {
  max-width: min(78%, 640px);
  padding: 10px 16px;
  background: linear-gradient(135deg, rgba(229, 184, 105, 0.22), rgba(229, 184, 105, 0.1));
  border: 1px solid var(--border-gold);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
  word-break: break-word;
}
.bot-bubble {
  flex: 1;
  max-width: 860px;
  padding: 14px 18px;
  background: rgba(10, 14, 23, 0.55);
  border: 1px solid var(--border-subtle);
  border-top-left-radius: 4px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.bot-bubble .verification-banner { margin-bottom: 0; padding: 0.85rem 1rem; }
.bot-bubble .citation-card:last-child { margin-bottom: 0; }
.bot-bubble .statute-list { margin-top: 10px; }
.bot-bubble .hint { margin-top: 0; }

.status-pill {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.76rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 20px;
  cursor: help;
}
.status-pill.status-ok { background: rgba(16, 185, 129, 0.12); border: 1px solid rgba(16, 185, 129, 0.35); color: #34d399; }
.status-pill.status-warn { background: rgba(245, 158, 11, 0.12); border: 1px solid rgba(245, 158, 11, 0.35); color: #fbbf24; }

.status-caveat { font-size: 0.74rem; color: var(--text-dim); margin-top: -6px; line-height: 1.5; }
.answer-meta { font-size: 0.72rem; color: var(--text-dim); margin-top: -6px; }
.block-label { font-size: 0.82rem; font-weight: 600; color: var(--gold-light); margin-bottom: 8px; }
.rejected-block .block-label { color: #f87171; margin-bottom: 0; }
.rejected-block ul { margin-top: 6px; }

.follow-ups { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.follow-ups-label { font-size: 0.78rem; color: var(--text-dim); margin-right: 2px; }
.follow-up-chip { text-align: left; white-space: normal; line-height: 1.5; }

.msg-details > summary { font-size: 0.82rem; }
.retry-btn { align-self: flex-start; width: auto; padding: 8px 16px; font-size: 0.88rem; }

.statute-note { font-size: 0.82rem; color: var(--text-dim); margin: 4px 0; }

/* ให้คะแนนคำตอบในฟองแชต: แถวเดียวกะทัดรัด */
.bot-bubble .feedback-box { padding: 10px 0 0; border-top: 1px solid var(--border-subtle); }
.fb-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.fb-row .fb-question { margin-bottom: 0; font-size: 0.82rem; font-weight: 500; color: var(--text-muted); }
.fb-row .fb-vote { padding: 4px 12px; font-size: 0.95rem; }

.typing .bot-bubble { flex: 0 1 auto; flex-direction: row; align-items: center; gap: 10px; color: var(--text-muted); font-size: 0.88rem; }
.typing-dots { display: inline-flex; gap: 4px; }
.typing-dots i {
  width: 7px; height: 7px; border-radius: 50%; background: var(--gold-primary);
  animation: typing-bounce 1.2s infinite ease-in-out;
}
.typing-dots i:nth-child(2) { animation-delay: 0.15s; }
.typing-dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-5px); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .typing-dots i { animation: none; opacity: 0.8; }
}

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0 18px;
  padding: 8px 8px 8px 14px;
  background: rgba(10, 14, 23, 0.7);
  border: 1px solid var(--border-subtle);
  border-radius: 16px;
  transition: var(--transition);
}
.chat-composer:focus-within { border-color: var(--gold-primary); box-shadow: 0 0 15px rgba(229, 184, 105, 0.15); }
.composer-input {
  flex: 1;
  min-width: 0;
  resize: none;
  background: none;
  border: 0;
  color: var(--text-main);
  font-family: var(--font-thai);
  font-size: 1rem;
  line-height: 1.6;
  padding: 7px 0;
  max-height: 168px;
}
.composer-input:focus-visible { outline-offset: 4px; border-radius: 4px; }
.composer-search { border-radius: 12px; }
.send-btn {
  width: 40px; height: 40px; flex: 0 0 auto;
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 12px; cursor: pointer;
  background: linear-gradient(135deg, var(--gold-primary), var(--gold-dark));
  color: #0b0f19;
  transition: var(--transition);
}
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.composer-hint { margin: 6px 18px 0; }
.composer-note { font-size: 0.72rem; color: var(--text-dim); text-align: center; padding: 8px 18px 12px; }

/* แชตเต็มจอ: footer ไม่ต้องเว้นระยะมาก */
body:has(#tab-ask.active) footer { margin-top: 1rem; padding: 1rem; }

@media (max-width: 900px) {
  .chat-layout { grid-template-columns: minmax(0, 1fr); }
  .chat-list-toggle { display: inline-flex; }
  /* รายการบทสนทนาเป็นลิ้นชักซ้อนด้านซ้าย */
  .chat-sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(300px, 85vw);
    z-index: 200;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    background: var(--bg-secondary);
    transform: translateX(-105%);
    transition: transform 0.25s ease;
  }
  .chat-sidebar.open { transform: translateX(0); box-shadow: 0 0 0 100vmax rgba(5, 8, 15, 0.6); }
}

@media (max-width: 760px) {
  .chat-layout { height: calc(100dvh - 150px); min-height: 440px; }
  .chat-header { padding: 10px 12px; }
  .chat-header .model-picker .kb-select { max-width: 140px; width: auto; }
  .model-desc { display: none; }
  .chat-messages { padding: 14px 10px; gap: 14px; }
  .avatar { display: none; }
  .bot-bubble { padding: 12px 12px; }
  .user-bubble { max-width: 88%; }
  .chat-composer { margin: 0 10px; }
  .composer-note { padding: 6px 10px 10px; }
  .welcome-chips { justify-content: flex-start; }
  .bot-bubble .statute-card { padding: 1rem; }
}
