/* «Расширенный учёт» (js/financeDashboard/operationStatistics.js) — shared by BOTH designs.
 * Colours come from component variables: «Малахит» light/dark values here,
 * «Эфир» re-maps them to --rd-* tokens in css/redesign-web/forms.css. */

:is(.yf-operation-statistics, .yf-stat-options) {
    --st-ink: #1f3a33;
    --st-muted: #6a7f78;
    --st-line: rgba(25, 92, 73, .14);
    --st-bg: rgba(40, 160, 120, .035);
    --st-field: #fff;
    --st-fill: rgba(25, 92, 73, .07);
    --st-thumb: #fff;
    --st-thumb-shadow: 0 1px 2px rgba(12, 42, 34, .14), 0 0 0 .5px rgba(12, 42, 34, .08);
    --st-accent: #168166;
    --st-accent-soft: rgba(22, 129, 102, .1);
    --st-danger: #b84d55;
    --st-radius: 12px;
    --st-ease: cubic-bezier(.2, .8, .2, 1);
}
.dark-theme :is(.yf-operation-statistics, .yf-stat-options) {
    --st-ink: #e3ece9;
    --st-muted: #9aaba6;
    --st-line: rgba(211, 225, 230, .1);
    --st-bg: rgba(255, 255, 255, .025);
    --st-field: #232b30;
    --st-fill: rgba(255, 255, 255, .06);
    --st-thumb: #34424a;
    --st-thumb-shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --st-accent: #6fd3ad;
    --st-on-accent: #0b2621;
    --st-accent-soft: rgba(111, 211, 173, .12);
    --st-danger: #e9868b;
}

.yf-operation-statistics {
    grid-column: 1 / -1;
    flex-basis: 100%;
    width: 100%;
    min-width: 0;
    margin-top: 2px;
    border: 1px solid var(--st-line);
    border-radius: var(--st-radius);
    background: var(--st-bg);
    color: var(--st-ink);
    font-size: 13px;
}
.yf-operation-statistics [hidden] { display: none !important; }

/* Toggle row */
.yf-stat-toggle {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 48px;
    padding: 8px 12px;
    border: 0;
    border-radius: var(--st-radius);
    background: transparent;
    color: inherit;
    text-align: left;
    cursor: pointer;
}
.yf-stat-toggle:focus-visible { outline: 2px solid var(--st-accent); outline-offset: -2px; }
.yf-stat-toggle__icon {
    display: inline-grid;
    flex: none;
    width: 30px;
    height: 30px;
    place-items: center;
    border-radius: 9px;
    background: var(--st-accent-soft);
    color: var(--st-accent);
    font-size: 16px;
}
.yf-stat-toggle__text { display: grid; flex: 1 1 auto; min-width: 0; line-height: 1.25; }
.yf-stat-toggle__text strong { font-size: 13.5px; font-weight: 700; }
.yf-stat-toggle__text small { overflow: hidden; color: var(--st-muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.yf-statistics-summary {
    flex: none;
    max-width: 44%;
    padding: 3px 9px;
    overflow: hidden;
    border-radius: 999px;
    background: var(--st-accent-soft);
    color: var(--st-accent);
    font-size: 12px;
    font-weight: 650;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.yf-statistics-chevron { flex: none; color: var(--st-muted); font-size: 16px; transition: transform .38s var(--st-ease); }
.yf-operation-statistics.is-open .yf-statistics-chevron { transform: rotate(180deg); }

/* Smooth open/close: rows 0fr → 1fr keeps real content height */
:is(.yf-stat-panel, .yf-stat-collapse) {
    display: grid;
    grid-template-rows: 0fr;
    opacity: 0;
    transition: grid-template-rows .4s var(--st-ease), opacity .28s ease;
}
:is(.yf-stat-panel__inner, .yf-stat-collapse__inner) { min-height: 0; overflow: hidden; }
.yf-operation-statistics.is-open > .yf-stat-panel,
.yf-stat-collapse.is-open { grid-template-rows: 1fr; opacity: 1; }
.yf-stat-collapse.is-open > .yf-stat-collapse__inner { overflow: visible; }
.yf-operation-statistics:not(.is-open) .yf-stat-panel__inner { visibility: hidden; transition: visibility 0s .4s; }

.yf-statistics-body {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr);
    gap: 14px 16px;
    padding: 4px 12px 14px;
}
.yf-statistics-body > :is(.yf-stat-collapse, .yf-statistics-reason) { grid-column: 1 / -1; }
.yf-statistics-field { display: grid; gap: 7px; min-width: 0; }
.yf-stat-label { display: inline-flex; align-items: center; gap: 5px; margin: 0; color: var(--st-muted); font-size: 12px; font-weight: 650; }
.yf-stat-note { color: var(--st-muted); font-size: 11.5px; }

/* Segmented choices */
.yf-stat-seg {
    display: grid;
    grid-auto-columns: minmax(0, 1fr);
    grid-auto-flow: column;
    gap: 3px;
    padding: 3px;
    border-radius: 11px;
    background: var(--st-fill);
}
.yf-stat-seg__item {
    min-height: 34px;
    padding: 0 8px;
    overflow: hidden;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--st-muted);
    font-size: 12.5px;
    font-weight: 650;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: background-color .3s var(--st-ease), color .3s var(--st-ease), box-shadow .3s var(--st-ease);
}
.yf-stat-seg__item:hover:not(:disabled):not(.is-active) { color: var(--st-ink); }
.yf-stat-seg__item.is-active { background: var(--st-thumb); box-shadow: var(--st-thumb-shadow); color: var(--st-ink); }
.yf-stat-seg__item:focus-visible { outline: 2px solid var(--st-accent); outline-offset: 1px; }
.yf-stat-seg.is-disabled { opacity: .55; }

/* Linked expenses */
.yf-statistics-links-card {
    display: grid;
    gap: 12px;
    padding: 12px;
    border: 1px solid var(--st-line);
    border-radius: var(--st-radius);
    background: var(--st-field);
}
.yf-statistics-links-heading { display: flex; align-items: center; gap: 5px; font-size: 13px; font-weight: 700; }
.yf-statistics-links-heading em { color: var(--st-muted); font-style: normal; font-weight: 500; }
.yf-statistics-expenses { display: grid; gap: 10px; }
.yf-statistics-expenses:empty { display: none; }
.yf-statistics-link-row {
    display: grid;
    grid-template-columns: minmax(0, .9fr) minmax(0, 1fr) minmax(0, 1.5fr) 34px;
    gap: 8px;
    align-items: end;
    animation: yf-stat-row-in .32s var(--st-ease) both;
}
@keyframes yf-stat-row-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.yf-stat-step { display: grid; gap: 5px; min-width: 0; }
.yf-stat-step__label { display: inline-flex; align-items: center; gap: 6px; color: var(--st-muted); font-size: 11.5px; font-weight: 650; }
.yf-stat-step__label b {
    display: inline-grid;
    width: 18px;
    height: 18px;
    place-items: center;
    border-radius: 50%;
    background: var(--st-fill);
    font-size: 10.5px;
    transition: background-color .3s ease, color .3s ease;
}
.yf-stat-step.is-filled .yf-stat-step__label b { background: var(--st-accent); color: var(--st-on-accent, #fff); }
.yf-stat-picker {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    min-height: 42px;
    padding: 0 10px 0 12px;
    border: 1px solid var(--st-line);
    border-radius: 10px;
    background: var(--st-field);
    color: var(--st-ink);
    font-size: 13px;
    text-align: left;
    transition: border-color .2s ease, box-shadow .2s ease, opacity .25s ease;
}
.yf-stat-picker:hover:not(:disabled) { border-color: var(--st-accent); }
.yf-stat-picker[aria-expanded="true"] { border-color: var(--st-accent); box-shadow: 0 0 0 3px var(--st-accent-soft); }
.yf-stat-picker:disabled { opacity: .5; cursor: not-allowed; }
.yf-stat-picker > i { flex: none; margin-left: auto; color: var(--st-muted); }
.yf-stat-picker__value { display: flex; flex: 1 1 auto; align-items: center; gap: 7px; min-width: 0; overflow: hidden; white-space: nowrap; }
.yf-stat-step:not(.is-filled) .yf-stat-picker__value { color: var(--st-muted); }
.yf-stat-picker__value > span:last-child,
.yf-stat-option__comment { overflow: hidden; text-overflow: ellipsis; }
.yf-stat-loading { color: var(--st-muted); }
.yf-statistics-remove {
    display: inline-grid;
    width: 34px;
    height: 42px;
    place-items: center;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--st-muted);
    transition: background-color .2s ease, color .2s ease;
}
.yf-statistics-remove:hover { background: rgba(184, 77, 85, .1); color: var(--st-danger); }
.yf-statistics-row-status { grid-column: 1 / -1; color: var(--st-muted); font-size: 12px; }
.yf-statistics-row-status:empty,
.yf-statistics-link-status:empty { display: none; }
.yf-statistics-link-status { color: var(--st-muted); font-size: 12px; }
.yf-stat-add {
    display: inline-flex;
    align-items: center;
    justify-self: start;
    gap: 7px;
    min-height: 38px;
    padding: 0 14px;
    border: 1px dashed color-mix(in srgb, var(--st-accent) 45%, transparent);
    border-radius: 10px;
    background: transparent;
    color: var(--st-accent);
    font-size: 13px;
    font-weight: 650;
    transition: background-color .2s ease, border-color .2s ease;
}
.yf-stat-add:hover:not(:disabled) { border-color: var(--st-accent); background: var(--st-accent-soft); }
.yf-stat-add:disabled { opacity: .5; }
.yf-stat-add--quiet { min-height: 30px; padding: 0 10px; border-style: solid; }
.yf-statistics-reason {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 8px;
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--st-accent-soft);
    color: var(--st-ink);
    font-size: 12.5px;
}

/* Floating lists (moved to <body> while open) */
.yf-stat-options { padding: 6px !important; color: var(--st-ink); }
.yf-stat-option {
    display: flex;
    align-items: center;
    gap: 9px;
    width: 100%;
    min-height: 38px;
    padding: 7px 10px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: inherit;
    font-size: 13px;
    text-align: left;
}
.yf-stat-option:hover:not(:disabled),
.yf-stat-option:focus-visible { background: var(--st-fill); outline: none; }
.yf-stat-option.is-selected { background: var(--st-accent-soft); color: var(--st-accent); }
.yf-stat-option:disabled { opacity: .45; cursor: not-allowed; }
.yf-stat-option > span:last-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.yf-stat-option__date { flex: none; min-width: 42px; font-variant-numeric: tabular-nums; font-weight: 700; }
.yf-stat-option__amount { flex: none; font-variant-numeric: tabular-nums; font-weight: 650; }
.yf-stat-option__comment { color: var(--st-muted); }
.yf-stat-option.is-selected .yf-stat-option__comment { color: inherit; }
.yf-stat-empty { display: block; padding: 10px; color: var(--st-muted); font-size: 12.5px; }
.yf-stat-more { width: 100%; min-height: 34px; margin-top: 4px; border: 0; border-radius: 8px; background: var(--st-fill); color: var(--st-accent); font-size: 12.5px; font-weight: 650; }

/* Step 1: compact month calendar */
.yf-stat-calendar { min-width: 240px; }
.yf-stat-calendar__head { display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 8px; }
.yf-stat-calendar__head strong { font-size: 14px; }
.yf-stat-calendar__head button {
    display: inline-grid;
    width: 30px;
    height: 30px;
    place-items: center;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--st-ink);
}
.yf-stat-calendar__head button:hover:not(:disabled) { background: var(--st-fill); }
.yf-stat-calendar__head button:disabled { opacity: .3; }
.yf-stat-calendar__grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
.yf-stat-calendar__month {
    min-height: 36px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--st-ink);
    font-size: 12.5px;
    font-weight: 650;
}
.yf-stat-calendar__month:hover:not(:disabled) { background: var(--st-fill); }
.yf-stat-calendar__month.is-selected { background: var(--st-accent); color: var(--st-on-accent, #fff); }
.yf-stat-calendar__month:disabled { color: var(--st-muted); opacity: .4; cursor: not-allowed; }

/* Hosts */
.income-plan-modal-row > .yf-operation-statistics { grid-column: 1 / -1; margin: .25rem 0 0; }
.yf-entry-editor > .yf-operation-statistics,
.yf-expense-plan-row > .yf-operation-statistics { grid-column: 1 / -1; }
.income-plan-modal-row.is-deleted { opacity: .5; }
.yf-year-planning-table th,
.yf-year-planning-table td { white-space: nowrap; }

@media (max-width: 767.98px) {
    .yf-statistics-body { grid-template-columns: minmax(0, 1fr); padding: 2px 10px 12px; }
    .yf-statistics-link-row { grid-template-columns: minmax(0, 1fr) 34px; }
    .yf-statistics-link-row > .yf-stat-step { grid-column: 1; }
    .yf-statistics-remove { grid-column: 2; grid-row: 1; align-self: end; }
    .yf-stat-toggle__text small { display: none; }
    .yf-entry-editor:has(> .yf-operation-statistics) .yf-entry-editor__actions { position: static; justify-content: flex-end; }
}
/* Form-level widget (new entry / new plan) sits under the field grid */
form > .yf-operation-statistics { margin-top: 14px; }

/* Asset classification composes the existing disclosure and account-switch primitives. */
.yf-operation-statistics.yf-operation-statistics--inline { margin-top: 0; border: 0; background: transparent; box-shadow: none; }
.yf-operation-statistics--inline > .yf-stat-toggle { width: auto; min-height: 32px; gap: 6px; padding: 4px 0; }
.yf-operation-statistics--inline > .yf-stat-toggle .yf-stat-toggle__text { flex: none; }
.yf-asset-edit > .yf-asset-liquidity { display: block; }
.yf-asset-liquidity__body { padding: 4px 0 6px; }
.yf-asset-liquidity__body .yf-account-switch { margin: 0; }
.yf-asset-liquidity__body .yf-account-switch > .yf-asset-liquidity__label { display: inline-flex; align-items: center; gap: 4px; min-width: 0; }
.yf-asset-create > .yf-asset-liquidity ~ button { grid-column: 1 / -1; }

/* Adapt to the widget's own width (narrow editors and modals), not only the viewport */
.yf-operation-statistics { container-type: inline-size; }
@container (max-width: 640px) {
    .yf-statistics-body { grid-template-columns: minmax(0, 1fr); }
    .yf-statistics-link-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 34px; }
    .yf-statistics-link-row > .yf-stat-step[data-step="3"] { grid-column: 1 / 3; }
    .yf-statistics-link-row > .yf-statistics-remove { grid-column: 3; grid-row: 1; }
}
@container (max-width: 420px) {
    .yf-statistics-link-row { grid-template-columns: minmax(0, 1fr) 34px; }
    .yf-statistics-link-row > .yf-stat-step { grid-column: 1; }
    .yf-statistics-link-row > .yf-stat-step[data-step="3"] { grid-column: 1; }
    .yf-statistics-remove { grid-column: 2; grid-row: 1; }
    .yf-stat-toggle__text small { display: none; }
}

/* ── Date popover (js/financeDashboard/formControls.js) ── */
:is(.yf-date-popover) {
    --st-ink: #1f3a33; --st-muted: #6a7f78; --st-fill: rgba(25, 92, 73, .07);
    --st-accent: #168166; --st-accent-soft: rgba(22, 129, 102, .1);
}
.dark-theme .yf-date-popover { --st-on-accent: #0b2621; --st-ink: #e3ece9; --st-muted: #9aaba6; --st-fill: rgba(255, 255, 255, .06); --st-accent: #6fd3ad; --st-accent-soft: rgba(111, 211, 173, .12); }
.yf-date-popover { padding: 10px !important; color: var(--st-ink); }
.yf-date-popover__head { display: flex; align-items: center; justify-content: space-between; padding: 0 0 8px; }
.yf-date-popover__head strong { font-size: 14px; font-weight: 700; text-transform: capitalize; }
.yf-date-popover__head button {
    display: inline-grid; width: 30px; height: 30px; place-items: center;
    border: 0; border-radius: 8px; background: transparent; color: var(--st-ink);
}
.yf-date-popover__head button:hover:not(:disabled) { background: var(--st-fill); }
.yf-date-popover__head button:disabled { opacity: .3; }
.yf-date-popover__week,
.yf-date-popover__grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 3px; }
.yf-date-popover__week span { padding: 2px 0 6px; color: var(--st-muted); font-size: 11px; font-weight: 650; text-align: center; }
.yf-date-popover__day {
    height: 34px; border: 0; border-radius: 9px; background: transparent;
    color: var(--st-ink); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums;
    transition: background-color .18s ease, color .18s ease;
}
.yf-date-popover__day:hover:not(:disabled) { background: var(--st-fill); }
.yf-date-popover__day.is-today { color: var(--st-accent); font-weight: 800; }
.yf-date-popover__day.is-selected { background: var(--st-accent); color: var(--st-on-accent, #fff); }
.yf-date-popover__day:disabled { opacity: .3; cursor: not-allowed; }
.yf-compact-date input[type="date"]::-webkit-calendar-picker-indicator { display: none; }

/* A planned expense in the list of a planned income */
.yf-stat-option__kind { flex: none; padding: 1px 7px; border-radius: 999px; background: var(--st-accent-soft); color: var(--st-accent); font-size: 10.5px; font-weight: 700; line-height: 1.6; text-transform: uppercase; letter-spacing: .03em; }

/* «Сумма для статистики»: a compact field, as wide as an ordinary amount, not the whole panel */
.yf-statistics-custom .yf-amount-input { width: 100%; max-width: 280px; }
@media (max-width: 575.98px) { .yf-statistics-custom .yf-amount-input { max-width: none; } }
.yf-date-popover--months .yf-date-popover__grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.yf-date-popover--months .yf-date-popover__day { min-height: 40px; text-transform: capitalize; }
