/* =====================================================================
   RESET & TOKENS
   ===================================================================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue:       #1e40af;
  --blue-lt:    #dbeafe;
  --blue-dk:    #1e3a8a;
  --green:      #059669;
  --red:        #dc2626;
  --yellow-lt:  #fefce8;
  --yellow-bd:  #fde047;
  --yellow-dk:  #854d0e;
  --gray-50:    #f9fafb;
  --gray-100:   #f3f4f6;
  --gray-200:   #e5e7eb;
  --gray-300:   #d1d5db;
  --gray-400:   #9ca3af;
  --gray-600:   #4b5563;
  --gray-800:   #1f2937;
  --white:      #ffffff;
  --radius:     12px;
  --shadow:     0 1px 3px rgba(0,0,0,.10), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md:  0 4px 6px -1px rgba(0,0,0,.10), 0 2px 4px -1px rgba(0,0,0,.06);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  background: var(--gray-50);
  color: var(--gray-800);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#app { min-height: 100vh; max-width: 480px; margin: 0 auto; }

/* =====================================================================
   LOADING
   ===================================================================== */
.loading-screen {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; min-height: 100vh; gap: 16px; color: var(--gray-400);
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--gray-200); border-top-color: var(--blue);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-spinner { text-align: center; padding: 48px 20px; color: var(--gray-400); }

/* =====================================================================
   AUTH
   ===================================================================== */
.auth-screen {
  min-height: 100vh; display: flex; align-items: center;
  justify-content: center; padding: 24px 16px;
  background: linear-gradient(135deg, var(--blue-dk) 0%, var(--blue) 100%);
}
.auth-card {
  background: var(--white); border-radius: 20px;
  padding: 32px 24px; width: 100%; max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.auth-header { text-align: center; margin-bottom: 28px; }
.auth-icon   { font-size: 40px; margin-bottom: 12px; }
.auth-header h1 { font-size: 20px; font-weight: 700; line-height: 1.3; }
.subtitle { font-size: 14px; color: var(--gray-400); margin-top: 4px; }

.tab-toggle {
  display: flex; background: var(--gray-100);
  border-radius: 8px; padding: 3px; margin-bottom: 24px;
}
.tab-btn {
  flex: 1; padding: 9px; border: none; background: none;
  border-radius: 6px; font-size: 14px; font-weight: 500;
  cursor: pointer; color: var(--gray-600); transition: all .2s;
}
.tab-btn.active {
  background: var(--white); color: var(--blue);
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
}

/* =====================================================================
   SETUP
   ===================================================================== */
.setup-screen {
  min-height: 100vh; display: flex; align-items: center;
  justify-content: center; padding: 24px 16px; background: var(--gray-50);
}
.setup-card {
  background: var(--white); border-radius: var(--radius);
  padding: 32px 24px; width: 100%; max-width: 400px; box-shadow: var(--shadow-md);
}
.setup-card h2 { font-size: 22px; margin-bottom: 8px; }
.setup-card p  { color: var(--gray-600); margin-bottom: 24px; }

/* =====================================================================
   FORM ELEMENTS
   ===================================================================== */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block; font-size: 12px; font-weight: 700; color: var(--gray-600);
  margin-bottom: 6px; text-transform: uppercase; letter-spacing: .5px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="datetime-local"],
select,
textarea {
  width: 100%; padding: 12px 14px;
  border: 1.5px solid var(--gray-200); border-radius: 8px;
  font-size: 16px; font-family: inherit; color: var(--gray-800);
  background: var(--white); transition: border-color .2s;
  -webkit-appearance: none;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(30,64,175,.1);
}
textarea { resize: vertical; }

.mrn-input-row { display: flex; gap: 8px; }
.mrn-input-row input { flex: 1; }

/* =====================================================================
   BUTTONS
   ===================================================================== */
.btn-primary {
  display: block; width: 100%; padding: 14px;
  background: var(--blue); color: var(--white);
  border: none; border-radius: 10px;
  font-size: 16px; font-weight: 600; cursor: pointer;
  transition: background .2s, opacity .2s; margin-top: 8px;
}
.btn-primary:hover:not(:disabled) { background: var(--blue-dk); }
.btn-primary:disabled { opacity: .45; cursor: not-allowed; }

.btn-secondary {
  padding: 12px 16px; background: var(--gray-100); color: var(--gray-800);
  border: none; border-radius: 8px; font-size: 15px;
  font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn-link {
  display: block; width: 100%; padding: 10px; background: none;
  border: none; color: var(--blue); font-size: 14px; cursor: pointer; margin-top: 4px;
}
.icon-btn {
  background: none; border: none; font-size: 20px;
  cursor: pointer; padding: 6px 10px; color: var(--gray-600);
}

/* =====================================================================
   APP SHELL
   ===================================================================== */
.app-screen { min-height: 100vh; display: flex; flex-direction: column; background: var(--gray-50); }

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px; background: var(--white);
  border-bottom: 1px solid var(--gray-200);
  position: sticky; top: 0; z-index: 20;
}
.app-header h2 { font-size: 18px; font-weight: 600; }
.back-btn {
  background: none; border: none; font-size: 16px;
  color: var(--blue); cursor: pointer; font-weight: 500; padding: 4px 0;
}
.header-info { display: flex; flex-direction: column; }
.header-name { font-weight: 600; font-size: 15px; }
.header-pgy  { font-size: 12px; color: var(--gray-400); }

/* =====================================================================
   HOME
   ===================================================================== */
.home-main {
  flex: 1; padding: 24px 20px;
  display: flex; flex-direction: column; gap: 20px;
}
.stats-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat-card {
  background: var(--white); border-radius: var(--radius);
  padding: 20px; text-align: center; box-shadow: var(--shadow-md);
}
.stat-num   { font-size: 42px; font-weight: 800; color: var(--blue); line-height: 1; }
.stat-label { font-size: 13px; color: var(--gray-400); margin-top: 6px; font-weight: 500; }

.log-btn {
  width: 100%; padding: 24px; background: var(--blue); color: var(--white);
  border: none; border-radius: 16px; font-size: 20px; font-weight: 700; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  box-shadow: 0 8px 20px rgba(30,64,175,.35);
  transition: transform .12s, box-shadow .12s;
}
.log-btn:active { transform: scale(.97); box-shadow: 0 4px 10px rgba(30,64,175,.25); }
.log-btn-icon { font-size: 28px; font-weight: 300; }

.quick-actions { display: flex; gap: 10px; }
.action-btn {
  flex: 1; padding: 14px; background: var(--white);
  border: 1.5px solid var(--gray-200); border-radius: var(--radius);
  font-size: 14px; font-weight: 500; cursor: pointer; text-align: center;
}
.action-btn:active { background: var(--gray-100); }

/* =====================================================================
   SCAN
   ===================================================================== */
.scan-main { flex: 1; padding: 20px; }
.scan-hint { text-align: center; color: var(--gray-600); margin-bottom: 16px; font-size: 14px; }

.scanner-container {
  width: 100%; border-radius: var(--radius);
  overflow: hidden; background: #000; margin-bottom: 16px;
  min-height: 200px;
}
/* Hide html5-qrcode's own UI chrome — we handle navigation ourselves */
#barcode-reader { width: 100% !important; }
#barcode-reader video { width: 100% !important; border-radius: 0; }
#barcode-reader button,
#barcode-reader select,
#barcode-reader img[alt="Info icon"],
#barcode-reader div[style*="text-align: center"] { display: none !important; }

.scan-divider { text-align: center; color: var(--gray-400); font-size: 13px; margin: 16px 0; }

/* =====================================================================
   LOG FORM
   ===================================================================== */
.form-main {
  flex: 1; padding: 16px 20px 120px; overflow-y: auto;
}

.mrn-display {
  display: flex; align-items: center; gap: 10px;
  background: var(--blue-lt); border-radius: 10px;
  padding: 12px 16px; margin-bottom: 20px;
}
.mrn-label  { font-size: 11px; font-weight: 800; text-transform: uppercase; color: var(--blue); flex-shrink: 0; }
.mrn-value  { font-size: 17px; font-weight: 600; color: var(--blue-dk); font-family: monospace; flex: 1; }
.mrn-rescan {
  background: none; border: 1px solid var(--blue); color: var(--blue);
  border-radius: 6px; padding: 4px 10px; font-size: 12px; cursor: pointer;
}

.suggestions-box {
  background: var(--yellow-lt); border: 1.5px solid var(--yellow-bd);
  border-radius: 10px; padding: 14px; margin-bottom: 20px;
}
.suggestions-title { font-size: 13px; font-weight: 700; color: var(--yellow-dk); margin-bottom: 10px; }
.suggestions-list  { display: flex; flex-wrap: wrap; gap: 8px; }
.suggestion-chip {
  display: inline-block; padding: 6px 13px;
  background: var(--white); border: 1.5px solid var(--yellow-bd);
  border-radius: 20px; font-size: 13px; cursor: pointer; transition: all .15s;
}
.suggestion-chip.selected { background: var(--yellow-dk); color: var(--white); border-color: var(--yellow-dk); }

/* Procedure list */
.procedure-list { margin-bottom: 20px; }

.proc-category {
  margin-bottom: 8px; background: var(--white);
  border-radius: 10px; overflow: hidden; box-shadow: var(--shadow);
}
.proc-category-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 13px 16px; font-weight: 600; font-size: 14px;
  cursor: pointer; user-select: none;
  background: var(--gray-50); border-bottom: 1px solid var(--gray-100);
}
.cat-count { font-size: 12px; color: var(--green); font-weight: 700; }

.proc-items { padding: 6px 8px; }
.proc-items.collapsed { display: none; }

.proc-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 8px; border-radius: 8px;
  cursor: pointer; font-size: 15px; transition: background .1s;
  -webkit-tap-highlight-color: transparent;
}
.proc-item:active          { background: var(--gray-100); }
.proc-item.selected        { background: var(--blue-lt); color: var(--blue-dk); font-weight: 500; }

.proc-check {
  width: 22px; height: 22px; flex-shrink: 0;
  border: 2px solid var(--gray-300); border-radius: 5px;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; background: var(--white);
}
.proc-item.selected .proc-check { background: var(--blue); border-color: var(--blue); color: var(--white); }

/* Form section */
.form-section {
  background: var(--white); border-radius: var(--radius);
  padding: 16px; margin-bottom: 20px; box-shadow: var(--shadow);
}

.selected-summary {
  text-align: center; font-size: 14px; color: var(--gray-600); margin-bottom: 12px;
}

.submit-btn {
  position: sticky; bottom: 20px; margin-top: 0;
  box-shadow: 0 8px 20px rgba(30,64,175,.35);
}

/* =====================================================================
   HISTORY / ADMIN
   ===================================================================== */
.list-main { flex: 1; padding: 16px 20px 32px; overflow-y: auto; }

.log-entry {
  background: var(--white); border-radius: 10px;
  padding: 14px 16px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.log-procedure { font-weight: 600; font-size: 15px; margin-bottom: 4px; }
.log-meta      { font-size: 13px; color: var(--gray-400); display: flex; flex-wrap: wrap; gap: 3px; }
.log-mrn       { font-size: 12px; color: var(--gray-400); margin-top: 4px; font-family: monospace; }
.log-resident  { font-size: 13px; color: var(--blue); font-weight: 500; margin-bottom: 3px; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--gray-400); font-size: 16px; }

.admin-stats {
  background: var(--white); border-radius: var(--radius);
  padding: 16px; margin-bottom: 20px; box-shadow: var(--shadow-md); overflow-x: auto;
}
.admin-stats h3 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }

.stats-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.stats-table th { text-align: left; padding: 8px; border-bottom: 2px solid var(--gray-200); font-weight: 600; color: var(--gray-600); }
.stats-table td { padding: 8px 10px; border-bottom: 1px solid var(--gray-100); }
.stats-table tr:last-child td { border-bottom: none; }

/* =====================================================================
   TOAST
   ===================================================================== */
.toast {
  position: fixed; bottom: 32px; left: 50%;
  transform: translateX(-50%) translateY(16px);
  background: var(--gray-800); color: var(--white);
  padding: 12px 24px; border-radius: 30px;
  font-size: 14px; font-weight: 500;
  opacity: 0; transition: all .25s; z-index: 1000; white-space: nowrap;
}
.toast-visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* =====================================================================
   ERROR
   ===================================================================== */
.error-msg {
  background: #fee2e2; color: var(--red);
  padding: 10px 14px; border-radius: 8px;
  font-size: 14px; margin-top: 10px;
}
