/* ==================== ROOT VARIABLES ==================== */
:root {
    /* Colors */
    --bg: #F5F7FA;
    --card-bg: #FFFFFF;
    --text-primary: #1A1A2E;
    --text-secondary: #6B7280;
    --text-tertiary: #9CA3AF;
    --primary: #3B82F6;
    --primary-dark: #2563EB;
    --income: #10B981;
    --income-bg: #D1FAE5;
    --expense: #EF4444;
    --expense-bg: #FEE2E2;
    --warning: #F59E0B;
    --primary-bg: #DBEAFE;
    
    /* Spacing */
    --padding-xs: 4px;
    --padding-sm: 8px;
    --padding-md: 16px;
    --padding-lg: 20px;
    --padding-xl: 24px;
    
    /* Radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    
    /* Shadows */
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);
    --shadow-xl: 0 20px 60px rgba(0,0,0,0.16);
    
    /* Sizes */
    --header-height: 60px;
    --tab-height: 64px;
    
    /* Font */
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ==================== RESET & BASE ==================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text-primary);
    font-size: 16px;
    line-height: 1.5;
    overflow: hidden;
}

body {
    overflow: hidden;
}

#app {
    height: 100%;
    display: flex;
    flex-direction: column;
    max-width: 480px;
    margin: 0 auto;
    background: var(--bg);
    position: relative;
    overflow: hidden;
}

/* ==================== SCROLLBAR ==================== */
::-webkit-scrollbar {
    width: 4px;
    height: 4px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--text-tertiary);
    border-radius: 4px;
}

/* ==================== LOADING ==================== */
.loading {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    z-index: 999;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--primary-bg);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading p {
    margin-top: 16px;
    color: var(--text-secondary);
}

/* ==================== HEADER ==================== */
.header {
    background: var(--card-bg);
    padding: 12px var(--padding-md) 12px;
    flex-shrink: 0;
    z-index: 10;
    position: sticky;
    top: 0;
    border-bottom: 1px solid rgba(0,0,0,0.04);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.app-title {
    font-size: 20px;
    font-weight: 700;
    color: var(--text-primary);
}

.app-version {
    font-size: 12px;
    color: var(--text-tertiary);
    background: var(--bg);
    padding: 2px 10px;
    border-radius: 12px;
}

/* ==================== MAIN CONTENT ==================== */
.main-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding-bottom: calc(var(--tab-height) + 16px);
    -webkit-overflow-scrolling: touch;
}

.page {
    display: none;
    animation: fadeIn 0.3s ease;
}

.page.active {
    display: block;
}

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

.page-content {
    padding: var(--padding-md);
    padding-bottom: 0;
}

/* ==================== BALANCE CARD ==================== */
.balance-card {
    background: linear-gradient(135deg, #1A1A2E 0%, #16213E 100%);
    border-radius: var(--radius-lg);
    padding: var(--padding-lg);
    margin-bottom: var(--padding-md);
    color: white;
    box-shadow: 0 8px 24px rgba(26, 26, 46, 0.25);
}

.balance-title {
    font-size: 14px;
    opacity: 0.7;
    font-weight: 500;
}

.balance-amount {
    font-size: 32px;
    font-weight: 700;
    margin: 4px 0 16px;
    letter-spacing: -0.5px;
}

.balance-amount.positive { color: var(--income); }
.balance-amount.negative { color: var(--expense); }

.balance-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.balance-item {
    background: rgba(255,255,255,0.08);
    border-radius: var(--radius-sm);
    padding: 12px;
    text-align: center;
}

.balance-item .label {
    font-size: 12px;
    opacity: 0.7;
}

.balance-item .value {
    font-size: 18px;
    font-weight: 700;
    margin-top: 2px;
}

.balance-item .value.income { color: var(--income); }
.balance-item .value.expense { color: var(--expense); }

/* ==================== QUICK ACTIONS ==================== */
.quick-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: var(--padding-md);
}

.quick-actions .btn {
    padding: 14px;
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 16px;
    cursor: pointer;
    transition: transform 0.15s, opacity 0.15s;
    min-height: 48px;
    touch-action: manipulation;
}

.quick-actions .btn:active {
    transform: scale(0.96);
}

.btn-income {
    background: var(--income-bg);
    color: var(--income);
}

.btn-expense {
    background: var(--expense-bg);
    color: var(--expense);
}

/* ==================== SECTION ==================== */
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.section-title {
    font-size: 18px;
    font-weight: 600;
}

.section-count {
    font-size: 14px;
    color: var(--text-secondary);
}

/* ==================== TRANSACTION LIST ==================== */
.transaction-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.transaction-item {
    background: var(--card-bg);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow-sm);
    transition: transform 0.15s;
    position: relative;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateX(-10px); }
    to { opacity: 1; transform: translateX(0); }
}

.transaction-item:active {
    transform: scale(0.98);
}

.transaction-left {
    display: flex;
    flex-direction: column;
    gap: 2px;
    flex: 1;
    min-width: 0;
}

.transaction-category {
    font-weight: 600;
    font-size: 15px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.transaction-meta {
    font-size: 12px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.transaction-meta .comment {
    color: var(--text-tertiary);
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.transaction-right {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.transaction-amount {
    font-weight: 700;
    font-size: 16px;
}

.transaction-amount.income { color: var(--income); }
.transaction-amount.expense { color: var(--expense); }

.transaction-delete {
    background: none;
    border: none;
    color: var(--text-tertiary);
    font-size: 16px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: all 0.2s;
    touch-action: manipulation;
}

.transaction-delete:hover {
    background: var(--expense-bg);
    color: var(--expense);
}

.transaction-delete:active {
    transform: scale(0.8);
}

/* ==================== EMPTY STATE ==================== */
.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.empty-icon {
    font-size: 48px;
    margin-bottom: 12px;
}

.empty-state p {
    font-size: 16px;
}

.empty-hint {
    font-size: 14px;
    color: var(--text-tertiary);
    margin-top: 4px;
}

/* ==================== BOTTOM SHEET ==================== */
.bottom-sheet {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 480px;
    z-index: 200;
    pointer-events: none;
    height: 100vh;
}

.bottom-sheet.open {
    pointer-events: auto;
}

.sheet-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
    opacity: 0;
    transition: opacity 0.3s;
}

.bottom-sheet.open .sheet-overlay {
    opacity: 1;
}

.sheet-content {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--card-bg);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding: 8px var(--padding-lg) 30px;
    transform: translateY(100%);
    transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
    max-height: 85vh;
    overflow-y: auto;
    padding-bottom: calc(30px + env(safe-area-inset-bottom));
}

.bottom-sheet.open .sheet-content {
    transform: translateY(0);
}

.sheet-handle {
    width: 40px;
    height: 4px;
    background: var(--text-tertiary);
    border-radius: 4px;
    margin: 0 auto 12px;
    opacity: 0.4;
}

.sheet-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.sheet-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.sheet-close {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px 8px;
    touch-action: manipulation;
}

/* ==================== FORM ==================== */
.type-toggle {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 16px;
}

.type-toggle .type-btn {
    padding: 10px;
    border: 2px solid var(--bg);
    border-radius: var(--radius-md);
    background: var(--bg);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
    touch-action: manipulation;
}

.type-toggle .type-btn.active {
    border-color: var(--primary);
    background: var(--primary-bg);
    color: var(--primary);
}

.type-toggle .type-btn:active {
    transform: scale(0.96);
}

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

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid var(--bg);
    border-radius: var(--radius-md);
    font-size: 16px;
    font-family: var(--font);
    background: var(--bg);
    transition: border-color 0.2s, background 0.2s;
    -webkit-appearance: none;
    appearance: none;
    min-height: 48px;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary);
    background: var(--card-bg);
}

.form-group select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236B7280' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
}

.btn-save {
    width: 100%;
    padding: 14px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
    min-height: 48px;
    touch-action: manipulation;
}

.btn-save:active {
    opacity: 0.7;
}

.btn-save:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==================== TAB BAR ==================== */
.tab-bar {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 480px;
    display: flex;
    background: var(--card-bg);
    border-top: 1px solid rgba(0,0,0,0.06);
    height: var(--tab-height);
    z-index: 100;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.04);
    padding-bottom: env(safe-area-inset-bottom);
}

.tab-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    background: none;
    border: none;
    padding: 6px 0;
    cursor: pointer;
    color: var(--text-tertiary);
    transition: color 0.2s;
    touch-action: manipulation;
    position: relative;
}

.tab-item.active {
    color: var(--primary);
}

.tab-item .tab-icon {
    font-size: 24px;
    line-height: 1;
}

.tab-item .tab-label {
    font-size: 10px;
    font-weight: 500;
}

/* Специальная кнопка "Добавить" */
.tab-item[data-page="add"] .add-icon {
    font-size: 28px;
    background: var(--primary);
    color: white;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: -8px;
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.35);
    transition: transform 0.2s;
}

.tab-item[data-page="add"]:active .add-icon {
    transform: scale(0.9);
}

/* ==================== REPORTS ==================== */
.report-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
}

.report-tabs::-webkit-scrollbar {
    display: none;
}

.report-tabs .tab {
    padding: 8px 16px;
    border-radius: 20px;
    border: none;
    background: var(--bg);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s;
    flex-shrink: 0;
    touch-action: manipulation;
}

.report-tabs .tab.active {
    background: var(--primary);
    color: white;
}

.report-tabs .tab:active {
    transform: scale(0.96);
}

.report-stats {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
    margin-bottom: 16px;
}

.report-stat {
    background: var(--bg);
    border-radius: var(--radius-md);
    padding: 12px;
    text-align: center;
}

.report-stat .stat-value {
    font-size: 18px;
    font-weight: 700;
}

.report-stat .stat-label {
    font-size: 11px;
    color: var(--text-tertiary);
}

.monthly-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}

.month-item {
    background: var(--bg);
    border-radius: var(--radius-md);
    padding: 12px;
    text-align: center;
}

.month-item .month-name {
    font-weight: 600;
    font-size: 14px;
}

.month-item .month-income {
    font-size: 12px;
    color: var(--income);
}

.month-item .month-expense {
    font-size: 12px;
    color: var(--expense);
}

.month-item .month-balance {
    font-weight: 700;
    font-size: 14px;
}

.month-item .month-balance.positive { color: var(--income); }
.month-item .month-balance.negative { color: var(--expense); }

/* ==================== SETTINGS ==================== */
.settings-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.settings-item {
    background: var(--card-bg);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow-sm);
}

.settings-item .setting-label {
    font-weight: 500;
}

.settings-item .setting-desc {
    font-size: 13px;
    color: var(--text-secondary);
}

.settings-item .setting-action {
    color: var(--primary);
    font-weight: 600;
    cursor: pointer;
    background: none;
    border: none;
    padding: 6px 12px;
    border-radius: 6px;
    transition: background 0.2s;
    touch-action: manipulation;
}

.settings-item .setting-action:active {
    background: var(--primary-bg);
}

.settings-item.danger {
    border-left: 3px solid var(--expense);
}

.settings-item .setting-action.danger {
    color: var(--expense);
}

.settings-item .setting-action.danger:active {
    background: var(--expense-bg);
}

/* ==================== CATEGORY LIST ==================== */
.category-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
}

.category-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--card-bg);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    box-shadow: var(--shadow-sm);
}

.category-item .category-info {
    display: flex;
    align-items: center;
    gap: 8px;
}

.category-item .category-icon {
    font-size: 20px;
}

.category-item .category-name {
    font-weight: 500;
}

.category-item .category-type {
    font-size: 12px;
    color: var(--text-secondary);
}

.category-item .category-delete {
    background: none;
    border: none;
    color: var(--text-tertiary);
    cursor: pointer;
    font-size: 16px;
    padding: 4px 8px;
    border-radius: 4px;
    transition: all 0.2s;
    touch-action: manipulation;
}

.category-item .category-delete:active {
    background: var(--expense-bg);
    color: var(--expense);
}

/* ==================== MODAL ==================== */
.modal {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: none;
    align-items: center;
    justify-content: center;
    padding: var(--padding-lg);
}

.modal.open {
    display: flex;
}

.modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.5);
}

.modal-content {
    position: relative;
    background: var(--card-bg);
    border-radius: var(--radius-lg);
    padding: var(--padding-lg);
    max-width: 400px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    animation: modalIn 0.3s ease;
}

@keyframes modalIn {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}

.modal-header {
    margin-bottom: 16px;
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.modal-body {
    margin-bottom: 16px;
}

.modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}

.modal-btn {
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: opacity 0.2s;
    touch-action: manipulation;
}

.modal-btn:active {
    opacity: 0.7;
}

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

.modal-btn-secondary {
    background: var(--bg);
    color: var(--text-secondary);
}

.modal-btn-danger {
    background: var(--expense);
    color: white;
}

/* ==================== TOAST ==================== */
.toast {
    position: fixed;
    bottom: calc(var(--tab-height) + 24px);
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    border-radius: var(--radius-md);
    background: var(--text-primary);
    color: white;
    font-weight: 500;
    font-size: 14px;
    z-index: 400;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    pointer-events: none;
    max-width: 90%;
    text-align: center;
    box-shadow: var(--shadow-lg);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.success {
    background: var(--income);
}

.toast.error {
    background: var(--expense);
}

/* ==================== RESPONSIVE ==================== */
/* Mobile Small */
@media (max-width: 375px) {
    .balance-amount { font-size: 28px; }
    .transaction-item { padding: 12px 14px; }
    .sheet-content { padding: 8px var(--padding-md) 20px; }
}

/* Tablet */
@media (min-width: 481px) and (max-width: 768px) {
    #app { max-width: 600px; }
    .balance-amount { font-size: 40px; }
    .balance-row { gap: 16px; }
    .balance-item .value { font-size: 20px; }
    .monthly-grid { grid-template-columns: 1fr 1fr 1fr 1fr; }
}

/* Desktop */
@media (min-width: 769px) {
    #app { 
        max-width: 600px;
        border-radius: 20px;
        box-shadow: var(--shadow-xl);
        margin-top: 20px;
        margin-bottom: 20px;
        max-height: 90vh;
    }
    
    body {
        background: #E5E7EB;
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
    }
    
    .tab-bar {
        border-radius: 0 0 20px 20px;
    }
    
    .balance-amount { font-size: 40px; }
}
