:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --background: #f8fafc;
    --surface: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    overflow-x: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--background);
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

/* Pull to Refresh */
.pull-to-refresh {
    position: fixed;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    background: var(--primary-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 20px;
    transition: top 0.3s ease, transform 0.3s ease;
    z-index: 9999;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.pull-to-refresh.visible {
    top: 20px;
}

.pull-to-refresh.refreshing {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: translateX(-50%) rotate(0deg); }
    to { transform: translateX(-50%) rotate(360deg); }
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    width: 100%;
    overflow-x: hidden;
}

header {
    text-align: center;
    padding: 40px 20px;
    background: var(--surface);
    border-radius: 12px;
    box-shadow: var(--shadow);
    margin-bottom: 30px;
    width: 100%;
    overflow-x: hidden;
}

.user-greeting-top {
    color: var(--text-secondary);
    font-size: 0.85rem;
    text-align: left;
    margin-bottom: 15px;
    padding: 0 20px;
}

.header-container {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 20px;
    padding: 0 20px;
}

.header-container h1 {
    font-size: 1.5rem;
    color: var(--primary-color);
    margin: 0;
    flex: 1;
    text-align: left;
}

.header-logo {
    width: 80px;
    height: 80px;
    border-radius: 18px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    flex-shrink: 0;
}

header p {
    color: var(--text-secondary);
    font-size: 1.1rem;
}

main {
    display: grid;
    gap: 30px;
    width: 100%;
    overflow-x: hidden;
}

section {
    background: var(--surface);
    padding: 30px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    width: 100%;
    overflow-x: hidden;
}

section h2 {
    margin-bottom: 20px;
    color: var(--text-primary);
    font-size: 1.5rem;
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 10px;
}

/* Formulário */
.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: var(--text-primary);
}

.form-group input,
.form-group textarea {
    width: 100%;
    padding: 12px;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    font-size: 1rem;
    transition: border-color 0.3s;
    -webkit-appearance: none;
    appearance: none;
    background-color: var(--surface);
}

.form-group input:focus,
.form-group textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.form-group input[readonly] {
    background-color: var(--background);
    cursor: not-allowed;
}

.btn-primary {
    width: 100%;
    padding: 14px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.3s;
}

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

.btn-danger {
    padding: 8px 16px;
    background: var(--danger-color);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: opacity 0.3s;
}

.btn-danger:hover {
    opacity: 0.9;
}

.btn-secondary {
    padding: 14px 28px;
    background: var(--border-color);
    color: var(--text-primary);
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.3s;
}

.btn-secondary:hover {
    background: var(--text-secondary);
    color: white;
}

/* Botão de Registo Rápido */
.quick-action-section {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    padding: 0;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(16, 185, 129, 0.3);
    overflow: hidden;
}

.btn-register-large {
    width: 100%;
    padding: 28px 32px;
    background: transparent;
    border: none;
    color: white;
    font-size: 1.5rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: all 0.3s ease;
    position: relative;
}

.btn-register-large:hover {
    transform: translateY(-2px);
    background: rgba(255, 255, 255, 0.1);
}

.btn-register-large:active {
    transform: translateY(0);
}

.btn-icon {
    font-size: 2.5rem;
    animation: pulse 2s infinite;
}

.btn-text {
    flex: 1;
    text-align: center;
}

.btn-arrow {
    font-size: 2rem;
    transition: transform 0.3s ease;
}

.btn-register-large:hover .btn-arrow {
    transform: translateX(8px);
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    overflow-y: auto;
    animation: fadeIn 0.3s ease;
}

.modal.show {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-content {
    background: var(--surface);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    width: 100%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideUp 0.3s ease;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24px;
    border-bottom: 2px solid var(--border-color);
}

.modal-header h2 {
    margin: 0;
    color: var(--primary-color);
    font-size: 1.8rem;
    border: none;
    padding: 0;
}

.modal-close {
    background: none;
    border: none;
    font-size: 2.5rem;
    color: var(--text-secondary);
    cursor: pointer;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.3s;
}

.modal-close:hover {
    background: var(--danger-color);
    color: white;
    transform: rotate(90deg);
}

.modal-content form {
    padding: 24px;
}

.modal-actions {
    display: flex;
    gap: 12px;
    margin-top: 24px;
    justify-content: flex-end;
}

.modal-actions button {
    flex: 1;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        transform: translateY(50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Seletor de Mês */
.month-selector {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    margin-bottom: 20px;
}

.month-nav {
    background: var(--primary-color);
    color: white;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.month-nav:hover {
    background: var(--primary-hover);
    transform: scale(1.1);
}

#statsMonth {
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--text-primary);
    min-width: 150px;
    text-align: center;
}

/* Estatísticas */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    width: 100%;
}

.stat-card {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    color: white;
    padding: 25px;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    box-shadow: var(--shadow-lg);
    min-width: 0;
}

.stat-label {
    font-size: 0.9rem;
    opacity: 0.9;
    margin-bottom: 10px;
}

.stat-value {
    font-size: 1.8rem;
    font-weight: 700;
}

/* Tabela */
.table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    max-width: 100%;
}

table {
    width: 100%;
    border-collapse: collapse;
    min-width: 500px;
}

thead {
    background: var(--background);
}

th {
    padding: 15px;
    text-align: left;
    font-weight: 600;
    color: var(--text-primary);
    border-bottom: 2px solid var(--border-color);
}

td {
    padding: 15px;
    border-bottom: 1px solid var(--border-color);
}

tbody tr:hover {
    background: var(--background);
}

.no-data {
    text-align: center;
    color: var(--text-secondary);
    font-style: italic;
}

/* Footer */
footer {
    text-align: center;
    padding: 20px;
    margin-top: 30px;
    color: var(--text-secondary);
}

/* Dashboard de Orçamento */
.budget-section {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    position: relative;
    overflow: hidden;
}

.budget-section h2 {
    color: white;
    border-bottom-color: rgba(255, 255, 255, 0.3);
}

.budget-card {
    display: flex;
    flex-direction: column;
    gap: 25px;
}

.budget-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 15px;
}

.budget-amount {
    display: flex;
    flex-direction: column;
}

.budget-label {
    font-size: 0.9rem;
    opacity: 0.9;
    margin-bottom: 5px;
}

.budget-total {
    font-size: 2.5rem;
    font-weight: 700;
    line-height: 1;
}

.budget-month {
    background: rgba(255, 255, 255, 0.2);
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 0.95rem;
    font-weight: 600;
    backdrop-filter: blur(10px);
}

.budget-progress-container {
    position: relative;
}

.budget-progress-bar {
    height: 30px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 15px;
    overflow: hidden;
    position: relative;
}

.budget-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #10b981 0%, #059669 100%);
    border-radius: 15px;
    transition: width 0.5s ease, background 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: 15px;
    min-width: 0%;
}

.budget-progress-fill.warning {
    background: linear-gradient(90deg, #f59e0b 0%, #d97706 100%);
}

.budget-progress-fill.danger {
    background: linear-gradient(90deg, #ef4444 0%, #dc2626 100%);
}

.budget-percentage {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-weight: 700;
    font-size: 1rem;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.budget-details {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 15px;
    width: 100%;
}

.budget-detail {
    background: rgba(255, 255, 255, 0.15);
    padding: 20px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 15px;
    backdrop-filter: blur(10px);
    transition: transform 0.2s ease;
    min-width: 0;
}

.budget-detail:hover {
    transform: translateY(-2px);
}

.budget-detail.remaining {
    background: rgba(16, 185, 129, 0.2);
}

.detail-icon {
    font-size: 2rem;
}

.detail-content {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.detail-label {
    font-size: 0.85rem;
    opacity: 0.9;
    margin-bottom: 5px;
}

.detail-value {
    font-size: 1.5rem;
    font-weight: 700;
}

.budget-alert {
    background: rgba(239, 68, 68, 0.2);
    border: 2px solid rgba(239, 68, 68, 0.5);
    padding: 15px 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    gap: 12px;
    backdrop-filter: blur(10px);
    animation: pulse 2s infinite;
}

.alert-icon {
    font-size: 1.5rem;
}

.alert-text {
    font-weight: 600;
    flex: 1;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.8;
    }
}

/* Mensagens */
.message {
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 20px;
    font-weight: 500;
}

.message.success {
    background: #d1fae5;
    color: #065f46;
    border: 1px solid var(--success-color);
}

.message.error {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid var(--danger-color);
}

/* Responsive */
@media (max-width: 768px) {
    .container {
        padding: 10px;
        max-width: 100vw;
    }

    header {
        padding: 25px 15px;
        margin-bottom: 15px;
        border-radius: 10px;
    }

    .header-logo {
        width: 70px;
        height: 70px;
        margin-bottom: 12px;
    }

    header h1 {
        font-size: 1.6rem;
        word-break: break-word;
    }

    header p {
        font-size: 0.95rem;
    }

    section {
        padding: 20px 15px;
        border-radius: 10px;
    }

    section h2 {
        font-size: 1.25rem;
        margin-bottom: 15px;
    }

    main {
        gap: 15px;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .stat-card {
        padding: 15px 10px;
    }

    .stat-label {
        font-size: 0.8rem;
    }

    .stat-value {
        font-size: 1.4rem;
    }

    .budget-total {
        font-size: 2rem;
    }

    .budget-details {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .detail-value {
        font-size: 1.3rem;
    }

    .form-group {
        margin-bottom: 15px;
    }

    .form-group label {
        font-size: 0.95rem;
        margin-bottom: 6px;
    }

    .form-group input,
    .form-group textarea {
        padding: 12px;
        font-size: 16px; /* Evita zoom no iOS */
    }

    table {
        font-size: 0.85rem;
    }

    th, td {
        padding: 8px 6px;
    }

    .btn-danger {
        padding: 6px 12px;
        font-size: 0.85rem;
    }
}

@media (max-width: 480px) {
    .container {
        padding: 8px;
        max-width: 100vw;
    }

    header {
        padding: 20px 12px;
        margin-bottom: 12px;
    }

    .header-logo {
        width: 60px;
        height: 60px;
        margin-bottom: 10px;
    }

    header h1 {
        font-size: 1.5rem;
        margin-bottom: 5px;
        word-break: break-word;
    }

    header p {
        font-size: 0.9rem;
    }

    section {
        padding: 16px 12px;
    }

    section h2 {
        font-size: 1.15rem;
        padding-bottom: 8px;
        margin-bottom: 12px;
        word-break: break-word;
    }

    main {
        gap: 12px;
    }

    .form-group {
        margin-bottom: 12px;
    }

    .form-group label {
        font-size: 0.9rem;
        margin-bottom: 5px;
        font-weight: 600;
    }

    .form-group input,
    .form-group textarea {
        padding: 14px 12px; /* Mais padding para toque */
        font-size: 16px; /* Previne zoom no iOS */
        border-radius: 10px;
        width: 100%;
    }

    .btn-primary {
        padding: 16px 12px;
        font-size: 1.05rem;
        border-radius: 10px;
        font-weight: 700;
        width: 100%;
    }

    .stats-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .stat-card {
        padding: 16px 12px;
    }

    .stat-label {
        font-size: 0.85rem;
    }

    .stat-value {
        font-size: 1.5rem;
    }

    .budget-header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .budget-month {
        text-align: center;
        padding: 8px 16px;
    }

    .budget-total {
        font-size: 2rem;
    }

    .budget-label {
        font-size: 0.85rem;
    }

    .budget-progress-bar {
        height: 28px;
    }

    .budget-percentage {
        font-size: 0.9rem;
    }

    .budget-details {
        gap: 10px;
        grid-template-columns: 1fr;
    }

    .budget-detail {
        padding: 16px 12px;
    }

    .detail-icon {
        font-size: 1.8rem;
    }

    .detail-label {
        font-size: 0.8rem;
    }

    .detail-value {
        font-size: 1.3rem;
        word-break: break-word;
    }

    .budget-alert {
        padding: 12px 16px;
        font-size: 0.9rem;
    }

    /* Tabela responsiva - scroll horizontal apenas na tabela */
    .table-container {
        margin: 0 -12px;
        padding: 0;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        width: calc(100% + 24px);
        max-width: calc(100% + 24px);
    }

    table {
        font-size: 0.75rem;
        min-width: 600px;
        margin: 0 12px;
    }

    th, td {
        padding: 10px 6px;
        white-space: nowrap;
    }

    th {
        font-size: 0.8rem;
        position: sticky;
        top: 0;
        background: var(--background);
        z-index: 10;
    }

    .btn-danger {
        padding: 8px 12px;
        font-size: 0.8rem;
        border-radius: 6px;
        white-space: nowrap;
    }

    /* Melhorias para toque em dispositivos móveis */
    button, .btn-primary, .btn-danger {
        min-height: 44px;
        -webkit-tap-highlight-color: rgba(37, 99, 235, 0.2);
        touch-action: manipulation;
    }

    input, textarea, select {
        font-size: 16px !important; /* Previne zoom no iOS */
        -webkit-appearance: none;
        appearance: none;
    }

    /* Melhorar mensagens em mobile */
    .message {
        margin: 0 -12px 12px -12px;
        border-radius: 0;
        padding: 12px 16px;
    }

    /* Ajustar footer */
    footer {
        padding: 16px 12px;
        margin-top: 20px;
        font-size: 0.85rem;
    }
}

/* Ajustes para iPhones muito pequenos (SE, etc) */
@media (max-width: 375px) {
    .container {
        padding: 6px;
    }

    header h1 {
        font-size: 1.3rem;
    }

    .budget-total {
        font-size: 1.8rem;
    }

    .stat-value {
        font-size: 1.3rem;
    }

    .detail-value {
        font-size: 1.2rem;
    }

    .stats-grid {
        gap: 8px;
    }

    .stat-card {
        padding: 14px 10px;
    }
}

/* Ajustes específicos para iPhone X e superiores (com notch) */
@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
    
    .container {
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
}

/* Orientação landscape em mobile */
@media (max-width: 896px) and (orientation: landscape) {
    header {
        padding: 15px;
    }

    header h1 {
        font-size: 1.4rem;
    }

    section {
        padding: 15px;
    }

    .budget-header {
        flex-direction: row;
        align-items: center;
    }

    .stats-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
    }

    .stat-card {
        padding: 12px;
    }
}

/* Modo dark para sistemas que suportam */
@media (prefers-color-scheme: dark) {
    :root {
        --background: #0f172a;
        --surface: #1e293b;
        --text-primary: #f1f5f9;
        --text-secondary: #94a3b8;
        --border-color: #334155;
    }
}
