/* Components — DESIGN_SYSTEM.md §6. Every state (hover, focus, disabled, busy,
   error) is explicit; colour never carries meaning alone (chips have an icon). */

/* --- Buttons ------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-block-size: var(--control-h); padding-inline: var(--s4);
  border-radius: var(--r-control); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); font-weight: 500; white-space: nowrap;
  transition: background var(--dur), border-color var(--dur);
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { border-color: var(--err-fg); color: var(--err-fg); }
.btn.danger.solid { background: var(--err-fg); color: var(--surface); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn.sm { min-block-size: 28px; padding-inline: var(--s3); font-size: 12.5px; }
.btn:disabled { opacity: .55; }
.btn[aria-busy="true"]::after { content: ""; inline-size: 12px; block-size: 12px; border: 2px solid currentColor; border-inline-end-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
/* Icon-only controls keep a square hit area. Without the minimum the width
 * collapsed to the icon plus padding (measured 26×40 px in the phone header),
 * which is below the 40 px target this console holds itself to elsewhere; the
 * header renders three of them side by side on a narrow screen. */
.icon-btn { inline-size: var(--control-h); min-inline-size: var(--control-h); min-block-size: var(--control-h); padding-inline: 0; }

/* --- Fields -------------------------------------------------------------- */
.field { display: grid; gap: var(--s1); min-inline-size: 0; }
.field > label, .field > .label { font-weight: 500; font-size: 13px; color: var(--text); }
.field .hint { font-size: 12px; color: var(--text-3); }
.field .error-text { font-size: 12px; color: var(--err-fg); font-weight: 500; }
.field input[type="text"], .field input[type="password"], .field input[type="email"], .field input[type="number"],
.field input[type="search"], .field input[type="datetime-local"], .field select, .field textarea {
  inline-size: 100%; min-block-size: var(--control-h); padding: var(--s2) var(--s3);
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--r-control);
}
.field textarea { min-block-size: 88px; resize: vertical; }
.field input[aria-invalid="true"], .field select[aria-invalid="true"], .field textarea[aria-invalid="true"] { border-color: var(--err-fg); box-shadow: 0 0 0 1px var(--err-fg); }
.field input:disabled, .field select:disabled, .field textarea:disabled { background: var(--surface-2); color: var(--text-3); }
.field .with-unit { display: flex; gap: var(--s2); align-items: center; }
.field .unit { color: var(--text-3); font-size: 12px; white-space: nowrap; }
.check { display: inline-flex; align-items: center; gap: var(--s2); min-block-size: var(--control-h); }
.check input { inline-size: 16px; block-size: 16px; accent-color: var(--accent); }
.checks { display: grid; gap: var(--s3); }
/* A multi-checkbox field is a block, not a cell: the token dialog's scope list is
 * 30 items and 1,251 px tall, and sharing a band with two text inputs left them
 * alone at the top of an empty column. It spans the grid, and its options flow in
 * columns so the list stays scannable. */
.form-grid > .field-checks { grid-column: 1 / -1; }
.form-grid > .field-checks .checks { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: var(--s2) var(--s4); }
.checks-group { display: grid; gap: var(--s1); }
.checks-title { font-weight: 600; }
.checks-grid { display: grid; gap: var(--s1) var(--s4); grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.checks .check { min-block-size: auto; }
.form-grid { display: grid; gap: var(--s4); grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
/* A section heading inside a form grid separates groups without becoming a cell:
 * without this it took one column and pushed the following fields out of rhythm. */
.form-grid > h3 { grid-column: 1 / -1; margin: var(--s2) 0 0; font-size: 13px; font-weight: 600; color: var(--text-2); }
.form-grid > h3:first-child { margin-block-start: 0; }

/* A row of fields must read as a row. Labels wrap to different heights ("لحظهٔ
 * استعلام (ISO-8601 با منطقهٔ زمانی)" takes two lines where "هدف" takes one) and
 * hints differ in length, so each field's control landed at a different offset —
 * measured on the legal-lookup form: inputs at 345, 351 and 364 px in one row.
 * Subgrid makes the label, control and hint rows shared across the band, so the
 * controls align whatever the text does; without subgrid support the previous
 * per-field layout stands. */
@supports (grid-template-rows: subgrid) {
  /* Four rows, not three: a field always carries label, control, hint and the
   * (hidden) error line, and a three-row span pushed the error line into the next
   * band — two dialogs still measured misaligned after the first attempt. Each
   * child is placed explicitly so a field without a hint keeps its error line in
   * the same row as its neighbours'. */
  .form-grid > .field { grid-row: span 4; grid-template-rows: subgrid; }
  .form-grid > .field > label,
  .form-grid > .field > .label { grid-row: 1; }
  .form-grid > .field > .hint { grid-row: 3; }
  .form-grid > .field > .error-text { grid-row: 4; }
  .form-grid > .field > :not(label):not(.label):not(.hint):not(.error-text) { grid-row: 2; }
  /* A checkbox field wraps its input inside the label, so it has no separate
   * control line: placing that label in the control row keeps a checkbox level
   * with the text inputs beside it (measured: 609 px against 641 px before). */
  .form-grid > .field > label.check { grid-row: 2; align-self: start; }
}
.form-actions { display: flex; gap: var(--s2); justify-content: flex-start; padding-block-start: var(--s3); }
fieldset { border: 1px solid var(--border); border-radius: var(--r-control); padding: var(--s3) var(--s4); margin: 0; }
legend { padding-inline: var(--s2); font-weight: 600; font-size: 13px; }

/* --- Status chips -------------------------------------------------------- */
.chip {
  display: inline-flex; align-items: center; gap: var(--s1);
  padding: 1px var(--s3); border-radius: var(--r-pill);
  font-size: 12px; font-weight: 600; line-height: 1.7; white-space: nowrap;
  background: var(--info-bg); color: var(--info-fg); border: 1px solid transparent;
}
.chip .icon { inline-size: 14px; block-size: 14px; }
.chip.ok { background: var(--ok-bg); color: var(--ok-fg); }
.chip.warn { background: var(--warn-bg); color: var(--warn-fg); }
.chip.err { background: var(--err-bg); color: var(--err-fg); }
.chip.info { background: var(--info-bg); color: var(--info-fg); }
.chip.unknown { background: var(--unk-bg); color: var(--unk-fg); border: 1px dashed var(--unk-fg); }
.chip.plain { background: var(--surface-2); color: var(--text-2); }
.chips { display: flex; flex-wrap: wrap; gap: var(--s1); }

/* --- Tables -------------------------------------------------------------- */
.table-wrap { overflow-x: auto; }
table.data { inline-size: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
table.data caption { text-align: start; padding: var(--s3) var(--s4); font-weight: 600; }
table.data th, table.data td { text-align: start; padding: 0 var(--s4); block-size: var(--row-h); border-block-end: 1px solid var(--border); vertical-align: middle; }
table.data thead th { position: sticky; inset-block-start: 0; background: var(--surface-2); font-weight: 600; color: var(--text-2); white-space: nowrap; z-index: 1; }
table.data td .ltr { white-space: nowrap; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr[aria-selected="true"] { background: var(--accent-soft); }
table.data td.actions { text-align: end; white-space: nowrap; }
table.data td.actions .row { flex-wrap: nowrap; justify-content: flex-end; }
table.data th button.sort { all: unset; cursor: pointer; display: inline-flex; gap: var(--s1); align-items: center; }
table.data th button.sort:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.table-foot { display: flex; flex-wrap: wrap; gap: var(--s3); align-items: center; justify-content: space-between; padding: var(--s3) var(--s4); }

/* --- Tabs ---------------------------------------------------------------- */
/* The strip must never be wider than the box it sits in: inside a phone-width
 * dialog it grew to its content (measured 500 px in a 354 px panel), so the last
 * two tabs sat off-screen and `overflow-x: auto` never engaged. Capping it is what
 * makes the strip scroll instead of the page. */
.tabs [role="tablist"] { display: flex; gap: var(--s1); border-block-end: 1px solid var(--border); overflow-x: auto; max-inline-size: 100%; }
.tabs [role="tab"] {
  all: unset; cursor: pointer; padding: var(--s3) var(--s4); color: var(--text-2);
  border-block-end: 2px solid transparent; white-space: nowrap; font-weight: 500;
}
.tabs [role="tab"]:hover { color: var(--text); }
.tabs [role="tab"][aria-selected="true"] { color: var(--accent); border-block-end-color: var(--accent); font-weight: 600; }
.tabs [role="tab"]:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tabs [role="tabpanel"] { padding-block-start: var(--s4); }

/* --- Key/value, stat, meter ---------------------------------------------- */
dl.kv { display: grid; grid-template-columns: minmax(120px, 220px) minmax(0, 1fr); gap: var(--s2) var(--s4); margin: 0; }
dl.kv dt { color: var(--text-3); }
dl.kv dd { margin: 0; min-inline-size: 0; overflow-wrap: anywhere; }
dl.kv dd .chip { white-space: normal; max-inline-size: 100%; }
dl.kv dd .ltr { max-inline-size: 100%; }
.stat { padding: var(--s4); display: grid; gap: var(--s1); }
.stat .label { color: var(--text-2); font-size: 13px; }
.stat .value { font-size: 26px; font-weight: 600; line-height: 1.2; }
.stat .meta { font-size: 12px; color: var(--text-3); display: flex; gap: var(--s2); align-items: center; flex-wrap: wrap; }
.meter { block-size: 8px; border-radius: var(--r-pill); background: var(--surface-2); overflow: hidden; border: 1px solid var(--border); }
.meter > span { display: block; block-size: 100%; background: var(--series-1); }
.meter.warn > span { background: var(--warn-fg); }
.meter.err > span { background: var(--err-fg); }

/* --- Empty / loading / error states ------------------------------------- */
.state { display: grid; justify-items: center; text-align: center; gap: var(--s2); padding: var(--s7) var(--s5); color: var(--text-2); }
.state .icon { inline-size: 32px; block-size: 32px; color: var(--text-3); }
.state h3 { color: var(--text); }
.skeleton { display: grid; gap: var(--s3); padding: var(--s4); }
.skeleton i { display: block; block-size: 14px; border-radius: 4px; background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.4s linear infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* --- Dialog & toast ------------------------------------------------------ */
dialog.dlg {
  padding: 0; border: 1px solid var(--border); border-radius: var(--r-card);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-3);
  inline-size: min(92vw, 560px); max-block-size: 90vh;
}
dialog.dlg::backdrop { background: rgba(7, 12, 22, .55); }
dialog.dlg .dlg-head { padding: var(--s4) var(--s5); border-block-end: 1px solid var(--border); display: flex; align-items: center; gap: var(--s3); }
dialog.dlg .dlg-head h2 { flex: 1; }
dialog.dlg .dlg-body { padding: var(--s5); overflow-y: auto; max-block-size: 60vh; display: grid; gap: var(--s4); }
/* Same grid-item rule as `.page > *`: without it a wide child (the tab strip)
 * takes its min-content width, so a 5-tab dialog on a phone laid out 500 px of
 * tabs inside a 354 px panel and the last tabs were unreachable. */
dialog.dlg .dlg-body > * { min-inline-size: 0; }
dialog.dlg .dlg-foot { padding: var(--s3) var(--s5) var(--s5); display: flex; gap: var(--s2); }
dialog.dlg.wide { inline-size: min(94vw, 860px); }

.toasts { position: fixed; inset-block-end: var(--s5); inset-inline-end: var(--s5); display: grid; gap: var(--s2); z-index: var(--z-toast); max-inline-size: min(92vw, 420px); }
.toast { padding: var(--s3) var(--s4); border-radius: var(--r-control); background: var(--text); color: var(--bg); box-shadow: var(--shadow-3); display: flex; gap: var(--s3); align-items: flex-start; }
.toast.ok { background: var(--ok-fg); color: var(--surface); }
.toast.err { background: var(--err-fg); color: var(--surface); }
.toast .toast-close { all: unset; cursor: pointer; margin-inline-start: auto; opacity: .85; }

/* --- Pager, diff, code --------------------------------------------------- */
.pager { display: flex; gap: var(--s2); align-items: center; }
table.diff td.old { background: var(--err-bg); color: var(--err-fg); }
table.diff td.new { background: var(--ok-bg); color: var(--ok-fg); }
.code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-control); padding: var(--s3); direction: ltr; text-align: start; white-space: pre-wrap; overflow-wrap: anywhere; }
.copy-btn { all: unset; cursor: pointer; color: var(--text-3); padding: 2px var(--s1); border-radius: 4px; }
.copy-btn:hover { color: var(--accent); }
.copy-btn:focus-visible { outline: 2px solid var(--accent); }

/* --- Charts -------------------------------------------------------------- */
/* `figure` carries the UA's `margin: 1em 40px`; inside a 324 px card that alone
 * pushed the chart 40 px past each edge (measured: left −7 px, page scroll width
 * 381 px in a 358 px column). The chart is a block, not a floated figure. */
figure.chart { margin: 0; }
.chart { inline-size: 100%; }
.chart svg { inline-size: 100%; block-size: auto; display: block; direction: ltr; }
.chart .axis text, .chart text.axis { fill: var(--text-3); font-size: 11px; font-family: var(--font); }
.chart .grid-line { stroke: var(--border); stroke-width: 1; }
.chart .series-1 { stroke: var(--series-1); fill: var(--series-1); }
.chart .series-2 { stroke: var(--series-2); fill: var(--series-2); }
.chart .series-3 { stroke: var(--series-3); fill: var(--series-3); }
.chart .series-4 { stroke: var(--series-4); fill: var(--series-4); }
.chart path.line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart path.area { stroke: none; opacity: .14; }
.chart rect.bar { stroke: var(--surface); stroke-width: 2; }
.chart circle.pt { stroke: var(--surface); stroke-width: 2; }
.legend { display: flex; flex-wrap: wrap; gap: var(--s4); font-size: 12px; color: var(--text-2); }
.legend .swatch { display: inline-block; inline-size: 10px; block-size: 10px; border-radius: 2px; margin-inline-end: var(--s1); vertical-align: middle; }
.legend .swatch.series-1 { background: var(--series-1); }
.legend .swatch.series-2 { background: var(--series-2); }
.legend .swatch.series-3 { background: var(--series-3); }
.legend .swatch.series-4 { background: var(--series-4); }

@media (max-width: 720px) {
  dl.kv { grid-template-columns: minmax(0, 1fr); }
  dl.kv dt { margin-block-start: var(--s2); }
}
