:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #dc2626;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.05), 0 1px 2px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.08);
  --header-grad: linear-gradient(135deg, #2563eb, #1d4ed8);
}

body.dark {
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #0f172a;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #334155;
  --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.4);
  --header-grad: linear-gradient(135deg, #1e3a8a, #1e293b);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  transition: background 0.2s, color 0.2s;
  overscroll-behavior-y: contain;
}

header {
  background: var(--header-grad);
  color: white;
  padding: 1rem 1.5rem;
  padding-top: max(1rem, env(safe-area-inset-top));
  padding-left: max(1.5rem, env(safe-area-inset-left));
  padding-right: max(1.5rem, env(safe-area-inset-right));
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 100;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0;
}

header nav { display: flex; gap: 0.25rem; flex-wrap: wrap; align-items: center; }
header nav a {
  color: white;
  text-decoration: none;
  padding: 0.5rem 0.85rem;
  border-radius: 8px;
  font-size: 0.85rem;
  transition: background 0.15s;
  white-space: nowrap;
}
header nav a:hover { background: rgba(255,255,255,0.18); }

.theme-toggle {
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  width: 36px; height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1rem;
  display: inline-flex;
  align-items: center; justify-content: center;
  transition: background 0.15s;
}
.theme-toggle:hover { background: rgba(255,255,255,0.3); }

.nav-toggle {
  display: none;
  background: rgba(255,255,255,0.15);
  border: none;
  color: white;
  width: 36px; height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1rem;
}

main {
  max-width: 1100px;
  margin: 1.5rem auto;
  padding: 0 1rem;
}

h2 { font-size: 1.4rem; margin-bottom: 1rem; color: var(--text); }
h3 { font-size: 1.05rem; margin-bottom: 0.75rem; color: var(--text); }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
  border: 1px solid var(--border);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.75rem;
}

.grid-menu {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
  margin-top: 1.5rem;
}

.menu-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem 1rem;
  text-align: center;
  box-shadow: var(--shadow);
  text-decoration: none;
  color: var(--text);
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
  border: 2px solid var(--border);
}
.menu-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-lg);
  border-color: var(--primary);
}
.menu-card .icon { font-size: 2.5rem; margin-bottom: 0.75rem; }
.menu-card h3 { color: var(--primary); margin-bottom: 0.25rem; }
.menu-card p { color: var(--text-muted); font-size: 0.85rem; margin: 0; }

.kpi {
  background: var(--surface);
  padding: 1.25rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  text-align: center;
}
.kpi .value {
  font-size: 2rem; font-weight: 700; color: var(--primary);
  line-height: 1.1;
}
.kpi .label {
  color: var(--text-muted); font-size: 0.75rem;
  margin-top: 0.25rem; text-transform: uppercase; letter-spacing: 0.05em;
}

label {
  display: block; margin-bottom: 0.25rem;
  font-size: 0.85rem; font-weight: 500; color: var(--text);
}

input, select, textarea {
  width: 100%;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  margin-bottom: 0.85rem;
  transition: border 0.15s, background 0.15s;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
}
select {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path fill='%2364748b' d='M0 0l5 6 5-6z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 30px;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.18);
}

button, .btn {
  padding: 0.6rem 1.15rem;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 500;
  transition: background 0.15s, transform 0.05s;
  display: inline-block;
  text-decoration: none;
  font-family: inherit;
}
button:hover, .btn:hover { background: var(--primary-dark); }
button:active, .btn:active { transform: scale(0.98); }
button:disabled { background: #94a3b8; cursor: not-allowed; }
.btn-success { background: var(--success); }
.btn-success:hover { background: #059669; }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; }
.btn-ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--surface-2); }

.video-container {
  position: relative;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  justify-content: center;
}
#video, #cadastroVideo, #matriculaVideo, #editVideo {
  width: 100%;
  height: auto;
  display: block;
}
#overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
}

.btn-flip-camera {
  position: absolute;
  top: 10px; right: 10px;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
  font-size: 1.15rem;
  display: inline-flex;
  align-items: center; justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 5;
  transition: transform 0.2s, background 0.15s;
}
.btn-flip-camera:hover { background: rgba(0,0,0,0.75); }
.btn-flip-camera:active { transform: rotate(180deg); }
.btn-flip-camera:disabled { opacity: 0.6; cursor: wait; }

.status-bar {
  display: flex; gap: 0.5rem;
  margin-top: 0.85rem; flex-wrap: wrap;
  align-items: center;
}
.badge {
  padding: 0.4rem 0.85rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text);
}
.badge-success { background: rgba(16,185,129,0.15); color: var(--success); }
.badge-warning { background: rgba(245,158,11,0.15); color: var(--warning); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
th, td {
  text-align: left;
  padding: 0.65rem 0.5rem;
  border-bottom: 1px solid var(--border);
}
th {
  background: var(--surface-2);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}
tbody tr:hover { background: var(--surface-2); }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.alert {
  padding: 0.85rem 1rem;
  border-radius: 8px;
  margin-bottom: 0.85rem;
  font-size: 0.9rem;
}
.alert-success { background: rgba(16,185,129,0.15); color: var(--success); border-left: 4px solid var(--success); }
.alert-error { background: rgba(220,38,38,0.15); color: var(--danger); border-left: 4px solid var(--danger); }
.alert-info { background: rgba(37,99,235,0.15); color: var(--primary); border-left: 4px solid var(--primary); }

.recognized-list {
  display: flex; flex-direction: column; gap: 0.5rem;
  max-height: 400px; overflow-y: auto;
}
.recognized-item {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.65rem 0.85rem;
  background: var(--surface-2);
  border-radius: 8px;
  animation: slideIn 0.3s ease-out;
  font-size: 0.9rem;
}
.recognized-item.success { border-left: 4px solid var(--success); }
.recognized-item.warning { border-left: 4px solid var(--warning); }

@keyframes slideIn {
  from { opacity: 0; transform: translateX(-10px); }
  to { opacity: 1; transform: translateX(0); }
}

.progress-bar {
  width: 100%; height: 8px;
  background: var(--surface-2);
  border-radius: 4px; overflow: hidden;
  margin-top: 0.5rem;
}
.progress-fill {
  height: 100%; background: var(--primary);
  transition: width 0.3s;
}

.row { display: flex; gap: 0.85rem; align-items: flex-end; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 0; }

/* ===== Toast estilo aeroporto / boarding pass ===== */
.boarding-container {
  position: fixed;
  bottom: 1.5rem; right: 1.5rem;
  z-index: 9999;
  display: flex; flex-direction: column; gap: 0.75rem;
  pointer-events: none;
  max-width: calc(100vw - 3rem);
}
.boarding-pass {
  display: flex; align-items: center; gap: 1rem;
  background: linear-gradient(135deg, #10b981, #059669);
  color: white;
  padding: 1rem 1.25rem;
  border-radius: 14px;
  box-shadow: 0 20px 40px rgba(16,185,129,0.45);
  min-width: 320px;
  animation: boardingIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1), boardingOut 0.5s ease-in 3s forwards;
}
.boarding-pass .photo {
  width: 64px; height: 64px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.7);
  object-fit: cover;
  background: rgba(255,255,255,0.2);
  flex-shrink: 0;
}
.boarding-pass .info { flex: 1; min-width: 0; }
.boarding-pass .name {
  font-size: 1rem; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.boarding-pass .sub {
  font-size: 0.8rem; opacity: 0.95;
  margin-top: 0.15rem;
}
.boarding-pass .check {
  font-size: 1.5rem;
  flex-shrink: 0;
}

@keyframes boardingIn {
  from { opacity: 0; transform: translateX(120%); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes boardingOut {
  to { opacity: 0; transform: translateX(20px) scale(0.95); }
}

/* ===== Mobile ===== */
@media (max-width: 768px) {
  header {
    padding: 0.75rem 1rem;
    padding-top: max(0.75rem, env(safe-area-inset-top));
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }
  header h1 { font-size: 1.1rem; }
  .nav-toggle { display: inline-flex; align-items: center; justify-content: center; }
  header nav {
    display: none;
    flex-basis: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 0.25rem;
    padding-top: 0.5rem;
  }
  header nav.open { display: flex; }
  header nav a {
    text-align: left;
    padding: 0.85rem 0.85rem;
    background: rgba(255,255,255,0.08);
    font-size: 0.95rem;
  }

  main {
    padding-left: max(0.75rem, env(safe-area-inset-left));
    padding-right: max(0.75rem, env(safe-area-inset-right));
    padding-bottom: env(safe-area-inset-bottom);
    margin: 1rem auto;
  }
  .card { padding: 1rem; border-radius: 10px; }
  .row { flex-direction: column; gap: 0.5rem; align-items: stretch; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 0.5rem; }
  .grid-menu { grid-template-columns: 1fr; gap: 0.75rem; }
  .kpi .value { font-size: 1.6rem; }
  table { font-size: 0.85rem; }
  th, td { padding: 0.5rem 0.4rem; }

  /* Touch targets confortáveis (44px Apple HIG) */
  button, .btn, .btn-ghost {
    min-height: 44px;
    padding: 0.7rem 1rem;
    font-size: 0.95rem;
  }
  .nav-toggle, .theme-toggle { min-height: 44px; min-width: 44px; }
  header nav a { min-height: 44px; display: flex; align-items: center; }

  /* Inputs: 16px previne zoom automático no iOS Safari */
  input, select, textarea {
    font-size: 16px;
    padding: 0.7rem 0.9rem;
    min-height: 44px;
  }

  /* Modais quase fullscreen no mobile, com scroll interno e safe area */
  .modal { padding: 0; align-items: flex-end; }
  .modal-content {
    max-width: 100% !important;
    width: 100% !important;
    max-height: 92dvh !important;
    border-radius: 16px 16px 0 0 !important;
    padding: 1.25rem !important;
    padding-bottom: max(1.25rem, env(safe-area-inset-bottom)) !important;
    animation: modalSlideUp 0.25s ease-out;
  }
  .modal-close { min-width: 44px; min-height: 44px; font-size: 1.75rem; }

  /* Vídeo / câmera mantém proporção mesmo em portrait */
  .video-container { max-width: 100%; }

  /* Boarding pass / toast */
  .boarding-container {
    bottom: max(0.75rem, env(safe-area-inset-bottom));
    right: 0.75rem; left: 0.75rem;
    max-width: none;
  }
  .boarding-pass { min-width: 0; width: 100%; }

  /* Tabela com scroll horizontal (já existe .table-wrap, garantir hint visual) */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 8px; }
}

@media (max-width: 420px) {
  .grid { grid-template-columns: 1fr; }
  header h1 { font-size: 1rem; }
  .kpi { padding: 1rem 0.75rem; }
  .kpi .value { font-size: 1.4rem; }
  .card { padding: 0.85rem; }
  main { padding-left: max(0.5rem, env(safe-area-inset-left)); padding-right: max(0.5rem, env(safe-area-inset-right)); }
}

@keyframes modalSlideUp {
  from { transform: translateY(100%); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

/* ===== Portal do aluno (linktree) ===== */
.portal {
  max-width: 480px;
  margin: 0 auto;
  padding: 0 1rem 2rem;
}
.portal-hero {
  text-align: center;
  padding: 2rem 1rem 1.5rem;
}
.portal-hero .logo { font-size: 3rem; line-height: 1; }
.portal-hero h1 { font-size: 1.5rem; margin: 0.5rem 0 0.25rem; }
.portal-hero p { color: var(--text-muted); font-size: 0.95rem; }
.portal-links { display: flex; flex-direction: column; gap: 0.85rem; }
.portal-link {
  display: flex; align-items: center; gap: 1rem;
  padding: 1.15rem 1.25rem;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 16px;
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: transform 0.12s, box-shadow 0.15s, border-color 0.15s;
  min-height: 68px;
}
.portal-link:hover, .portal-link:active {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  border-color: var(--primary);
}
.portal-link .pl-icon {
  font-size: 1.9rem; width: 48px; text-align: center; flex-shrink: 0;
}
.portal-link .pl-text { flex: 1; min-width: 0; }
.portal-link .pl-title { font-weight: 700; font-size: 1.05rem; }
.portal-link .pl-sub { color: var(--text-muted); font-size: 0.82rem; margin-top: 0.1rem; }
.portal-link .pl-arrow { color: var(--text-muted); font-size: 1.3rem; flex-shrink: 0; }
.portal-link.accent-green { border-color: rgba(16,185,129,0.4); }
.portal-link.accent-green .pl-title { color: var(--success); }
.portal-link.accent-wa { border-color: rgba(37,211,102,0.5); }
.portal-link.accent-wa .pl-title { color: #128c4b; }
body.dark .portal-link.accent-wa .pl-title { color: #25d366; }

/* ===== Selfie / confirmação self-service ===== */
.selfie-frame {
  max-width: 360px;
  margin: 0 auto 1rem;
  background: #000;
  border-radius: 50%;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  position: relative;
  border: 4px solid var(--primary);
  box-shadow: var(--shadow-lg);
}
.selfie-frame video, .selfie-frame canvas, .selfie-frame img {
  width: 100%; height: 100%; object-fit: cover;
  transform: scaleX(-1); /* espelho, como selfie */
}
.confirm-photo {
  width: 140px; height: 140px;
  border-radius: 50%;
  object-fit: cover;
  border: 4px solid var(--success);
  margin: 0 auto 0.75rem;
  display: block;
  background: var(--surface-2);
}
.big-choice { display: flex; gap: 0.75rem; margin-top: 1rem; }
.big-choice button { flex: 1; padding: 1rem; font-size: 1rem; }
.big-success { text-align: center; padding: 2rem 1rem; }
.big-success .icon {
  font-size: 4.5rem; margin-bottom: 0.5rem;
  animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.big-success h2 { color: var(--success); font-size: 1.6rem; margin-bottom: 0.35rem; }
@keyframes popIn { from { transform: scale(0); } to { transform: scale(1); } }

/* Lista de nomes (marcar por nome / atualizar) */
.name-list { display: flex; flex-direction: column; gap: 0.4rem; max-height: 60vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.name-item {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.65rem 0.85rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: var(--text);
  min-height: 56px;
}
.name-item:hover { border-color: var(--primary); }
.name-item img, .name-item .avatar {
  width: 42px; height: 42px; border-radius: 50%; object-fit: cover;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; flex-shrink: 0;
}
.name-item .ni-nome { font-weight: 600; }
.name-item .ni-classe { font-size: 0.8rem; color: var(--text-muted); }

/* ===== Reduce-motion: respeita pref de acessibilidade ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
