/*
 * Раскладка переключается на одной границе: узкий экран — до 640px включительно,
 * широкий — от 641px. Медиазапросы не умеют переменные, поэтому числа 640/641
 * повторяются литералом — других ширин в @media быть не должно.
 */

:root {
    --bg: #f6f6f4;
    --surface: #ffffff;
    --line: #dcdcd6;
    --text: #1d1d1b;
    --muted: #6d6d68;
    --accent: #2f6f4f;
    --danger: #a3372c;

    /* категориальные цвета графиков: порядок слотов — по убыванию суммы, подобран так,
       что соседи на кольце различимы (validate_palette.js из skill dataviz) */
    --series-1: #2a78d6;
    --series-2: #eb6834;
    --series-3: #1baf7a;
    --series-4: #eda100;
    --series-5: #e87ba4;
    --series-6: #008300;
    --series-7: #e34948;
    --series-other: #c4c4bd;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.page {
    max-width: 1000px;
    margin: 0 auto;
    padding: 0 max(16px, env(safe-area-inset-right)) 48px max(16px, env(safe-area-inset-left));
}

.boot {
    padding: 48px;
    text-align: center;
    color: var(--muted);
}

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 14px 0;
    border-bottom: 1px solid var(--line);
    margin-bottom: 24px;
    flex-wrap: wrap;
}

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

.topbar a.active {
    color: var(--accent);
}

@media (hover: hover) {
    .topbar a:hover {
        color: var(--accent);
    }
}

.topbar .brand {
    font-weight: 600;
    font-size: 17px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.brand-logo {
    width: 24px;
    height: 24px;
}

.spacer {
    flex: 1;
}

/* нижняя панель разделов — только на узком экране */
.tabbar {
    display: none;
}


.topbar .user-menu {
    color: var(--muted);
}

.deletion-toggle {
    min-height: 44px;
}

h1 {
    font-size: 24px;
    margin: 0 0 20px;
}

/* заголовок страницы с кнопкой создания; на узком экране кнопка становится плавающей «+» */
.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 20px;
}

.page-head h1 {
    margin: 0;
}

button.create {
    flex: none;
}

h2 {
    font-size: 17px;
    margin: 24px 0 12px;
}

h3 {
    font-size: 15px;
    margin: 16px 0 8px;
    color: var(--muted);
}

.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 20px;
}

.panel h2 {
    margin-top: 0;
}

.row {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    flex-wrap: wrap;
    margin-bottom: 8px;
}

.row .grow {
    flex: 1;
}

label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 13px;
    color: var(--muted);
}

label.toggle {
    flex-direction: row;
    align-items: center;
    gap: 6px;
    margin-bottom: 12px;
}

input,
select {
    font: inherit;
    padding: 7px 9px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
}

input.currency {
    width: 64px;
    text-transform: uppercase;
}

button {
    font: inherit;
    padding: 7px 14px;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
}

button:disabled {
    opacity: 0.5;
    cursor: default;
}

button.link {
    background: none;
    border: none;
    color: var(--accent);
    padding: 4px 6px;
}

button.link.danger {
    color: var(--danger);
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
}

th,
td {
    text-align: left;
    padding: 9px 12px;
    border-bottom: 1px solid var(--line);
    vertical-align: top;
}

tbody tr:last-child td {
    border-bottom: none;
}

th {
    font-size: 13px;
    color: var(--muted);
    font-weight: 500;
}

.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

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

.positive {
    color: var(--accent);
}

.tx-amount {
    display: inline-flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 2px 8px;
    color: var(--text);
    font-weight: 600;
    white-space: nowrap;
}

.tx-amount-fee {
    font-size: 13px;
    font-weight: 400;
}

.actions {
    white-space: nowrap;
    text-align: right;
}

.archived {
    color: var(--muted);
}

.tag {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 6px;
    font-size: 11px;
    border-radius: 10px;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--muted);
}

/* долг у себя под остатком счёта: «долг» подписью, чтобы не путать с отрицательным остатком */
.account-debt {
    display: block;
    font-size: 13px;
    font-weight: 400;
    color: var(--muted);
}

/* группы счетов: одинаковые столбцы во всех таблицах, чтобы остатки стояли друг под другом */
.account-group {
    table-layout: fixed;
}

/* длинное название переносится по слогам, а не обрывается на произвольной букве */
.account-group td {
    overflow-wrap: break-word;
    hyphens: auto;
}

.account-group .account-kind {
    width: 120px;
}

.account-group .account-balance {
    width: 180px;
}

.account-group .account-actions {
    width: 360px;
}

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

.error {
    color: var(--danger);
    background: #fdf1ef;
    border: 1px solid #f0cfc9;
    border-radius: 6px;
    padding: 8px 12px;
}

.balanced {
    color: var(--accent);
}

.unbalanced {
    color: var(--danger);
}

.entry-line {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    max-width: 380px;
}

.cat-tree {
    list-style: none;
    margin: 0;
    padding: 0;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
}

.cat-tree li {
    position: relative;
    border-bottom: 1px solid var(--line);
}

.cat-tree li:last-child {
    border-bottom: none;
}

.cat-tree .root .cat-name {
    font-weight: 600;
}

/* направляющая ветки: вертикаль на всю высоту строки и отвод к середине строки; у последней подкатегории вертикаль обрывается на отводе */
.cat-tree .child::before,
.cat-tree .child::after {
    content: "";
    position: absolute;
    left: 22px;
    pointer-events: none;
}

.cat-tree .child::before {
    top: 0;
    bottom: 0;
    border-left: 1px solid var(--muted);
    opacity: 0.4;
}

.cat-tree .child.last::before {
    bottom: 50%;
}

.cat-tree .child::after {
    top: 50%;
    width: 20px;
    border-top: 1px solid var(--muted);
    opacity: 0.4;
}

.cat-row {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    padding: 6px 8px 6px 14px;
}

.cat-tree .child .cat-row {
    padding-left: 50px;
}

.cat-search {
    width: 100%;
    margin-bottom: 10px;
}

/* стрелка раскрытия: её центр совпадает с направляющей подкатегорий (left: 22px) */
button.cat-toggle,
.cat-toggle-slot {
    flex: none;
    width: 28px;
    margin-left: -6px;
}

button.cat-toggle {
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: none;
    color: var(--muted);
    font-size: 20px;
    line-height: 1;
    transition: transform 0.15s;
}

button.cat-toggle.expanded {
    transform: rotate(90deg);
}

button.cat-toggle:hover {
    color: var(--accent);
}

mark.match {
    background: none;
    color: inherit;
    font-weight: 700;
    text-decoration: underline;
    text-decoration-color: var(--accent);
    text-decoration-thickness: 2px;
    text-underline-offset: 3px;
}

.cat-row.clickable {
    cursor: pointer;
}

.cat-name {
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
}

.cat-row .count {
    flex: none;
    min-width: 24px;
    padding: 0 7px;
    font-size: 12px;
    line-height: 20px;
    text-align: center;
    border-radius: 10px;
    background: var(--bg);
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.more-slot {
    flex: none;
    width: 36px;
}

button.link.more {
    width: 36px;
    padding: 0 0 4px;
    font-size: 20px;
    line-height: 1;
    color: var(--muted);
}

button.link.more:hover {
    color: var(--accent);
}

@media (hover: hover) {
    .cat-row.clickable:hover {
        background: var(--bg);
    }
}

.tiles {
    list-style: none;
    padding: 0;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.tiles a {
    display: block;
    padding: 16px 20px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    text-decoration: none;
    color: var(--text);
}

.totals {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.month-nav {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
}

.month-nav input {
    flex: 1;
    min-width: 0;
    max-width: 220px;
    text-align: center;
}

.month-nav button {
    flex: none;
    width: 44px;
    height: 44px;
    padding: 0;
    font-size: 20px;
    line-height: 1;
}

.month-nav select {
    flex: 1;
    min-width: 0;
    max-width: 220px;
    text-align: center;
}

/* переключатель режима периода: сегменты одной строкой */

.period-modes {
    display: flex;
    max-width: 420px;
    margin-bottom: 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    overflow: hidden;
}

.period-modes button {
    flex: 1;
    min-width: 0;
    padding: 7px 4px;
    border: none;
    border-radius: 0;
    background: var(--surface);
    color: var(--text);
}

.period-modes button + button {
    border-left: 1px solid var(--line);
}

.period-modes button.active {
    background: var(--accent);
    color: #fff;
}

.period-range {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
}

.period-range label {
    flex: 1;
    min-width: 0;
    max-width: 180px;
}

.period-range input {
    width: 100%;
    min-width: 0;
}

.report-filter {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    margin-bottom: 20px;
}

/* автор держится вместе со своей кнопкой сброса при переносе строки */
.report-filter-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* графики */

/* вкладки на узком экране переносятся на вторую строку, а не прокручиваются */
.chart-tabs {
    display: flex;
    flex-wrap: wrap;
    column-gap: 20px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--line);
}

.chart-tabs button {
    flex: none;
    padding: 8px 0;
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    margin-bottom: -1px;
    background: none;
    color: var(--muted);
}

.chart-tabs button.active {
    color: var(--text);
    font-weight: 600;
    border-bottom-color: var(--accent);
}

.chart-options {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 16px;
    margin-bottom: 20px;
}

/* переключатель вкл/выкл: чекбокс в виде ползунка */
label.switch {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    font-size: 15px;
    color: var(--text);
    cursor: pointer;
    user-select: none;
}

label.switch input {
    appearance: none;
    flex: none;
    position: relative;
    width: 36px;
    height: 22px;
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 11px;
    background: var(--line);
    cursor: pointer;
    transition: background-color 0.15s ease;
}

label.switch input::before {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    transition: transform 0.15s ease;
}

label.switch input:checked {
    background: var(--accent);
}

label.switch input:checked::before {
    transform: translateX(14px);
}

label.switch input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.chart-card {
    display: grid;
    grid-template-columns: 240px minmax(0, 1fr);
    gap: 28px;
    align-items: start;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 24px;
}

.donut-wrap {
    position: relative;
    width: 100%;
    max-width: 240px;
    aspect-ratio: 1;
    margin: 0 auto;
}

.donut {
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
}

.donut-arc {
    fill: none;
    stroke: var(--arc-color);
    stroke-width: 26;
    cursor: pointer;
    transition: stroke-dasharray 0.45s ease, stroke-dashoffset 0.45s ease,
        stroke-width 0.15s ease, opacity 0.15s ease;
}

.donut-arc.is-selected {
    stroke-width: 32;
}

.donut-arc.is-dimmed {
    opacity: 0.28;
}

/* подпись в центре кольца — HTML поверх SVG, чтобы длинные названия переносились */
.donut-center {
    position: absolute;
    inset: 22%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    text-align: center;
    pointer-events: none;
}

.donut-caption {
    font-size: 12px;
    color: var(--muted);
}

.donut-name {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    color: var(--text);
    overflow-wrap: anywhere;
}

.donut-value {
    font-size: 20px;
    font-weight: 600;
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
    overflow-wrap: anywhere;
}

.donut-share {
    font-size: 13px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.chart-legend {
    list-style: none;
    margin: 0;
    padding: 0;
    min-width: 0;
}

.legend-group {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-top: 8px;
    padding: 10px 10px 4px;
    border-top: 1px dashed var(--line);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.04em;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.legend-row {
    display: grid;
    grid-template-columns: 12px minmax(0, 1fr) auto 40px;
    grid-template-areas:
        "dot name amount share"
        ".   bar  bar    bar";
    column-gap: 10px;
    row-gap: 5px;
    align-items: baseline;
    width: 100%;
    padding: 8px 10px;
    border: none;
    border-radius: 8px;
    background: none;
    color: var(--text);
    font: inherit;
    text-align: left;
    transition: background-color 0.15s ease, opacity 0.15s ease;
}

.legend-row.is-selected {
    background: var(--bg);
}

.legend-row.is-dimmed {
    opacity: 0.5;
}

.legend-dot {
    grid-area: dot;
    align-self: start;
    margin-top: 4px;
    width: 12px;
    height: 12px;
    border-radius: 3px;
    background: var(--arc-color);
}

/* длинное название переносится на вторую строку и только потом сокращается */
.legend-name {
    grid-area: name;
    min-width: 0;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    overflow-wrap: anywhere;
    line-height: 1.3;
}

.legend-parent {
    color: var(--muted);
}

.legend-amount {
    grid-area: amount;
    font-variant-numeric: tabular-nums;
}

.legend-share {
    grid-area: share;
    text-align: right;
    font-size: 13px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.legend-bar {
    grid-area: bar;
    align-self: center;
    justify-self: start;
    height: 4px;
    border-radius: 2px;
    background: var(--arc-color);
    opacity: 0.55;
    transition: width 0.45s ease;
}

.chart-empty {
    background: var(--surface);
    border: 1px dashed var(--line);
    border-radius: 12px;
    padding: 40px 16px;
    text-align: center;
    color: var(--muted);
}

@media (hover: hover) {
    .legend-row:hover {
        background: var(--bg);
    }
}

/* графики по шагам периода: столбики расходов, линия остатка */

.chart-card.flow-card {
    display: block;
}

.flow-card > * + * {
    margin-top: 16px;
}

.flow-summary {
    display: flex;
    flex-direction: column;
    gap: 2px;
    font-variant-numeric: tabular-nums;
}

.flow-caption {
    font-size: 12px;
    color: var(--muted);
}

.flow-value {
    font-size: 20px;
    font-weight: 600;
    line-height: 1.2;
}

.flow-figures {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 16px;
}

/* сетка: подписи оси значений слева, под графиком — подписи шагов */
.step-plot {
    display: grid;
    grid-template-columns: 52px minmax(0, 1fr);
    grid-template-rows: 180px auto;
    column-gap: 6px;
}

.step-plot-compact {
    grid-template-rows: 110px auto;
}

.step-plot-y {
    position: relative;
    font-size: 11px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.step-plot-y span {
    position: absolute;
    right: 0;
    transform: translateY(50%);
    white-space: nowrap;
}

.step-plot-area {
    position: relative;
    min-width: 0;
}

.step-plot-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
}

.step-plot-grid {
    stroke: var(--line);
    stroke-width: 1;
}

.step-plot-zero {
    stroke: var(--muted);
    stroke-width: 1;
}

/* область выбора — весь шаг по высоте, шире самого столбика */
.step-plot-hits {
    position: absolute;
    inset: 0;
    display: grid;
    grid-template-columns: repeat(var(--steps), minmax(0, 1fr));
}

button.step-plot-hit {
    min-width: 0;
    min-height: 0;
    padding: 0;
    border: none;
    border-radius: 0;
    background: transparent;
}

button.step-plot-hit.is-selected {
    background: rgba(29, 29, 27, 0.06);
}

button.step-plot-hit:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.step-plot-x {
    grid-column: 2;
    display: grid;
    grid-template-columns: repeat(var(--steps), minmax(0, 1fr));
    padding-top: 4px;
    font-size: 11px;
    color: var(--muted);
}

/* подпись шире шага выходит за него поровну в обе стороны */
.step-plot-x span {
    display: flex;
    justify-content: center;
    white-space: nowrap;
}

.flow-bar {
    transition: opacity 0.15s ease;
}

.flow-bar-expense {
    fill: var(--series-2);
}

.flow-bar-income {
    fill: var(--series-3);
}

.flow-bar.is-dimmed {
    opacity: 0.35;
}

.flow-line {
    fill: none;
    stroke: var(--series-1);
    stroke-width: 2;
    stroke-linejoin: round;
}

/* точки — HTML поверх растянутого SVG, чтобы оставаться круглыми */
.flow-dot {
    position: absolute;
    width: 8px;
    height: 8px;
    border: 2px solid var(--surface);
    border-radius: 50%;
    background: var(--series-1);
    transform: translate(-50%, 50%);
    pointer-events: none;
}

.flow-dot.is-selected {
    width: 12px;
    height: 12px;
}

.flow-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 16px;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 13px;
}

.flow-legend li {
    display: flex;
    align-items: center;
    gap: 6px;
}

.flow-swatch {
    width: 12px;
    height: 12px;
    border-radius: 3px;
}

.flow-swatch-line {
    width: 14px;
    height: 2px;
    border-radius: 1px;
    background: var(--series-1);
}

.flow-swatch-income {
    background: var(--series-3);
}

.flow-swatch-expense {
    background: var(--series-2);
}

.filter-currency {
    font-size: 13px;
    color: var(--muted);
}

@media (hover: hover) {
    button.step-plot-hit:not(.is-selected):hover {
        background: rgba(29, 29, 27, 0.04);
    }
}

@media (prefers-reduced-motion: reduce) {
    .donut-arc,
    .flow-bar,
    .legend-row,
    .legend-bar,
    label.switch input,
    label.switch input::before {
        transition: none;
    }
}

/* выбор категорий отчёта в шторке */

.filter-bulk {
    display: flex;
    align-items: center;
    gap: 4px;
}

.filter-count {
    color: var(--muted);
    font-size: 13px;
}

.filter-kind {
    margin: 4px 0 0;
    font-size: 15px;
}

.filter-tree {
    list-style: none;
    margin: 0;
    padding: 0;
}

.filter-row {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 36px;
    padding: 2px 6px;
    border-radius: 4px;
    cursor: pointer;
}

.filter-row.filter-child {
    padding-left: 40px;
}

@media (hover: hover) {
    .filter-row:hover {
        background: var(--bg);
    }
}

.check {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: var(--surface);
    color: #fff;
    font-size: 13px;
    line-height: 1;
}

.check.checked,
.check.mixed {
    border-color: var(--accent);
    background: var(--accent);
}

.report-block {
    margin-bottom: 20px;
}

/* отчёт «Долги»: счёт-кредитор, его суммы за период и журнал займов и погашений */
.debt-account {
    margin-bottom: 24px;
}

.debt-account h3 {
    margin: 0 0 4px;
}

.debt-figures {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 16px;
    margin-bottom: 8px;
}

.debt-entries {
    list-style: none;
    margin: 0;
    padding: 0;
    border-top: 1px solid var(--line);
}

.debt-entry {
    padding: 8px 0;
    border-bottom: 1px solid var(--line);
}

.debt-entry-head {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 2px 12px;
}

.debt-entry-title {
    margin-left: 8px;
}

.debt-entry-note {
    margin-left: 6px;
    font-size: 13px;
    color: var(--muted);
}

.debt-entry-detail {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 16px;
    font-size: 13px;
    color: var(--muted);
}

.debt-entry-detail .tag {
    margin-left: 0;
}

tr.breakdown td:first-child {
    padding-left: 32px;
    color: var(--muted);
}

.login {
    max-width: 320px;
    margin: 80px auto;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 24px;
}

.login h1 {
    margin-bottom: 4px;
}

.login label {
    margin-bottom: 12px;
}

.login button {
    width: 100%;
}

#blazor-error-ui {
    display: none;
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 10px 16px;
    background: #f8e6b0;
    border-top: 1px solid #e0cd92;
}

.picker {
    position: relative;
    display: block;
    min-width: 0;
}

.picker-value {
    display: flex;
    cursor: pointer;
    user-select: none;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 100%;
    font: inherit;
    text-align: left;
    padding: 7px 9px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
}

.picker-value.open {
    border-color: var(--accent);
}

.picker-caret {
    color: var(--muted);
    font-size: 11px;
}

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

.picker-value > span:first-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.picker-drop {
    position: absolute;
    z-index: 20;
    top: calc(100% + 4px);
    left: 0;
    min-width: 100%;
    max-width: 320px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
    padding: 8px;
}

.picker-search {
    width: 100%;
    margin-bottom: 6px;
}

.picker-tree {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 260px;
    overflow-y: auto;
}

.picker-tree li {
    padding: 0;
}

.picker-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 6px;
    border-radius: 4px;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

.picker-row.picker-child {
    padding-left: 22px;
}

.picker-row.highlighted {
    background: var(--bg);
}

.picker-row.selected {
    color: var(--accent);
    font-weight: 600;
}

.picker-row.picker-locked {
    cursor: default;
    font-style: italic;
}

.picker-name {
    overflow: hidden;
    text-overflow: ellipsis;
}

.picker-toggle {
    flex: none;
    width: 18px;
    height: 18px;
    padding: 0;
    line-height: 1;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: var(--bg);
    color: var(--muted);
}

.picker-toggle-slot {
    flex: none;
    width: 18px;
}

.picker-nothing {
    margin: 6px 0 0;
    color: var(--muted);
    font-size: 13px;
}

@media (max-width: 640px) {
    /* четыре вкладки графиков помещаются в одну строку на 360px только с узким зазором */
    .chart-tabs {
        column-gap: 12px;
    }

    .picker-drop {
        max-width: none;
        width: max(100%, 220px);
    }
}

/* в шторке список раскрывается в потоке: иначе его обрезал бы край прокручиваемой шторки */
.sheet .picker-drop {
    position: static;
    width: 100%;
    max-width: none;
    margin-top: 4px;
}

button.secondary:not(.link) {
    background: var(--surface);
    color: var(--text);
    border-color: var(--line);
}

button.danger:not(.link) {
    background: var(--danger);
    border-color: var(--danger);
}

/* шторка: снизу на телефоне, окном по центру на широком экране */

body.sheet-open {
    position: fixed;
    left: 0;
    right: 0;
    overflow: hidden;
}

.sheet-backdrop {
    position: fixed;
    inset: 0;
    z-index: 50;
    background: rgba(0, 0, 0, 0.4);
}

.sheet {
    position: fixed;
    z-index: 51;
    left: 0;
    right: 0;
    bottom: var(--keyboard-inset, 0px);
    max-height: calc(100dvh - var(--keyboard-inset, 0px) - env(safe-area-inset-top) - 24px);
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--surface);
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.18);
    padding: 8px max(16px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    outline: none;
}

.sheet-handle {
    width: 36px;
    height: 4px;
    margin: 0 auto 12px;
    border-radius: 2px;
    background: var(--line);
}

.sheet-title {
    margin: 0 0 12px;
}

.sheet-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.sheet-body p {
    margin: 0;
}

.sheet-body input,
.sheet-body select {
    width: 100%;
}

.sheet-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    margin-top: 16px;
}

.sheet-actions button,
.sheet-menu button {
    min-height: 44px;
}

.sheet-menu {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sheet-menu button {
    width: 100%;
}

.sheet-summary {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.sheet .error {
    margin: 12px 0 0;
}

.amount-input {
    display: flex;
    gap: 6px;
    align-items: stretch;
}

.amount-input input {
    flex: 1;
    min-width: 0;
    text-align: right;
}

.sign-toggle {
    flex: none;
    min-width: 44px;
    padding: 0 10px;
    background: var(--surface);
    color: var(--text);
    border-color: var(--line);
}

/* форма операции в шторке */

.tx-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.tx-form p {
    margin: 0;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
    gap: 12px;
    align-items: end;
}

.form-grid .span-all {
    grid-column: 1 / -1;
}

.form-grid label.toggle {
    margin-bottom: 0;
}

.form-grid input:not([type="checkbox"]),
.form-grid select {
    width: 100%;
    min-width: 0;
}

.allocations {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.allocation {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 8em 44px;
    gap: 6px;
    align-items: start;
}

.allocation > input {
    width: 100%;
    min-width: 0;
    text-align: right;
}

.allocation > button.link {
    min-height: 38px;
    font-size: 18px;
}

.allocations-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

/* список операций карточками — на узком экране */

.tx-cards {
    list-style: none;
    margin: 0;
    padding: 0;
}

.tx-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 8px;
    cursor: pointer;
}

.tx-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    color: var(--muted);
    font-size: 13px;
}

.tx-card-head .tag {
    margin-left: 6px;
}

.tx-card-head .tx-amount {
    font-size: 15px;
}

.tx-card-description {
    overflow-wrap: anywhere;
}

.tx-card .entry-line {
    max-width: none;
}

.tx-card-author {
    color: var(--muted);
    font-size: 13px;
}

.entry-line > span:first-child {
    min-width: 0;
    overflow-wrap: anywhere;
}

.entry-line > .num {
    flex: none;
    white-space: nowrap;
}

/* фильтры: на узком экране свёрнуты в строку-сводку */

.filters-body .picker {
    min-width: 180px;
}

.filters-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 100%;
    min-height: 44px;
    background: var(--surface);
    color: var(--text);
    border-color: var(--line);
}

tr.clickable {
    cursor: pointer;
}

@media (hover: hover) {
    tr.clickable:hover td {
        background: var(--bg);
    }
}

@media (min-width: 641px) {
    .sheet {
        left: 50%;
        right: auto;
        top: 50%;
        bottom: auto;
        width: min(440px, calc(100vw - 32px));
        max-height: calc(100vh - 48px);
        transform: translate(-50%, -50%);
        border-radius: 10px;
        padding: 20px;
    }

    .sheet.sheet-wide {
        width: min(640px, calc(100vw - 32px));
    }

    .sheet-handle {
        display: none;
    }

    .hide-wide {
        display: none;
    }
}

@media (max-width: 640px) {
    /* шрифт полей меньше 16px заставляет Safari масштабировать страницу при фокусе */
    input,
    select,
    textarea {
        font-size: 16px;
    }

    .sheet-actions {
        flex-direction: column-reverse;
    }

    .sheet-actions button {
        width: 100%;
    }

    .hide-narrow {
        display: none;
    }

    .account-group th,
    .account-group td {
        padding-left: 8px;
        padding-right: 8px;
    }

    .account-group .account-kind {
        width: 96px;
    }

    .account-group .account-balance {
        width: 104px;
    }

    button.link {
        min-height: 44px;
    }

    tr.clickable td {
        padding-top: 11px;
        padding-bottom: 11px;
    }

    .cat-row {
        min-height: 44px;
    }

    .period-modes button,
    .report-filter button,
    .report-filter select,
    .filter-row {
        min-height: 44px;
    }

    .period-modes {
        max-width: none;
    }

    .chart-tabs button,
    .chart-options button,
    .chart-options select,
    .chart-options label.switch {
        min-height: 44px;
    }

    .chart-card {
        grid-template-columns: minmax(0, 1fr);
        gap: 20px;
        padding: 20px 12px 12px;
    }

    .donut-wrap {
        max-width: 220px;
    }

    .legend-row {
        min-height: 44px;
    }

    button.cat-toggle,
    .cat-toggle-slot {
        width: 44px;
        margin-left: -14px;
    }

    button.cat-toggle {
        height: 44px;
    }

    .cat-tree .child .cat-row {
        padding-left: 58px;
    }

    .cat-tree .child::after {
        width: 28px;
    }

    /* открытый выбор категории занимает строку целиком, сумма и «×» уходят под него */
    .allocation:has(.picker-drop) {
        grid-template-columns: minmax(0, 1fr) 44px;
    }

    .allocation:has(.picker-drop) .picker {
        grid-column: 1 / -1;
    }

    .filters {
        padding: 0;
        border: none;
        background: none;
    }

    .filters-body {
        margin-top: 12px;
    }

    .filters-body.collapsed {
        display: none;
    }

    .filters-body .row > label {
        flex: 1 1 140px;
    }

    .filters-body .row > label input,
    .filters-body .row > label select {
        width: 100%;
    }

    /* место под нижнюю панель и плавающую кнопку создания */
    .page {
        padding-bottom: calc(140px + env(safe-area-inset-bottom));
    }

    .topbar {
        gap: 8px;
        padding: 6px 0;
        margin-bottom: 16px;
        flex-wrap: nowrap;
    }

    .topbar .user-menu {
        min-width: 0;
        max-width: 60%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    button.create {
        position: fixed;
        z-index: 30;
        right: max(16px, calc(env(safe-area-inset-right) + 8px));
        bottom: calc(52px + env(safe-area-inset-bottom) + 16px);
        width: 56px;
        height: 56px;
        padding: 0;
        border-radius: 50%;
        font-size: 28px;
        line-height: 1;
        box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
    }

    button.create .create-label {
        display: none;
    }

    .tabbar {
        position: fixed;
        z-index: 40;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        background: var(--surface);
        border-top: 1px solid var(--line);
        padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }

    .tabbar a {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 52px;
        min-width: 0;
        font-size: 13px;
        color: var(--muted);
        text-decoration: none;
        border-top: 2px solid transparent;
    }

    .tabbar a.active {
        color: var(--accent);
        font-weight: 600;
        border-top-color: var(--accent);
    }
}

/* заголовок раздела с кнопкой: группа «Люди» на странице счетов */
.section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 24px 0 12px;
}

.section-head h2 {
    margin: 0;
}

/* график «Люди»: линия на контрагента, выше нуля — зелёная, ниже — красная */
.counterparty-line {
    fill: none;
    stroke-width: 2;
    stroke-linejoin: round;
}

.counterparty-stop-positive {
    stop-color: var(--accent);
}

.counterparty-stop-negative {
    stop-color: var(--danger);
}

.flow-dot.counterparty-dot {
    background: var(--muted);
}

.flow-dot.counterparty-dot.positive {
    background: var(--accent);
}

.flow-dot.counterparty-dot.negative {
    background: var(--danger);
}

/* имя у последней точки: слева от неё, чтобы не уходить за правый край */
.counterparty-label {
    position: absolute;
    padding: 0 6px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    background: var(--surface);
    border-radius: 4px;
    transform: translate(calc(-100% - 6px), 50%);
    pointer-events: none;
}

.counterparty-figures {
    display: flex;
    flex-wrap: wrap;
    gap: 2px 16px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.counterparty-figures li {
    display: flex;
    gap: 6px;
}

/* отчёт «Люди»: дата вместо периода и группы «Нам должны» / «Мы должны» */
.report-date {
    display: inline-flex;
    margin-bottom: 20px;
}

.counterparty-group h3 {
    margin-top: 8px;
}

.counterparty-group tfoot td {
    border-top: 1px solid var(--line);
}

/* суммы групп переносятся целиком по валютам, не меняя ширину столбцов */
.account-group-head {
    flex-wrap: wrap;
}

.account-group-totals {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 16px;
    min-width: 0;
}

.account-group-totals .num {
    white-space: normal;
    overflow-wrap: anywhere;
}

@media (max-width: 600px) {
    .account-group-totals {
        flex-basis: 100%;
    }
}
