/* static/app.css */

/* ฟอนต์หลักทั้งระบบ */
:root {
  --bg-soft: #f3f4f6;
  --bg-card: #ffffff;
  --bg-chat-user: #2563eb;
  --bg-chat-bot: #e5e7eb;
  --text-main: #111827;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --border-soft: #e5e7eb;
  --radius-lg: 16px;
  --shadow-soft: 0 18px 40px rgba(15, 23, 42, 0.12);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif, "Prompt";
  background: radial-gradient(circle at top left, #e0f2fe, #f9fafb 40%, #f3f4f6);
  color: var(--text-main);
}

/* Layout หลัก */
.layout {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: stretch;
  padding: 5px;
}

.full-height {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.layout-inner {
  max-width: 1120px;
  width: 100%;
  display: flex;
  flex-direction: column;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.app-title {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title-badge {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid rgba(37, 99, 235, 0.3);
}

.app-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Card หลักของแต่ละหน้า */
.app-card {
  background: var(--bg-card);
  border-radius: 24px;
  padding: 20px 24px;
  box-shadow: var(--shadow-soft);
  border: 1px solid rgba(148, 163, 184, 0.18);
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 100% !important;
}

/* Form ทั่วไป */
.form-section-title {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 4px;
}

.form-section-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.form-grid {
  display: grid;
  gap: 12px;
}

.form-label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-main);
  margin-bottom: 4px;
}

.input,
.select,
.textarea {
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border-soft);
  padding: 10px 12px;
  font-size: 0.9rem;
  outline: none;
  background: #f9fafb;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.input:focus,
.select:focus,
.textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(37, 99, 235, 0.35);
  background: #ffffff;
}

.textarea {
  min-height: 120px;
  resize: vertical;
}

/* ปุ่ม */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-radius: 999px;
  border: none;
  padding: 10px 18px;
  font-size: 0.9rem;
  cursor: pointer;
  font-weight: 500;
  transition: transform 0.08s ease, box-shadow 0.12s ease, background 0.12s ease;
}

.btn-primary {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  color: #ffffff;
  box-shadow: 0 12px 28px rgba(37, 99, 235, 0.35);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 16px 38px rgba(37, 99, 235, 0.45);
}

.btn-primary:active {
  transform: translateY(0);
  box-shadow: 0 8px 20px rgba(37, 99, 235, 0.35);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  padding-inline: 10px;
}

.btn-ghost:hover {
  background: rgba(148, 163, 184, 0.08);
}

/* Chat layout */
.chat-layout {
  display: flex;
  flex-direction: column;
  height: 72vh;
  max-height: 720px;
  gap: 12px;
}

.chat-history-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  border-radius: 18px;
  background: #f9fafb;
  border: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* bubble ของ AI/ผู้ใช้ */
.msg-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
}

.msg-avatar.user {
  background: var(--accent);
  color: #ffffff;
}

.msg-avatar.bot {
  background: #0f172a;
  color: #e5e7eb;
}

.msg-bubble {
  max-width: 95%;
  padding: 9px 12px;
  border-radius: 18px;
  font-size: 0.9rem;
  line-height: 1.5;
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.08);
}

.msg-bubble.user {
  background: var(--bg-chat-user);
  color: #ffffff;
  border-bottom-right-radius: 4px;
}

.msg-bubble.bot {
  background: var(--bg-chat-bot);
  color: var(--text-main);
  border-bottom-left-radius: 4px;
}

/* ฟอร์มส่งข้อความ */
/* .chat-input-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
} */

.chat-input-main {
  display: flex;
  gap: 8px;
}

.chat-input-main .textarea {
  flex: 1;
}

/* Modal Thinking... */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.28);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: #ffffff;
  border-radius: 20px;
  padding: 20px 24px;
  min-width: 260px;
  max-width: 360px;
  box-shadow: var(--shadow-soft);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.modal-title {
  font-weight: 600;
  font-size: 1rem;
}

.modal-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
}

/* วงกลม Loading */
.spinner {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 3px solid #e5e7eb;
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Responsive เบสิค */
@media (max-width: 768px) {
  .layout {
    padding: 12px;
  }

  .app-card {
    padding: 16px 14px;
  }

  .chat-layout {
    height: 78vh;
  }

  .msg-bubble {
    max-width: 100%;
  }
}


/* Upload Page Layout */
.upload-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: 1.75rem;
  align-items: flex-start;
}

@media (max-width: 960px) {
  .upload-layout {
    grid-template-columns: 1fr;
  }
}

.upload-panel,
.doc-list-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* section titles */
.section-title {
  font-size: 1.15rem;
  font-weight: 600;
  color: #111827;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.section-subtitle {
  font-size: 0.9rem;
  color: #6b7280;
}

/* metadata grid */
.metadata-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
}

@media (max-width: 960px) {
  .metadata-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 640px) {
  .metadata-grid {
    grid-template-columns: 1fr;
  }
}

.button-row {
  margin-top: 0.25rem;
}

/* document list */
.doc-list {
  border-radius: 16px;
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: radial-gradient(circle at top left, #f8fafc 0, #ffffff 45%, #f9fafb 100%);
  padding: 0.9rem;
  max-height: 420px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.doc-item {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.65rem 0.7rem;
  border-radius: 12px;
  border: 1px solid transparent;
  background: rgba(248, 250, 252, 0.9);
  transition: all 0.15s ease;
}

.doc-item:hover {
  border-color: rgba(59, 130, 246, 0.45);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.08);
  transform: translateY(-1px);
  background: white;
}

.doc-main {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
  min-width: 0;
}

.doc-title-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.1rem;
}

.doc-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: #111827;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge-soft {
  background: rgba(59, 130, 246, 0.08);
  color: #1d4ed8;
  border-radius: 999px;
  padding: 0.08rem 0.55rem;
  font-size: 0.7rem;
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.doc-meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.doc-meta {
  font-size: 0.78rem;
  color: #4b5563;
}

.doc-meta-secondary {
  font-size: 0.75rem;
  color: #9ca3af;
}

.doc-actions {
  display: flex;
  align-items: center;
}

/* small button */
.btn-sm {
  padding: 0.25rem 0.55rem;
  font-size: 0.78rem;
}

/* empty state */
.empty-state {
  border-radius: 16px;
  border: 1px dashed rgba(148, 163, 184, 0.6);
  background: rgba(248, 250, 252, 0.85);
  padding: 1.25rem 1rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  align-items: center;
  justify-content: center;
}

.empty-icon {
  font-size: 1.8rem;
}

.empty-title {
  font-size: 0.98rem;
  font-weight: 600;
  color: #111827;
}

.empty-subtitle {
  font-size: 0.82rem;
  color: #6b7280;
  max-width: 22rem;
}

.msg {
  margin-bottom: 1rem;
  display: flex;
}

.msg-user {
  justify-content: flex-end;
}

.msg-assistant {
  justify-content: flex-start;
}

.bubble {
  max-width: 70%;
  padding: 0.75rem 1rem;
  border-radius: 1rem;
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.12);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.bubble-user {
  background: #4f46e5;
  color: #f9fafb;
  border-bottom-right-radius: 0.2rem;
}

.bubble-assistant {
  background: #ffffff;
  color: #111827;
  border-bottom-left-radius: 0.2rem;
}

.msg-role {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  opacity: 0.7;
  margin-bottom: 0.25rem;
}

.msg-content {
  font-size: 0.95rem;
  line-height: 1.6;
}

.msg-meta {
  margin-top: 0.4rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #ecfeff;
  color: #0369a1;
}

.sources {
  margin-top: 0.5rem;
  border-top: 1px solid #e5e7eb;
  padding-top: 0.4rem;
}

.sources-title {
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
  color: #6b7280;
}

.sources ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.sources li {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  padding: 0.15rem 0;
}

.source-title {
  color: #111827;
}

.source-score {
  color: #4b5563;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  display: none;
  /* ซ่อนเริ่มต้น */
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.35);
  z-index: 999;
}

.modal-overlay.open {
  display: flex;
  /* แสดงเมื่อมี class open */
}

.rag-image {
  max-width: 320px;
  border-radius: 10px;
  margin-top: 8px;
  display: block;
}

/* ⭐ NEW: Markdown Table Styles */
.msg-bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 0.9rem;
  background-color: #ffffff;
  /* พื้นหลังตารางขาว */
  border-radius: 8px;
  overflow: hidden;
  /* เพื่อให้ border-radius ทำงานกับหัวตาราง */
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.msg-bubble th,
.msg-bubble td {
  padding: 10px 14px;
  border-bottom: 1px solid #e5e7eb;
  text-align: left;
  vertical-align: top;
}

.msg-bubble th {
  background-color: #f3f4f6;
  /* สีเทาอ่อนสำหรับหัวตาราง */
  font-weight: 600;
  color: #1f2937;
  border-bottom: 2px solid #e5e7eb;
}

/* ลายสลับบรรทัด */
.msg-bubble tr:nth-child(even) {
  background-color: #f9fafb;
}

/* เส้นปิดท้าย */
.msg-bubble tr:last-child td {
  border-bottom: none;
}

/* Hover effect */
.msg-bubble tr:hover td {
  background-color: #eff6ff;
  /* สีฟ้าจางๆ เมื่อเอาเมาส์ชี้ */
}

/* แก้ไข List ใน Markdown ให้มี bullet point */
.msg-bubble ul,
.msg-bubble ol {
  padding-left: 1.5rem;
  margin: 0.5rem 0;
  list-style-position: outside;
}

.msg-bubble ul {
  list-style-type: disc;
}

.msg-bubble ol {
  list-style-type: decimal;
}

.msg-bubble li {
  margin-bottom: 0.25rem;
}

/* Code block style */
.msg-bubble pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
}

.msg-bubble code {
  font-family: monospace;
  background: rgba(0, 0, 0, 0.06);
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 0.85em;
}

.msg-bubble pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

/* ⭐ NEW: Insurance Card Design */
.ins-card {
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  overflow: hidden;
  background-color: #ffffff;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  font-family: 'Prompt', sans-serif;
  margin: 10px 0;
  max-width: 100%;
}

.ins-header {
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
  /* สีน้ำเงิน */
  color: #ffffff;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.ins-title {
  font-weight: 600;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ins-badge {
  background-color: rgba(255, 255, 255, 0.2);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
}

.ins-body {
  padding: 16px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  /* แบ่ง 2 คอลัมน์ */
  gap: 12px 16px;
}

/* บนมือถือให้เหลือ 1 คอลัมน์ */
@media (max-width: 480px) {
  .ins-body {
    grid-template-columns: 1fr;
  }
}

.ins-row {
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid #f1f5f9;
  padding-bottom: 8px;
}

.ins-row:last-child {
  border-bottom: none;
}

.ins-label {
  font-size: 0.75rem;
  color: #64748b;
  margin-bottom: 2px;
}

.ins-value {
  font-size: 0.95rem;
  color: #0f172a;
  font-weight: 500;
}

.ins-footer {
  background-color: #f8fafc;
  padding: 10px 16px;
  border-top: 1px solid #e2e8f0;
  font-size: 0.85rem;
  color: #475569;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.ins-highlight {
  color: #2563eb;
  font-weight: 600;
}

/* ⭐ Settings Panel & Chat Styles (Updated) ⭐ */
.chat-settings-toggle {
  position: fixed !important;
  top: 16px;
  right: 22px;
  z-index: 9000 !important;
  /* ต่ำกว่า Modal อื่นๆ เล็กน้อย */
  cursor: pointer;
  background: rgba(15, 1, 141, 0.8);
  padding: 8px 12px;
  border-radius: 20px;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid #e5e7eb;
  transition: all 0.2s ease;
  /* ⭐ แก้ไข: บังคับให้ตัวอักษรและไอคอนเฟืองเป็นสีดำ ⭐ */
  color: #ffffff !important;
}

.chat-settings-toggle:hover {
  background: rgba(80, 65, 218, 0.8);
  transform: translateY(-1px);
}

.settings-panel {
  position: fixed !important;
  top: 64px;
  right: 22px;
  z-index: 9001 !important;
  /* ตั้งค่า z-index ให้ชัดเจน */

  width: 380px !important;
  max-width: 92vw;

  /* ⭐ เปลี่ยนสีพื้นหลังเป็นสีขาวให้เหมือน Upload Modal ⭐ */
  background: #ffffff !important;
  padding: 20px 22px;
  border-radius: 18px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  border: 1px solid #e2e8f0;

  opacity: 0;
  pointer-events: none;
  transform: translateY(-10px);
  transition: all 0.25s ease;
}

/* ปรับ dropdown ให้เต็ม panel */
.settings-panel .select,
.settings-panel input,
.settings-panel textarea {
  width: 100% !important;
}

.settings-panel.open {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

/* Header ของ Settings ปรับให้เหมือน Upload Modal */
.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding: 0 0 12px 0;
  border-bottom: 1px solid #f1f5f9;
  background: transparent !important;
  /* ลบพื้นหลังเดิมออก */
}

.settings-close-btn {
  border: none;
  background: transparent;
  font-size: 1.6rem;
  cursor: pointer;
  color: #f8f9fa !important;
  transition: color 0.2s;
}

.settings-close-btn:hover {
  color: #ef4444;
}

.chat-container {
  margin-top: 60px;
  display: flex;
  flex-direction: column;
  height: 80vh;
  gap: 12px;
  position: relative;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 22px;
  background: #ffffff;
  border-radius: 18px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.06);
}

/* Force chat input row inline like Gemini */
.chat-input-row {
  display: flex !important;
  flex-direction: row !important;
  align-items: center !important;
  justify-content: space-between;
  gap: 10px;

  background: #ffffff;
  padding: 12px 14px;
  border-radius: 18px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);

  width: 100%;
}

/* Textarea must not break to new line */
.chat-input-row textarea {
  flex: 1 !important;
  width: 100% !important;

  min-height: 48px;
  max-height: 180px;

  resize: none;
  border-radius: 14px;
  padding: 14px 16px;
  font-size: 1rem;
  background: #f3f4f6;
}

/* Send button */
.chat-send-btn {
  height: 48px !important;
  width: 52px !important;
  min-width: 52px !important;

  display: flex;
  align-items: center;
  justify-content: center;

  border-radius: 14px;
  font-size: 1.25rem;
  flex-shrink: 0 !important;
}