* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: "Segoe UI", "Noto Sans Thai", sans-serif;
  background: #101418;
  color: #f0f2f5;
  font-size: 18px;
  /* ขยายจาก default ~16px เพื่อให้ผู้สูงอายุ/ผู้ป่วยหลังผ่าตัดอ่านง่าย */
  display: flex;
  flex-direction: column;
  overflow: auto;
  /* ปกติจะพอดี viewport ที่ >= 1920x1080 ไม่ scroll แต่ยอมให้ scroll ได้
     เผื่อพื้นที่จริงเหลือน้อยกว่านั้น (เช่น browser chrome/แถบที่อยู่กินพื้นที่ไป)
     กัน content ถูกตัดหายไปเฉยๆ */
}

header {
  flex: 0 0 auto;
  padding: 12px 24px;
  background: #161b22;
  border-bottom: 1px solid #2a2f37;
}

header h2 {
  margin: 0;
  font-size: 1.4rem;
}

footer {
  flex: 0 0 auto;
  padding: 12px 24px;
  background: #161b22;
  border-top: 1px solid #2a2f37;
}

.disclaimer {
  margin: 0;
  font-size: 0.75rem;
  color: #f5c451;
  background: rgba(245, 196, 81, 0.08);
  border: 1px solid rgba(245, 196, 81, 0.3);
  border-radius: 6px;
  padding: 8px 12px;
  line-height: 1.4;
}

main {
  flex: 1 1 auto;
  min-height: 0;
  /* จำเป็นให้ลูก flex ที่ scroll ภายในทำงานถูกต้อง */
  display: flex;
  gap: 16px;
  padding: 16px;
}

.camera-pane {
  flex: 1 1 55%;
  min-width: 0;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.output-canvas {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  background: #000;
  border-radius: 8px;
  border: 1px solid #2a2f37;
}

.debug-pane {
  flex: 0 1 auto;
  min-height: 0;
  overflow-y: auto;
  background: #1c222b;
  border: 1px solid #2a2f37;
  border-radius: 8px;
  padding: 12px;
  margin-top: 16px;
  font-family: "SF Mono", "Consolas", monospace;
}

.debug-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  color: #9aa4af;
  margin-bottom: 8px;
}

.debug-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-family: "Segoe UI", "Noto Sans Thai", sans-serif;
}

.debug-content.hidden {
  display: none;
}

.debug-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 0.85rem;
  border-bottom: 1px solid #2a2f37;
}

.debug-label {
  color: #9aa4af;
}

.debug-value {
  color: #4ade80;
  font-weight: 600;
}

#debugChart {
  width: 100%;
  height: 120px;
  margin-top: 10px;
  background: #101418;
  border-radius: 6px;
  border: 1px solid #2a2f37;
}

.threshold-controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #2a2f37;
}

.threshold-controls.hidden {
  display: none;
}

.threshold-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.threshold-field label {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: #9aa4af;
  font-family: "SF Mono", "Consolas", monospace;
}

.threshold-value {
  color: #4ade80;
  font-weight: 600;
}

.threshold-field input[type="range"] {
  width: 100%;
  accent-color: #3b82f6;
  cursor: pointer;
}

.threshold-reset-btn {
  padding: 6px 12px;
  border: 1px solid #2a2f37;
  border-radius: 6px;
  background: #1c222b;
  color: #9aa4af;
  cursor: pointer;
  font-size: 0.8rem;
  font-family: "Segoe UI", "Noto Sans Thai", sans-serif;
}

.threshold-reset-btn:hover {
  background: #2a2f37;
  color: #f0f2f5;
}

.content-pane {
  flex: 1 1 45%;
  min-width: 320px;
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.panel {
  flex: 0 0 auto;
}

.mode-switch {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.mode-switch button {
  flex: 1;
  padding: 16px 12px;
  border: 1px solid #2a2f37;
  background: #1c222b;
  color: #f0f2f5;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
  min-height: 36px;
}

.mode-switch button.active {
  background: #3b82f6;
  border-color: #3b82f6;
}

.panel.hidden {
  display: none;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-header h3 {
  margin: 0;
}

.panel-action-btn {
  flex: 0 0 auto;
  padding: 12px 20px;
  border: none;
  border-radius: 6px;
  background: #3b82f6;
  color: #fff;
  cursor: pointer;
  font-size: 0.8rem;
  min-height: 30px;
  white-space: nowrap;
}

.panel-action-btn:hover {
  background: #2563eb;
}

.guideline-card {
  background: rgba(59, 130, 246, 0.08);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 16px;
}

.guideline-item {
  margin: 0;
  font-size: 0.90rem;
  line-height: 1.6;
  color: #d7dce2;
}

.guideline-item+.guideline-item {
  margin-top: 6px;
}

.guideline-item strong {
  color: #93c5fd;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 16px 0;
}

.stat-card {
  background: #1c222b;
  border: 1px solid #2a2f37;
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 0.95rem;
  color: #9aa4af;
}

.stat-value {
  font-size: 2rem;
  font-weight: 600;
}

.controls {
  display: flex;
  gap: 8px;
}

.controls button {
  padding: 16px 20px;
  border: none;
  border-radius: 6px;
  background: #3b82f6;
  color: #fff;
  cursor: pointer;
  font-size: 1.1rem;
  min-height: 52px;
}

.controls button:hover {
  background: #2563eb;
}

.game-result {
  margin-top: 12px;
  font-size: 1.2rem;
  color: #4ade80;
}

/* ===== Responsive ===== */

/* จอเล็กกว่า desktop (แท็บเล็ต แนวตั้ง/แนวนอน): เรียง camera กับ content ซ้อนกันแทนเคียงข้าง
   และปลดล็อก overflow เพราะพื้นที่ไม่พอให้ทุกอย่างพอดีจอเดียวแบบ 1920x1080 อีกต่อไป */
@media (max-width: 1024px) {
  body {
    overflow: auto;
    height: auto;
    min-height: 100%;
  }

  main {
    flex-direction: column;
    padding: 12px;
  }

  .camera-pane {
    flex: 0 0 auto;
    height: auto;
  }

  .output-canvas {
    height: auto;
    aspect-ratio: 4 / 3;
  }

  .content-pane {
    height: auto;
    overflow-y: visible;
  }
}

/* จอมือถือ: ลดขนาดตัวอักษร/ปุ่ม และให้ stat-grid เรียงคอลัมน์เดียว */
@media (max-width: 600px) {
  body {
    font-size: 16px;
  }

  header {
    padding: 10px 16px;
  }

  header h2 {
    font-size: 1.3rem;
  }

  .mode-switch {
    flex-direction: column;
  }

  .stat-grid {
    grid-template-columns: 1fr;
  }

  .panel-header {
    flex-direction: column;
    align-items: stretch;
  }

  .panel-action-btn {
    width: 100%;
  }
}