:root {
  --primary: #2fa36b;
  --primary-mid: #1f8f63;
  --primary-dark: #10704e;
  --accent: #e08a1e;
  --green: #2e9e66;
  --red: #d9534f;
  --blue: #4a90d9;
  --bg: #f2f8f4;
  --card-bg: #ffffff;
  --border: #dde9e1;
  --text: #24312b;
  --muted: #7d8c84;
  --radius: 14px;
  --grad: linear-gradient(135deg, #3fbc82 0%, var(--primary) 45%, var(--primary-dark) 100%);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", "Noto Sans Thai", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--grad);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: var(--radius);
  padding: 28px 24px;
  width: 100%;
  max-width: 320px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}

.login-brand { font-size: 1.4rem; font-weight: 800; margin-bottom: 4px; }
.login-card input { text-align: center; font-size: 1.2rem; letter-spacing: 4px; margin: 14px 0 10px; }
.login-card .btn.full { margin-top: 4px; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: var(--grad);
  color: white;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { font-weight: 700; font-size: 1.1rem; margin-right: auto; }

.profile-select {
  padding: 6px 10px;
  border-radius: 8px;
  border: none;
  font-size: 0.9rem;
}

.icon-btn {
  background: rgba(255,255,255,0.25);
  border: none;
  color: white;
  width: 32px; height: 32px;
  border-radius: 50%;
  font-size: 1.1rem;
  cursor: pointer;
}

.tabs {
  display: flex;
  overflow-x: auto;
  background: white;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 58px;
  z-index: 9;
}

.tab-btn {
  flex: 1 0 auto;
  padding: 12px 10px;
  border: none;
  background: transparent;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  border-bottom: 3px solid transparent;
}

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

main { padding: 14px; padding-bottom: 40px; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 1px 3px rgba(20, 20, 40, 0.06);
}

.card h3 { margin: 0 0 10px 0; font-size: 1rem; }

.latest-card { text-align: center; }
.latest-reading { font-size: 2.4rem; font-weight: 800; color: var(--accent); }
.latest-unit { font-size: 1rem; color: var(--muted); }
.latest-badge {
  display: inline-block;
  margin-top: 8px;
  padding: 6px 16px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 0.9rem;
}
.badge-normal { background: #e5f6ec; color: var(--green); }
.badge-elevated { background: #fff3d6; color: #b8790a; }
.badge-high1 { background: #ffe4d6; color: #c25a1e; }
.badge-high2, .badge-crisis { background: #fde0e0; color: var(--red); }
.badge-low { background: #e3ecfb; color: var(--blue); }

.muted { color: var(--muted); }
.small { font-size: 0.8rem; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}

.stat-box {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 12px;
  text-align: center;
  box-shadow: 0 1px 3px rgba(20,20,40,0.06);
}

.stat-box .val { font-size: 1.3rem; font-weight: 700; }
.stat-box .lbl { font-size: 0.75rem; color: var(--muted); }

.log-mode-switch {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
}

.mode-btn {
  flex: 1;
  padding: 8px 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #f8f9fc;
  font-size: 0.8rem;
  cursor: pointer;
}

.mode-btn.active {
  background: var(--grad);
  color: white;
  border-color: var(--primary-dark);
}

.log-mode-panel { margin-bottom: 12px; }

#cameraVideo {
  width: 100%;
  border-radius: 10px;
  background: #000;
  max-height: 320px;
  object-fit: cover;
}

#photoPreviewWrap { margin-bottom: 12px; }
#photoPreview {
  width: 100%;
  max-height: 260px;
  object-fit: contain;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #fafafa;
}

.row { display: flex; align-items: flex-end; }
.row.gap { gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; }

label {
  display: flex;
  flex-direction: column;
  font-size: 0.78rem;
  color: var(--muted);
  gap: 4px;
  margin-bottom: 10px;
  flex: 1;
}

input, select {
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 0.95rem;
  color: var(--text);
  width: 100%;
}

.confirm-form {
  border-top: 1px dashed var(--border);
  padding-top: 12px;
}

.confirm-title { font-weight: 600; margin: 0 0 10px 0; font-size: 0.9rem; }

.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #f8f9fc;
  cursor: pointer;
  font-size: 0.9rem;
}

.btn.primary {
  background: var(--grad);
  border-color: var(--primary-dark);
  color: white;
  font-weight: 600;
}

.btn.full { width: 100%; padding: 12px; font-size: 1rem; margin-top: 4px; }

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  gap: 10px;
}

.history-item:last-child { border-bottom: none; }

.history-thumb {
  width: 44px; height: 44px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: #eee;
}

.history-main { flex: 1; }
.history-vals { font-weight: 700; font-size: 1.05rem; }
.history-time { font-size: 0.75rem; color: var(--muted); }
.history-cat { font-size: 0.75rem; font-weight: 600; }

.history-actions button {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
  color: var(--muted);
}

.appt-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.appt-item.done { opacity: 0.5; text-decoration: line-through; }
.appt-item:last-child { border-bottom: none; }

.profile-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.profile-row:last-child { border-bottom: none; }

.cat-normal { color: var(--green); }
.cat-elevated { color: #b8790a; }
.cat-high1 { color: #c25a1e; }
.cat-high2, .cat-crisis { color: var(--red); }
.cat-low { color: var(--blue); }

/* medication expiry risk tiers */
.med-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.med-stat-box {
  border-radius: 12px;
  padding: 10px;
  text-align: center;
}
.med-stat-box .val { font-size: 1.3rem; font-weight: 800; }
.med-stat-box .lbl { font-size: 0.72rem; }

.med-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 0.78rem;
}

.med-expired-bg { background: #fde0e0; }
.med-urgent-bg { background: #ffe4d6; }
.med-near-bg { background: #fff3d6; }
.med-watch-bg { background: #fff9db; }
.med-normal-bg { background: #e5f6ec; }

.med-expired, .badge-med-expired { color: var(--red); }
.med-urgent, .badge-med-urgent { color: #c25a1e; }
.med-near, .badge-med-near { color: #b8790a; }
.med-watch, .badge-med-watch { color: #a68b1f; }
.med-normal, .badge-med-normal { color: var(--green); }
.med-unknown, .badge-med-unknown { color: var(--muted); }

.badge-med-expired { background: #fde0e0; }
.badge-med-urgent { background: #ffe4d6; }
.badge-med-near { background: #fff3d6; }
.badge-med-watch { background: #fff9db; }
.badge-med-normal { background: #e5f6ec; }
.badge-med-unknown { background: #eee; }

.med-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.med-item:last-child { border-bottom: none; }
.med-main { flex: 1; }
.med-title { font-weight: 700; }
.med-sub { font-size: 0.78rem; color: var(--muted); }

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .row.gap { flex-wrap: wrap; }
}
