:root {
  --primary: #00457e;
  --primary-dark: #003057;
  --primary-light: #eaf2f9;
  --accent: #2e86de;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --bg: #f3f6f9;
  --card-bg: #ffffff;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --card-border: #d4e3f3;
  --dots: rgba(0, 69, 126, 0.35);
  --primary-text: var(--primary);
  --radius: 16px;
  /* tilt state, written by initTilt() in app.js */
  --rx: 0deg;
  --ry: 0deg;
  --gx: 50%;
  --gy: 30%;
  --sx: 0px;
  --sy: 12px;
  --foil: linear-gradient(115deg, #ffd1f0, #c7d2fe, #a5f3fc, #bbf7d0, #fef08a, #fecaca, #ffd1f0);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary-light: #13243a;
    --primary-text: #8cbcec;
    --bg: #0b1220;
    --card-bg: #111827;
    --text-main: #e5e7eb;
    --text-muted: #9ca3af;
    --border: #374151;
    --card-border: #1f3a5c;
    --dots: rgba(140, 188, 236, 0.3);
  }
}

/* remaining fraction of the code's lifetime; registered so it can transition */
@property --p {
  syntax: "<number>";
  inherits: true;
  initial-value: 1;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  background-color: var(--bg);
  color: var(--text-main);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  padding: 16px;
}

.app-container {
  position: relative;
  width: 100%;
  max-width: 440px;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.08), 0 8px 10px -6px rgba(0, 0, 0, 0.04);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Header */
/* Dot network behind the content; header and main paint over it in DOM order */
.dots {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  color: var(--dots); /* read by initDots() */
}

.app-header {
  position: relative;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #ffffff;
  padding: 20px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand-wrapper {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 38px;
  height: 38px;
  background: #ffffff;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  color: var(--primary);
  font-size: 20px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}

.brand-title h1 {
  font-size: 19px;
  font-weight: 700;
  line-height: 1.2;
}

.brand-title p {
  font-size: 12px;
  opacity: 0.85;
}

.icon-btn {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* Main Content */
.main-content {
  position: relative;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

/* Profile switcher chips */
.profile-bar {
  position: relative;
  width: 100%;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.profile-bar[hidden] { display: none; }
.profile-bar::-webkit-scrollbar { display: none; }

.profile-chip {
  flex: none;
  max-width: 60%;
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--primary-light);
  color: var(--text-main);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.profile-chip[aria-pressed="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* QR Code Display Container — floating 3D card */
.tilt-wrap {
  width: 100%;
  max-width: 280px;
  perspective: 900px;
  animation: float 4s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.qr-frame {
  position: relative;
  background: #ffffff; /* stays white in dark mode: scanners need the contrast */
  /* 4px ring + a white quiet zone of ~4 modules (QR is 33 modules wide), so the
     countdown ring never touches the code; % padding scales with the card */
  padding: calc(10% + 4px);
  border-radius: 16px;
  transition: --p 1s linear;
  /* shadow shifts opposite to tilt so the card looks lifted off the page */
  box-shadow:
    var(--sx) var(--sy) 30px -8px rgba(0, 48, 87, 0.28),
    0 2px 6px rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  transform: rotateX(var(--rx)) rotateY(var(--ry));
  transform-style: preserve-3d;
  will-change: transform;
  overflow: hidden;
}

.tilt-glare {
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
  background: radial-gradient(circle at var(--gx) var(--gy), rgba(255,255,255,0.55), rgba(255,255,255,0) 55%);
  mix-blend-mode: soft-light;
  z-index: 1;
}

/* Countdown ring: blue arc drains over a holographic foil track */
.qr-frame::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  padding: 4px;
  border-radius: inherit;
  pointer-events: none;
  background:
    conic-gradient(var(--primary) calc(var(--p) * 360deg), transparent 0),
    var(--foil);
  background-size: 100% 100%, 300% 300%;
  background-position: 0 0, var(--gx) var(--gy);
  /* keep only the 4px border strip */
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
}

#qrcode-canvas.fresh {
  animation: qr-in 0.35s ease-out;
}

@keyframes qr-in {
  from { opacity: 0.35; transform: scale(0.97); }
}

@media (prefers-reduced-motion: reduce) {
  .tilt-wrap { animation: none; }
  #qrcode-canvas.fresh { animation: none; }
  .qr-frame { transform: none; }
}

#qrcode-canvas {
  width: 100%;
  max-width: 240px;
  aspect-ratio: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

#qrcode-canvas img, #qrcode-canvas canvas {
  width: 100% !important;
  height: 100% !important;
}

/* Student Badge Card */
.student-card {
  position: relative;
  overflow: hidden;
  width: 100%;
  background: var(--primary-light);
  border-radius: 14px;
  padding: 16px;
  border: 1px solid var(--card-border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* foil sheen that shifts with the tilt, under the text */
.student-card::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: var(--foil);
  background-size: 300% 300%;
  background-position: var(--gx) var(--gy);
  opacity: 0.35;
}

@media (prefers-color-scheme: dark) {
  .student-card::after { opacity: 0.1; }
}

.card-row {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.card-label {
  color: var(--text-muted);
  font-weight: 500;
}

.card-value {
  color: var(--text-main);
  font-weight: 700;
}

/* Credit */
.credit {
  font-size: 12px;
  color: var(--text-muted);
}

.credit a {
  color: inherit;
  font-weight: 600;
}

/* Controls */
.action-buttons {
  width: 100%;
  display: flex;
  gap: 10px;
}

.btn {
  flex: 1;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.2s;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-secondary {
  background: var(--bg);
  color: var(--text-main);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border);
}

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 16px;
  z-index: 999;
}

.modal-overlay.hidden {
  display: none;
}

.modal-card {
  background: var(--card-bg);
  border-radius: 16px;
  width: 100%;
  max-width: 420px;
  padding: 24px;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
  max-height: 90vh;
  overflow-y: auto;
}

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

.profile-select {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  color: var(--primary-text);
  font-size: 17px;
  font-weight: 700;
}

.close-btn {
  flex: none;
  background: var(--bg);
  color: var(--text-main);
}

.close-btn:hover {
  background: var(--border);
}

.form-row {
  display: flex;
  gap: 12px;
}

.form-row .form-group {
  flex: 1;
  margin-bottom: 0;
}

.advanced {
  margin-bottom: 16px;
}

.advanced summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.modal-actions {
  margin-top: 20px;
  flex-direction: column;
  gap: 8px;
}

.btn-danger {
  color: var(--danger);
  border-color: #fca5a5;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-main);
  margin-bottom: 6px;
}

.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
  background: var(--card-bg);
  color: var(--text-main);
}

.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 69, 126, 0.15);
}

.form-help {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* Home-screen web app: fill the screen edge to edge, no scrolling */
@media (display-mode: standalone) {
  html, body {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
  }

  body {
    padding: 0;
    background: var(--card-bg);
  }

  .app-container {
    max-width: none;
    height: 100%;
    border-radius: 0;
    box-shadow: none;
  }

  .app-header {
    padding: calc(10px + env(safe-area-inset-top)) 20px 10px;
  }

  .main-content {
    flex: 1;
    min-height: 0;
    padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
    gap: 12px;
    justify-content: space-evenly;
  }

  /* QR shrinks on short screens (iPhone SE) so everything fits */
  #qrcode-canvas {
    max-width: clamp(150px, calc(100vh - 430px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 240px);
  }

  /* the chip row takes ~40px more */
  body.multi-profile #qrcode-canvas {
    max-width: clamp(150px, calc(100vh - 470px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), 240px);
  }

  .student-card {
    padding: 12px 16px;
    gap: 6px;
  }
}

/* Scan mode: QR full width, flat, no effects */
.qr-frame { cursor: pointer; }
.scan-hint, .scan-name { display: none; }

body.scan-mode { overflow: hidden; }
/* perspective/transform on an ancestor would trap position:fixed */
body.scan-mode .tilt-wrap { animation: none; perspective: none; }

body.scan-mode .qr-frame {
  position: fixed;
  inset: 0;
  z-index: 1000;
  max-width: none;
  justify-content: center;
  padding: 24px;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  transform: none;
}

body.scan-mode .tilt-glare { display: none; }

/* 80vw leaves the ~4-module white quiet zone scanners expect */
body.scan-mode #qrcode-canvas {
  width: min(80vw, 70vh);
  max-width: none;
}

body.scan-mode #qrcode-canvas.fresh { animation: none; }

/* whose QR this is, when more than one profile is saved */
body.scan-mode.multi-profile .scan-name {
  display: block;
  margin-bottom: 16px;
  font-size: 18px;
  font-weight: 700;
  color: #00457e; /* frame stays white in dark mode */
}

body.scan-mode .scan-hint {
  display: block;
  margin-top: 16px;
  font-size: 13px;
  color: var(--text-muted);
}
