/* ============================================================
   Shared components: buttons, cards, tables, forms, chips,
   dialogs, toasts, progress, empty states.
   ============================================================ */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  height: 36px;
  padding: 0 var(--s-4);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-sm);
  font-weight: 550;
  white-space: nowrap;
  transition: background var(--dur-fast) var(--ease),
              border-color var(--dur-fast) var(--ease),
              color var(--dur-fast) var(--ease);
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn--primary { background: var(--brand); color: var(--brand-fg); }
.btn--primary:hover:not(:disabled) { background: var(--brand-hover); }

.btn--secondary { background: var(--surface); border-color: var(--border-strong); }
.btn--secondary:hover:not(:disabled) { background: var(--surface-2); }

.btn--ghost { background: transparent; color: var(--text-2); }
.btn--ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn--danger { background: var(--danger); color: #fff; }
[data-theme="dark"] .btn--danger { color: #2a1211; }
.btn--danger:hover:not(:disabled) { filter: brightness(1.08); }

.btn--sm { height: 30px; padding: 0 var(--s-3); font-size: var(--fs-xs); }

.btn-icon {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: var(--r-sm);
  color: var(--text-2);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.btn-icon:hover { background: var(--surface-2); color: var(--text); }
.btn-icon--danger:hover { background: var(--danger-soft); color: var(--danger); }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
}
.card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  padding: var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.card__title { font-size: var(--fs-h2); }
.card__body { padding: var(--s-5); }
.card__body--flush { padding: 0; }
.card__foot {
  padding: var(--s-3) var(--s-5);
  border-top: 1px solid var(--border);
}

/* ---------- Stat tile ---------- */
.stat {
  padding: var(--s-4) var(--s-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  display: grid;
  gap: var(--s-1);
  align-content: start;
}
.stat__label {
  font-family: var(--font-mono);
  font-size: var(--fs-mono);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--text-3);
}
.stat__value {
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
  line-height: 1.2;
  overflow-wrap: anywhere;
}
.stat__note {
  font-size: var(--fs-xs);
  color: var(--text-2);
  display: flex;
  align-items: center;
  gap: var(--s-1);
  flex-wrap: wrap;
}

/* ---------- Delta ---------- */
.delta {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: var(--fs-xs);
  font-weight: 550;
  font-variant-numeric: tabular-nums;
}
.delta--up   { color: var(--pos); }
.delta--down { color: var(--neg); }
.delta--flat { color: var(--text-3); }

/* ---------- Amount ---------- */
.amount { font-variant-numeric: tabular-nums lining-nums; white-space: nowrap; }
/* Only income is tinted; expenses stay in the default ink so a ledger of
   mostly-outgoings does not turn into a wall of colour. */
.amount--in { color: var(--pos); }

/* ---------- Chips / badges ---------- */
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  padding: 2px var(--s-2);
  border-radius: var(--r-xs);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: var(--fs-mono);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--text-2);
  white-space: nowrap;
}
.chip__dot {
  width: 6px;
  height: 6px;
  border-radius: var(--r-full);
  background: currentColor;
  flex: none;
}
.chip--ok    { color: var(--pos);    border-color: color-mix(in srgb, var(--pos) 34%, transparent);    background: var(--pos-soft); }
.chip--warn  { color: var(--warn);   border-color: color-mix(in srgb, var(--warn) 34%, transparent);   background: var(--warn-soft); }
.chip--over  { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 34%, transparent); background: var(--danger-soft); }
.chip--muted { color: var(--text-3); }

/* ---------- Category swatch ---------- */
.swatch {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  flex: none;
  background: var(--swatch, var(--text-3));
}
.cat-icon {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: var(--r-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--swatch, var(--text-2));
}

/* ---------- Progress ---------- */
.progress {
  height: 6px;
  border-radius: var(--r-full);
  background: var(--track);
  overflow: hidden;
}
.progress__fill {
  height: 100%;
  border-radius: var(--r-full);
  background: var(--brand);
  transition: width var(--dur) var(--ease);
}
.progress--warn .progress__fill { background: var(--warn); }
.progress--over .progress__fill { background: var(--danger); }

/* ---------- Segmented control ---------- */
.segmented {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
}
.segmented__opt {
  padding: 3px var(--s-3);
  border-radius: var(--r-xs);
  font-family: var(--font-mono);
  font-size: var(--fs-mono);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--text-2);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.segmented__opt:hover { color: var(--text); }
.segmented__opt[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.theme-switch {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
}
.theme-switch__opt {
  padding: var(--s-1) 0;
  border-radius: var(--r-xs);
  font-size: var(--fs-xs);
  color: var(--text-2);
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.theme-switch__opt:hover { color: var(--text); }
.theme-switch__opt[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 550; }

/* ---------- Forms ---------- */
.field { display: grid; gap: var(--s-2); }
.field__label {
  font-size: var(--fs-sm);
  font-weight: 550;
  color: var(--text-2);
}
.field__hint { font-size: var(--fs-xs); color: var(--text-3); }
.field__error {
  font-size: var(--fs-xs);
  color: var(--danger);
  display: flex;
  align-items: center;
  gap: var(--s-1);
}

.input, .select, .textarea {
  width: 100%;
  min-height: 38px;
  padding: var(--s-2) var(--s-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--surface-inset);
  color: var(--text);
  font-size: var(--fs-body);
}
.textarea { min-height: 76px; resize: vertical; line-height: var(--lh-body); }
.input::placeholder, .textarea::placeholder { color: var(--text-3); }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: -1px;
  border-color: var(--brand);
}
.input[aria-invalid="true"], .select[aria-invalid="true"] { border-color: var(--danger); }

.select {
  appearance: none;
  padding-inline-end: var(--s-8);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.field-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--s-4);
}

.toggle {
  position: relative;
  width: 38px;
  height: 22px;
  flex: none;
  border-radius: var(--r-full);
  background: var(--track);
  border: 1px solid var(--border-strong);
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.toggle::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: var(--r-full);
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: transform var(--dur) var(--ease);
}
.toggle[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); }
.toggle[aria-pressed="true"]::after { transform: translateX(16px); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; min-width: 0; }
.table { font-size: var(--fs-sm); }
.table thead th {
  font-family: var(--font-mono);
  font-size: var(--fs-mono);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--text-3);
  font-weight: 500;
  text-align: left;
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table tbody td {
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--surface-2); }
.table .col-amount { text-align: right; }
.table .col-actions { text-align: right; width: 1%; white-space: nowrap; }

.th-sort {
  display: inline-flex;
  align-items: center;
  gap: var(--s-1);
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
}
.th-sort:hover { color: var(--text); }
.th-sort__arrow { opacity: 0; transition: opacity var(--dur-fast) var(--ease); }
[aria-sort="ascending"] .th-sort__arrow,
[aria-sort="descending"] .th-sort__arrow { opacity: 1; color: var(--brand); }
[aria-sort="descending"] .th-sort__arrow { transform: rotate(180deg); }

/* ---------- Row list (mobile-friendly) ---------- */
.rows { display: grid; }
.row {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-5);
  border-bottom: 1px solid var(--border);
  text-align: left;
  width: 100%;
  background: none;
  transition: background var(--dur-fast) var(--ease);
}
.row:last-child { border-bottom: 0; }
.row:hover { background: var(--surface-2); }
.row__main { flex: 1; min-width: 0; display: grid; gap: 2px; }
.row__title {
  font-weight: 550;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row__meta {
  font-size: var(--fs-xs);
  color: var(--text-3);
  display: flex;
  align-items: center;
  gap: var(--s-2);
  flex-wrap: wrap;
}
.row__meta .dot { color: var(--border-strong); }
.row__end { text-align: right; display: grid; gap: 2px; flex: none; }
.row__amount { font-weight: 600; font-variant-numeric: tabular-nums lining-nums; }

/* ---------- Empty / error states ---------- */
.empty {
  display: grid;
  justify-items: center;
  text-align: center;
  gap: var(--s-3);
  padding: var(--s-12) var(--s-5);
  color: var(--text-2);
}
.empty__icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: var(--r-md);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-3);
}
.empty__title { font-size: var(--fs-h3); color: var(--text); font-weight: 600; }
.empty__text { font-size: var(--fs-sm); max-width: 46ch; }

.notice {
  display: flex;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface-2);
  font-size: var(--fs-sm);
  color: var(--text-2);
  align-items: flex-start;
}
.notice svg { flex: none; margin-top: 2px; }
.notice--warn   { background: var(--warn-soft);   border-color: color-mix(in srgb, var(--warn) 32%, transparent);   color: var(--text); }
.notice--danger { background: var(--danger-soft); border-color: color-mix(in srgb, var(--danger) 32%, transparent); color: var(--text); }
.notice--info   { background: var(--brand-soft);  border-color: color-mix(in srgb, var(--brand) 28%, transparent);  color: var(--text); }
.notice__title { font-weight: 600; color: var(--text); display: block; }

/* ---------- Dialog ---------- */
.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: var(--z-dialog);
  background: var(--scrim);
  display: grid;
  place-items: center;
  padding: var(--s-5);
  overflow-y: auto;
}
.dialog {
  width: 100%;
  max-width: 520px;
  max-height: calc(100vh - var(--s-10));
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-dialog);
  animation: dialog-in var(--dur) var(--ease);
}
.dialog--wide { max-width: 780px; }
@keyframes dialog-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}
.dialog__head {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  padding: var(--s-5) var(--s-5) var(--s-4);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.dialog__titles { flex: 1; min-width: 0; }
.dialog__title { font-size: var(--fs-h2); }
.dialog__desc { font-size: var(--fs-sm); color: var(--text-2); margin-top: 2px; }
.dialog__body { padding: var(--s-5); overflow-y: auto; display: grid; gap: var(--s-4); }
.dialog__foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--s-2);
  padding: var(--s-4) var(--s-5);
  border-top: 1px solid var(--border);
  flex: none;
  flex-wrap: wrap;
}

/* ---------- Toasts ---------- */
.toaster {
  position: fixed;
  bottom: var(--s-5);
  right: var(--s-5);
  z-index: var(--z-toast);
  display: grid;
  gap: var(--s-2);
  width: min(340px, calc(100vw - var(--s-8)));
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  box-shadow: var(--shadow-ambient);
  font-size: var(--fs-sm);
  pointer-events: auto;
  animation: toast-in var(--dur) var(--ease);
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}
.toast__bar { width: 3px; align-self: stretch; border-radius: var(--r-full); background: var(--brand); flex: none; }
.toast__text { flex: 1; min-width: 0; }
.toast__action {
  font-weight: 600;
  color: var(--brand);
  padding: 0 var(--s-1);
  flex: none;
}
.toast__action:hover { text-decoration: underline; }
.toast--error { border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.toast--error .toast__bar { background: var(--danger); }

/* ---------- Filter bar ---------- */
.filters {
  display: flex;
  gap: var(--s-2);
  flex-wrap: wrap;
  align-items: center;
}
.filters .select,
.filters .input {
  width: auto;
  min-width: 132px;
  height: 34px;
  min-height: 34px;
  font-size: var(--fs-sm);
}

/* ---------- Meter row ---------- */
.meter-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--s-3);
  font-size: var(--fs-sm);
}
