/* ============================================================================
   UFUQ · أفق — components
   Every interactive component ships default / hover / focus / active /
   disabled. A component with half its states is not finished.
   ========================================================================== */

/* ──────────────────────────────────────────────────────────────── buttons ── */
.btn {
  --btn-h: 44px;                       /* 44px is the touch-target floor */
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--s-2);
  block-size: var(--btn-h);
  padding-inline: var(--s-5);
  border-radius: var(--r);
  font-size: var(--t-body); font-weight: 600;
  white-space: nowrap; cursor: pointer;
  border: 1px solid transparent;
  transition: background-color var(--d-hover) var(--ease-out),
              border-color var(--d-hover) var(--ease-out),
              color var(--d-hover) var(--ease-out),
              transform var(--d-press) var(--ease-out);
}
.btn:active { transform: scale(0.97); }      /* the interface heard you */
.btn:disabled, .btn[aria-disabled='true'] { opacity: 0.45; pointer-events: none; }
.btn .ico { flex: none; }

.btn-primary { background: var(--brand); color: var(--brand-ink); }
.btn-secondary { background: var(--surface); color: var(--ink); border-color: var(--line-strong); }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-danger { background: var(--crit); color: var(--knob); }
.btn-quiet { background: var(--surface-2); color: var(--ink); }

@media (hover: hover) and (pointer: fine) {
  .btn-primary:hover { background: var(--brand-hover); }
  .btn-secondary:hover { background: var(--surface-2); border-color: var(--line-control); }
  .btn-ghost:hover { background: var(--surface-2); color: var(--ink); }
  .btn-quiet:hover { background: var(--surface-sunk); }
}

.btn-sm  { --btn-h: 34px; padding-inline: var(--s-3); font-size: var(--t-dense); }
.btn-lg  { --btn-h: 52px; font-size: var(--t-lg); }
.btn-block { inline-size: 100%; }
.btn-icon { --btn-h: 40px; inline-size: 40px; padding-inline: 0; border-radius: var(--r); }

.btn.is-loading { color: transparent; position: relative; pointer-events: none; }
.btn.is-loading::after {
  content: ''; position: absolute; inline-size: 16px; block-size: 16px;
  border: 2px solid currentColor; border-block-start-color: transparent;
  border-radius: 50%; color: var(--brand-ink);
  animation: uf-spin 620ms linear infinite;
}
@keyframes uf-spin { to { transform: rotate(360deg); } }

/* ───────────────────────────────────────────────────────────────── fields ── */
.field { display: block; }
.field + .field { margin-block-start: var(--s-4); }

.label {
  display: block; margin-block-end: var(--s-2);
  font-size: var(--t-dense); font-weight: 500; color: var(--ink-2);
}

.input, .select, .textarea {
  inline-size: 100%; block-size: 46px;
  padding-inline: var(--s-4);
  background: var(--bg);
  border: 1px solid var(--line-control);
  border-radius: var(--r);
  color: var(--ink);
  font-size: var(--t-lg);
  transition: border-color var(--d-hover) var(--ease-out), box-shadow var(--d-hover) var(--ease-out);
}
.textarea { block-size: auto; min-block-size: 84px; padding-block: var(--s-3); resize: vertical; font-size: var(--t-body); }
.input::placeholder, .textarea::placeholder { color: var(--ink-3); }  /* the floor, not lighter */
.input:hover, .select:hover, .textarea:hover { border-color: var(--ink-4); }
.input:focus-visible, .select:focus-visible, .textarea:focus-visible {
  outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-wash);
}
.input:disabled, .select:disabled { background: var(--surface-2); color: var(--ink-3); cursor: not-allowed; }
.field.is-error .input, .field.is-error .select { border-color: var(--crit); }
.field-error {
  display: flex; align-items: center; gap: var(--s-1);
  margin-block-start: var(--s-2); font-size: var(--t-meta); color: var(--crit);
}
.field-help { margin-block-start: var(--s-2); font-size: var(--t-meta); color: var(--ink-3); }

.select {
  appearance: none;
  padding-inline-end: var(--s-8);
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-3) 50%),
                    linear-gradient(135deg, var(--ink-3) 50%, transparent 50%);
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  background-position: right 20px top 21px, right 15px top 21px;
}
[dir='rtl'] .select { background-position: left 15px top 21px, left 20px top 21px; }

/* Amount input — the one field that gets its own treatment, because it is the
   field people use most and mis-typing it is the costliest mistake here. */
.amount-field {
  display: flex; align-items: baseline; justify-content: center; gap: var(--s-2);
  padding: var(--s-5) var(--s-4);
  background: var(--surface-2); border-radius: var(--r-lg);
}
.amount-field input {
  inline-size: auto; max-inline-size: 62%; border: 0; background: none; padding: 0;
  font-family: var(--font-num); font-size: var(--t-metric); font-weight: 500;
  text-align: center; color: var(--ink); font-variant-numeric: tabular-nums;
}
.amount-field input:focus-visible { outline: none; }
.amount-field .cur { font-size: var(--t-h3); font-weight: 500; color: var(--ink-3); }

/* ─────────────────────────────────────────────────────────────── switches ── */
.switch { position: relative; display: inline-flex; flex: none; cursor: pointer; }
.switch input { position: absolute; opacity: 0; inline-size: 100%; block-size: 100%; margin: 0; cursor: pointer; }
.switch .track {
  inline-size: 46px; block-size: 28px; border-radius: var(--r-full);
  background: var(--line-strong);
  transition: background-color var(--d-pop) var(--ease-out);
}
.switch .track::before {
  content: ''; position: absolute; inset-block-start: 3px; inset-inline-start: 3px;
  inline-size: 22px; block-size: 22px; border-radius: 50%;
  background: var(--knob); box-shadow: var(--e-1);
  transition: transform var(--d-pop) var(--ease-out);
}
.switch input:checked + .track { background: var(--brand); }
.switch input:checked + .track::before { transform: translateX(18px); }
/* Direction is genuinely the meaning here — the knob travels toward the end
   edge, which is the other way in RTL. One of only two physical overrides. */
[dir='rtl'] .switch input:checked + .track::before { transform: translateX(-18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--brand-ring); outline-offset: 2px; }
.switch input:disabled + .track { opacity: 0.45; cursor: not-allowed; }

/* ───────────────────────────────────────────────── segmented control ────── */
.seg {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-2); border-radius: var(--r); border: 1px solid var(--line);
}
.seg button {
  padding: 7px var(--s-3); border-radius: var(--r-sm);
  font-size: var(--t-dense); font-weight: 500; color: var(--ink-3);
  cursor: pointer; white-space: nowrap;
  transition: background-color var(--d-hover) var(--ease-out), color var(--d-hover) var(--ease-out);
}
.seg button[aria-pressed='true'] { background: var(--bg); color: var(--ink); box-shadow: var(--e-1); font-weight: 600; }
@media (hover: hover) and (pointer: fine) { .seg button:hover:not([aria-pressed='true']) { color: var(--ink-2); } }
.seg-block { display: flex; inline-size: 100%; }
.seg-block button { flex: 1; text-align: center; }

/* ────────────────────────────────────────────────────── chips & badges ──── */
.chip {
  display: inline-flex; align-items: center; gap: var(--s-1);
  padding: 4px var(--s-3); border-radius: var(--r-full);
  font-size: var(--t-meta); font-weight: 500;
  background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line);
}
.chip .ico { inline-size: 13px; block-size: 13px; }
.chip-in    { background: var(--in-fill);   color: var(--in);   border-color: var(--in-line); }
.chip-out   { background: var(--out-fill);  color: var(--out);  border-color: var(--out-line); }
.chip-warn  { background: var(--warn-fill); color: var(--warn); border-color: var(--warn-line); }
.chip-crit  { background: var(--crit-fill); color: var(--crit); border-color: var(--out-line); }
.chip-brand { background: var(--brand-wash);color: var(--brand);border-color: var(--brand-line); }

button.chip { cursor: pointer; transition: background-color var(--d-hover) var(--ease-out); }
button.chip[aria-pressed='true'] { background: var(--ink); color: var(--ink-inv); border-color: var(--ink); }

/* ───────────────────────────────────────────────────────── category mark ── */
.cat {
  display: grid; place-items: center; flex: none;
  inline-size: 38px; block-size: 38px; border-radius: var(--r);
  background: var(--surface-2); color: var(--ink-2);
}
.cat .ico { inline-size: 19px; block-size: 19px; }
.cat-1 { background: color-mix(in oklab, var(--viz-1) 14%, transparent); color: var(--viz-1); }
.cat-2 { background: color-mix(in oklab, var(--viz-2) 14%, transparent); color: var(--viz-2); }
.cat-3 { background: color-mix(in oklab, var(--viz-3) 16%, transparent); color: var(--viz-3); }
.cat-4 { background: color-mix(in oklab, var(--viz-4) 18%, transparent); color: var(--viz-4); }
.cat-5 { background: color-mix(in oklab, var(--viz-5) 18%, transparent); color: var(--viz-5); }
.cat-in { background: var(--in-fill); color: var(--in); }
.cat-sm { inline-size: 30px; block-size: 30px; border-radius: var(--r-sm); }
.cat-sm .ico { inline-size: 15px; block-size: 15px; }

/* ────────────────────────────────────────────────────────────── surfaces ── */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
}
.panel-pad { padding: var(--s-4); }
.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s-3);
  padding: var(--s-4) var(--s-4) var(--s-3);
}
.panel-head h3 { font-size: var(--t-body); font-weight: 600; }
.panel-head .sub { font-size: var(--t-meta); color: var(--ink-3); }

.section-title {
  display: flex; align-items: baseline; justify-content: space-between; gap: var(--s-3);
  margin-block-end: var(--s-3);
}
.section-title h2 { font-size: var(--t-body); font-weight: 600; color: var(--ink-2); }
.section-title a { font-size: var(--t-meta); font-weight: 500; color: var(--brand); }

/* ──────────────────────────────────────────────────────────── KPI strip ────
   One bordered surface divided by hairlines, not four detached cards. Detached
   cards in a 2x2 grid is the reflex, and it wastes a whole gutter of space on
   a 390px screen for no gain.                                              */
.kpi-strip {
  display: grid; grid-template-columns: 1fr 1fr;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-lg); overflow: hidden;
}
.kpi { padding: var(--s-4); display: grid; gap: 2px; align-content: start; min-inline-size: 0; }
.kpi + .kpi { border-inline-start: 1px solid var(--line); }
.kpi:nth-child(n + 3) { border-block-start: 1px solid var(--line); }
.kpi:nth-child(2n + 1) { border-inline-start: 0; }
.kpi .k-label {
  display: flex; align-items: center; gap: var(--s-1);
  font-size: var(--t-meta); color: var(--ink-3); font-weight: 500;
}
.kpi .k-label .ico { inline-size: 13px; block-size: 13px; }
.kpi .k-value {
  font-size: var(--t-h2); font-weight: 600; letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums; line-height: 1.2;
}
.kpi .k-value .unit { font-size: var(--t-meta); font-weight: 500; color: var(--ink-3); margin-inline-start: 3px; }
.kpi .k-note { font-size: var(--t-meta); color: var(--ink-3); }
.kpi .k-note .up { color: var(--in); font-weight: 600; }
.kpi .k-note .down { color: var(--out); font-weight: 600; }
.kpi-3 { grid-template-columns: repeat(3, 1fr); }
.kpi-3 .kpi { padding-inline: var(--s-3); }
.kpi-3 .k-value { font-size: var(--t-h3); }
.kpi-3 .k-label, .kpi-3 .k-note { font-size: var(--t-micro); }
.kpi-3 .kpi:nth-child(n + 3) { border-block-start: 0; }
.kpi-3 .kpi:nth-child(2n + 1) { border-inline-start: 1px solid var(--line); }
.kpi-3 .kpi:first-child { border-inline-start: 0; }

/* ─────────────────────────────────────────────────────────────── rows ───── */
.rows { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; }
.row-item {
  display: flex; align-items: center; gap: var(--s-3);
  inline-size: 100%; padding: var(--s-3) var(--s-4);
  text-align: start; min-block-size: 60px;
  transition: background-color var(--d-hover) var(--ease-out);
}
.row-item + .row-item { border-block-start: 1px solid var(--line); }
a.row-item, button.row-item { cursor: pointer; }
@media (hover: hover) and (pointer: fine) { a.row-item:hover, button.row-item:hover { background: var(--surface-2); } }
a.row-item:active, button.row-item:active { background: var(--surface-sunk); }
.row-item .r-main { flex: 1; min-inline-size: 0; }
.row-item .r-title { font-size: var(--t-body); font-weight: 500; }
.row-item .r-meta {
  display: flex; align-items: center; gap: var(--s-2);
  font-size: var(--t-meta); color: var(--ink-3); margin-block-start: 1px;
}
.row-item .r-amount { font-size: var(--t-body); font-weight: 600; font-variant-numeric: tabular-nums; text-align: end; flex: none; }
.row-item .r-after { flex: none; color: var(--ink-4); display: flex; align-items: center; }
/* Chevrons encode reading direction, so they mirror. */
[dir='rtl'] .icon-dir { transform: scaleX(-1); }

.row-group-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: var(--s-3) var(--s-1) var(--s-2);
  font-size: var(--t-meta); font-weight: 600; color: var(--ink-3);
}

/* ────────────────────────────────────────────────────────────── alerts ──── */
.alert {
  display: flex; gap: var(--s-3);
  padding: var(--s-4); border-radius: var(--r-lg);
  border: 1px solid var(--line);
  background: var(--surface);
}
.alert .a-ico { flex: none; margin-block-start: 1px; }
.alert .a-title { font-size: var(--t-body); font-weight: 600; }
.alert .a-body { font-size: var(--t-dense); color: var(--ink-2); margin-block-start: 2px; }
.alert .a-cta { margin-block-start: var(--s-3); }
.alert-crit { background: var(--crit-fill); border-color: var(--out-line); }
.alert-crit .a-ico, .alert-crit .a-title { color: var(--crit); }
.alert-crit .a-body { color: var(--ink-2); }
.alert-warn { background: var(--warn-fill); border-color: var(--warn-line); }
.alert-warn .a-ico, .alert-warn .a-title { color: var(--warn); }
.alert-in { background: var(--in-fill); border-color: var(--in-line); }
.alert-in .a-ico, .alert-in .a-title { color: var(--in); }
.alert-brand { background: var(--brand-wash); border-color: var(--brand-line); }
.alert-brand .a-ico, .alert-brand .a-title { color: var(--brand); }

/* ───────────────────────────────────────────────────────── empty states ─── */
/* An empty state teaches the interface. "Nothing here" teaches nothing. */
.empty { text-align: center; padding: var(--s-9) var(--s-5); }
.empty .e-ico {
  display: grid; place-items: center; inline-size: 56px; block-size: 56px;
  margin: 0 auto var(--s-4); border-radius: var(--r-lg);
  background: var(--surface-2); color: var(--ink-4);
}
.empty .e-ico .ico { inline-size: 26px; block-size: 26px; }
.empty h3 { font-size: var(--t-h3); margin-block-end: var(--s-2); }
.empty p { font-size: var(--t-dense); color: var(--ink-3); max-inline-size: 34ch; margin: 0 auto var(--s-5); line-height: var(--lh-prose); }

/* ─────────────────────────────────────────────────────────────── sheet ──── */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: var(--z-backdrop);
  background: rgba(16, 17, 24, 0.42);
  opacity: 0; pointer-events: none;
  transition: opacity var(--d-sheet) var(--ease-out);
}
.sheet-backdrop.on { opacity: 1; pointer-events: auto; }
.sheet {
  position: absolute; inset-inline: 0; inset-block-end: 0; z-index: var(--z-sheet);
  background: var(--bg);
  border-start-start-radius: var(--r-xl); border-start-end-radius: var(--r-xl);
  box-shadow: var(--e-3);
  transform: translateY(100%);
  transition: transform var(--d-sheet) var(--ease-out);
  max-block-size: 88%; display: flex; flex-direction: column;
}
.sheet.on { transform: translateY(0); }
.sheet-grab { inline-size: 36px; block-size: 4px; border-radius: 2px; background: var(--line-strong); margin: var(--s-2) auto var(--s-1); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); padding: var(--s-3) var(--s-4); }
.sheet-head h2 { font-size: var(--t-h3); }
.sheet-body { overflow-y: auto; padding: 0 var(--s-4) var(--s-4); }
.sheet-foot { padding: var(--s-3) var(--s-4) calc(var(--s-4) + var(--safe-bot)); border-block-start: 1px solid var(--line); display: flex; gap: var(--s-3); }

/* ─────────────────────────────────────────────────────────────── tables ─── */
/* Every chart has a table behind it. That is the relief rule for the category
   colours, and it is also just how a number-heavy product should behave.   */
.dtable { inline-size: 100%; border-collapse: collapse; font-size: var(--t-dense); }
.dtable th, .dtable td { padding: var(--s-2) var(--s-3); text-align: start; }
.dtable th { font-weight: 600; color: var(--ink-3); font-size: var(--t-meta); }
.dtable tbody tr + tr td { border-block-start: 1px solid var(--line); }
.dtable td.n, .dtable th.n { text-align: end; font-variant-numeric: tabular-nums; }
.dtable .swatch { inline-size: 9px; block-size: 9px; border-radius: 2px; display: inline-block; margin-inline-end: var(--s-2); }

/* ──────────────────────────────────────────────────────────── skeletons ─── */
.skel { background: var(--surface-2); border-radius: var(--r-sm); position: relative; overflow: hidden; }
.skel::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, var(--surface-sunk), transparent);
  animation: uf-shimmer 1.3s infinite;
}
@keyframes uf-shimmer { 100% { transform: translateX(100%); } }

/* ──────────────────────────────────────────────────────────── progress ──── */
.meter { block-size: 6px; border-radius: 3px; background: var(--surface-sunk); overflow: hidden; }
.meter > span { display: block; block-size: 100%; border-radius: 3px; background: var(--brand); transition: inline-size var(--d-sheet) var(--ease-out); }

/* ────────────────────────────────────────────────────────────── charts ──── */
.ufc { inline-size: 100%; display: block; overflow: visible; }
.ufc text, .ufc-donut text { font-family: var(--font-ui); }
/* Numbers are an LTR run even inside an RTL document — without this the
   leading minus of "−20k" is treated as a neutral and reordered to "20k−". */
.ufc-axis, .ufc-lbl, .ufc-center-v { direction: ltr; unicode-bidi: isolate; }
.ufc-axis { font-size: 10px; font-variant-numeric: tabular-nums; }
.ufc-lbl { font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }
.ufc-hit { cursor: crosshair; }
.ufc-spark { inline-size: 100%; block-size: 34px; display: block; }
.ufc-donut { inline-size: 100%; max-inline-size: 220px; margin-inline: auto; display: block; }
.ufc-center-v { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; }
.ufc-center-l { font-size: 12px; }

/* The chart wipes in once, on first paint. It runs long by UI standards
   (900ms) because it is not a state change — it is the one moment the product
   explains itself, and the eye needs to follow the line to the dip.

   A clip-path wipe rather than a stroke-dashoffset draw, for two reasons: a
   dashoffset animation needs a solid dasharray, which would fight the dashed
   projection stroke; and it needs the path's length, which on a wide chart
   exceeds any hardcoded value and silently leaves the tail undrawn.
   A wipe is correct at every width. */
.chart-draw .ufc { animation: uf-wipe var(--d-draw) var(--ease-out) both; }
@keyframes uf-wipe { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }
[dir='rtl'] .chart-draw .ufc { animation-name: uf-wipe-rtl; }
@keyframes uf-wipe-rtl { from { clip-path: inset(0 0 0 100%); } to { clip-path: inset(0 0 0 0); } }
@media (prefers-reduced-motion: reduce) {
  .chart-draw .ufc { animation: uf-fade 200ms ease both; }
  @keyframes uf-fade { from { opacity: 0; } to { opacity: 1; } }
}

#uf-charttip {
  position: fixed; inset-block-start: 0; inset-inline-start: 0; z-index: var(--z-tooltip);
  pointer-events: none; max-inline-size: 240px;
  background: var(--chrome); color: var(--ink-on-chrome);
  border-radius: var(--r); padding: var(--s-2) var(--s-3);
  font-size: var(--t-meta); line-height: 1.45;
  box-shadow: var(--e-2);
  opacity: 0; transition: opacity 120ms ease;
}
#uf-charttip.on { opacity: 1; }
#uf-charttip b { display: block; font-weight: 600; }
#uf-charttip i { display: block; font-style: normal; color: var(--ink-on-chrome-2); font-size: var(--t-micro); }
#uf-charttip u {
  display: block; text-decoration: none; font-family: var(--font-num);
  font-size: var(--t-body); font-weight: 500; margin-block-start: 2px;
  font-variant-numeric: tabular-nums;
}
#uf-charttip u.in { color: var(--in-on-chrome); }
#uf-charttip u.out { color: var(--out-on-chrome); }
#uf-charttip .tip-evs { margin-block-start: var(--s-2); padding-block-start: var(--s-2); border-block-start: 1px solid var(--chrome-line); display: grid; gap: 3px; }
#uf-charttip .tip-evs span { display: flex; gap: var(--s-3); justify-content: space-between; }
#uf-charttip .tip-evs em { font-style: normal; color: var(--ink-on-chrome-2); }
#uf-charttip .tip-evs b { display: inline; font-variant-numeric: tabular-nums; }
#uf-charttip .tip-evs b.in { color: var(--in-on-chrome); }
#uf-charttip .tip-evs b.out { color: var(--out-on-chrome); }
#uf-charttip .tip-evs .more { color: var(--ink-on-chrome-2); }

/* legend */
.legend { display: flex; flex-wrap: wrap; gap: var(--s-3); font-size: var(--t-meta); color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: var(--s-2); }
.legend i { inline-size: 14px; block-size: 3px; border-radius: 2px; flex: none; }
.legend i.dot { inline-size: 9px; block-size: 9px; border-radius: 2px; }
.legend i.dash { background: repeating-linear-gradient(90deg, currentColor 0 4px, transparent 4px 7px); }
