/* ============================================================================
   QMINC Shell — camada global responsiva (PC · tablet · celular)
   ----------------------------------------------------------------------------
   Carregada por templates/_shell_head.html em TODA página com layout.
   Não mexe em métrica de tela nenhuma: só decide o regime e o comportamento
   do menu, do toque e do overflow. Regime por largura + tipo de ponteiro,
   nunca por user-agent:

     phone    < 768px                → menu vira gaveta por cima do conteúdo
     tablet   ≤ 1366px OU toque      → menu recolhido por padrão, grades 3/linha
     desktop  o resto                → como está, com botão de recolher

   Estado mora no <html> (decidido antes do primeiro paint pelo script inline
   de _shell_head.html, mantido por static/js/qminc_shell.js):
     data-shell="phone|tablet|desktop"
     .qs-collapsed    sidebar oculta (tablet/desktop) — salvo em localStorage
     .qs-drawer-open  gaveta aberta (phone) — nunca persiste
     .qs-standalone   modo app (display-mode: standalone)
     .qs-ready        JS carregado: só a partir daqui existem transições
   ========================================================================== */

:root {
    --qs-strip: 52px;          /* faixa reservada ao botão de recolher */
    --qs-drawer-w: min(300px, 85vw);
    --qs-accent: #00d4ff;
}

/* ── 5. A página nunca rola de lado ──────────────────────────────────────── */
html, body {
    overflow-x: hidden;
    max-width: 100%;
}

.layout { min-width: 0; }

/* filhos de flex têm min-width:auto — é a causa nº 1 de overflow lateral */
.layout > .content,
.layout > main {
    min-width: 0;
    max-width: 100%;
}

/* tabela larga rola dentro dela mesma (o JS embrulha as que não têm scroll) */
.qs-table-scroll {
    overflow-x: auto;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
}

/* ── 6. Altura real da tela em modo app (barra do navegador some/aparece) ── */
@supports (height: 100dvh) {
    .layout { min-height: 100dvh; }
    .layout > aside.sidebar { height: 100dvh; }
}

/* ── 2. Botão de recolher (ícone de painel, estilo VS Code) ──────────────── */
.qs-toggle {
    position: fixed;
    top: 4px;
    left: 4px;
    z-index: 1300;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #94a3b8;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.qs-toggle svg {
    width: 22px;
    height: 22px;
    display: block;
}

/* painel "preenchido" = barra visível; vazio = barra oculta */
.qs-toggle .qs-ico-panel { fill: currentColor; opacity: .85; }
.qs-toggle.is-hidden .qs-ico-panel { fill: none; opacity: 1; }

.qs-toggle:focus-visible {
    outline: 2px solid var(--qs-accent);
    outline-offset: 2px;
}

/* hover só onde hover existe (item 3) */
@media (hover: hover) {
    .qs-toggle:hover {
        color: #fff;
        background: rgba(255, 255, 255, .06);
    }
}

/* banners fixos (simulação / playground) ocupam o topo: o botão desce */
body:has(> div[style*="z-index:9999"]) .qs-toggle { top: 36px; }

/* ── Regimes tablet e desktop ─────────────────────────────────────────────── */
/* a faixa do botão fica DENTRO da sidebar aberta — o botão nunca muda de lugar */
html[data-shell="desktop"] .layout > aside.sidebar,
html[data-shell="tablet"] .layout > aside.sidebar {
    padding-top: var(--qs-strip) !important;
    flex-shrink: 0;
}

/* aberta entre 768 e 900px: vence o display:none antigo das páginas master */
html:not(.qs-collapsed)[data-shell="tablet"] .layout > aside.sidebar,
html:not(.qs-collapsed)[data-shell="desktop"] .layout > aside.sidebar {
    display: flex;
    flex-direction: column;
}

/* recolhida: some, e o trilho de 52px sobra para o botão */
html.qs-collapsed[data-shell="desktop"] .layout > aside.sidebar,
html.qs-collapsed[data-shell="tablet"] .layout > aside.sidebar {
    display: none;
}

html.qs-collapsed[data-shell="desktop"] .layout,
html.qs-collapsed[data-shell="tablet"] .layout {
    padding-left: var(--qs-strip);
    box-sizing: border-box;
}

/* tablet: grades com 4+ colunas passam a 3 por linha */
html[data-shell="tablet"] [data-qs-wide] {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
}

/* ── Regime celular: gaveta ───────────────────────────────────────────────── */
html[data-shell="phone"] .layout {
    padding-top: var(--qs-strip);
    box-sizing: border-box;
}

html[data-shell="phone"] .layout > aside.sidebar {
    /* páginas antigas têm "@media (max-width: 900px) .sidebar { display: none }";
       a gaveta precisa existir mesmo assim */
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    height: 100vh;
    width: var(--qs-drawer-w) !important;
    max-width: 85vw;
    padding-top: var(--qs-strip) !important;
    margin: 0;
    z-index: 1200;
    overflow-y: auto;
    overscroll-behavior: contain;
    transform: translateX(-100%);
    visibility: hidden;
    box-shadow: 0 0 48px rgba(0, 0, 0, .7);
}

@supports (height: 100dvh) {
    html[data-shell="phone"] .layout > aside.sidebar { height: 100dvh; }
}

html[data-shell="phone"].qs-drawer-open .layout > aside.sidebar {
    transform: none;
    visibility: visible;
}

/* fundo escurecido; toque nele fecha (JS) */
.qs-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1100;
    background: rgba(0, 0, 0, .65);
    opacity: 0;
    pointer-events: none;
}

html[data-shell="phone"].qs-drawer-open .qs-backdrop {
    opacity: 1;
    pointer-events: auto;
}

html[data-shell="phone"].qs-drawer-open body { overflow: hidden; }

/* conteúdo em 1 coluna: o padding grande de PC vira 16px e as grades empilham
   (o calendário é a exceção — data-qs-keep, ver tagGrids no JS) */
html[data-shell="phone"] .layout > .content,
html[data-shell="phone"] .layout > main {
    padding: 16px !important;
}

html[data-shell="phone"] .page-header {
    flex-wrap: wrap;
    gap: 8px;
}

html[data-shell="phone"] [data-qs-grid]:not([data-qs-keep]) {
    grid-template-columns: minmax(0, 1fr) !important;
}

/* transições só depois do JS marcar qs-ready — o estado inicial não anima */
html.qs-ready[data-shell="phone"] .layout > aside.sidebar {
    transition: transform .25s ease, visibility 0s linear .25s;
}

html.qs-ready[data-shell="phone"].qs-drawer-open .layout > aside.sidebar {
    transition: transform .25s ease, visibility 0s;
}

html.qs-ready .qs-backdrop { transition: opacity .2s ease; }

/* ── 3. Toque e caneta ────────────────────────────────────────────────────── */
@media (pointer: coarse) {
    body { touch-action: manipulation; }

    /* alvo mínimo de 44px */
    button,
    .btn,
    [role="button"],
    select,
    .nav-link,
    .nav-link-logout,
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]),
    textarea {
        min-height: 44px;
    }

    .nav-link,
    .nav-link-logout {
        display: flex;
        align-items: center;
    }

    input[type="checkbox"],
    input[type="radio"] {
        min-width: 22px;
        min-height: 22px;
    }

    /* ≥ 16px em campo: iOS não dá zoom ao focar */
    input,
    select,
    textarea {
        font-size: max(16px, 1em);
    }
}

/* tooltip abre por toque (o JS alterna .qs-tip-open onde não há hover) */
@media (hover: none) {
    .info-tooltip-wrapper.qs-tip-open .info-tooltip-content {
        visibility: visible !important;
        opacity: 1 !important;
    }
}

/* bolha genérica para [title] / [data-tooltip] em telas de toque */
.qs-tip {
    position: fixed;
    z-index: 1400;
    max-width: min(320px, 90vw);
    padding: 8px 10px;
    background: #1a1a1a;
    color: #eee;
    border: 1px solid #333;
    border-radius: 6px;
    font-size: 13px;
    line-height: 1.4;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
    pointer-events: none;
}

/* ── 6. Modo app: respeitar as áreas seguras ──────────────────────────────── */
html.qs-standalone body {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-bottom: env(safe-area-inset-bottom);
}

html.qs-standalone .qs-toggle {
    top: calc(4px + env(safe-area-inset-top));
    left: calc(4px + env(safe-area-inset-left));
}

html.qs-standalone .layout > aside.sidebar {
    padding-top: calc(var(--qs-strip) + env(safe-area-inset-top)) !important;
    padding-bottom: env(safe-area-inset-bottom);
}

html.qs-standalone[data-shell="phone"] .layout {
    padding-top: calc(var(--qs-strip) + env(safe-area-inset-top));
}

/* ── 7. Menos movimento ───────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}
