/* ===== Cards ============================================================= */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
  min-width: 0;
}
.card__head {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: 16px 20px 0;
}
.card__head h2 { margin-right: auto; }
.card__body { padding: 16px 20px 20px; }
.card__foot { padding: 12px 20px; border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 var(--radius-lg) var(--radius-lg); }
.card--flush .card__body { padding: 0; }

/* ===== Buttons =========================================================== */
.btn {
  --btn-bg: var(--surface);
  --btn-ink: var(--ink);
  --btn-line: var(--line-strong);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--btn-line);
  border-radius: var(--radius-sm);
  background: var(--btn-bg);
  color: var(--btn-ink);
  font: inherit; font-size: var(--fs-sm); font-weight: 600; line-height: 1.2;
  text-decoration: none; white-space: nowrap;
  cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease), color var(--dur) var(--ease);
}
.btn:hover { --btn-bg: var(--surface-sunken); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }
.btn--primary { --btn-bg: var(--accent); --btn-ink: #fff; --btn-line: var(--accent); }
.btn--primary:hover { --btn-bg: var(--accent-hover); --btn-line: var(--accent-hover); }
.btn--ghost { --btn-line: transparent; --btn-bg: transparent; }
.btn--ghost:hover { --btn-bg: var(--surface-sunken); }
.btn--danger { --btn-ink: var(--over); --btn-line: color-mix(in srgb, var(--over) 40%, transparent); }
.btn--danger:hover { --btn-bg: var(--over-soft); }
.btn--sm { min-height: 32px; padding: 4px 10px; font-size: var(--fs-xs); }
.btn--icon { min-width: var(--touch); min-height: var(--touch); padding: 0; }
.btn--block { width: 100%; }
@media (pointer: coarse) {
  .btn { min-height: var(--touch); }
  .btn--sm { min-height: 36px; }
}
.btn-group { display: inline-flex; flex-wrap: wrap; gap: 8px; }
.button_to { display: inline; }

/* Segmented control (radio-like links / buttons) */
.segmented {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-sunken);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius-sm) + 3px);
}
.segmented a, .segmented button, .segmented label {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 34px; padding: 4px 12px;
  border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--ink-2);
  font: inherit; font-size: var(--fs-sm); font-weight: 600;
  text-decoration: none; cursor: pointer;
}
.segmented [aria-current="true"], .segmented [aria-pressed="true"], .segmented input:checked + span {
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow-1);
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-1); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 1px; }

/* ===== Forms ============================================================= */
.form { display: grid; gap: 16px; }
.form-grid { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 640px) {
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid .span-2 { grid-column: span 2; }
}
@media (min-width: 1100px) {
  .form-grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .form-grid--3 .span-3 { grid-column: span 3; }
}
.field { display: grid; gap: 6px; min-width: 0; align-content: start; }
.field > label, .field__label { font-size: var(--fs-sm); font-weight: 600; color: var(--ink-2); }
.field__hint { font-size: var(--fs-xs); color: var(--ink-3); }
.field__error { font-size: var(--fs-xs); color: var(--over); font-weight: 600; }

.input, .select, .textarea {
  width: 100%;
  min-height: var(--touch);
  padding: 9px 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  font: inherit; font-size: 1rem;
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.textarea { min-height: 88px; resize: vertical; }
.input:hover, .select:hover, .textarea:hover { border-color: var(--ink-3); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent);
}
.input[aria-invalid="true"], .field--error .input, .field--error .select { border-color: var(--over); }
.input--num { font-variant-numeric: tabular-nums; text-align: right; }
.input-affix { position: relative; display: flex; align-items: center; }
.input-affix .input { padding-right: 40px; }
.input-affix__suffix { position: absolute; right: 12px; color: var(--ink-3); pointer-events: none; font-weight: 600; }

.check { display: inline-flex; align-items: center; gap: 10px; min-height: var(--touch); cursor: pointer; font-size: var(--fs-sm); }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.checks { display: flex; flex-wrap: wrap; gap: 4px 16px; }

.form-errors {
  border: 1px solid color-mix(in srgb, var(--over) 35%, transparent);
  background: var(--over-soft);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 12px 16px;
}
.form-errors h3 { color: var(--over); margin-bottom: 6px; }
.form-errors ul { margin: 0; padding-left: 18px; }
.form-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* Range slider */
.range {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: var(--touch);
  background: transparent;
  cursor: pointer;
  --pct: 0%;
}
.range::-webkit-slider-runnable-track {
  height: 6px; border-radius: 999px;
  background: linear-gradient(to right, var(--accent) 0 var(--pct), var(--line) var(--pct) 100%);
}
.range::-moz-range-track { height: 6px; border-radius: 999px; background: var(--line); }
.range::-moz-range-progress { height: 6px; border-radius: 999px; background: var(--accent); }
.range::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 22px; height: 22px; margin-top: -8px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--accent);
  box-shadow: var(--shadow-2);
  transition: transform var(--dur) var(--ease);
}
.range::-moz-range-thumb {
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--accent); box-shadow: var(--shadow-2);
}
.range:active::-webkit-slider-thumb { transform: scale(1.12); }
.range:focus-visible { outline: none; }
.range:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px color-mix(in srgb, var(--focus) 35%, transparent); }
.range:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 4px color-mix(in srgb, var(--focus) 35%, transparent); }
.range:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Badges & chips ==================================================== */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: var(--fs-xs); font-weight: 600; line-height: 1.5;
  background: var(--plan-soft); color: var(--plan);
  white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge--plain::before { display: none; }
.badge--planned { background: var(--accent-soft); color: var(--accent-ink); }
.badge--paid, .badge--fact, .badge--good { background: var(--fact-soft); color: var(--fact); }
.badge--partially_paid, .badge--pending, .badge--warn { background: var(--pending-soft); color: var(--pending); }
.badge--cancelled { background: var(--plan-soft); color: var(--ink-3); text-decoration: line-through; }
.badge--over, .badge--bad { background: var(--over-soft); color: var(--over); }
.badge--estimate { background: var(--pending-soft); color: var(--pending); }

.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; vertical-align: middle; }

/* ===== KPI tiles ========================================================= */
.kpis { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 768px) { .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 1280px) { .kpis { grid-template-columns: repeat(6, minmax(0, 1fr)); } }
.kpi {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  min-width: 0;
}
.kpi__label { font-size: var(--fs-xs); color: var(--ink-3); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi__value { font-size: var(--fs-lg); font-weight: 700; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi__meta { font-size: var(--fs-xs); color: var(--ink-2); margin-top: 2px; }
.kpi__value--good { color: var(--fact); }
.kpi__value--bad { color: var(--over); }

/* ===== Tables ============================================================ */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-3); font-weight: 600; background: var(--surface-2); }
.table td.num, .table th.num { text-align: right; }
.table tbody tr:hover { background: var(--surface-2); }
.table tfoot td { font-weight: 700; background: var(--surface-2); }
.table .actions { text-align: right; white-space: nowrap; }

/* Definition list */
.dl { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 16px; margin: 0; font-size: var(--fs-sm); }
.dl dt { color: var(--ink-2); }
.dl dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.dl .dl__total { border-top: 1px solid var(--line); padding-top: 8px; }

/* ===== Progress ========================================================== */
progress.bar {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 8px; border: 0; border-radius: 999px;
  background: var(--line); overflow: hidden;
}
progress.bar::-webkit-progress-bar { background: var(--line); border-radius: 999px; }
progress.bar::-webkit-progress-value { background: var(--accent); border-radius: 999px; }
progress.bar::-moz-progress-bar { background: var(--accent); border-radius: 999px; }
progress.bar--good::-webkit-progress-value { background: var(--fact); }
progress.bar--good::-moz-progress-bar { background: var(--fact); }
progress.bar--over::-webkit-progress-value { background: var(--over); }
progress.bar--over::-moz-progress-bar { background: var(--over); }

/* ===== Notices =========================================================== */
.flashes { position: fixed; z-index: 60; right: var(--gutter); bottom: var(--gutter); left: var(--gutter); display: grid; gap: 8px; justify-items: end; pointer-events: none; }
.flash {
  pointer-events: auto;
  display: flex; align-items: flex-start; gap: 10px;
  max-width: 480px;
  padding: 12px 14px;
  background: var(--ink); color: var(--ink-inverse);
  border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
  font-size: var(--fs-sm);
  animation: flash-in 220ms var(--ease);
}
.flash--alert { background: var(--over); color: #fff; }
.flash button { margin-left: auto; background: none; border: 0; color: inherit; cursor: pointer; font-size: 18px; line-height: 1; padding: 0 2px; min-width: 24px; min-height: 24px; }
@keyframes flash-in { from { transform: translateY(8px); opacity: 0; } }

.callout {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--accent-soft);
  color: var(--ink);
  font-size: var(--fs-sm);
  border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
}
.callout p:last-child { margin: 0; }
.callout--warn { background: var(--pending-soft); border-color: color-mix(in srgb, var(--pending-mark) 45%, transparent); }
.callout--bad { background: var(--over-soft); border-color: color-mix(in srgb, var(--over) 35%, transparent); }
.callout__icon { flex: none; font-weight: 800; }

.empty {
  display: grid; justify-items: center; gap: 8px;
  padding: 36px 20px; text-align: center; color: var(--ink-2);
}
.empty h3 { color: var(--ink); }

/* ===== Drawer (native <dialog>) ========================================= */
dialog.drawer {
  margin: 0 0 0 auto;
  width: min(100vw, 460px);
  max-width: 100vw;
  height: 100vh; height: 100dvh; max-height: none;
  padding: 0; border: 0;
  background: var(--surface); color: var(--ink);
  box-shadow: var(--shadow-pop);
  overflow: hidden;
}
dialog.drawer[open] { display: flex; flex-direction: column; animation: drawer-in 220ms var(--ease); }
dialog.drawer::backdrop { background: var(--overlay); }
@keyframes drawer-in { from { transform: translateX(24px); opacity: 0; } }
@media (max-width: 640px) {
  /* A bottom sheet: a grab handle on top, lifted above the on-screen keyboard
     (--keyboard-inset is set by drawer_controller from visualViewport), and the
     form buttons pinned to its bottom edge so they never hide under the keys. */
  dialog.drawer {
    margin: auto 0 var(--keyboard-inset, 0); width: 100vw; height: auto;
    max-height: calc(92dvh - var(--keyboard-inset, 0px)); border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }
  dialog.drawer::before { content: ""; flex: none; width: 40px; height: 4px; margin: 8px auto 0; border-radius: 2px; background: var(--line-strong); }
  @keyframes drawer-in { from { transform: translateY(24px); opacity: 0; } }
  .drawer__body:has(.form-actions) { padding-bottom: 0; }
  .drawer__body .form-actions {
    position: sticky; bottom: 0; z-index: 1; margin: 0 -20px; padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--line);
  }
}
.drawer__head { display: flex; align-items: center; gap: 8px; padding: 14px 16px 14px 20px; border-bottom: 1px solid var(--line); }
.drawer__head h2 { margin-right: auto; font-size: var(--fs-lg); }
.drawer__body { padding: 16px 20px 24px; overflow-y: auto; flex: 1; display: grid; gap: 16px; align-content: start; }
.drawer turbo-frame { display: contents; }

/* ===== Lists ============================================================= */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.list > li:last-child { border-bottom: 0; }
.list__main { min-width: 0; flex: 1; }
.list__title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.list__meta { font-size: var(--fs-xs); color: var(--ink-3); }

/* Entry card in planner drawer */
.entry {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; display: grid; gap: 6px;
  background: var(--surface-2);
}
.entry__top { display: flex; align-items: center; gap: 8px; }
.entry__title { font-weight: 600; margin-right: auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.entry__amounts { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: var(--fs-xs); color: var(--ink-2); }
.entry__actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.entry__state { margin: 0; font-size: var(--fs-xs); color: var(--ink-2); }

/* Limit (○) or commitment (◆): the marks repeat a text label, never replace it. */
.nature-mark { color: var(--ink-3); margin-right: 3px; }
.nature-toggle { border: 0; padding: 0; margin: 0; min-width: 0; display: grid; gap: 6px; }
.nature-toggle .segmented { display: flex; flex-wrap: wrap; }
.nature-toggle .segmented label { flex: 1 1 11rem; min-height: var(--touch); }

/* Monthly overspending by category (planner, home screen). */
.overspent__list { margin: 4px 0 0; padding-left: 1.1em; display: grid; gap: 2px; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--ink-2); text-decoration: none; font-weight: 600; font-size: var(--fs-sm); border-bottom: 2px solid transparent; white-space: nowrap; min-height: var(--touch); display: inline-flex; align-items: center; }
.tabs a[aria-current="page"] { color: var(--accent-ink); border-bottom-color: var(--accent); }

details.disclosure { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-2); }
details.disclosure > summary { cursor: pointer; padding: 12px 16px; font-weight: 600; min-height: var(--touch); display: flex; align-items: center; }
details.disclosure > div { padding: 0 16px 16px; }

/* ===== Month switcher ==================================================== */
.month-switcher { display: inline-flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.month-switcher__form { display: inline-flex; gap: 6px; }
.select--inline { width: auto; min-height: 40px; padding: 6px 30px 6px 10px; font-weight: 600; }
.input--year { width: 5.5em; min-height: 40px; padding: 6px 8px; font-weight: 600; text-align: center; }

.drawer__notice { padding: 10px 12px; border-radius: var(--radius-sm); background: var(--fact-soft); color: var(--fact); font-size: var(--fs-sm); font-weight: 600; }
.drawer__notice.is-alert { background: var(--over-soft); color: var(--over); }

/* Never truncate money on small screens – wrap and shrink instead. */
@media (max-width: 639px) {
  .kpi { padding: 12px; }
  .kpi__value { white-space: normal; overflow: visible; text-overflow: clip; font-size: var(--fs-md); overflow-wrap: anywhere; }
}

/* ===== Quick entry sheet («Записать», SPEC-6) ============================ */
.quick-entry fieldset { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; min-width: 0; }
.quick-entry__direction { display: flex; }
.quick-entry__direction label { flex: 1; min-height: var(--touch); }
.quick-entry__amount { font-size: 1.75rem; font-weight: 650; min-height: 56px; }
/* Only the categories of the chosen direction are shown (no script needed). */
.quick-entry:has(.quick-entry__direction input[value="in"]:checked) .quick-entry__chips--out,
.quick-entry:not(:has(.quick-entry__direction input[value="in"]:checked)) .quick-entry__chips--in { display: none; }
.quick-entry__more summary { cursor: pointer; min-height: var(--touch); display: flex; align-items: center; color: var(--ink-2); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  position: relative; display: inline-flex; align-items: center; min-height: var(--touch); padding: 6px 14px;
  border: 1px solid var(--line-strong); border-radius: 999px; background: var(--surface); color: var(--ink);
  font-size: var(--fs-sm); font-weight: 600; cursor: pointer;
}
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-ink); }
.chip:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.quick-entry__days { margin-top: 8px; }
button.chip { font: inherit; font-size: var(--fs-sm); font-weight: 600; }
