/* =============================================
   TB Care Plus - Premium Medical UI v4.0
   Dark Theme + Glass Morphism + Rainbow Cards
   Robot FAB + SPA Navigation
   ============================================= */

/* === CSS VARIABLES === */
:root {
  --max-width: 1280px;
  --touch-min: 60px;    /* ใหญ่ขึ้น กดง่ายสำหรับผู้สูงอายุ */
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius-sm: 0.5rem;
  --radius-md: 0.75rem;
  --radius-lg: 1rem;
  --radius-xl: 1.5rem;
  --radius-pill: 9999px;
  --font-base: 1.4rem;    /* 22.4px - ผู้สูงอายุอ่านง่าย */
  --font-lg: 1.5625rem;   /* 25px */
  --font-xl: 1.875rem;    /* 30px */
}

/* === BASE RESET === */
*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Sarabun', -apple-system, BlinkMacSystemFont, sans-serif;
  overflow-x: hidden;
  background: #ffffff;
  color: #1e293b;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: var(--font-base);
  line-height: 1.75;
  position: relative;
}

body::before {
  display: none;
}

/* === CONTAINER === */
.container {
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
}

/* === GLASS MORPHISM === */
.glass-morphism {
  background: rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(25px);
  -webkit-backdrop-filter: blur(25px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

/* === SOLID COLOR BACKGROUNDS === */
.gradient-primary {
  background: #4f46e5;
  box-shadow: 0 8px 25px rgba(79, 70, 229, 0.35);
}
.gradient-healthcare {
  background: #3b82f6;
  box-shadow: 0 12px 35px rgba(59, 130, 246, 0.35);
}
.gradient-success {
  background: #059669;
  box-shadow: 0 8px 25px rgba(5, 150, 105, 0.35);
}
.gradient-warning {
  background: #d97706;
  box-shadow: 0 8px 25px rgba(217, 119, 6, 0.35);
}
.gradient-danger {
  background: #e11d48;
  box-shadow: 0 8px 25px rgba(225, 29, 72, 0.35);
}

/* === COLORFUL CARD (Clean Solid Border) === */
.colorful-card-light {
  border: 2px solid #e2e8f0;
  border-radius: var(--radius-xl);
  position: relative;
  overflow: visible;
  background-color: #ffffff;
  color: #1e293b;
  box-shadow: 0 15px 30px rgba(0,0,0,0.15), 0 0 0 1px rgba(203, 213, 225, 0.3);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
@media (hover: hover) {
  .colorful-card-light:hover {
    transform: translateY(-3px);
    box-shadow: 0 22px 40px rgba(0,0,0,0.18), 0 0 0 1px rgba(203, 213, 225, 0.3);
  }
}

/* Dark card: Subtle blue glow border instead of rainbow */
.colorful-card-dark {
  border: 1px solid rgba(99, 102, 241, 0.25);
  border-radius: var(--radius-xl);
  position: relative;
  overflow: visible;
  background-color: #0f172a;
  color: #e2e8f0;
  box-shadow: 0 15px 35px rgba(0,0,0,0.4), 0 0 20px rgba(99, 102, 241, 0.08);
}

/* === SPA SECTIONS === */
.app-section {
  display: none;
  min-height: 100vh;
  min-height: 100svh;
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
}

#riskHeatMap {
  height: 430px;
  width: 100%;
  border-radius: 18px;
  border: 1px solid rgba(103, 232, 249, 0.28);
  box-shadow: 0 22px 55px rgba(2, 6, 23, 0.35);
  margin-bottom: 0;
  background: #07111f;
  z-index: 1;
}

.leaflet-container {
  background: #0f172a !important;
}

.leaflet-tile-pane {
  filter: brightness(0.6) invert(1) contrast(3) hue-rotate(200deg) saturate(0.3);
}

@media (max-width: 640px) {
  #riskHeatMap {
    height: 360px;
    border-radius: 16px;
  }
}

.app-section.active {
  display: block;
  will-change: opacity, transform;
  animation: sectionFadeIn 0.28s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}
@keyframes sectionFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* === COUNTER ANIMATION === */
.counter {
  font-variant-numeric: tabular-nums;
  transition: all 0.3s ease;
}

/* === CHART CONTAINER === */
.chart-container {
  position: relative;
  width: 100%;
  height: 300px;
  transition: all 0.3s ease;
}
@media (max-width: 768px) { .chart-container { height: 250px; } }
@media (max-width: 480px) { .chart-container { height: 200px; } }

/* === BUTTON BASE - Elderly-friendly === */
.btn {
  transition: all 0.2s ease;
  cursor: pointer;
  border: none;
  outline: none;
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  font-size: var(--font-lg);
  font-weight: 700;
  letter-spacing: 0.01em;
  padding: 0.875rem 1.25rem;
}
@media (hover: hover) {
  .btn:hover { transform: translateY(-1px); }
}
.btn:active { transform: scale(0.97); }

/* === MODAL === */
.modal {
  display: none;
  position: fixed;
  /* ใช้ inset: 0 แทน top/left/right/bottom เพื่อรองรับ iOS keyboard resize */
  top: 0; left: 0; right: 0; bottom: 0;
  width: 100%; height: 100%;
  background: rgba(2, 6, 23, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.22s ease;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  overscroll-behavior: none;
  /* ไม่ใช้ overflow: hidden เพื่อให้ iOS scroll ทำงานภายใน modal ได้ */
  overflow: hidden;
}
.modal.active {
  display: flex;
  opacity: 1;
}
/* Prevent body scroll when modal is open — ไม่ใส่ touch-action: none
   เพราะทำให้ iOS keyboard ไม่ scroll input เข้าหน้าจอ */
body.modal-open {
  overflow: hidden !important;
  position: fixed;
  width: 100%;
  height: 100%;
  overscroll-behavior: none;
}
.modal .modal-content {
  transform: scale(0.97) translateY(12px);
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-height: 92vh;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}
.modal .modal-content > div:first-child {
  flex-shrink: 0;
}
.modal .modal-content > div:last-child {
  flex-shrink: 0;
}
.modal .modal-content .p-5,
.modal .modal-content > div:nth-child(2) {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
  /* ป้องกันการเลื่อนที่ทำให้เกิดความรู้สึกไม่มั่นคง */
  scroll-behavior: auto;
}
.modal.active .modal-content {
  transform: scale(1) translateY(0);
}
/* Mobile: bottom-sheet modal — ป้องกันขยับเมื่อ keyboard ขึ้น */
@media (max-width: 640px) {
  .modal {
    padding: 0;
    align-items: flex-end;
    /* ให้ modal ใช้ visual viewport height จริง ไม่ขยับเมื่อ keyboard ขึ้น */
    height: 100%;
    height: 100dvh;
  }
  .modal .modal-content {
    /* ใช้ svh (small viewport) ไม่รวม keyboard เพื่อไม่ให้ขยับ */
    max-height: 92svh;
    width: 100%;
    border-radius: 1.25rem 1.25rem 0 0;
    /* เมื่อ keyboard ขึ้น modal content หดตัวแทนการขยับ */
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
                max-height 0.2s ease;
  }
  .modal .modal-content.sm\:rounded-2xl {
    border-radius: 1.25rem 1.25rem 0 0 !important;
  }
}

/* === PRINT / A4 REPORT STYLES === */
@media print {
  body > *:not(#printReportArea) { display: none !important; }
  #printReportArea {
    display: block !important;
    position: static !important;
    width: 210mm;
    min-height: 297mm;
    padding: 15mm;
    font-family: 'Sarabun', sans-serif;
    font-size: 12pt;
    color: #000;
    background: #fff;
  }
  #printReportArea h1 { font-size: 16pt; font-weight: 700; text-align: center; margin-bottom: 4mm; }
  #printReportArea h2 { font-size: 13pt; font-weight: 700; border-bottom: 1px solid #333; padding-bottom: 2mm; margin: 4mm 0 2mm; }
  #printReportArea table { width: 100%; border-collapse: collapse; margin-bottom: 4mm; }
  #printReportArea table th, #printReportArea table td { border: 1px solid #999; padding: 2mm 3mm; font-size: 11pt; }
  #printReportArea table th { background: #f0f0f0; font-weight: 700; }
  #printReportArea .print-img { max-width: 80mm; max-height: 60mm; object-fit: cover; border: 1px solid #ccc; margin: 1mm; }
  @page { size: A4 portrait; margin: 0; }
}
#printReportArea { display: none; }

/* === FORM STYLES (White Card Context) - Elderly-friendly sizes === */
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 16px 20px;
  border-radius: 14px;
  font-size: var(--font-lg);
  font-weight: 600;
  transition: all 0.3s ease;
  background: #ffffff;
  border: 3px solid #94a3b8;
  color: #1e293b;
  font-family: inherit;
  min-height: var(--touch-min);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 6px rgba(37, 99, 235, 0.25);
  background: #fffbeb;
}
.field-invalid,
.field-invalid .form-input,
.form-input.field-invalid,
.form-select.field-invalid,
.form-textarea.field-invalid {
  border-color: #dc2626 !important;
  background: #fff1f2 !important;
  box-shadow: 0 0 0 4px rgba(220, 38, 38, 0.14) !important;
}
.field-error-msg {
  margin-top: 0.4rem;
  color: #dc2626;
  font-size: 0.85rem;
  font-weight: 800;
}
.modal .modal-content {
  scroll-behavior: smooth;
}
.modal button:disabled {
  opacity: 0.72;
  cursor: wait;
}
.form-input::placeholder, .form-textarea::placeholder {
  font-size: 1.125rem;
  color: #94a3b8;
}
.form-label {
  display: block;
  font-size: var(--font-lg);
  font-weight: 800;
  margin-bottom: 12px;
  color: #0f172a;
}

/* === PILL LABELS (Custom Radio/Checkbox) - Elderly-friendly sizes === */
.pill-label {
  border: 3px solid #94a3b8;
  padding: 0.9rem 1.125rem;
  border-radius: var(--radius-pill);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  text-align: center;
  font-size: 1.125rem;   /* 18px — readable in forms regardless of screen size */
  font-weight: 700;
  color: #334155;
  background-color: #f8fafc;
  user-select: none;
  -webkit-user-select: none;
  min-height: var(--touch-min);
  line-height: 1.4;
}
input[type="radio"]:checked + .pill-label,
input[type="checkbox"]:checked + .pill-label {
  color: #fff;
  font-weight: 700;
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
input[type="radio"].pill-radio:checked + .pill-label {
  background-color: #4f46e5;
  border-color: #4f46e5;
}
input[type="checkbox"].pill-checkbox:checked + .pill-label {
  background-color: #10B981;
  border-color: #10B981;
}
input[type="checkbox"].pill-symptom:checked + .pill-label {
  background-color: #DC2626;
  border-color: #DC2626;
}

/* === PATIENT SEARCHABLE DROPDOWN === */
.patient-search-drop {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: #fff;
  border: 1.5px solid #cbd5e1;
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.2);
  max-height: 250px;
  overflow-y: auto;
  z-index: 500;
  scrollbar-width: thin;
  scrollbar-color: rgba(99,102,241,0.3) transparent;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.patient-drop-item {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid #f1f5f9;
  transition: background 0.1s;
}
.patient-drop-item:last-child { border-bottom: none; }
.patient-drop-item:hover,
.patient-drop-item.focused { background: #eff6ff; }
.patient-drop-empty {
  padding: 14px;
  text-align: center;
  color: #94a3b8;
  font-size: 0.85rem;
}

/* === VISIT CHECKLIST SUB-ITEMS === */
.checklist-sub-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 7px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.82rem;
  color: #334155;
  line-height: 1.4;
  transition: background 0.15s;
}
.checklist-sub-item:hover { background: #f1f5f9; }
.checklist-sub-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  min-width: 18px;
  margin-top: 1px;
  accent-color: #10b981;
  cursor: pointer;
}
.checklist-sub-item input[type="checkbox"]:checked + span {
  color: #065f46;
  font-weight: 600;
  text-decoration: line-through;
  text-decoration-color: #10b981;
}
.checklist-group .pill-label {
  min-height: unset !important;
  padding: 0.45rem 0.85rem !important;
  font-size: 0.82rem !important;
}

/* === TOAST NOTIFICATIONS === */
.toast {
  position: fixed;
  top: 20px;
  right: 20px;
  background: white;
  border-radius: 14px;
  padding: 20px 24px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  transform: translateX(120%);
  transition: all 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
  z-index: 2001;
  display: flex;
  align-items: center;
  min-width: 300px;
  max-width: 420px;
  color: #1e293b;
  font-size: var(--font-lg);
  font-weight: 700;
}
.toast.show { transform: translateX(0); }
.toast-icon { margin-right: 14px; font-size: 24px; }
.toast.success { border-left: 5px solid #10b981; }
.toast.success .toast-icon { color: #059669; }
.toast.warning { border-left: 5px solid #f59e0b; }
.toast.warning .toast-icon { color: #d97706; }
.toast.error { border-left: 5px solid #ef4444; }
.toast.error .toast-icon { color: #dc2626; }
.toast.info { border-left: 5px solid #3b82f6; }
.toast.info .toast-icon { color: #2563eb; }

/* =============================================
   ROBOT CHARACTER (FAB)
   ============================================= */
@keyframes wave-animation {
  0%, 100% { transform: translateY(0) rotate(-10deg); }
  50% { transform: translateY(-10px) rotate(20deg); }
}
@keyframes dance-animation {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}
@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 5px #67e8f9, 0 0 10px rgba(14,165,233,0.65), inset 0 0 2px rgba(255,255,255,0.85); }
  50% { box-shadow: 0 0 9px #67e8f9, 0 0 18px rgba(14,165,233,0.95), inset 0 0 4px rgba(255,255,255,0.95); }
}
@keyframes aura-pulse {
  0%, 100% { opacity: 0.16; transform: scale(0.96); }
  50% { opacity: 0.36; transform: scale(1.04); }
}

#fabContainer {
  width: 80px;
  height: 150px;
  position: fixed;
  z-index: 50;
  bottom: 24px;
  right: 24px;
  transition: opacity 0.3s ease, transform 0.3s ease, right 0.3s ease, bottom 0.3s ease;
}
@media (max-width: 640px) {
  #fabContainer {
    width: 64px;
    height: 120px;
    bottom: 16px;
    right: 12px;
  }
}

#robotCharacter {
  width: 80px;
  height: 110px;
  position: relative;
  cursor: grab;
  animation: dance-animation 3s ease-in-out infinite;
  filter: drop-shadow(0 16px 22px rgba(15, 23, 42, 0.24));
}
@media (max-width: 640px) {
  #robotCharacter {
    width: 64px;
    height: 90px;
    transform: scale(0.85);
  }
}

.bot-aura {
  position: absolute;
  inset: -12px -10px 0;
  background-image:
    radial-gradient(circle at 50% 36%, rgba(34, 211, 238, 0.34) 0%, rgba(59, 130, 246, 0.18) 38%, rgba(165, 209, 255, 0) 68%);
  animation: aura-pulse 4s ease-in-out infinite;
  z-index: 0;
}
.bot-shadow {
  position: absolute;
  bottom: -3px;
  left: 50%;
  transform: translateX(-50%);
  width: 76%;
  height: 12px;
  background: rgba(15, 23, 42, 0.24);
  border-radius: 50%;
  filter: blur(6px);
  z-index: 1;
}
.bot-part {
  position: absolute;
  background:
    linear-gradient(145deg, rgba(255,255,255,0.98) 0%, #eef7ff 48%, #dbeafe 100%);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.95),
    inset 0 -5px 10px rgba(59,130,246,0.11),
    0 8px 18px rgba(15, 23, 42, 0.18);
  border: 2px solid rgba(14, 165, 233, 0.28);
}
.bot-head {
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  width: 54px;
  height: 50px;
  border-radius: 22px;
  z-index: 3;
}
.bot-screen {
  position: absolute;
  inset: 6px;
  background:
    radial-gradient(circle at 50% 24%, rgba(14,165,233,0.28), transparent 34%),
    linear-gradient(180deg, #020617 0%, #0f172a 100%);
  border: 1px solid rgba(125, 211, 252, 0.45);
  border-radius: 17px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow:
    inset 0 3px 8px rgba(0, 0, 0, 0.75),
    0 0 0 1px rgba(255,255,255,0.18);
}
.bot-eyes {
  display: flex;
  gap: 8px;
  position: relative;
  transform: translateY(-1px);
}
.bot-eyes .eye {
  width: 8px;
  height: 11px;
  background: linear-gradient(180deg, #e0f2fe 0%, #22d3ee 55%, #0284c7 100%);
  border-radius: 8px;
  animation: pulse-glow 2s ease-in-out infinite;
}
.bot-smile {
  position: absolute;
  bottom: 7px;
  left: 50%;
  transform: translateX(-50%);
  width: 16px;
  height: 8px;
  border: 2px solid #67e8f9;
  border-top-color: transparent;
  border-radius: 0 0 8px 8px;
  box-shadow: 0 0 8px rgba(34, 211, 238, 0.85);
}
.bot-antenna {
  position: absolute;
  top: 3px;
  width: 5px;
  height: 10px;
  z-index: 2;
}
.bot-antenna.left { left: 5px; transform: rotate(-15deg); }
.bot-antenna.right { right: 5px; transform: rotate(15deg); }
.antenna-stick {
  width: 2px;
  height: 7px;
  background: #0ea5e9;
  border-radius: 2px;
  position: absolute;
  bottom: 0;
  left: 1px;
}
.antenna-tip {
  width: 7px;
  height: 7px;
  background: #67e8f9;
  border-radius: 50%;
  position: absolute;
  top: -2px;
  left: -1px;
  box-shadow: 0 0 10px rgba(34, 211, 238, 0.9);
}
.bot-body {
  top: 48px;
  left: 50%;
  transform: translateX(-50%);
  width: 48px;
  height: 44px;
  border-radius: 14px 14px 18px 18px;
  z-index: 2;
}
.chest-panel {
  width: 76%;
  height: 15px;
  background: linear-gradient(180deg, #0f172a 0%, #172554 100%);
  border: 1px solid rgba(125, 211, 252, 0.35);
  border-radius: 6px;
  margin: 6px auto 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.5rem;
  letter-spacing: 0;
  color: #cffafe;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.75), 0 0 8px rgba(56,189,248,0.55);
  text-shadow: 0 0 6px rgba(125, 211, 252, 0.95);
}
.bot-arm {
  top: 56px;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  z-index: 1;
}
.bot-arm.left { left: -1px; }
.bot-arm.right { right: -1px; }
.bot-ai-badge {
  position: absolute;
  left: 50%;
  bottom: 9px;
  transform: translateX(-50%);
  z-index: 4;
  min-width: 28px;
  height: 18px;
  padding: 0 7px;
  border-radius: 999px;
  border: 1px solid rgba(14, 165, 233, 0.35);
  background: linear-gradient(180deg, #ffffff 0%, #e0f2fe 100%);
  color: #0369a1;
  font-size: 0.63rem;
  line-height: 17px;
  font-weight: 900;
  text-align: center;
  box-shadow: 0 4px 10px rgba(14, 165, 233, 0.28);
}
#robotCharacter:hover .bot-part {
  border-color: rgba(6, 182, 212, 0.48);
}
#robotCharacter:hover .bot-aura {
  opacity: 0.5;
}
.is-waving { animation: wave-animation 1.5s ease-in-out infinite !important; }

/* === CHATBOT === */
#chatBot {
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.15);
}
#chatMessages {
  scrollbar-width: thin;
  scrollbar-color: #6366f1 #f1f5f9;
}
#chatMessages::-webkit-scrollbar { width: 6px; }
#chatMessages::-webkit-scrollbar-track { background: #f1f5f9; }
#chatMessages::-webkit-scrollbar-thumb { background-color: #6366f1; border-radius: 3px; }

/* === STATUS BADGES === */
.status-badge {
  padding: 0.375rem 0.875rem;
  border-radius: var(--radius-pill);
  font-weight: 700;
  font-size: 1rem;
  display: inline-block;
}

/* === PATIENT TABLE === */
.patient-row { cursor: pointer; transition: background 0.15s ease, transform 0.15s ease; }
@media (hover: hover) {
  .patient-row:hover { background: rgba(59, 130, 246, 0.07); }
}

.patient-name-link {
  cursor: pointer;
  color: #3b82f6;
  font-weight: 600;
  transition: color 0.2s;
}
.patient-name-link:hover {
  text-decoration: underline;
  color: #2563eb;
}

/* === TAMBON BAR CHART (CSS) === */
.tambon-row {
  display: grid;
  grid-template-columns: 100px 1fr;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.tambon-label {
  font-size: 0.95rem;
  font-weight: 700;
  color: #94a3b8;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tambon-bar-bg {
  width: 100%;
  background: rgba(71, 85, 105, 0.5);
  border-radius: var(--radius-pill);
  height: 1.75rem;
  overflow: hidden;
}
.tambon-bar-fill {
  height: 100%;
  border-radius: var(--radius-pill);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 0.5rem;
  color: #fff;
  font-size: 0.9rem;
  font-weight: 700;
  background: #4f46e5;
  transition: width 0.8s cubic-bezier(0.22, 0.61, 0.36, 1);
  min-width: 2rem;
}

/* === AI RISK MAP === */
.risk-dashboard-bg {
  background-color: #0f172a; /* slate-900 */
  background-image: 
    radial-gradient(ellipse at top right, rgba(30, 58, 138, 0.3), transparent 50%), /* blue-900 */
    radial-gradient(ellipse at bottom left, rgba(15, 118, 110, 0.2), transparent 50%), /* teal-700 */
    repeating-linear-gradient(90deg, rgba(255,255,255,0.015) 0 1px, transparent 1px 40px),
    repeating-linear-gradient(0deg, rgba(255,255,255,0.015) 0 1px, transparent 1px 40px);
}
.risk-dashboard-bg::before {
  display: none;
}
.risk-page-header {
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 1.25rem;
  background: rgba(30, 41, 59, 0.7); /* slate-800 */
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.risk-command-panel {
  background: rgba(15, 23, 42, 0.85); /* slate-900 */
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 1.25rem;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 10px 25px rgba(0,0,0,0.15);
}
.risk-map-shell {
  position: relative;
  margin-bottom: 1rem;
}
.risk-map-legend {
  position: absolute;
  left: 12px;
  bottom: 12px;
  z-index: 450;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  max-width: calc(100% - 24px);
}
.risk-map-legend span {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 30px;
  padding: 0.35rem 0.65rem;
  border-radius: 0.75rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(8px);
  color: #f8fafc;
  font-size: 0.75rem;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.risk-insight {
  min-height: 80px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 1rem;
  padding: 1rem;
  background: rgba(30, 41, 59, 0.6);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  transition: transform 0.2s, box-shadow 0.2s;
}
.risk-insight:hover {
  transform: translateY(-2px);
  background: rgba(30, 41, 59, 0.8);
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
}
.risk-insight span,
.risk-insight small {
  display: block;
  color: #94a3b8;
  font-size: 0.8rem;
  font-weight: 600;
}
.risk-insight b {
  display: block;
  color: #f8fafc;
  font-size: 1.5rem;
  line-height: 1.2;
  font-weight: 800;
  margin: 0.25rem 0;
}
.visit-pin-marker {
  background: transparent;
  border: 0;
}
.visit-pin-marker span {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: #3b82f6; /* blue-500 */
  border: 2px solid #fff;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
  transition: transform 0.2s;
}
.visit-pin-marker:hover span {
  transform: rotate(-45deg) scale(1.1);
  background: #2563eb; /* blue-600 */
}
.visit-pin-marker i {
  transform: rotate(45deg);
  font-size: 0.9rem;
}
.risk-map-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1.25rem;
}
.risk-tile {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 1.25rem;
  padding: 1.25rem;
  background: rgba(30, 41, 59, 0.5);
  backdrop-filter: blur(12px);
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
  transition: transform 0.2s, box-shadow 0.2s, background 0.2s;
}
.risk-tile:hover {
  transform: translateY(-4px);
  background: rgba(30, 41, 59, 0.8);
  box-shadow: 0 12px 30px rgba(0,0,0,0.25);
  border-color: rgba(255,255,255,0.15);
}
.risk-tile::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: var(--risk-color);
  border-radius: 4px 0 0 4px;
}
.risk-tile::after {
  content: '';
  position: absolute;
  inset: -10% -20% -10% auto;
  width: 8rem;
  height: 100%;
  border-radius: 9999px;
  background: var(--risk-color);
  opacity: calc(var(--risk-alpha) * 0.15);
  filter: blur(24px);
  pointer-events: none;
}

/* === HOMEPAGE HERO === */
.hero-bg {
  background: linear-gradient(135deg, #0f172a 0%, #1e3a5f 25%, #1e293b 50%, #312e81 75%, #1e293b 100%);
  position: relative;
  overflow: hidden;
}
.hero-bg::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 20% 80%, rgba(59, 130, 246, 0.2) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(139, 92, 246, 0.2) 0%, transparent 50%);
  animation: backgroundFloat 15s ease-in-out infinite;
}

/* === WHITE HERO (หน้าหลักสีขาว) === */
.hero-white {
  background: #ffffff;
  position: relative;
  overflow: hidden;
}
.hero-white::before {
  display: none;
}
.hero-white,
.hero-shell,
.hero-copy {
  width: min(100%, 100vw);
  max-width: 100vw;
  min-width: 0;
}
.hero-white .grid {
  width: 100%;
  max-width: calc(100vw - 2rem);
  min-width: 0;
}
.hero-white .hero-subtitle {
  display: block;
  width: min(100%, calc(100vw - 2rem));
  max-width: calc(100vw - 2rem);
  overflow-wrap: anywhere;
  word-break: normal;
  line-height: 1.45;
}

/* === BOTTOM NAVIGATION BAR === */
#bottomNavBar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 45;
  background: #ffffff;
  border-top: 2px solid #e2e8f0;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.10), 0 -1px 0 rgba(0,0,0,0.05);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.bnav-btn {
  background: none;
  border: none;
  cursor: pointer;
  min-height: 62px;
  padding: 10px 4px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
  font-family: 'Sarabun', sans-serif;
  flex: 1;
}
.bnav-btn:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: -3px;
  background: #eff6ff;
}
.bnav-btn:active { background: #f1f5f9; }
.bnav-btn.bnav-active { background: #eff6ff; }
.bnav-btn i { font-size: 1.5rem; opacity: 0.85; transition: transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.15s, color 0.15s; }
.bnav-btn span { font-size: 0.75rem; font-weight: 700; color: #475569; line-height: 1; transition: color 0.15s; }
.bnav-btn.bnav-active i { opacity: 1; transform: translateY(-2px) scale(1.18); }
.bnav-btn.bnav-active span { color: #2563eb; }

/* Adjust FAB + content when bottom nav visible */
body.has-bottom-nav #fabContainer {
  bottom: calc(72px + env(safe-area-inset-bottom, 0px) + 16px) !important;
}
body.has-bottom-nav {
  padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
}
@media (max-width: 640px) {
  body.has-bottom-nav #fabContainer {
    right: 0 !important;
    bottom: calc(68px + env(safe-area-inset-bottom, 0px)) !important;
    transform: scale(0.62);
    transform-origin: bottom right;
  }
  body.has-bottom-nav #chatBot {
    right: 0.5rem !important;
    bottom: calc(140px + env(safe-area-inset-bottom, 0px)) !important;
  }
}

/* Animated blobs */
.blob {
  display: none;
}
@keyframes blob-float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(30px, -50px) scale(1.1); }
  66% { transform: translate(-20px, 20px) scale(0.9); }
}

/* === SKELETON LOADING === */
.skeleton {
  background: linear-gradient(90deg, rgba(71,85,105,0.3) 25%, rgba(100,116,139,0.3) 50%, rgba(71,85,105,0.3) 75%);
  background-size: 200% 100%;
  animation: skeleton-pulse 1.8s ease-in-out infinite;
  border-radius: var(--radius-md);
}
@keyframes skeleton-pulse {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* === MODAL FORM BODY SCROLLBAR === */
.modal-form-body {
  scrollbar-width: thin;
  scrollbar-color: #6366f1 #f1f5f9;
}
.modal-form-body::-webkit-scrollbar { width: 6px; }
.modal-form-body::-webkit-scrollbar-track { background: #f1f5f9; }
.modal-form-body::-webkit-scrollbar-thumb { background-color: #6366f1; border-radius: 3px; }

/* === FADE IN UP === */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(15px); }
  to { opacity: 1; transform: translateY(0); }
}
.fade-in-up {
  animation: fadeInUp 0.5s ease-out forwards;
}

/* === STAGGER CARD ENTRANCE === */
@keyframes cardSlideIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.card-enter {
  animation: cardSlideIn 0.22s cubic-bezier(0.22, 1, 0.36, 1) both;
}

/* === MOBILE PATIENT CARD HOVER === */
@media (hover: hover) {
  #userContent .space-y-3 > div {
    transition: transform 0.18s ease, box-shadow 0.18s ease;
  }
  #userContent .space-y-3 > div:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.1);
  }
}

/* === RESPONSIVE === */

/* Mobile: up to 639px */
@media (max-width: 639px) {
  :root {
    --font-base: 1.25rem;   /* 20px on mobile */
    --font-lg: 1.375rem;    /* 22px */
    --font-xl: 1.625rem;    /* 26px */
    --touch-min: 58px;
  }

  .modal {
    padding: 0;
    align-items: flex-end;
    /* ใช้ dvh เพื่อให้ modal ปรับขนาดตาม visual viewport เมื่อ keyboard ขึ้น */
    height: 100dvh;
  }
  .modal .modal-content {
    /* svh = small viewport height ไม่รวม browser chrome/keyboard
       ทำให้ modal ไม่กระโดดเมื่อ keyboard ขึ้น */
    max-height: 96svh;
    max-height: 96dvh;
    max-width: 100vw;
    width: 100%;
    margin: 0;
    border-radius: 1.25rem 1.25rem 0 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  .modal .modal-content > .sticky {
    position: relative;
    flex-shrink: 0;
  }
  .modal .modal-content .p-5 {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    min-height: 0;
    /* padding bottom เผื่อ safe area (notch phones) */
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  }
  .modal .modal-content .modal-form-body {
    max-height: none;
    overflow-y: visible;
  }

  .tambon-row { grid-template-columns: 72px 1fr; }
  .tambon-label { font-size: 0.85rem; }
  .tambon-bar-fill { font-size: 0.8rem; }
  .tambon-bar-bg, .tambon-bar-fill { height: 1.5rem; }

  /* Chatbot responsive */
  #chatBot {
    width: calc(100vw - 2rem) !important;
    max-width: 20rem !important;
    right: 0 !important;
    bottom: 160px !important;
  }

  /* Fix form inputs - keep large for elderly users */
  .form-input, .form-select, .form-textarea {
    font-size: 20px !important; /* Prevents iOS zoom + readable for elderly */
    padding: 16px 18px;
    border-width: 3px;
  }
  .form-label {
    font-size: 1.125rem !important;
  }
  .pill-label {
    font-size: 1rem !important;
    padding: 0.75rem 1rem;
  }

  /* Facility dropdown mobile fix */
  #facilityDropdown {
    width: calc(100% - 1rem) !important;
    max-width: 100% !important;
    left: 0.5rem;
  }

  /* Fix stat cards text - keep readable for elderly */
  .counter {
    font-size: 2rem !important;
  }

  /* Ensure nothing overflows horizontally */
  .container, .max-w-7xl {
    max-width: 100vw;
    overflow-x: hidden;
  }
  .hero-white {
    width: 100vw;
    max-width: 100vw;
  }
  .hero-shell {
    width: 100vw !important;
    max-width: 100vw !important;
  }
  .hero-copy {
    width: 100%;
    max-width: 100%;
  }
  .hero-white .hero-subtitle {
    padding-left: 0.25rem;
    padding-right: 0.25rem;
    word-break: break-all;
  }
  .hero-white .max-w-7xl {
    max-width: 100%;
  }
  .hero-white .grid {
    min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .hero-white .grid > * {
    min-width: 0;
  }

  /* Personal dashboard header - compact for mobile */
  .gradient-healthcare .container {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }
  .gradient-healthcare h1 {
    font-size: 1.125rem !important;
  }
  .gradient-healthcare #userInfo {
    font-size: 0.75rem !important;
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Filter row - stacked on mobile */
  #fiscalYearFilter, #statusFilter {
    min-height: 40px;
    font-size: 0.875rem !important;
  }

  /* Action buttons stacked full width */
  .btn {
    font-size: 1.125rem;
    padding: 0.875rem 1rem;
  }

  /* Fix patient cards from overflowing */
  .colorful-card-light {
    word-break: break-word;
    overflow-wrap: break-word;
  }
}

/* Small phones: up to 374px (iPhone SE etc.) */
@media (max-width: 374px) {
  :root {
    --font-base: 1.125rem;   /* 18px */
    --font-lg: 1.25rem;      /* 20px */
    --font-xl: 1.5rem;       /* 24px */
    --touch-min: 54px;
  }
  .counter {
    font-size: 1.75rem !important;
  }
  #chatBot {
    max-width: 17rem !important;
  }
  .modal .modal-content {
    max-width: 100vw;
    border-radius: 0;
  }
  .form-input, .form-select, .form-textarea {
    font-size: 18px !important;
    padding: 14px 16px;
  }
}

/* Tablet: 640px - 1023px */
@media (min-width: 640px) and (max-width: 1023px) {
  .tambon-row { grid-template-columns: 90px 1fr; }
}

/* Desktop: 1024px+ */
@media (min-width: 1024px) {
  .tambon-row { grid-template-columns: 120px 1fr; }
}

/* === UTILITY === */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  body { padding-bottom: var(--safe-bottom); }
  footer { padding-bottom: calc(1rem + var(--safe-bottom)); }
  #fabContainer { bottom: calc(24px + var(--safe-bottom)); }
  .modal { padding-bottom: var(--safe-bottom); }
}

/* Print */
@media print {
  header, footer, #fabContainer, .modal, .toast { display: none !important; }
  body { background: #fff; color: #000; }
  .colorful-card-light, .colorful-card-dark { box-shadow: none; border: 1px solid #ccc; }
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* Custom scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: #1e293b; }
::-webkit-scrollbar-thumb { background: #475569; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #64748b; }

/* Focus visible */
:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
  border-radius: 2px;
}
