/* ==========================================================================
   Base: reset, tipografia e estrutura do app (menu lateral, cabeçalho,
   área de conteúdo, rodapé e grade). Usa somente tokens de tokens.css.
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
:root { font-family: var(--font); font-size: 16px; color: var(--ink); }
body {
  margin: 0; min-height: 100vh; background: var(--bg); color: var(--ink);
  font: 400 var(--fs-base)/1.45 var(--font);
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
}
button, input, select, textarea { font: inherit; color: inherit; }
button, a, select { touch-action: manipulation; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }
img, svg { max-width: 100%; }
a { color: var(--accent-ink); text-underline-offset: 3px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

h1, h2, h3, h4 { font-family: var(--font); color: var(--ink); margin: 0; letter-spacing: -.01em; }
h1 { font-size: var(--fs-xl); font-weight: 800; line-height: 1.2; letter-spacing: -.02em; }
h2 { font-size: var(--fs-lg); font-weight: 700; line-height: 1.3; }
h3 { font-size: var(--fs-base); font-weight: 700; line-height: 1.35; }
p { margin: 0; color: var(--muted); line-height: 1.5; }
small { display: block; font-size: var(--fs-sm); color: var(--muted); font-weight: 400; margin-top: 2px; }
code, pre { font-family: var(--font-mono); font-size: 12px; }
hr { border: 0; border-top: 1px solid var(--line); margin: var(--s-4) 0; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* Barras de rolagem discretas */
* { scrollbar-width: thin; scrollbar-color: var(--scroll-thumb) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; background: transparent; }
*::-webkit-scrollbar-thumb { background-color: var(--scroll-thumb); background-clip: padding-box; border: 3px solid transparent; border-radius: 999px; min-height: 32px; }
*::-webkit-scrollbar-thumb:hover { background-color: var(--scroll-thumb-hover); }
*::-webkit-scrollbar-track, *::-webkit-scrollbar-corner { background: transparent; }

/* --------------------------------------------------------------------------
   Menu lateral (card flutuante)
   -------------------------------------------------------------------------- */
#sidebar {
  position: fixed; z-index: 20; inset: var(--shell-gap) auto var(--shell-gap) var(--shell-gap);
  width: var(--sidebar-w); display: flex; flex-direction: column;
  padding: var(--s-4) var(--s-3) var(--s-3);
  background: var(--surface); border: 1px solid var(--card-border); border-radius: var(--r-lg);
  box-shadow: var(--shadow-card); overflow-y: auto; overscroll-behavior: contain;
  transition: width .32s var(--ease), transform .2s ease;
}
#sidebar-collapse {
  position: absolute; top: var(--s-3); right: var(--s-3); z-index: 1;
  display: grid; place-items: center; width: 28px; height: 28px; padding: 0;
  border: 1px solid var(--line); border-radius: var(--r-pill); background: var(--surface);
  color: var(--muted); font-size: 18px; line-height: 1;
}
#sidebar-collapse:hover { color: var(--accent); border-color: var(--accent-soft-2); }

.brand, #sidebar .brand.clinic-brand {
  display: flex; flex-direction: column; align-items: center; gap: var(--s-1);
  padding: 0 0 var(--s-3); margin: 0 0 var(--s-3); border-bottom: 1px solid var(--line);
  text-decoration: none; color: var(--ink);
}
.clinic-wordmark { display: block; flex: none; width: 88px; aspect-ratio: 1; background: transparent var(--clinic-logo) center / 190% auto no-repeat; }
#sidebar .clinic-wordmark { width: 76px; height: 60px; aspect-ratio: auto; background-size: 160px auto; }
.clinic-brand-label { text-align: center; line-height: 1.2; }
.clinic-brand-label small, .brand small { margin: 0; font-size: 10px; font-weight: 700; letter-spacing: .14em; color: var(--muted); }
.brand-icon { display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: var(--r-md); background: var(--accent-gradient); color: var(--on-accent); font-size: 20px; box-shadow: var(--shadow-accent); }

.nav-label { padding: 0 var(--s-3) var(--s-2); font-size: 10px; font-weight: 700; letter-spacing: .14em; color: var(--subtle); }
#nav { display: flex; flex-direction: column; gap: 2px; flex-shrink: 0; }
#nav > a {
  display: flex; align-items: center; gap: var(--s-3); min-height: 38px; padding: var(--s-2) var(--s-3);
  border-radius: var(--r-sm); color: var(--muted); font-size: var(--fs-base); font-weight: 600; text-decoration: none;
  transition: background .15s, color .15s;
}
#nav > a:hover { background: var(--surface-soft); color: var(--ink); }
#nav > a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 700; }
#nav > a.active .nav-icon { color: var(--accent); }
.nav-icon { display: grid; place-items: center; width: 20px; flex: none; font-size: 17px; }
#nav .nav-icon svg { display: block; width: 19px; height: 19px; stroke-width: 1.6; }
#nav .nav-text { white-space: nowrap; overflow: hidden; }
.comm-subnav { display: flex; flex-direction: column; gap: 1px; margin: 2px 0 var(--s-2) 21px; padding-left: var(--s-2); border-left: 1px solid var(--line); }
.comm-subnav a { padding: 6px var(--s-2); border-radius: var(--r-xs); font-size: var(--fs-sm); color: var(--muted); text-decoration: none; line-height: 1.4; }
.comm-subnav a:hover { color: var(--ink); background: var(--surface-soft); }
.comm-subnav a.active { color: var(--accent-ink); background: var(--accent-soft); font-weight: 700; }

#theme-toggle {
  display: flex; align-items: center; gap: var(--s-3); width: 100%; min-height: 38px; flex-shrink: 0;
  margin-top: auto; padding: var(--s-2) var(--s-3); text-align: left;
  border: 0; border-radius: var(--r-sm); background: transparent; color: var(--muted);
  font-size: var(--fs-base); font-weight: 600;
}
#theme-toggle:hover { background: var(--surface-soft); color: var(--ink); }
#theme-toggle > span:first-child { width: 20px; text-align: center; font-size: 16px; }
.sidebar-bottom { margin-top: var(--s-2); padding: var(--s-3) var(--s-1) 0; border-top: 1px solid var(--line); }
.sidebar-bottom .badge { display: inline-block; }
.sidebar-bottom p { margin-top: var(--s-2); font-size: var(--fs-xs); line-height: 1.45; color: var(--subtle); }
.sidebar-bottom p br { display: none; }
.badge { padding: 3px var(--s-2); border-radius: var(--r-pill); background: var(--warning-soft); color: var(--warning-ink); font-size: var(--fs-xs); font-weight: 700; }
.workspace { display: flex; align-items: center; gap: var(--s-2); margin-top: var(--s-3); padding: var(--s-2); border-radius: var(--r-sm); background: var(--surface-soft); font-size: var(--fs-md); font-weight: 700; min-width: 0; }
.workspace small { font-size: var(--fs-xs); margin: 0; }
#workspace-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar { display: inline-grid; place-items: center; width: 32px; height: 32px; flex: none; border-radius: var(--r-pill); background: var(--accent-soft); color: var(--accent-ink); font-size: 11px; font-weight: 800; letter-spacing: .02em; }
#clinic-identity { overflow: hidden; }
#clinic-identity img { width: 100%; height: 100%; object-fit: contain; background: #fff; }

/* --------------------------------------------------------------------------
   Área principal + cabeçalho compacto (mesma altura em todas as telas)
   -------------------------------------------------------------------------- */
.app { margin-left: calc(var(--sidebar-w) + var(--shell-gap)); min-width: 0; min-height: 100vh; display: flex; flex-direction: column; transition: margin-left .32s var(--ease); }
.app > header {
  position: sticky; top: 0; z-index: 10; flex: none;
  height: var(--header-h); margin: 0; padding: 0 var(--page-x);
  display: flex; align-items: center; justify-content: space-between; gap: var(--s-4);
  background: color-mix(in srgb, var(--bg) 88%, transparent); border: 0;
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.breadcrumb { font-size: var(--fs-md); color: var(--muted); white-space: nowrap; }
.breadcrumb span { margin: 0 var(--s-2); color: var(--subtle); }
.breadcrumb b { color: var(--ink); font-weight: 700; }
.header-right { display: flex; align-items: center; gap: var(--s-3); min-width: 0; margin-left: auto; }
.demo-dot { display: inline-flex; align-items: center; gap: 6px; padding: 5px var(--s-3); border-radius: var(--r-pill); background: var(--warning-soft); color: var(--warning-ink); font-size: var(--fs-xs); font-weight: 700; white-space: nowrap; }
.demo-dot::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
#profile-demo { display: flex; align-items: center; gap: var(--s-3); min-width: 0; }
#profile-demo label { display: flex; flex-direction: row; align-items: center; gap: var(--s-2); margin: 0; font-size: var(--fs-xs); font-weight: 600; color: var(--muted); white-space: nowrap; }
#profile-demo select { width: auto; max-width: 260px; height: 32px; padding: 0 28px 0 var(--s-3); font-size: var(--fs-md); border-radius: var(--r-pill); }
#profile-demo a { font-size: var(--fs-md); font-weight: 700; white-space: nowrap; text-decoration: none; color: var(--accent-ink); }
#profile-demo a:hover { text-decoration: underline; }
.user { font-size: var(--fs-md); font-weight: 700; }
#menu-toggle { display: none; }

main, #main { width: 100%; max-width: var(--content-max); margin: 0 auto; padding: var(--s-2) var(--page-x) var(--s-6); min-width: 0; flex: 1 0 auto; }
.app > footer {
  flex: none; display: flex; justify-content: space-between; gap: var(--s-3);
  padding: var(--s-3) var(--page-x); color: var(--subtle); font-size: var(--fs-xs); line-height: 1.4;
}

/* Cabeçalho da página: mesma altura e alinhamento em todas as telas */
.page-head, .app main > .page-head {
  display: flex; align-items: flex-end; justify-content: space-between; flex-wrap: wrap;
  gap: var(--s-3) var(--s-4); min-height: 56px; margin: 0 0 var(--s-4);
}
.page-head > div:first-child { flex: 1 1 280px; min-width: 0; }
.page-head h1 { margin: 2px 0 0 !important; }
.page-head p { margin-top: 2px; font-size: var(--fs-md); max-width: 85ch; }
.page-head :is(.filters, .crm-actions) { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: var(--s-2); flex: 0 1 auto; }
.eyebrow { display: block; font-size: 10px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; color: var(--accent-ink); }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); margin: var(--s-5) 0 var(--s-3); }
.section-head.first, .panel > .section-head:first-child { margin-top: 0; }
.section-head small { font-size: var(--fs-sm); margin: 0; }

/* Grade: 12 colunas + utilitários */
.grid-12 { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: var(--gutter); }
.grid-two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--gutter); align-items: stretch; }
.cards { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: var(--gutter); }
.results { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--gutter); margin-bottom: var(--gutter); }

/* --------------------------------------------------------------------------
   Menu recolhido (desktop)
   -------------------------------------------------------------------------- */
@media (min-width: 851px) {
  body.sidebar-collapsed #sidebar { width: var(--sidebar-w-collapsed); padding-inline: var(--s-2); }
  body.sidebar-collapsed .app { margin-left: calc(var(--sidebar-w-collapsed) + var(--shell-gap)); }
  body.sidebar-collapsed #sidebar-collapse { position: static; align-self: center; margin-bottom: var(--s-2); }
  body.sidebar-collapsed #sidebar:not(:hover):not(:has(#nav :focus-visible, #theme-toggle:focus-visible)) :is(.nav-label, .clinic-brand-label, .sidebar-bottom, .comm-subnav) { display: none; }
  body.sidebar-collapsed #sidebar:not(:hover):not(:has(#nav :focus-visible, #theme-toggle:focus-visible)) .clinic-wordmark { width: 44px; height: 40px; background-size: 96px auto; }
  body.sidebar-collapsed #sidebar:not(:hover):not(:has(#nav :focus-visible, #theme-toggle:focus-visible)) :is(#nav > a, #theme-toggle) { justify-content: center; gap: 0; padding-inline: 0; }
  body.sidebar-collapsed #sidebar:not(:hover):not(:has(#nav :focus-visible, #theme-toggle:focus-visible)) :is(.nav-text, #theme-toggle > span:last-child) { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  body.sidebar-collapsed #sidebar:is(:hover, :has(#nav :focus-visible, #theme-toggle:focus-visible)) { width: var(--sidebar-w); padding-inline: var(--s-3); box-shadow: var(--shadow-pop); }
}

/* --------------------------------------------------------------------------
   Responsivo
   -------------------------------------------------------------------------- */
@media (min-width: 1600px) { :root { --page-x: 32px; --gutter: 20px; } }
@media (max-width: 1280px) { .cards { grid-template-columns: repeat(5, minmax(0, 1fr)); } }
@media (max-width: 1150px) {
  :root { --sidebar-w: 212px; --page-x: 20px; }
  .demo-dot { display: none; }
  .cards { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 850px) {
  :root { --page-x: 16px; }
  #sidebar { transform: translateX(calc(-100% - 24px)); width: 260px; visibility: hidden; transition: transform .2s ease, visibility 0s linear .2s; }
  body.menu-open #sidebar { transform: none; box-shadow: var(--shadow-pop); visibility: visible; transition: transform .2s ease; }
  #sidebar-collapse { display: none; }
  .app { margin-left: 0; }
  #menu-toggle { display: grid; }
  .breadcrumb { display: none; }
  .grid-two { grid-template-columns: 1fr; }
  .results { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #profile-demo label { font-size: 0; gap: 0; }
  #profile-demo select { max-width: 200px; }
}
@media (max-width: 520px) {
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cards > :last-child:nth-child(odd) { grid-column: span 2; }
  .app > footer span { display: none; }
  #profile-demo select { max-width: 150px; }
  .page-head :is(.filters, .crm-actions) { justify-content: flex-start; width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
