:root {
    --bg: #f4f4f2;
    --surface: #ffffff;
    --text: #1a1a18;
    --text-muted: #6b6b66;
    --border: #e3e2dd;
    --accent: #185fa5;
    --danger: #a32d2d;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #16171a;
        --surface: #1f2023;
        --text: #f0efe9;
        --text-muted: #9a9a94;
        --border: #33342f;
        --accent: #85b7eb;
        --danger: #f09595;
    }
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-text-size-adjust: 100%;
}

body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

a {
    color: var(--accent);
    text-decoration: none;
}

.flash {
    padding: 10px 16px;
    font-size: 14px;
    text-align: center;
}

.flash-error {
    background: var(--danger);
    color: #fff;
}

.flash-success {
    background: var(--accent);
    color: var(--surface);
}

/* Шапка страницы: липкая, с тонкой линией снизу, чтобы контент под ней
   при прокрутке визуально отделялся */
.topbar {
    position: sticky;
    top: 0;
    z-index: 5;
    background: var(--bg);
    padding: max(12px, env(safe-area-inset-top)) 16px 12px;
    box-shadow: 0 0.5px 0 var(--border);
}

.search-form {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 12px;
}

.search-form input[type="search"] {
    flex: 1;
    min-width: 0;
    min-height: 40px;
    padding: 9px 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 15px;
    font-family: inherit;
}

.search-clear {
    flex: none;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: var(--text-muted);
    border-radius: 50%;
}

/* Квадратные кнопки-иконки в шапке (домой, категории, валюты, курсы) */
.icon-btn {
    flex: none;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 19px;
    line-height: 1;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    transition: background-color 0.15s, border-color 0.15s;
}

.icon-btn:active {
    background: var(--border);
}

.search-summary {
    text-align: center;
    font-size: 13px;
    color: var(--text-muted);
    padding: 4px 0 2px;
}

.view-switcher {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    margin-bottom: 10px;
    padding-bottom: 2px;
}

.view-switcher a {
    flex: none;
    padding: 6px 13px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    font-size: 13px;
    color: var(--text-muted);
    white-space: nowrap;
    transition: background-color 0.15s, color 0.15s;
}

.view-switcher a.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--surface);
}

.range-form {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

.range-form input[type="date"] {
    flex: 1;
    min-width: 0;
    min-height: 40px;
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 14px;
    font-family: inherit;
}

.range-sep {
    flex: none;
    color: var(--text-muted);
}

.range-form button {
    flex: none;
    min-height: 40px;
    padding: 8px 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 14px;
    cursor: pointer;
}

.month-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

.month-nav a {
    color: var(--text-muted);
    padding: 8px;
    font-size: 18px;
    line-height: 1;
}

.month-nav .month-label {
    font-size: 15px;
    font-weight: 600;
    min-width: 160px;
    text-align: center;
}

.period-picker {
    display: inline-flex;
}

.period-input {
    width: auto;
    font-size: 15px;
    font-weight: 600;
    font-family: inherit;
    text-align: center;
    color: var(--text);
    background: transparent;
    border: none;
    min-height: auto;
    min-width: 150px;
    padding: 4px 2px;
}

.period-input[type="number"]::-webkit-outer-spin-button,
.period-input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.period-input[type="number"] {
    -moz-appearance: textfield;
    min-width: 80px;
}

.month-total {
    text-align: center;
    margin-top: 8px;
}

.month-total .amount {
    font-size: 30px;
    font-weight: 700;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
}

.month-total .amount-secondary {
    font-size: 14px;
    color: var(--text-muted);
}

.today-link {
    display: block;
    text-align: center;
    font-size: 13px;
    margin-top: 4px;
}

main {
    padding: 0 0 96px;
    max-width: 640px;
    margin: 0 auto;
}

/* Подсказка над списком на служебных страницах */
.page-hint {
    margin: 12px 16px 0;
    font-size: 12px;
    line-height: 1.45;
    color: var(--text-muted);
}

/* Кнопка под длинным списком (дубль той, что в шапке) */
.list-actions {
    padding: 16px 16px 0;
}

/* Карточки (разбивка, список): одинаковый внешний отступ 12/16 */
.breakdown {
    margin: 12px 16px 0;
    background: var(--surface);
    border-radius: 12px;
    border: 0.5px solid var(--border);
    padding: 6px 12px;
}

.breakdown-row {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 4px;
    font-size: 14px;
    border-bottom: 0.5px solid var(--border);
    overflow: hidden;
}

.breakdown-row:last-child {
    border-bottom: none;
}

.breakdown-bar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: -4px;
    right: auto;
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    z-index: 0;
}

.breakdown-icon,
.breakdown-name,
.breakdown-amount {
    position: relative;
    z-index: 1;
}

.breakdown-icon {
    flex: none;
    font-size: 16px;
    line-height: 1;
}

.breakdown-name {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.breakdown-amount {
    flex: none;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
}

.list {
    list-style: none;
    margin: 12px 16px 0;
    padding: 0;
    background: var(--surface);
    border-radius: 12px;
    border: 0.5px solid var(--border);
    overflow: hidden;
}

.row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-bottom: 0.5px solid var(--border);
    transition: background-color 0.15s;
}

.row:has(.row-link:active) {
    background: var(--bg);
}

.row:last-child {
    border-bottom: none;
}

.row-link {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
    color: inherit;
}

.avatar {
    flex: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background: var(--border);
    line-height: 1;
}

.row-main {
    flex: 1;
    min-width: 0;
}

.row-category {
    display: block;
    font-size: 15px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.row-meta {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.row-amount {
    flex: none;
    font-size: 15px;
    font-weight: 500;
    text-align: right;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.row-delete {
    flex: none;
    margin: 0;
    padding: 0;
    border: none;
    background: none;
}

.row-delete button {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: none;
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
}

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

.empty {
    text-align: center;
    color: var(--text-muted);
    padding: 48px 16px;
    font-size: 14px;
}

.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    padding: 20px 16px 8px;
}

.pagination a {
    font-size: 14px;
    font-weight: 500;
}

.pagination-disabled {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-muted);
    opacity: 0.4;
}

.pagination-info {
    font-size: 13px;
    color: var(--text-muted);
}

.fab {
    position: fixed;
    right: max(20px, env(safe-area-inset-right));
    bottom: max(20px, env(safe-area-inset-bottom));
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--surface);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: 300;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
    z-index: 10;
}

.fab:active {
    transform: scale(0.96);
}

.form-page {
    max-width: 480px;
    margin: 0 auto;
    padding: max(16px, env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
}

.form-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}

.form-header a {
    flex: none;
    width: 36px;
    height: 36px;
    margin-left: -8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    color: var(--text-muted);
    border-radius: 50%;
    transition: background-color 0.15s;
}

@media (hover: hover) {
    .form-header a:hover {
        background: var(--border);
    }
}

.form-header h1 {
    flex: 1;
    font-size: 18px;
    font-weight: 600;
    margin: 0;
}

.header-save-btn {
    flex: none;
    padding: 8px 14px;
    border-radius: 10px;
    border: 1px solid var(--accent);
    background: none;
    color: var(--accent);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
}

.header-save-btn:active {
    background: var(--accent);
    color: var(--surface);
}

form .field {
    margin-bottom: 16px;
}

form label {
    display: block;
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

form input,
form select,
form textarea {
    width: 100%;
    font-size: 17px;
    padding: 12px 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-family: inherit;
    min-height: 44px;
}

form textarea {
    resize: vertical;
}

.icon-input {
    width: 72px !important;
    text-align: center;
    font-size: 24px !important;
}

.sort-order-input {
    flex: none;
    width: 64px !important;
    min-height: 40px !important;
    padding: 8px !important;
    text-align: center;
}

.field-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin: 6px 0 0;
}

/* Ряд кнопок-иконок вместо select (icon-toggle-select.js) -- член семьи и
   подобные необязательные поля с малым фиксированным числом вариантов */
.icon-select {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.icon-select-option {
    flex: 1;
    min-width: 64px;
    min-height: 44px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 10px 6px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: inherit;
    -webkit-tap-highlight-color: transparent;
}

.icon-select-icon {
    font-size: 26px;
    line-height: 1;
}

.icon-select-label {
    font-size: 12px;
    color: var(--text-muted);
    text-align: center;
}

.icon-select-option.active {
    border-color: var(--accent);
    background: color-mix(in srgb, var(--accent) 12%, var(--surface));
}

.icon-select-option.active .icon-select-label {
    color: var(--accent);
    font-weight: 600;
}

@media (hover: hover) {
    .icon-select-option:hover {
        border-color: var(--accent);
    }
}

form .form-errors {
    color: var(--danger);
    font-size: 13px;
    margin-top: 4px;
}

.btn-primary {
    display: block;
    width: 100%;
    padding: 14px;
    border-radius: 10px;
    border: none;
    background: var(--accent);
    color: var(--surface);
    font-size: 16px;
    font-weight: 600;
    min-height: 44px;
    cursor: pointer;
    margin-top: 8px;
}

.btn-primary:active {
    opacity: 0.85;
}

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

.rate-info {
    font-size: 13px;
    color: var(--text-muted);
    margin: 8px 0 0;
}

.rate-info.rate-error {
    color: var(--danger);
}

.searchable-select {
    position: relative;
}

.searchable-select-native {
    display: none;
}

.searchable-select-panel {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 4px);
    z-index: 20;
    max-height: 260px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
}

.searchable-select-option {
    display: block;
    width: 100%;
    text-align: left;
    padding: 11px 14px;
    border: none;
    border-bottom: 0.5px solid var(--border);
    background: none;
    font-size: 15px;
    color: var(--text);
    font-family: inherit;
    cursor: pointer;
}

.searchable-select-option:last-child {
    border-bottom: none;
}

.searchable-select-option:hover,
.searchable-select-option.active {
    background: var(--bg);
}

.searchable-select-empty {
    padding: 11px 14px;
    font-size: 14px;
    color: var(--text-muted);
}

.delete-category-form {
    margin-top: 20px;
}

.btn-danger {
    display: block;
    width: 100%;
    padding: 14px;
    border-radius: 10px;
    border: 1px solid var(--danger);
    background: none;
    color: var(--danger);
    font-size: 15px;
    font-weight: 500;
    min-height: 44px;
    cursor: pointer;
}

.btn-danger:active {
    background: var(--danger);
    color: var(--surface);
}

/* Фильтр по категории: чип под итогом и активная строка разбивки */
.filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin: 6px auto 0;
    padding: 4px 6px 4px 10px;
    font-size: 13px;
    border-radius: 999px;
    border: 0.5px solid var(--border);
    background: var(--surface);
    color: var(--text-muted);
}

.topbar .filter-chip {
    display: flex;
    width: fit-content;
}

.filter-chips {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 6px;
}

.filter-chips .filter-chip {
    margin: 0;
}

.filter-chip a {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    font-size: 16px;
    line-height: 1;
    text-decoration: none;
    color: var(--text-muted);
}

.filter-chip a:hover {
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    color: var(--accent);
}

a.breakdown-row {
    color: inherit;
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
}

a.breakdown-row:hover .breakdown-name {
    color: var(--accent);
}

/* Компактные строки при фильтре по категории: без аватара и названия категории */
.list-compact .row {
    padding: 9px 14px;
}

.row-main-compact {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.row-line {
    display: flex;
    align-items: baseline;
    gap: 10px;
    min-width: 0;
}

.row-date {
    flex: none;
    font-size: 13px;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.row-comment {
    flex: 1;
    min-width: 0;
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.row-subcategory {
    font-size: 13px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Перетаскивание строк (валюты, категории): ручка, состояние, тост о сохранении */
.drag-handle {
    flex: none;
    width: 28px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    font-size: 14px;
    letter-spacing: -2px;
    cursor: grab;
    touch-action: none;   /* иначе палец на ручке начнёт скроллить страницу */
    user-select: none;
    -webkit-user-select: none;
}

.sorting .drag-handle {
    cursor: grabbing;
}

.row.dragging {
    position: relative;
    z-index: 2;
    background: var(--surface);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
    opacity: 0.95;
}

.sorting .row {
    user-select: none;
    -webkit-user-select: none;
}

.sort-status {
    position: fixed;
    left: 50%;
    bottom: calc(24px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    padding: 8px 14px;
    border-radius: 999px;
    font-size: 13px;
    background: var(--surface);
    border: 0.5px solid var(--border);
    color: var(--text-muted);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    z-index: 5;
}

.sort-status:empty {
    display: none;
}

.sort-status-ok {
    color: var(--accent);
}

.sort-status-error {
    color: #b3261e;
}

/* Заголовок служебных страниц (категории, валюты, курсы): стрелка назад,
   название, опционально кнопка справа. Внутри .topbar, поэтому без нижнего
   отступа -- его даёт сама шапка. */
.page-header {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
}

.page-header .back-link {
    flex: none;
    width: 36px;
    height: 36px;
    margin-left: -8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    color: var(--text-muted);
    border-radius: 50%;
    transition: background-color 0.15s;
}

.page-header h1 {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.page-header + .search-form {
    margin: 10px auto 0;
}

/* Hover -- только там, где есть мышь: на тачскринах :hover "залипает" */
@media (hover: hover) {
    .icon-btn:hover,
    .page-header .back-link:hover,
    .search-clear:hover,
    .row-delete button:hover {
        background: var(--border);
    }

    .view-switcher a:not(.active):hover {
        color: var(--text);
        border-color: var(--text-muted);
    }

    .row:has(.row-link:hover) {
        background: var(--bg);
    }

    a.breakdown-row:hover .breakdown-bar {
        background: color-mix(in srgb, var(--accent) 22%, transparent);
    }

    .fab:hover {
        filter: brightness(1.08);
    }

    .month-nav a:hover,
    .pagination a:hover,
    .today-link:hover {
        text-decoration: underline;
    }
}

/* Видимый фокус с клавиатуры, без рамок при клике мышью */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 6px;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible {
    outline-offset: 0;
}

.search-form input[type="search"]:focus,
form input:focus,
form select:focus,
form textarea:focus,
.range-form input:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

/* На широком экране содержимое шапки держится в той же колонке 640px, что и
   main, -- иначе на десктопе заголовок прижат к левому краю, а список по центру */
.topbar > * {
    max-width: 640px;
    margin-left: auto;
    margin-right: auto;
}

.field-hint-spaced {
    margin-top: 16px;
}
