/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    width: 100%;
}

:root {
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #64748b;
    --success-color: #10b981;
    --warning-color: #f59e0b;
    --danger-color: #ef4444;
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --border-radius: 12px;
    --transition: all 0.3s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    color: var(--text-primary);
    line-height: 1.6;
    padding: 2rem 1rem;
    margin: 0;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
}

.container {
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
    touch-action: pan-y;
}

/* Header */
.header {
    text-align: center;
    margin-bottom: 2rem;
    color: white;
}

.title {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.subtitle {
    font-size: 1.1rem;
    opacity: 0.95;
    font-weight: 300;
}

/* Card */
.card {
    background: var(--card-bg);
    border-radius: var(--border-radius);
    padding: 2rem;
    box-shadow: var(--shadow-lg);
    margin-bottom: 1.5rem;
    max-width: 100%;
    box-sizing: border-box;
    touch-action: pan-y;
}

/* Income Section */
.income-section {
    margin-bottom: 2rem;
}

.income-label {
    display: block;
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
}

.income-input {
    width: 100%;
    padding: 0.875rem 1rem;
    font-size: 1.25rem;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    transition: var(--transition);
    font-weight: 500;
}

.income-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

/* Progress Section */
.progress-section {
    background: white;
    border-radius: 8px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-lg);
    transition: box-shadow 0.3s ease;
    max-width: 900px;
    margin-left: auto;
    margin-right: auto;
    touch-action: pan-y;
}

.progress-section.sticky-active {
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.progress-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.progress-label {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 0.95rem;
}

.progress-stats {
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.progress-bar-container {
    width: 100%;
    height: 32px;
    background: white;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
    margin-bottom: 0.75rem;
}

.progress-bar {
    height: 100%;
    width: 0;
    background: var(--success-color);
    transition: width 0.5s ease, background-color 0.3s ease;
    border-radius: 16px;
    position: relative;
}

.progress-bar.warning {
    background: var(--warning-color);
}

.progress-bar.danger {
    background: var(--danger-color);
}

.progress-footer {
    text-align: right;
}

.remaining-label {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.remaining-label.positive {
    color: var(--success-color);
}

.remaining-label.negative {
    color: var(--danger-color);
}

/* Summary Section */
.summary-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: var(--bg-color);
    border-radius: 8px;
}

.summary-item {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.summary-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.summary-value {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
}

/* Expenses Section */
.expenses-section {
    margin-top: 2rem;
    padding-bottom: 120px;
}

.section-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 1.5rem;
    color: var(--text-primary);
}

/* Table View (Desktop) */
.expenses-table-wrapper {
    overflow-x: auto;
    margin-bottom: 1rem;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
}

.expenses-table {
    width: 100%;
    border-collapse: collapse;
}

.expenses-table thead th {
    text-align: left;
    padding: 0.75rem 0.5rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border-color);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.expenses-table tbody tr {
    border-bottom: 1px solid var(--border-color);
    transition: background-color 0.2s ease;
}

.expenses-table tbody tr:hover {
    background-color: var(--bg-color);
}

.expenses-table tbody td {
    padding: 1rem 0.5rem;
}

.expenses-table input {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.95rem;
    transition: var(--transition);
}

.expenses-table input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.expense-name-input {
    min-width: 150px;
}

.expense-price-input,
.expense-frequency-input {
    min-width: 80px;
}

.expense-monthly-total,
.expense-percentage {
    font-weight: 600;
    color: var(--text-primary);
}

.expense-percentage {
    color: var(--primary-color);
}

/* Mobile Card View */
.expenses-cards {
    display: none;
}

.expense-card {
    background: var(--bg-color);
    border-radius: 8px;
    padding: 1.25rem;
    margin-bottom: 1rem;
    border: 1px solid var(--border-color);
}

.expense-card-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
    gap: 1rem;
}

.expense-card-row:last-child {
    margin-bottom: 0;
}

.expense-card-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
    font-weight: 500;
    min-width: 90px;
}

.expense-card-value {
    flex: 1;
}

.expense-card-value input {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.95rem;
}

.expense-card-value input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.expense-card-display {
    font-weight: 600;
    color: var(--text-primary);
}

.expense-card-percentage {
    color: var(--primary-color);
    font-weight: 600;
}

/* Helper Text */
.helper-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
    font-style: italic;
}

/* Order Controls */
.order-controls {
    display: flex;
    gap: 0.25rem;
    align-items: center;
}

.btn-order {
    width: 32px;
    height: 32px;
    padding: 0;
    border: 1px solid var(--border-color);
    background: white;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: bold;
    color: var(--text-secondary);
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.btn-order:hover:not(:disabled) {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
    transform: translateY(-1px);
}

.btn-order:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.expense-card-header {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 0.75rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

/* Mobile Actions */
.mobile-actions {
    display: none;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 1.5rem;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    box-shadow: var(--shadow-sm);
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

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

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

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

.btn-secondary {
    background: white;
    color: var(--text-primary);
    border: 2px solid var(--border-color);
}

.btn-secondary:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.btn-delete {
    padding: 0.5rem 1rem;
    background: white;
    color: var(--danger-color);
    border: 1px solid var(--danger-color);
    font-size: 0.875rem;
}

.btn-delete:hover {
    background: var(--danger-color);
    color: white;
}

.btn-icon {
    font-size: 1.25rem;
    line-height: 1;
}

.actions-section {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    gap: 1rem;
    justify-content: center;
    align-items: center;
    padding: 1.25rem 1rem;
    background: white;
    box-shadow: 0 -4px 6px -1px rgba(0, 0, 0, 0.1), 0 -2px 4px -1px rgba(0, 0, 0, 0.06);
    z-index: 50;
    border-top: 1px solid var(--border-color);
}

/* Error Messages */
.error-message {
    color: var(--danger-color);
    font-size: 0.875rem;
    margin-top: 0.5rem;
    font-weight: 500;
    min-height: 1.25rem;
}

/* Success Messages */
.success-message {
    color: var(--success-color);
    font-size: 0.875rem;
    margin-top: 0.75rem;
    font-weight: 600;
    text-align: center;
    min-height: 1.25rem;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.success-message.show {
    opacity: 1;
}

/* Data Management Section */
.data-management-section {
    margin-top: 2.5rem;
    padding-top: 2rem;
    border-top: 2px solid var(--border-color);
}

.data-privacy-note {
    text-align: center;
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 1rem;
    font-style: italic;
}

.data-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    flex-wrap: wrap;
}

.checkbox-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    user-select: none;
}

.checkbox-container input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
    accent-color: var(--primary-color);
}

.checkbox-label {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-primary);
}

.btn-export-pdf {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    background: var(--success-color);
    color: white;
    box-shadow: var(--shadow-sm);
}

.btn-export-pdf:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
    filter: brightness(1.1);
}

.btn-export-pdf:active {
    transform: translateY(0);
}

.btn-clear-data {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    background: var(--warning-color);
    color: white;
    box-shadow: var(--shadow-sm);
}

.btn-clear-data:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
    filter: brightness(1.05);
}

.btn-clear-data:active {
    transform: translateY(0);
}

.btn-clear-data.confirm {
    background: var(--danger-color);
}

.btn-clear-data.confirm:hover {
    filter: brightness(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.5);
    backdrop-filter: blur(4px);
    animation: fadeIn 0.3s ease;
}

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

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

.modal-content {
    background: white;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
    width: 90%;
    max-width: 500px;
    animation: slideUp 0.3s ease;
}

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

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

.modal-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

.modal-close {
    background: none;
    border: none;
    font-size: 2rem;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: var(--transition);
}

.modal-close:hover {
    background: var(--bg-color);
    color: var(--text-primary);
}

.modal-body {
    padding: 1.5rem;
}

.modal-label {
    display: block;
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
}

.modal-input {
    width: 100%;
    padding: 0.875rem 1rem;
    font-size: 1rem;
    border: 2px solid var(--border-color);
    border-radius: 8px;
    transition: var(--transition);
    box-sizing: border-box;
}

.modal-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.modal-helper {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-top: 0.5rem;
    font-style: italic;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 1rem;
    padding: 1.5rem;
    border-top: 1px solid var(--border-color);
}

/* Footer */
.footer {
    text-align: center;
    color: white;
    font-size: 0.875rem;
    opacity: 0.9;
    padding: 1rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    body {
        padding: 1rem 0.5rem;
    }

    .title {
        font-size: 2rem;
    }

    .subtitle {
        font-size: 1rem;
    }

    .card {
        padding: 1.5rem;
    }

    .income-input {
        font-size: 1.1rem;
    }

    .summary-section {
        grid-template-columns: repeat(2, 1fr);
        padding: 1rem;
    }

    .summary-value {
        font-size: 1.1rem;
    }

    /* Hide table, show cards on mobile */
    .expenses-table-wrapper {
        display: none;
    }

    .expenses-cards {
        display: block;
    }

    .expenses-section {
        padding-bottom: 100px;
    }

    .actions-section {
        padding: 1rem;
    }

    #addItemBtn,
    #sortBtn {
        display: none;
    }

    .btn {
        width: 100%;
        max-width: 400px;
        justify-content: center;
    }

    .mobile-actions {
        display: flex;
    }

    .data-controls {
        flex-direction: column;
        gap: 1rem;
    }

    .btn-clear-data,
    .btn-export-pdf {
        width: 100%;
        max-width: 300px;
    }

    .modal-content {
        width: 95%;
        margin: 1rem;
    }

    .modal-footer {
        flex-direction: column;
    }

    .modal-footer .btn {
        width: 100%;
    }
}

@media (max-width: 480px) {
    .progress-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .progress-stats {
        font-size: 0.85rem;
    }

    .summary-section {
        grid-template-columns: 1fr;
    }
}
