/* ========== Variables ========== */
:root {
    --bg: #fff;
    --surface: #f6f6f6;
    --text: #2d2a26;
    --muted: #8a8680;
    --border: #eef0f4;
    --accent: #c4704f;
    --radius: 12px;
    --radius-sm: 8px;
}

/* ========== Reset ========== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
html,
body {
    height: 100%;
    overscroll-behavior: none;
    /*overflow: hidden;*/
}
body {
    font-family:
        -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto,
        Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.4;
    -webkit-font-smoothing: antialiased;
}

/* ========== Layout ========== */
#content {
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.screen {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
}

.container {
    width: 100%;
    max-width: 480px;
    margin: 0 auto;
    padding: 0 1rem;
}

/* ========== List Screen ========== */
.list-screen .header {
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.list-screen .header button {
    background: none;
    border: none;
    font-size: 1.25rem;
    color: var(--muted);
    cursor: pointer;
}

.summary {
    text-align: center;
    padding: 1.5rem 0 2rem;
}

.summary small {
    color: var(--muted);
    font-size: 0.875rem;
}

.summary .total {
    font-size: clamp(3rem, 10vw, 3.5rem);
    margin-top: 0.25rem;
}

.summary .total .currency {
    font-size: 0.6em;
    color: var(--muted);
    margin-right: 0.1em;
}

.expenses {
    flex: 1;
    overflow-y: auto;
    padding: 0 1rem 1rem;
}

.group-header {
    display: flex;
    justify-content: space-between;
    padding: 1.5rem 0 0;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.5rem;
}

.group-header span {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.expense-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.4rem 0;
    cursor: pointer;
    transition: opacity 0.15s;
}

.expense-item:active {
    opacity: 0.7;
}

.expense-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.cat-icon {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.125rem;
}

.expense-details strong {
    display: block;
    font-weight: 500;
}

.expense-details small {
    color: var(--muted);
    font-weight: 500;
}

.expense-amount {
    font-weight: 500;
    font-size: 1.1rem;
}

.expense-amount.income {
    color: #22c55e;
}

/* FAB */
.fab-bar {
    border-top: 1px solid var(--border);
    padding: 0.5rem 0 1.5rem;
    display: flex;
    justify-content: space-around;
    align-items: center;
    flex-shrink: 0;
}

.fab-bar button {
    width: 60px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: none;
    font-size: 1.25rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    transition:
        background 0.15s,
        border-color 0.15s,
        transform 0.1s;
}

.fab-bar button:active {
    background: var(--surface);
    border-color: var(--muted);
    transform: scale(0.95);
}

.fab-bar button.active {
    color: var(--text);
    background: var(--surface);
    border-color: var(--muted);
}

.fab-bar .fab-add {
    background: var(--text);
    color: var(--surface);
    font-size: 2rem;
    font-weight: 300;
    border-color: var(--text);
}

/* ========== Create/Edit Screen ========== */
.create-screen,
.edit-screen {
    display: flex;
    flex-direction: column;
    height: 100dvh;
}

.create-screen .header,
.edit-screen .header {
    padding: 0.5rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.edit-screen .header h1 {
    font-size: 1.1rem;
    font-weight: 600;
}

.close-btn,
.remove-btn {
    display: flex;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--border);
    border: none;
    color: var(--muted);
    cursor: pointer;
    align-items: center;
    justify-content: center;
}

.remove-btn {
    background: rgba(239, 68, 68, 0.32);
    color: #dc2626;
}

.create-form {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.amount-display {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
    min-height: 0;
}

.amount-row {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    width: 100%;
    padding: 0 3rem;
}

.amount-hero {
    font-size: clamp(2.5rem, 12vw, 4rem);
    font-weight: 500;
}

.amount-hero .currency {
    font-size: 0.4em;
    color: var(--muted);
    vertical-align: super;
}

.backspace-btn {
    position: absolute;
    right: 0;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--border);
    border: none;
    font-size: 1rem;
    color: var(--muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition:
        background 0.15s,
        color 0.15s;
}

.remove-btn:hover {
    background: #ef4444;
    color: white;
}

.remove-btn:active {
    background: #dc2626;
}

.note-input {
    margin-bottom: 2rem;
    background: none;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    text-align: center;
    font-size: 1rem;
    color: var(--muted);
    padding: 0.5rem;
    width: 200px;
    font-family: inherit;
    transition: border-color 0.2s;
}

.note-input:focus {
    outline: none;
    border-bottom-color: var(--accent);
    color: var(--text);
}

.note-input::placeholder {
    color: var(--muted);
}

/* Selectors */
.selectors {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    padding: 0 1rem 0.5rem;
}

.selector {
    position: relative;
    cursor: pointer;
}

.selector select {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
    width: 100%;
    height: 100%;
    z-index: 10;
}

.selector-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.6rem 1rem;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text);
    pointer-events: none;
}

/* Keypad */
.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    padding: 0.5rem 1rem 1rem;
}

.keypad button {
    height: 52px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: 1.5rem;
    font-weight: 500;
    font-family: inherit;
    color: var(--text);
    cursor: pointer;
    transition: background 0.1s;
}

.keypad button:active {
    background: var(--border);
}

.keypad .submit {
    background: var(--text);
    color: var(--surface);
    font-size: 1.5rem;
}

/* ========== Date Modal ========== */
.date-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 100;
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
}

.date-modal-overlay.open {
    display: flex;
    opacity: 1;
}

.date-modal {
    background: var(--bg);
    border-radius: var(--radius);
    padding: 1rem;
    width: 90%;
    max-width: 320px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.calendar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.calendar-header h3 {
    font-size: 1.1rem;
    font-weight: 600;
}

.calendar-header button {
    background: none;
    border: none;
    font-size: 1.25rem;
    padding: 0.25rem 0.5rem;
    cursor: pointer;
    color: var(--text);
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 0.25rem;
    text-align: center;
}

.calendar-day-header {
    font-size: 0.75rem;
    color: var(--muted);
    font-weight: 600;
    padding-bottom: 0.5rem;
}

.calendar-day {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    border-radius: 50%;
    cursor: pointer;
}

.calendar-day:hover {
    background: var(--surface);
}

.calendar-day.selected {
    background: var(--text);
    color: var(--surface);
}

.calendar-day.today {
    color: var(--accent);
    font-weight: 600;
}

.calendar-day.empty {
    pointer-events: none;
}

/* ========== Expense Dialog ========== */
.expense-dialog {
    border: none;
    padding: 0;
    background: var(--bg);
    border-radius: var(--radius) var(--radius) 0 0;
    width: 100%;
    max-width: 480px;
    max-height: 95dvh;
    flex-direction: column;
    box-shadow: 0 -10px 25px rgba(0, 0, 0, 0.15);
    position: fixed;
    inset: auto 0 0 0;
    margin: 0 auto;
}

/* iOS Safari scroll lock - avoid position:fixed which breaks theme-color */
html.modal-open,
body.modal-open {
    overflow: hidden;
    touch-action: none;
    overscroll-behavior: none;
}

.expense-dialog[open] {
    display: flex;
}

.expense-dialog::backdrop {
    background: rgba(0, 0, 0, 0.5);
}

.expense-dialog .modal-header {
    padding: 0.5rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.expense-dialog .modal-header h1 {
    font-size: 1.1rem;
    font-weight: 600;
}

.expense-dialog .create-form {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.expense-dialog .amount-display {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1rem;
    min-height: 120px;
}

.expense-dialog .date-modal-overlay {
    z-index: 150;
}

/* ========== Category Modal ========== */
.category-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 150;
    display: none;
    flex-direction: column;
    justify-content: flex-end;
    opacity: 0;
    transition: opacity 0.2s;
}

.category-modal-overlay.open {
    display: flex;
    opacity: 1;
}

.category-modal {
    background: var(--bg);
    border-radius: var(--radius) var(--radius) 0 0;
    padding: 1.5rem 1rem 2rem;
    width: 100%;
    max-width: 480px;
    max-height: 80vh;
    overflow-y: auto;
    margin: 0 auto;
    box-shadow: 0 -10px 25px rgba(0, 0, 0, 0.15);
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.category-modal-overlay.open .category-modal {
    transform: translateY(0);
}

.category-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
}

.category-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 1rem 0.5rem;
    background: var(--surface);
    border-radius: var(--radius);
    cursor: pointer;
    border: 2px solid transparent;
    transition:
        background 0.15s,
        transform 0.1s;
}

.category-option:active {
    transform: scale(0.96);
}

.category-option.selected {
    border-color: var(--text);
    background: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.category-option-icon {
    font-size: 2rem;
    line-height: 1;
}

.category-option-name {
    font-size: 0.8rem;
    font-weight: 600;
    text-align: center;
    color: var(--text);
}

/* ========== Statistics Screen ========== */
.stats-content {
    flex: 1;
    overflow-y: auto;
    padding: 0 1rem 1rem;
}

/* Insights Header */
.insights-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 1rem;
    margin-bottom: 0.5rem;
}

.insights-title {
    font-size: 1.75rem;
    font-weight: 700;
}

.view-selector {
    position: relative;
}

.view-selector select {
    appearance: none;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.4rem 2rem 0.4rem 0.75rem;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text);
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s;
}

.view-selector select:hover {
    background: var(--border);
}

.view-selector::after {
    content: "▾";
    position: absolute;
    right: 0.5rem;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    color: var(--muted);
}

/* Period Selector */
.period-selector {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0 1rem;
}

.period-nav {
    background: none;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    width: 40px;
    height: 40px;
    font-size: 1.5rem;
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.period-nav:hover {
    background: var(--surface);
}

.period-title {
    font-size: 1.25rem;
    font-weight: 600;
}

/* Enhanced Stats Summary */
.stats-summary-enhanced {
    display: flex;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.stat-card {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.stat-label {
    color: var(--muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
}

.stat-main {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.stat-amount {
    font-size: 1.75rem;
    font-weight: 600;
    line-height: 1;
}

.stat-amount .currency {
    font-size: 0.6em;
    color: var(--muted);
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 600;
    line-height: 1;
}

.stat-value .currency {
    font-size: 0.6em;
    color: var(--muted);
}

.percentage-badge {
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1;
}

.percentage-badge.decrease {
    background: rgba(34, 197, 94, 0.15);
    color: #16a34a;
}

.percentage-badge.increase {
    background: rgba(239, 68, 68, 0.15);
    color: #dc2626;
}

/* Chart Section */
.chart-section {
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border);
}

.chart-container {
    position: relative;
    height: 180px;
    padding: 1rem 0;
}

.chart-bars {
    display: flex;
    align-items: flex-end;
    height: 140px;
    gap: 2px;
    position: relative;
}

.chart-bar-wrapper {
    flex: 1;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    height: 100%;
    background: var(--surface);
    border-radius: 6px;
    position: relative;
}

.chart-bar {
    width: 100%;
    background: rgb(45, 42, 38);
    border-radius: 6px;
    transition:
        background 0.15s,
        opacity 0.15s;
    min-height: 2px;
}

.chart-bar-wrapper:hover .chart-bar {
    background: var(--accent);
    opacity: 0.8;
}

.chart-labels {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 0.5rem;
    gap: 2px;
}

.chart-label {
    flex: 1;
    text-align: center;
    font-size: 0.75rem;
    color: var(--muted);
    font-weight: 500;
}

.average-line {
    position: absolute;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--muted);
    opacity: 0.6;
    pointer-events: none;
}

.average-line::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--muted);
    border-top: 1px dashed var(--muted);
}

.average-label {
    position: absolute;
    left: 0;
    top: -1.25rem;
    font-size: 0.75rem;
    color: var(--muted);
    background: var(--bg);
    padding: 0 0.25rem;
    font-weight: 600;
}

/* Old styles kept for backward compatibility */
.month-selector {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 1rem;
}

.month-nav {
    background: none;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    width: 40px;
    height: 40px;
    font-size: 1.5rem;
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.month-nav:hover {
    background: var(--surface);
}

.month-title {
    font-size: 1.25rem;
    font-weight: 600;
}

.stats-summary {
    font-size: 1.5rem;
    text-align: center;
    padding: 0.5rem 0 1.5rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 1.5rem;
}

.stats-summary small {
    color: var(--muted);
    font-size: 0.875rem;
}

.category-breakdown {
    margin-bottom: 2rem;
}

.category-breakdown h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
}

.category-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

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

.category-info {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.category-details strong {
    display: block;
    font-weight: 500;
    text-transform: capitalize;
}

.category-details small {
    color: var(--muted);
    font-size: 0.875rem;
}

.category-amount {
    text-align: right;
}

.category-amount strong {
    display: block;
    font-weight: 600;
    font-size: 1.1rem;
}

.category-amount .percentage {
    color: var(--muted);
    font-size: 0.875rem;
}

/* Category Group - wrapper for expandable category */
.category-group {
    display: flex;
    flex-direction: column;
}

.category-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    padding: 0.5rem 0;
    transition: opacity 0.15s;
}

.category-item:active {
    opacity: 0.7;
}

.category-bar {
    height: 6px;
    background: var(--border);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 0;
}

.category-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
}

/* Category Transactions - expandable transaction list */
.category-transactions {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    background: var(--surface);
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    margin-bottom: 0.5rem;
}

.category-transactions .expense-list {
    padding: 0.5rem;
}

.category-transactions .expense-item {
    background: var(--bg);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.25rem;
}

.category-transactions .expense-item:last-child {
    margin-bottom: 0;
}

.category-transactions .no-transactions {
    padding: 1rem;
    text-align: center;
    color: var(--muted);
    font-size: 0.875rem;
}

/* Hide category icon in expanded transactions (redundant since we're already in the category) */
.category-transactions .cat-icon {
    display: none;
}

.category-transactions .expense-info {
    gap: 0;
}

.stats-expenses {
    margin-bottom: 2rem;
}

.stats-expenses h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
}

.expense-list {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--muted);
}

/* Spinner */
.submit-spinner {
    display: none;
    width: 24px;
    height: 24px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: #fff;
    animation: spin 0.8s linear infinite;
}

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

.htmx-request .submit-icon {
    display: none;
}

.htmx-request .submit-spinner {
    display: block;
}

.htmx-request .submit {
    opacity: 0.5;
    pointer-events: none;
}

.keypad .submit {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ========== Pull to Refresh ========== */
.pull-to-refresh {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 0;
    overflow: hidden;
    z-index: 50;
    pointer-events: none;
    background: var(--bg);
}

.pull-to-refresh.pulling,
.pull-to-refresh.refreshing {
    pointer-events: auto;
}

.ptr-spinner {
    width: 28px;
    height: 28px;
    border: 3px solid var(--border);
    border-radius: 50%;
    border-top-color: var(--text);
    opacity: 0;
    transform: scale(0.5) rotate(0deg);
    transition:
        opacity 0.2s,
        transform 0.2s;
}

.pull-to-refresh.pulling .ptr-spinner {
    opacity: 1;
    transform: scale(1) rotate(var(--pull-rotation, 0deg));
}

.pull-to-refresh.refreshing .ptr-spinner {
    opacity: 1;
    transform: scale(1);
    animation: ptr-spin 0.8s linear infinite;
}

@keyframes ptr-spin {
    to {
        transform: scale(1) rotate(360deg);
    }
}

/* Adjust content when pulling */
.screen.ptr-pulling {
    transform: translateY(var(--ptr-offset, 0px));
    transition: none;
}

.screen.ptr-releasing {
    transition: transform 0.3s ease-out;
}

/* ========== Infinite Scroll ========== */
.load-more-sentinel {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 1.5rem 0;
}

.loading-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border);
    border-radius: 50%;
    border-top-color: var(--text);
    animation: spin 0.8s linear infinite;
}

/* ========== Responsive ========== */
@media (min-width: 480px) {
    body {
        display: flex;
        justify-content: center;
        background-color: #f0f2f5;
    }

    #content {
        width: 100%;
        max-width: 480px;
        min-height: 100dvh;
        background: var(--bg);
        box-shadow: 0 0 40px rgba(0, 0, 0, 0.08);
    }

    .pull-to-refresh {
        max-width: 480px;
        margin: 0 auto;
    }
}
