html, body { height: 100%; margin: 0; padding: 0; }
:root {
    --tg-theme-bg-color: #ffffff;
    --tg-theme-text-color: #000000;
    --tg-theme-hint-color: #999999;
    --tg-theme-link-color: #2481cc;
    --tg-theme-button-color: #2481cc;
    --tg-theme-button-text-color: #ffffff;
    --tg-theme-secondary-bg-color: #f1f1f1;
    
    --primary: var(--tg-theme-button-color, #007aff);
    --bg-main: var(--tg-theme-bg-color, #f2f2f7);
    --bg-card: var(--tg-theme-secondary-bg-color, #ffffff);
    --text-main: var(--tg-theme-text-color, #1c1c1e);
    --text-muted: var(--tg-theme-hint-color, #8e8e93);
    
    --radius-lg: 16px;
    --radius-md: 12px;
    --shadow-sm: 0 2px 8px rgba(0,0,0,0.05);
    --shadow-md: 0 8px 24px rgba(0,0,0,0.1);
}

* { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
body { background-color: var(--bg-main); color: var(--text-main); overflow: hidden; -webkit-tap-highlight-color: transparent; }
#app { display: flex; flex-direction: column; height: 100%; max-height: 100vh; }
.view { display: none; flex: 1; overflow-y: auto; min-height: 0; -webkit-overflow-scrolling: touch; padding: 20px 16px 80px; animation: fadeIn 0.3s ease-out; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

header { text-align: center; margin-bottom: 24px; }
header h1 { font-size: 14px; font-weight: 500; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px; }
header h2 { font-size: 36px; font-weight: 700; margin-top: 4px; background: linear-gradient(135deg, var(--text-main), var(--primary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
#view-add header h2, #view-stats header h2 { font-size: 24px; background: none; -webkit-text-fill-color: var(--text-main); }

.section-header { margin-bottom: 12px; }
.section-header h3 { font-size: 18px; font-weight: 600; }
#transactions-list { background-color: var(--bg-card); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); overflow: hidden; }
.transaction-item { display: flex; align-items: center; padding: 16px; border-bottom: 1px solid rgba(0,0,0,0.05); }
.transaction-item:last-child { border-bottom: none; }
.tx-icon { width: 40px; height: 40px; border-radius: 50%; background-color: rgba(0, 122, 255, 0.1); color: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 18px; margin-right: 12px; }
.tx-details { flex: 1; }
.tx-category { font-weight: 600; font-size: 16px; margin-bottom: 2px; }
.tx-meta { font-size: 12px; color: var(--text-muted); display: flex; gap: 8px; }
.tx-amount { font-weight: 700; font-size: 16px; color: #ff3b30; }
.tx-amount-orig { font-size: 11px; color: var(--text-muted); text-align: right; margin-top: 2px; }

.form-group { margin-bottom: 20px; }
.row-flex { display: flex; justify-content: space-between; }
.form-group label { display: block; margin-bottom: 8px; font-size: 14px; font-weight: 500; color: var(--text-muted); }
.amount-input-wrapper { position: relative; display: flex; align-items: center; }
.amount-input-wrapper input { width: 100%; padding: 16px 16px 16px 16px; font-size: 24px; font-weight: 600; border: none; background-color: var(--bg-card); border-radius: var(--radius-md); color: var(--text-main); box-shadow: var(--shadow-sm); outline: none; transition: box-shadow 0.2s; }
.amount-input-wrapper input:focus { box-shadow: 0 0 0 2px var(--primary); }

select, input[type="text"], input[type="date"] { width: 100%; padding: 16px; font-size: 16px; border: none; background-color: var(--bg-card); border-radius: var(--radius-md); color: var(--text-main); box-shadow: var(--shadow-sm); outline: none; -webkit-appearance: none; }
select:focus, input[type="text"]:focus, input[type="date"]:focus { box-shadow: 0 0 0 2px var(--primary); }
select { appearance: none; background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e"); background-repeat: no-repeat; background-position: right 1rem center; background-size: 1em; }

.segmented-control { display: flex; background-color: var(--bg-card); border-radius: var(--radius-md); padding: 4px; box-shadow: var(--shadow-sm); }
.segmented-control input { display: none; }
.segmented-control label { cursor: pointer; flex: 1; text-align: center; padding: 12px 0; margin: 0; border-radius: 8px; font-weight: 500; font-size: 14px; color: var(--text-muted); transition: all 0.2s; }
.segmented-control input:checked + label { background-color: var(--primary); color: #fff; box-shadow: var(--shadow-sm); }
.segmented-control i { margin-right: 4px; }

.btn-primary, .btn-secondary { width: 100%; padding: 16px; border: none; border-radius: var(--radius-lg); font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: var(--shadow-md); transition: transform 0.1s, opacity 0.2s; margin-top: 20px; }
.btn-primary { background-color: var(--primary); color: #fff; }
.btn-secondary { background-color: #217346; color: #fff; margin-top: 10px; }
.btn-primary:active, .btn-secondary:active { transform: scale(0.98); }
.btn-primary:disabled, .btn-secondary:disabled { opacity: 0.7; cursor: not-allowed; }

.bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; height: 70px; display: flex; justify-content: space-around; align-items: center; border-top: 1px solid rgba(0,0,0,0.05); padding-bottom: env(safe-area-inset-bottom); z-index: 100; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); background-color: var(--nav-bg, rgba(255,255,255,0.9)); }
@media (prefers-color-scheme: dark) { .bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; height: 70px; display: flex; justify-content: space-around; align-items: center; border-top: 1px solid rgba(0,0,0,0.05); padding-bottom: env(safe-area-inset-bottom); z-index: 100; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); background-color: var(--nav-bg, rgba(255,255,255,0.9)); } }
.nav-btn { background: none; border: none; color: var(--text-muted); display: flex; flex-direction: column; align-items: center; font-size: 10px; width: 33%; transition: color 0.2s; cursor: pointer; }
.nav-btn i { font-size: 20px; margin-bottom: 4px; }
.nav-btn.active { color: var(--primary); }
.btn-add { transform: translateY(-15px); }
.add-icon-wrapper { width: 50px; height: 50px; background: linear-gradient(135deg, var(--primary), #00c6ff); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; box-shadow: 0 4px 15px rgba(0, 122, 255, 0.4); font-size: 20px; }

.chart-container { background-color: var(--bg-card); border-radius: var(--radius-lg); padding: 20px; box-shadow: var(--shadow-sm); margin-bottom: 20px; position: relative; height: 250px; }
#stats-legend { display: flex; flex-direction: column; gap: 12px; margin-bottom: 24px; }
.legend-item { display: flex; align-items: center; justify-content: space-between; background-color: var(--bg-card); padding: 12px 16px; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); }
.legend-color { width: 12px; height: 12px; border-radius: 50%; margin-right: 12px; }
.legend-label { flex: 1; font-weight: 500; }
.legend-value { font-weight: 700; }
.empty-state { padding: 40px 20px; text-align: center; color: var(--text-muted); }

.export-section { background-color: var(--bg-card); border-radius: var(--radius-lg); padding: 20px; box-shadow: var(--shadow-sm); margin-top: 10px; }
.export-section h3 { margin-bottom: 16px; font-size: 18px; font-weight: 600; }

.tx-actions-wrapper { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.tx-actions { display: flex; gap: 12px; }
.btn-action { background: none; border: none; color: var(--text-muted); font-size: 16px; cursor: pointer; padding: 4px; }
.btn-action:hover { color: var(--primary); }
.delete-btn:hover { color: #FF3B30; }

/* Settings Button */
#btn-settings { position: fixed; top: 15px; right: 15px; z-index: 1000; background: var(--bg-card); color: var(--text-main); border: none; border-radius: 50%; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; box-shadow: var(--shadow-sm); cursor: pointer; font-size: 20px; transition: transform 0.2s; }
#btn-settings:active { transform: scale(0.9); }

/* Modal */
.modal-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 2000; display: none; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.3s; }
.modal-overlay.active { display: flex; opacity: 1; }
.modal-content { background: var(--bg-main); width: 90%; max-width: 400px; border-radius: var(--radius-lg); padding: 20px; box-shadow: var(--shadow-md); transform: translateY(20px); transition: transform 0.3s; }
.modal-overlay.active .modal-content { transform: translateY(0); }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.modal-header h2 { margin: 0; font-size: 20px; }
.btn-close-modal { background: none; border: none; font-size: 24px; color: var(--text-muted); cursor: pointer; }

/* Theme Overrides */
body.theme-light { --nav-bg: rgba(255,255,255,0.9);
    --bg-main: #f2f2f7;
    --bg-card: #ffffff;
    --text-main: #1c1c1e;
    --text-muted: #8e8e93;
    --border-color: rgba(60, 60, 67, 0.1);
}
body.theme-dark { --nav-bg: rgba(28,28,30,0.9);
    --bg-main: #000000;
    --bg-card: #1c1c1e;
    --text-main: #ffffff;
    --text-muted: #8e8e93;
    --border-color: rgba(84, 84, 88, 0.65);
}


/* Categories Manager */
.cat-manage-list { max-height: 250px; overflow-y: auto; margin-bottom: 15px; border-top: 1px solid var(--border-color); }
.cat-manage-item { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--border-color); }
.cat-manage-name { display: flex; align-items: center; gap: 10px; }
.cat-manage-icon { width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; }
.btn-add-cat { width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px; }
.export-btn-small { background: var(--bg-card); color: var(--primary); border: 1px solid var(--primary); border-radius: var(--radius-md); width: 40px; height: 40px; display: flex; align-items: center; justify-content: center; font-size: 18px; cursor: pointer; transition: all 0.2s; }
.export-btn-small:hover { background: var(--primary); color: white; }
.stats-header-row { display: flex; gap: 10px; align-items: flex-end; margin-bottom: 20px; }
.stats-header-row .form-group { margin-bottom: 0; flex: 1; }

/* Comparison Badge */
.comparison-badge { display: inline-flex; align-items: center; gap: 5px; padding: 4px 10px; border-radius: 12px; font-size: 13px; font-weight: 600; margin-top: 10px; }
.comparison-badge.positive { background-color: rgba(46, 204, 113, 0.15); color: #2ecc71; }
.comparison-badge.negative { background-color: rgba(231, 76, 60, 0.15); color: #e74c3c; }
.comparison-badge.neutral { background-color: rgba(150, 150, 150, 0.15); color: var(--text-muted); }

/* Budgets */
#stats-budgets-list { background-color: var(--bg-card); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); } canvas { touch-action: pan-y; }
.budget-item { padding: 16px; border-bottom: 1px solid var(--border-color); margin-bottom: 0; }
.budget-item:last-child { border-bottom: none; }
.budget-info { display: flex; justify-content: space-between; align-items: center; margin-bottom: 5px; font-size: 14px; }
.budget-bar-bg { width: 100%; height: 8px; background-color: var(--border-color); border-radius: 4px; overflow: hidden; }
.budget-bar-fill { height: 100%; border-radius: 4px; transition: width 0.3s ease, background-color 0.3s ease; }
.budget-fill-safe { background-color: #2ecc71; }
.budget-fill-warning { background-color: #f39c12; }
.budget-fill-danger { background-color: #e74c3c; }

/* Budget Manage */
.budget-manage-item { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--border-color); }
.budget-manage-input { width: 90px; padding: 8px; border-radius: 8px; border: 1px solid var(--border-color); background: var(--card-bg); color: var(--text-color); text-align: right; font-size: 14px; }

.color-picker-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 5px; }
.color-option { width: 100%; aspect-ratio: 1; border-radius: 12px; cursor: pointer; border: 2px solid transparent; transition: transform 0.2s, border-color 0.2s; }
.color-option.selected { transform: scale(1.1); border-color: var(--text-main); }

.toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%) translateY(100px); background: var(--text-main); color: var(--bg-main); padding: 12px 24px; border-radius: 20px; font-size: 14px; font-weight: 500; opacity: 0; transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); z-index: 9999; pointer-events: none; white-space: nowrap; box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }

/* Incomes Styles */
.dashboard-triangle {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    margin-bottom: 20px;
}
.dash-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 4px 15px rgba(0,0,0,0.05);
}
.dash-balance {
    grid-column: 1 / -1;
}
.dash-card-title {
    font-size: 13px;
    opacity: 0.8;
    margin-bottom: 8px;
    font-weight: 500;
}
.dash-card-amount {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 8px;
}

.dash-balance .dash-card-amount {
    font-size: 28px;
}
.dash-expense .dash-card-amount {
    color: var(--danger);
}
.dash-income .dash-card-amount {
    color: var(--success);
}
.type-toggle {
    margin-bottom: 15px;
}
.type-toggle label {
    flex: 1;
    text-align: center;
    justify-content: center;
}

.stats-split {
    display: flex;
    flex-direction: column;
    gap: 20px;
}
@media (min-width: 600px) {
    .stats-split {
        flex-direction: row;
    }
    .stats-column {
        flex: 1;
    }
}
