:root {
  --bg: #f4f6fb;
  --bg-elevated: #ffffff;
  --bg-sidebar: #10152a;
  --bg-sidebar-hover: rgba(255, 255, 255, 0.06);
  --text: #1b2140;
  --text-muted: #6b7280;
  --text-inverse: #e7e9f7;
  --text-inverse-muted: #9aa1c4;
  --border: #e6e9f2;
  --accent: #6d5efc;
  --accent-2: #22d3ee;
  --success: #16a34a;
  --danger: #ef4444;
  --warning: #f59e0b;
  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 22px;
  --shadow-sm: 0 1px 2px rgba(16, 21, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(16, 21, 42, 0.08);
  --shadow-lg: 0 20px 48px rgba(16, 21, 42, 0.16);
  --transition: 180ms cubic-bezier(.4, 0, .2, 1);
}

[data-theme='dark'] {
  --bg: #0b0e1a;
  --bg-elevated: #131829;
  --bg-sidebar: #0b0e1a;
  --bg-sidebar-hover: rgba(255, 255, 255, 0.06);
  --text: #e7e9f7;
  --text-muted: #9aa1c4;
  --text-inverse: #e7e9f7;
  --text-inverse-muted: #9aa1c4;
  --border: rgba(255, 255, 255, 0.08);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 20px 48px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
  font-size: 14.5px;
  line-height: 1.5;
  transition: background var(--transition), color var(--transition);
}

a { color: inherit; text-decoration: none; }

.app-shell {
  display: grid;
  grid-template-columns: 264px 1fr;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: linear-gradient(180deg, var(--bg-sidebar) 0%, #171d38 100%);
  color: var(--text-inverse);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  padding: 8px 10px 22px;
}

.sidebar-logo {
  width: 100%;
  max-width: 190px;
  height: auto;
}

.public-brand {
  text-align: center;
  margin-bottom: 26px;
}

.public-brand img {
  width: 200px;
  max-width: 60vw;
  height: auto;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-inverse-muted);
  font-weight: 500;
  transition: background var(--transition), color var(--transition), transform var(--transition);
}

.nav-link:hover {
  background: var(--bg-sidebar-hover);
  color: var(--text-inverse);
  transform: translateX(2px);
}

.nav-link.active {
  background: linear-gradient(90deg, rgba(109, 94, 252, .25), rgba(34, 211, 238, .08));
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(109, 94, 252, .35);
}

.nav-link .icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: .9;
}

.sidebar-group-title {
  padding: 14px 12px 2px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-inverse-muted);
  opacity: .65;
}

/* Grupos (módulos) recolhíveis do menu lateral - ver views/layout/app.php */
.sidebar-group { margin-top: 6px; }

/* Título do grupo destacado (barra colorida + fundo) para separar os módulos com o menu todo aberto */
.sidebar-group-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 10px 12px;
  background: rgba(255, 255, 255, .06);
  border: none;
  border-left: 3px solid #38bdf8;
  border-radius: var(--radius-sm, 8px);
  cursor: pointer;
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-inverse-muted);
  text-align: left;
}

.sidebar-group-toggle:hover {
  background: var(--bg-sidebar-hover);
  color: var(--text-inverse);
}

.sidebar-group-chevron {
  flex-shrink: 0;
  transition: transform .15s ease;
}

.sidebar-group.open .sidebar-group-chevron { transform: rotate(90deg); }

.sidebar-group.open .sidebar-group-toggle { color: var(--text-inverse); background: rgba(56, 189, 248, .14); }
.sidebar-group:nth-of-type(6n+2) .sidebar-group-toggle { border-left-color: #a78bfa; }
.sidebar-group:nth-of-type(6n+3) .sidebar-group-toggle { border-left-color: #34d399; }
.sidebar-group:nth-of-type(6n+4) .sidebar-group-toggle { border-left-color: #fbbf24; }
.sidebar-group:nth-of-type(6n+5) .sidebar-group-toggle { border-left-color: #fb7185; }
.sidebar-group:nth-of-type(6n+6) .sidebar-group-toggle { border-left-color: #f472b6; }

.sidebar-group-items {
  display: none;
  padding-left: 6px;
  margin: 2px 0 4px 6px;
  border-left: 1px solid rgba(255, 255, 255, .12);
}

.sidebar-group.open .sidebar-group-items { display: block; }

.sidebar-footer {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid rgba(255, 255, 255, .08);
}

/* ---------- Main area ---------- */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(8px);
}

.topbar h1 {
  font-size: 19px;
  margin: 0;
  font-weight: 700;
}

.content {
  padding: 28px;
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(10px); }
  /* "none" (não "translateY(0)") - um transform residual, mesmo zerado, faz o elemento
     virar containing block de descendentes position:fixed (ex.: o backdrop dos modais),
     cortando/deslocando o modal em vez de centralizar na tela inteira. */
  to { opacity: 1; transform: none; }
}

/* ---------- Cards ---------- */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition), transform var(--transition);
}

.card:hover { box-shadow: var(--shadow-md); }

.card-body { padding: 22px; }

.grid {
  display: grid;
  gap: 18px;
}

.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }

@media (max-width: 1100px) {
  .grid-cols-4, .grid-cols-3 { grid-template-columns: repeat(2, 1fr); }
}

.stat-card {
  position: relative;
  overflow: hidden;
  padding: 20px;
}

.stat-card .label {
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.stat-card .value {
  font-size: 32px;
  font-weight: 800;
  margin-top: 6px;
  background: linear-gradient(120deg, var(--text), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
}

.stat-card .glow {
  position: absolute;
  right: -30px;
  top: -30px;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  filter: blur(10px);
  opacity: .35;
}

/* ---------- Buttons & forms ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
}

.btn:active { transform: scale(.97); }

.btn-primary {
  background: linear-gradient(120deg, var(--accent), #8b7bff);
  color: #fff;
  box-shadow: 0 6px 18px rgba(109, 94, 252, .35);
}

.btn-primary:hover { box-shadow: 0 10px 24px rgba(109, 94, 252, .45); }

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover { background: var(--bg); }

.btn-sm { padding: 6px 12px; font-size: 12.5px; }

.field { margin-bottom: 16px; }

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

.field input, .field select, .field textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(109, 94, 252, .15);
}

/* ---------- Table ---------- */
table.data { width: 100%; border-collapse: collapse; }

table.data th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

table.data th.sort-header { cursor: pointer; user-select: none; }
table.data th.sort-header:hover { color: var(--text); }
table.data th[aria-sort="ascending"]::after { content: " ▲"; font-size: 9px; }
table.data th[aria-sort="descending"]::after { content: " ▼"; font-size: 9px; }

table.data td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

table.data tr { transition: background var(--transition); }
table.data tbody tr:hover { background: var(--bg); }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.badge-admin { background: rgba(109, 94, 252, .15); color: var(--accent); }
.badge-manager { background: rgba(34, 211, 238, .15); color: #0891b2; }
.badge-operator { background: rgba(148, 163, 184, .2); color: var(--text-muted); }
.badge-on { background: rgba(22, 163, 74, .15); color: var(--success); }
.badge-off { background: rgba(239, 68, 68, .15); color: var(--danger); }
.badge-warning { background: rgba(245, 158, 11, .15); color: var(--warning); }

/* ---------- Fluxo do médico (Minhas Agendas: confirmação + check-in) ---------- */
.doctor-steps { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
.doctor-step { display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-radius: 999px; background: var(--bg); border: 1px solid var(--border); font-size: 13px; font-weight: 600; color: var(--text-muted); }
.doctor-step.done { color: var(--success); border-color: rgba(22, 163, 74, .3); }
.doctor-step.active { color: var(--accent); border-color: rgba(109, 94, 252, .35); background: rgba(109, 94, 252, .08); }
.doctor-progress { font-size: 13px; color: var(--text-muted); margin: -6px 0 16px; }
.checkin-shift { background: var(--bg); border-radius: var(--radius-sm); padding: 12px 14px; margin-bottom: 8px; font-size: 14px; }

/* ---------- Modules checkbox grid (Acessos) ---------- */
.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 10px;
}

.module-chip {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
}

.module-chip:hover { border-color: var(--accent); }

.module-chip input { margin-top: 3px; }

.module-chip .name { font-weight: 600; }
.module-chip .desc { font-size: 12px; color: var(--text-muted); }

/* ---------- Flash / toast ---------- */
.flash {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
  font-weight: 600;
  animation: slideDown 220ms ease both;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: none; }
}

.flash-success { background: rgba(22, 163, 74, .12); color: var(--success); }
.flash-error { background: rgba(239, 68, 68, .12); color: var(--danger); }

/* ---------- Login page ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 20% 20%, rgba(109, 94, 252, .25), transparent 40%),
              radial-gradient(circle at 80% 80%, rgba(34, 211, 238, .2), transparent 40%),
              var(--bg-sidebar);
}

.login-card {
  width: 380px;
  background: var(--bg-elevated);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 36px 32px;
  animation: fadeInUp 400ms ease both;
}

.login-card h1 {
  font-size: 22px;
  margin: 0 0 4px;
}

.login-card p.sub {
  color: var(--text-muted);
  margin: 0 0 24px;
  font-size: 13.5px;
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(10, 12, 24, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  /* backdrop rola (não só o painel) e o painel usa margin:auto - evita o bug clássico de
     flexbox onde "align-items:center" + conteúdo mais alto que a tela deixa o topo do modal
     inacessível (sem como rolar até ele), mesmo com overflow-y no próprio painel. */
  overflow-y: auto;
  padding: 5vh 16px;
  z-index: 50;
}

.modal-panel {
  width: 480px;
  max-width: 100%;
  margin: auto;
  background: var(--bg-elevated);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 24px;
  animation: fadeInUp 220ms ease both;
}

[x-cloak] { display: none !important; }

.text-muted { color: var(--text-muted); }
.mt-0 { margin-top: 0; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }

/* Leitor de PDF "ler até o fim" (js/pdf-read-to-end.js): onboarding do portal e recados do médico */
.pdf-viewer { height: 62vh; min-height: 360px; overflow-y: auto; background: #eef0f5; border-radius: 10px; padding: 10px; margin-bottom: 14px; }
.pdf-viewer canvas { display: block; width: 100%; height: auto; margin: 0 auto 10px; box-shadow: 0 1px 4px rgba(0,0,0,.15); background: #fff; }
.pdf-viewer iframe { width: 100%; height: 100%; border: 0; }
.pdf-progress { font-size: 12px; text-align: center; margin-bottom: 12px; }

/* ---------- App instalável (PWA) e celular ----------
   Metade dos acessos é pelo celular. Até 900px o menu lateral vira gaveta (botão ☰ no topo,
   views/layout/app.php + portalToggleSidebar em app.js) e o conteúdo usa a tela inteira. */
.topbar-menu, .topbar-logo, .sidebar-close, .sidebar-overlay { display: none; }

.pwa-sheet-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.pwa-sheet-head img { border-radius: 14px; flex-shrink: 0; }
.pwa-sheet-btn { width: 100%; justify-content: center; margin-top: 10px; padding: 12px 16px; font-size: 15px; }
.pwa-steps { margin: 0 0 12px; padding-left: 20px; line-height: 1.6; }
.pwa-steps li { margin-bottom: 8px; }
.nav-link-install { color: #7dd3fc; }

@media (max-width: 900px) {
  .app-shell { display: block; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 284px;
    max-width: 86vw;
    height: 100vh;
    height: 100dvh;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform 220ms ease;
    padding-top: calc(20px + env(safe-area-inset-top));
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    box-shadow: var(--shadow-lg);
  }
  .sidebar-open .sidebar { transform: none; }
  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 12, 24, .5);
    z-index: 55;
  }
  .sidebar-open .sidebar-overlay { display: block; }
  .sidebar-open { overflow: hidden; }
  .sidebar-brand { justify-content: space-between; gap: 10px; }
  .sidebar-logo { max-width: 160px; }
  .sidebar-close {
    display: block;
    background: none;
    border: 0;
    color: var(--text-inverse);
    font-size: 28px;
    line-height: 1;
    padding: 4px 8px;
    cursor: pointer;
  }
  .nav-link { padding: 12px; }
  .nav-link:hover { transform: none; }

  .topbar {
    gap: 10px;
    padding: 10px 14px;
    padding-top: calc(10px + env(safe-area-inset-top));
    justify-content: flex-start;
  }
  .topbar-menu {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
  }
  .topbar-logo { display: block; width: 30px; height: 30px; flex-shrink: 0; }
  .topbar h1 {
    font-size: 16px;
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar-user .badge { display: none; }
  .topbar-user a { max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }

  .content { padding: 16px 14px calc(24px + env(safe-area-inset-bottom)); }
  .grid-cols-4, .grid-cols-3 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .grid { gap: 12px; }
  .grid-cols-4, .grid-cols-3, .grid-cols-2 { grid-template-columns: 1fr; }
  .card { border-radius: var(--radius-md); }
  .card-body { padding: 16px; }
  .stat-card { padding: 16px; }
  .stat-card .value { font-size: 26px; }
  .topbar-user { display: none; }

  /* Tabelas largas rolam de lado dentro do card, em vez de estourar a tela. */
  table.data { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; white-space: nowrap; }
  table.data th, table.data td { padding: 10px 12px; }

  /* Filtros/formulários em linha (style="min-width:...") empilham. */
  .flex { flex-wrap: wrap; }
  .field[style*="min-width"], .field[style*="width:"] { min-width: 0 !important; width: 100% !important; flex: 1 1 100% !important; }

  /* iPhone dá zoom em campos com fonte < 16px. */
  .field input, .field select, .field textarea, input, select, textarea { font-size: 16px; }

  .btn { padding: 11px 16px; }
  .btn-sm { padding: 8px 12px; font-size: 13px; }

  .modal-backdrop { padding: 12px; align-items: flex-end; }
  .modal-panel { padding: 20px 18px calc(20px + env(safe-area-inset-bottom)); border-radius: var(--radius-lg) var(--radius-lg) var(--radius-md) var(--radius-md); }

  .login-screen { justify-content: flex-start; padding: calc(28px + env(safe-area-inset-top)) 14px 28px; }
  .login-card { width: 100% !important; max-width: 100%; padding: 26px 20px; }
  .public-brand { margin-bottom: 18px; }
  .public-brand img { width: 170px; }

  .doctor-step { font-size: 12px; padding: 7px 12px; }
  .pdf-viewer { height: 58vh; min-height: 300px; }
}

/* Kanbans: barra de rolagem horizontal em cima (espelho criado em app.js), a de baixo escondida. */
.kanban-scroll { scrollbar-width: none; }
.kanban-scroll::-webkit-scrollbar { display: none; }
.kanban-scroll-top { overflow-x: auto; overflow-y: hidden; margin-bottom: 8px; position: sticky; top: 0; z-index: 5; background: var(--bg); }
.kanban-scroll-top > div { height: 1px; }

/* Conferência MedSimples: tabela compacta que cabe na tela sem rolagem horizontal */
table.data.conf-compact td, table.data.conf-compact th { white-space: normal; word-break: break-word; vertical-align: top; padding: 10px 10px; }
table.data.conf-compact select, table.data.conf-compact textarea { max-width: 100%; }
@media (max-width: 900px) { table.data.conf-compact { table-layout: auto !important; } }
