/* =========================================================
   Aracaju Mobilidade — MODO APP (celular e tablet em pé)
   Tudo abaixo de 992px. Desktop não é afetado.
   Trabalha junto com assets/js/app.js, que marca as classes
   .page-head, .table-stack, .form-actions-sticky etc.
   ========================================================= */

:root {
    --app-bar-h:   56px;
    --tab-bar-h:   64px;
    --safe-top:    env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --app-ink:     #1a1a1a;
    --app-muted:   #6b6b6b;
    --app-line:    #e6e6e6;
    --app-surface: #ffffff;
    --app-canvas:  #f2f2f4;
    --app-accent-ink: #a8740a; /* dourado escuro: contraste AA sobre branco */
}

/* Elementos de app ficam escondidos no desktop */
.app-bar,
.tab-bar,
.sheet,
.sheet-backdrop { display: none; }

@media (max-width: 991.98px) {

    body.has-sidebar {
        background: var(--app-canvas);
        -webkit-tap-highlight-color: transparent;
    }

    /* Sidebar e topbar de desktop saem de cena */
    .sidebar,
    .topbar { display: none !important; }

    .main-content {
        margin-left: 0;
        min-height: 100vh;
    }

    .content-area {
        min-height: 0;
        padding: 16px 16px calc(var(--tab-bar-h) + var(--safe-bottom) + 24px) !important;
    }

    /* ----- Barra superior ----- */
    .app-bar {
        position: sticky;
        top: 0;
        z-index: 1030;
        display: flex;
        align-items: center;
        gap: 8px;
        height: calc(var(--app-bar-h) + var(--safe-top));
        padding: var(--safe-top) 8px 0;
        background: var(--sidebar-bg);
        color: #fff;
    }

    .app-bar-logo {
        display: flex;
        align-items: center;
        padding: 0 6px;
    }
    .app-bar-logo img { height: 32px; width: auto; }

    .app-bar-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        border-radius: 12px;
        color: #fff;
        font-size: 1.3rem;
        text-decoration: none;
    }
    .app-bar-btn:hover,
    .app-bar-btn:active { background: #222; color: #fff; }

    .app-bar-title {
        flex: 1;
        min-width: 0;
        margin: 0;
        font-size: 1.05rem;
        font-weight: 700;
        letter-spacing: -0.01em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .app-bar-avatar {
        width: 40px;
        height: 40px;
        margin-right: 4px;
        border-radius: 50%;
        border: 0;
        background: var(--gold);
        color: #1a0e00;
        font-weight: 800;
        font-size: 0.95rem;
        flex-shrink: 0;
    }

    /* ----- Barra inferior ----- */
    .tab-bar {
        position: fixed;
        inset: auto 0 0 0;
        z-index: 1030;
        display: flex;
        align-items: stretch;
        height: calc(var(--tab-bar-h) + var(--safe-bottom));
        padding-bottom: var(--safe-bottom);
        background: var(--app-surface);
        border-top: 1px solid var(--app-line);
    }

    .tab-bar-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        border: 0;
        background: none;
        color: var(--app-muted);
        font-size: 0.7rem;
        font-weight: 600;
        text-decoration: none;
    }
    .tab-bar-item i { font-size: 1.35rem; line-height: 1; }
    .tab-bar-item:hover { color: var(--app-ink); }
    .tab-bar-item.active { color: var(--app-accent-ink); }

    .tab-bar-fab {
        align-self: center;
        width: 52px;
        height: 52px;
        margin: -18px 6px 0;
        border-radius: 16px;
        border: 0;
        background: var(--gold);
        color: #1a0e00;
        font-size: 1.6rem;
        box-shadow: 0 4px 12px rgba(212, 150, 10, .35);
        transition: transform .15s ease-out;
    }
    .tab-bar-fab:active { transform: scale(.94); }

    .tab-bar-item:focus-visible,
    .tab-bar-fab:focus-visible,
    .app-bar-btn:focus-visible,
    .app-bar-avatar:focus-visible,
    .sheet-tile:focus-visible {
        outline: 2px solid var(--gold);
        outline-offset: 2px;
    }

    /* ----- Folhas (bottom sheets) ----- */
    .sheet-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 1040;
        background: rgba(0, 0, 0, .45);
        opacity: 0;
        pointer-events: none;
        transition: opacity .2s ease-out;
    }
    .sheet-backdrop.open { opacity: 1; pointer-events: auto; }

    .sheet {
        display: block;
        position: fixed;
        inset: auto 0 0 0;
        z-index: 1050;
        max-height: 85vh;
        overflow-y: auto;
        overscroll-behavior: contain;
        padding: 8px 16px calc(24px + var(--safe-bottom));
        background: var(--app-surface);
        border-radius: 20px 20px 0 0;
        transform: translateY(105%);
        transition: transform .25s ease-out, visibility 0s linear .25s;
        visibility: hidden;
    }
    .sheet.open {
        transform: translateY(0);
        visibility: visible;
        transition: transform .25s ease-out;
    }

    .sheet-grip {
        width: 40px;
        height: 4px;
        margin: 4px auto 12px;
        border-radius: 2px;
        background: #d4d4d4;
    }

    .sheet-title {
        font-weight: 700;
        font-size: 1.05rem;
        margin-bottom: 16px;
    }

    .sheet-section {
        margin: 20px 0 8px;
        font-size: 0.7rem;
        font-weight: 700;
        letter-spacing: .1em;
        text-transform: uppercase;
        color: var(--app-muted);
    }

    .sheet-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
    }

    .sheet-tile {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
        padding: 12px 4px;
        border-radius: 14px;
        color: var(--app-ink);
        font-size: 0.78rem;
        font-weight: 500;
        line-height: 1.2;
        text-align: center;
        text-decoration: none;
    }
    .sheet-tile:active,
    .sheet-tile:hover { background: var(--app-canvas); color: var(--app-ink); }

    .sheet-tile-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 48px;
        height: 48px;
        border-radius: 14px;
        background: var(--app-canvas);
        color: var(--app-accent-ink);
        font-size: 1.35rem;
    }
    .sheet-tile.active .sheet-tile-icon { background: var(--gold); color: #1a0e00; }

    .sheet-user {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 8px 4px 16px;
        border-bottom: 1px solid var(--app-line);
    }

    .sheet-user-avatar {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        background: var(--gold);
        color: #1a0e00;
        font-weight: 800;
        flex-shrink: 0;
    }

    body.sheet-open { overflow: hidden; }

    /* ----- Cabeçalho das telas (.page-head, marcado pelo app.js) ----- */
    .page-head {
        flex-wrap: wrap;
        gap: 12px !important;
        margin-bottom: 16px !important;
    }
    .page-head h4 { font-size: 1.15rem; }
    /* título repetido (já está na barra do app) */
    .page-head .page-title-dup { display: none !important; }
    /* botão voltar da tela (a barra do app já tem um) */
    .page-head .page-back { display: none !important; }

    /* Ações do cabeçalho: botões grandes ocupando a largura */
    .page-head .page-actions {
        flex: 1 1 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 8px !important;
    }
    .page-head .page-actions > .btn,
    .page-head > .btn {
        flex: 1 1 auto;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* ----- Cards ----- */
    .card { border-radius: 16px !important; }
    .card-header { border-radius: 16px 16px 0 0 !important; }

    /* ----- Formulários: toque confortável, sem zoom no iPhone ----- */
    .content-area .form-control,
    .content-area .form-select {
        font-size: 16px;
        min-height: 44px;
    }
    .content-area .form-control-sm,
    .content-area .form-select-sm { min-height: 40px; }
    .content-area textarea.form-control { min-height: 88px; }
    .content-area .btn { min-height: 40px; }
    .content-area .btn-sm { min-height: 36px; min-width: 36px; }
    .content-area .form-check-input { width: 1.3em; height: 1.3em; }

    /* Botão salvar fixo no rodapé enquanto o formulário está na tela */
    .form-actions-sticky {
        position: sticky;
        bottom: calc(var(--tab-bar-h) + var(--safe-bottom));
        z-index: 10;
        margin: 16px -16px 0 !important;
        padding: 12px 16px !important;
        background: rgba(242, 242, 244, .96);
        border-top: 1px solid var(--app-line);
    }
    .form-actions-sticky .btn {
        flex: 1;
        min-height: 48px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding-left: 12px !important;
        padding-right: 12px !important;
        white-space: nowrap;
    }
    .form-actions-sticky .btn-primary,
    .form-actions-sticky .btn-success { flex: 2; }

    /* Abas (ex.: Configurações) rolam de lado em vez de quebrar linha */
    .content-area .nav-tabs,
    .content-area .nav-pills {
        flex-wrap: nowrap;
        overflow-x: auto;
        scrollbar-width: none;
        margin-left: -16px;
        margin-right: -16px;
        padding: 0 16px;
    }
    .content-area .nav-tabs::-webkit-scrollbar,
    .content-area .nav-pills::-webkit-scrollbar { display: none; }
    .content-area .nav-tabs .nav-link,
    .content-area .nav-pills .nav-link { white-space: nowrap; }

    /* Agenda (FullCalendar): título em cima, botões embaixo */
    .fc .fc-header-toolbar {
        flex-wrap: wrap;
        gap: 8px;
    }
    .fc .fc-header-toolbar .fc-toolbar-chunk:nth-child(2) {
        order: -1;
        width: 100%;
        text-align: center;
    }
    .fc .fc-toolbar-title { font-size: 1.05rem; font-weight: 700; }
    .fc .fc-button { padding: 8px 12px; }
    .fc .fc-list-event td { padding: 12px 8px; }

    /* Busca e filtros ocupam a largura toda */
    .content-area form .input-group[style*="max-width"] { max-width: none !important; }

    /* Indicador de busca automática (CEP/CNPJ) */
    .lookup-hint {
        display: block;
        margin-top: 4px;
        font-size: 0.75rem;
        color: var(--app-muted);
    }
}

/* Dica de busca também aparece no desktop */
.lookup-hint { font-size: 0.75rem; color: #6b6b6b; }
.lookup-hint.ok   { color: #15803d; }
.lookup-hint.fail { color: #b91c1c; }

/* ----- Tabelas viram lista de cards (até 767px) ----- */
@media (max-width: 767.98px) {

    .card.has-stack {
        background: transparent;
        box-shadow: none !important;
        border: 0;
    }
    .card.has-stack > .card-header {
        background: transparent !important;
        border: 0 !important;
        padding-left: 4px;
        padding-right: 4px;
    }
    .card.has-stack > .card-body { padding: 0 !important; }

    .table-responsive.has-stack { overflow: visible; }

    .table-stack,
    .table-stack tbody,
    .table-stack tfoot,
    .table-stack tr,
    .table-stack td { display: block; width: 100% !important; }

    .table-stack thead { display: none; }

    .table-stack { background: transparent; margin-bottom: 0; min-width: 0 !important; }
    .table-stack > :not(caption) > * > * { box-shadow: none; }

    .table-stack tr {
        margin-bottom: 12px;
        padding: 12px 16px;
        background: var(--app-surface);
        border: 1px solid var(--app-line);
        border-radius: 16px;
    }

    .table-stack td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 6px 0 !important;
        border: 0 !important;
        background: transparent !important;
        text-align: right !important;
        min-width: 0 !important;
        white-space: normal !important;
    }

    .table-stack td::before {
        content: attr(data-label);
        flex-shrink: 0;
        max-width: 45%;
        color: var(--app-muted);
        font-size: 0.75rem;
        font-weight: 600;
        text-align: left;
    }

    /* 1ª coluna é o título do card */
    .table-stack td.stack-title {
        justify-content: flex-start;
        padding-bottom: 8px !important;
        margin-bottom: 4px;
        border-bottom: 1px solid var(--app-line) !important;
        font-size: 1rem;
        text-align: left !important;
    }
    .table-stack td.stack-title::before { display: none; }

    /* Células sem rótulo (ações) ficam no rodapé do card */
    .table-stack td.stack-actions {
        justify-content: flex-end;
        margin-top: 4px;
        padding-top: 10px !important;
        border-top: 1px solid var(--app-line) !important;
    }
    .table-stack td.stack-actions::before { display: none; }
    .table-stack td.stack-actions .d-flex { flex-wrap: wrap; justify-content: flex-end; gap: 8px !important; }
    .table-stack td.stack-actions .btn { min-height: 40px; min-width: 44px; }

    /* Campos de formulário dentro da tabela: rótulo em cima, campo inteiro */
    .table-stack td.stack-input {
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        text-align: left !important;
    }
    .table-stack td.stack-input::before { max-width: none; }

    /* Linha única ocupando a tabela toda (vazio, subtotal) */
    .table-stack td.stack-full {
        justify-content: center;
        text-align: center !important;
    }
    .table-stack td.stack-full::before { display: none; }

    /* Célula sem valor ("—") não ocupa espaço no card */
    .table-stack td.stack-empty { display: none; }

    .table-stack tfoot tr {
        background: #fbf6e9;
        border-color: #efdcae;
    }
}

@media (prefers-reduced-motion: reduce) {
    .sheet,
    .sheet-backdrop,
    .tab-bar-fab { transition: none; }
}
