/**
 * Formaliza+ Mobile Spacing Fix v3.9.64
 * 
 * Melhorias de responsividade:
 * - Margens e padding consistentes
 * - Safe area para notch/barra
 * - Recibo otimizado para mobile
 * - Cards e elementos com espaçamento adequado
 * 
 * NÃO ALTERA: lógica de negócio, cálculos fiscais, regras IRT/INSS
 */

/* ===== BASE MOBILE RESET ===== */
@media (max-width: 767px) {
    body {
        -webkit-text-size-adjust: 100%;
        -webkit-tap-highlight-color: transparent;
    }

    /* Container principal - evitar elementos colados às margens */
    main {
        padding-left: 1rem;
        padding-right: 1rem;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 80px);
    }

    /* Classe utilitária para shells de página */
    .mobile-page-shell {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    /* Stack vertical com espaçamento */
    .mobile-stack-tight > * + * {
        margin-top: 0.75rem;
    }

    .mobile-stack-normal > * + * {
        margin-top: 1rem;
    }

    .mobile-stack-loose > * + * {
        margin-top: 1.5rem;
    }

    /* Cards com padding adequado */
    .mobile-card {
        border-radius: 1rem;
        padding: 1rem;
        margin-left: 0;
        margin-right: 0;
    }

    /* Gap entre secções */
    .mobile-section-gap {
        margin-top: 1rem;
    }

    .mobile-section-gap-lg {
        margin-top: 1.5rem;
    }
}

/* ===== HEADER MOBILE ===== */
@media (max-width: 767px) {
    .mobile-header-safe {
        padding-top: max(env(safe-area-inset-top, 0px), 0.5rem);
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
    }

    .mobile-header-row {
        min-height: 60px;
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .mobile-action-hit {
        min-width: 2.75rem;
        min-height: 2.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* ===== MENU E NAVEGAÇÃO ===== */
@media (max-width: 767px) {
    .mobile-menu-panel {
        padding: 1rem;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 1rem);
    }

    .mobile-menu-panel a {
        min-height: 3rem;
        display: flex;
        align-items: center;
        padding: 0.75rem 1rem;
        margin: 0 -0.5rem;
        border-radius: 0.75rem;
    }

    .mobile-bottom-nav {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
}

/* ===== NOTIFICAÇÕES ===== */
@media (max-width: 767px) {
    .mobile-notification-sheet {
        padding: 1rem;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 1rem);
        max-height: 70vh;
        overflow-y: auto;
    }

    .mobile-notification-item {
        padding: 0.875rem 1rem;
        margin: 0 -0.25rem;
        border-radius: 0.75rem;
    }
}

/* ===== FLASH MESSAGES ===== */
@media (max-width: 767px) {
    .mobile-flash {
        position: fixed;
        left: 1rem;
        right: 1rem;
        max-width: none;
        border-radius: 0.75rem;
        z-index: 9999;
    }
}

/* ===== FORMULÁRIOS MOBILE ===== */
@media (max-width: 767px) {
    .mobile-form-group {
        margin-bottom: 1rem;
    }

    .mobile-form-group label {
        display: block;
        margin-bottom: 0.5rem;
    }

    .mobile-form-group input,
    .mobile-form-group select,
    .mobile-form-group textarea {
        width: 100%;
        padding: 0.875rem 1rem;
        font-size: 16px; /* Evita zoom em iOS */
        border-radius: 0.75rem;
    }

    .mobile-form-actions {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        margin-top: 1.5rem;
    }

    .mobile-form-actions button,
    .mobile-form-actions a {
        width: 100%;
        min-height: 3rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* ===== TABELAS MOBILE ===== */
@media (max-width: 639px) {
    .mobile-table-scroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-left: -1rem;
        margin-right: -1rem;
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .mobile-table-scroll table {
        min-width: 500px;
    }

    /* Cards em vez de tabela */
    .mobile-card-list {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .mobile-card-list-item {
        background: white;
        border-radius: 0.75rem;
        padding: 1rem;
        box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    }
}

/* ===== BOTÕES MOBILE ===== */
@media (max-width: 767px) {
    .mobile-btn-full {
        width: 100%;
        min-height: 3rem;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
    }

    .mobile-btn-group {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .mobile-btn-group-row {
        display: flex;
        gap: 0.75rem;
    }

    .mobile-btn-group-row > * {
        flex: 1;
    }
}

/* ===== MODAIS E SHEETS ===== */
@media (max-width: 767px) {
    .mobile-modal {
        position: fixed;
        inset: 0;
        z-index: 9999;
        display: flex;
        align-items: flex-end;
        justify-content: center;
        padding: 0;
    }

    .mobile-modal-content {
        width: 100%;
        max-height: 90vh;
        background: white;
        border-radius: 1.5rem 1.5rem 0 0;
        padding: 1.5rem;
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 1.5rem);
        overflow-y: auto;
    }

    .mobile-modal-handle {
        width: 40px;
        height: 4px;
        background: #e2e8f0;
        border-radius: 2px;
        margin: 0 auto 1rem;
    }
}

/* ===== STATS CARDS MOBILE ===== */
@media (max-width: 767px) {
    .mobile-stats-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }

    .mobile-stat-card {
        background: white;
        border-radius: 1rem;
        padding: 1rem;
        box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    }

    .mobile-stat-card-value {
        font-size: 1.25rem;
        font-weight: 700;
        line-height: 1.2;
    }

    .mobile-stat-card-label {
        font-size: 0.75rem;
        color: #64748b;
        margin-top: 0.25rem;
    }
}

/* ===== HISTÓRICO MOBILE ===== */
@media (max-width: 767px) {
    .mobile-history-item {
        background: white;
        border-radius: 0.75rem;
        padding: 1rem;
        margin-bottom: 0.75rem;
    }

    .mobile-history-item:last-child {
        margin-bottom: 0;
    }

    .mobile-history-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0.75rem;
    }

    .mobile-history-details {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 0.5rem;
        font-size: 0.875rem;
    }
}

/* ===== EMPTY STATES ===== */
@media (max-width: 767px) {
    .mobile-empty-state {
        text-align: center;
        padding: 2rem 1rem;
    }

    .mobile-empty-state svg,
    .mobile-empty-state img {
        max-width: 120px;
        margin: 0 auto 1rem;
    }
}

/* ===== RECIBO MOBILE ===== */
@media screen and (max-width: 640px) {
    /* Container principal */
    .receipt-container {
        padding: 16px !important;
        margin: 8px !important;
        min-height: auto !important;
    }

    /* Header do recibo */
    .receipt-container .header {
        flex-direction: column;
        gap: 16px;
        text-align: center;
        padding-bottom: 16px;
        margin-bottom: 20px;
    }

    .receipt-container .logo-section {
        justify-content: center;
    }

    .receipt-container .doc-info {
        text-align: center;
    }

    .receipt-container .doc-number {
        font-size: 14px;
    }

    /* Título */
    .receipt-container .doc-title h2 {
        font-size: 14px;
        letter-spacing: 0.5px;
    }

    .receipt-container .doc-title p {
        font-size: 11px;
    }

    .receipt-container .status-badge {
        padding: 5px 16px;
        font-size: 10px;
    }

    /* Grid de dados - coluna única em mobile */
    .receipt-container .data-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .receipt-container .data-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 2px;
    }

    .receipt-container .data-label {
        font-size: 10px;
        min-width: auto;
    }

    .receipt-container .data-value {
        font-size: 13px;
    }

    /* Tabela de valores */
    .receipt-container .values-table th,
    .receipt-container .values-table td {
        padding: 10px 12px;
        font-size: 11px;
    }

    .receipt-container .values-table tr.total-row td {
        font-size: 12px;
    }

    /* Info box */
    .receipt-container .info-box {
        padding: 12px;
        font-size: 10px;
    }

    .receipt-container .info-box p {
        margin-bottom: 6px;
    }

    /* Footer */
    .receipt-container .footer {
        flex-direction: column;
        gap: 20px;
        text-align: center;
        padding-top: 16px;
    }

    .receipt-container .footer-info {
        order: 2;
    }

    .receipt-container .footer-info p {
        font-size: 10px;
    }

    .receipt-container .qr-section {
        order: 1;
    }

    .receipt-container .qr-code {
        width: 100px;
        height: 100px;
        margin: 0 auto;
    }

    .receipt-container .qr-code img {
        width: 100%;
        height: 100%;
    }

    .receipt-container .qr-label {
        font-size: 9px;
    }

    /* Logo strip */
    .receipt-container .logo-strip {
        height: 20px;
        margin-top: 16px;
    }

    .receipt-container .logo-strip svg {
        width: 20px;
        height: 20px;
    }

    /* Security overlay */
    .receipt-container .security-overlay {
        font-size: 8px;
        padding: 6px 12px;
    }

    .receipt-container .security-hash {
        font-size: 7px;
    }

    /* Botões de acção */
    .action-buttons {
        flex-direction: column;
        padding: 16px;
        gap: 10px;
    }

    .action-buttons .btn {
        width: 100%;
        margin: 0;
        padding: 14px 20px;
    }

    /* Watermark menor */
    .receipt-container .watermark svg {
        width: 200px;
        height: 200px;
    }
}

/* ===== PRINT ADJUSTMENTS ===== */
@media print {
    .mobile-header-safe,
    .mobile-bottom-nav,
    .action-buttons {
        display: none !important;
    }

    body {
        padding: 0 !important;
        margin: 0 !important;
    }

    .receipt-container {
        margin: 0 !important;
        padding: 20px !important;
        box-shadow: none !important;
    }
}

/* ===== UTILITIES ===== */
@media (max-width: 767px) {
    /* Esconder em mobile */
    .mobile-hidden {
        display: none !important;
    }

    /* Mostrar apenas em mobile */
    .mobile-only {
        display: block !important;
    }

    /* Texto quebrado */
    .mobile-break-words {
        word-break: break-word;
        overflow-wrap: break-word;
    }

    /* Truncar texto */
    .mobile-truncate {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Margens de segurança */
    .mobile-safe-left {
        padding-left: max(1rem, env(safe-area-inset-left));
    }

    .mobile-safe-right {
        padding-right: max(1rem, env(safe-area-inset-right));
    }

    .mobile-safe-bottom {
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }

    /* Fix para elementos colados */
    .mobile-px-safe {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }

    .mobile-mx-safe {
        margin-left: 1rem !important;
        margin-right: 1rem !important;
    }
}

/* Desktop overrides */
@media (min-width: 768px) {
    .mobile-only {
        display: none !important;
    }
}
