/* =====================================================================
   PRESTEC CANONICAL COMPONENTS — UI-CORE-01B
   The `ui-` layer. Loads LAST, and every class here is a NEW name.

   New names on purpose. The application already carries two button
   systems, two form systems and a legacy sheet of 17,000 lines; a wave
   that redefined `.button` or `.card` would change several hundred pages
   at once, sight unseen. Nothing in this file matches an element
   selector or an existing class, so a page adopts it by asking for it
   and in no other way.

   That is also the migration plan. This wave establishes and proves the
   system; the reference pages come next; the remaining modules follow
   one at a time, each one a diff somebody can actually read.

   Every value below comes from styles/tokens.css. If a literal colour or
   size appears in this file, that is a bug in this file.
   ===================================================================== */

/* ---------------------------------------------------------------------
   BUTTONS
   Four intents. Solid orange means "this is the action" and is used once
   per surface; everything else is quieter than it. A screen where every
   control is orange has ranked nothing.
   --------------------------------------------------------------------- */
.ui-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  height: var(--control-h); padding: 0 14px;
  border: 1px solid transparent; border-radius: var(--radius-md);
  font-family: var(--font); font-size: var(--fs-body); font-weight: var(--fw-semibold);
  line-height: 1; white-space: nowrap; text-decoration: none; cursor: pointer;
  transition: background .15s ease, border-color .15s ease, color .15s ease;
}
.ui-btn:hover { text-decoration: none; }
.ui-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
/* Disabled must be visibly different from enabled, not merely paler by a
   few percent — a control nobody can tell is dead gets clicked. */
.ui-btn:disabled,
.ui-btn[aria-disabled="true"] {
  cursor: not-allowed; opacity: .55; box-shadow: none;
}

.ui-btn--primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.ui-btn--primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }

.ui-btn--secondary {
  background: var(--bg-surface); border-color: var(--border-strong); color: var(--text-primary);
}
.ui-btn--secondary:hover:not(:disabled) { background: var(--bg-subtle); }

.ui-btn--ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.ui-btn--ghost:hover:not(:disabled) { background: var(--bg-subtle); color: var(--text-primary); }

.ui-btn--danger { background: var(--bg-surface); border-color: var(--danger-border); color: var(--danger); }
.ui-btn--danger:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }

.ui-btn--sm { height: var(--control-h-sm); padding: 0 10px; font-size: var(--fs-secondary); }
.ui-btn--icon { width: var(--control-h); padding: 0; }
.ui-btn--icon.ui-btn--sm { width: var(--control-h-sm); }

/* ---------------------------------------------------------------------
   FORM CONTROLS
   One height across input, select and button so a filter row is a line
   rather than a staircase.
   --------------------------------------------------------------------- */
.ui-input, .ui-select, .ui-textarea {
  width: 100%; min-width: 0;
  border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--bg-surface); color: var(--text-primary);
  font-family: var(--font); font-size: var(--fs-body);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.ui-input, .ui-select { height: var(--control-h); padding: 0 12px; }
.ui-textarea { min-height: 84px; padding: 8px 12px; line-height: 1.5; resize: vertical; }
.ui-select { appearance: none; padding-right: 32px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-secondary) 50%),
                    linear-gradient(135deg, var(--text-secondary) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }

.ui-input::placeholder, .ui-textarea::placeholder { color: var(--text-muted); }
.ui-input:focus, .ui-select:focus, .ui-textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: var(--focus-ring);
}
.ui-input:disabled, .ui-select:disabled, .ui-textarea:disabled {
  background: var(--bg-subtle); color: var(--text-disabled); cursor: not-allowed;
}
/* Read-only is not disabled: the value is real and still worth reading, it
   simply is not yours to change here. */
.ui-input[readonly], .ui-textarea[readonly] {
  background: var(--bg-subtle); color: var(--text-primary);
}
.ui-input--invalid, .ui-select--invalid, .ui-textarea--invalid { border-color: var(--danger); }
.ui-input--invalid:focus, .ui-select--invalid:focus, .ui-textarea--invalid:focus {
  box-shadow: 0 0 0 3px var(--danger-soft);
}

/* ---------------------------------------------------------------------
   FIELD AND FORM LAYOUT
   Columns are a choice per form, not a default. A technical description
   forced into half a row is a description nobody can read.
   --------------------------------------------------------------------- */
.ui-field { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.ui-field__label {
  font-size: var(--fs-label); font-weight: var(--fw-medium); color: var(--text-secondary);
}
.ui-field__required { color: var(--danger); margin-left: 2px; }
.ui-field__help { font-size: var(--fs-meta); color: var(--text-muted); }
.ui-field__error { font-size: var(--fs-meta); color: var(--danger); font-weight: var(--fw-medium); }

.ui-form { display: grid; gap: var(--space-4) var(--space-5); grid-template-columns: minmax(0, 1fr); }
.ui-form--two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.ui-form--three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.ui-field--wide { grid-column: 1 / -1; }

/* ---------------------------------------------------------------------
   SURFACES
   A card groups things that belong together. It is not a wrapper for
   everything, and it is not a place to spend 32px of padding.
   --------------------------------------------------------------------- */
.ui-card {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); min-width: 0;
}
.ui-card__head {
  display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap;
  padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--border);
}
.ui-card__title { font-size: var(--fs-section); font-weight: var(--fw-semibold); color: var(--text-primary); }
.ui-card__actions { margin-left: auto; display: flex; gap: var(--space-2); }
.ui-card__body { padding: var(--space-4); display: flex; flex-direction: column; gap: var(--space-4); }
.ui-card--flush .ui-card__body { padding: 0; }
.ui-card--compact .ui-card__body { padding: var(--space-3); gap: var(--space-2); }

/* ---------------------------------------------------------------------
   PAGE HEADER
   The name of the screen and its one main action. No breadcrumb by
   default: a second navigation system on every page is a cost paid on
   every page for a benefit only a few of them have.
   --------------------------------------------------------------------- */
.ui-page-head {
  display: flex; align-items: flex-start; gap: var(--space-4); flex-wrap: wrap;
  margin-bottom: var(--space-4);
}
.ui-page-head__text { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.ui-page-head__title {
  margin: 0; font-size: var(--fs-page-title); font-weight: var(--fw-bold);
  color: var(--text-primary); line-height: 1.25;
}
.ui-page-head__sub { font-size: var(--fs-secondary); color: var(--text-secondary); }
.ui-page-head__actions {
  margin-left: auto; display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap;
}

/* ---------------------------------------------------------------------
   TABS — for a record's own sections. UI-CORE-01A settled where these
   belong: inside the record, never promoted into module navigation.
   --------------------------------------------------------------------- */
.ui-tabs {
  display: flex; gap: var(--space-1); border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.ui-tabs::-webkit-scrollbar { display: none; }
.ui-tab {
  position: relative; padding: 10px 14px; border: 0; background: none;
  font-family: var(--font); font-size: var(--fs-body); font-weight: var(--fw-medium);
  color: var(--text-secondary); white-space: nowrap; cursor: pointer; text-decoration: none;
}
.ui-tab:hover { color: var(--text-primary); text-decoration: none; }
.ui-tab:focus-visible { outline: 2px solid var(--primary); outline-offset: -2px; }
/* The active tab is marked by weight and an underline as well as colour, so
   it survives a reader who cannot separate orange from grey. */
.ui-tab.is-active { color: var(--primary-strong); font-weight: var(--fw-semibold); }
.ui-tab.is-active::after {
  content: ""; position: absolute; left: 8px; right: 8px; bottom: -1px; height: 2px;
  background: var(--primary); border-radius: 2px 2px 0 0;
}

/* ---------------------------------------------------------------------
   TABLES
   The most important surface in this application. Dense, readable, and
   never solved by making the page scroll sideways.
   --------------------------------------------------------------------- */
.ui-table-wrap { width: 100%; min-width: 0; overflow-x: hidden; }
.ui-table { width: 100%; min-width: 0; border-collapse: collapse; table-layout: fixed; }
.ui-table th, .ui-table td {
  text-align: left; vertical-align: top; padding: 9px 12px;
  border-bottom: 1px solid var(--border); overflow-wrap: anywhere;
}
.ui-table thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--bg-subtle); color: var(--text-secondary);
  font-size: var(--fs-label); font-weight: var(--fw-semibold);
  letter-spacing: .02em; white-space: nowrap;
}
.ui-table tbody td { font-size: var(--fs-secondary); color: var(--text-primary); }
.ui-table tbody tr:hover { background: var(--bg-subtle); }
.ui-table tbody tr.is-selected { background: var(--primary-soft); }
.ui-table tbody tr:last-child td { border-bottom: 0; }
.ui-table__num { text-align: right; font-variant-numeric: tabular-nums; }
/* Actions serve the row; they never outrank the data in it. */
.ui-table__actions { display: flex; flex-wrap: wrap; gap: var(--space-1); justify-content: flex-end; }
.ui-table__primary { font-weight: var(--fw-semibold); color: var(--text-primary); }
.ui-table__meta { font-size: var(--fs-meta); color: var(--text-secondary); margin-top: 2px; }

/* ---------------------------------------------------------------------
   FILTERS — an operational strip, not a hero panel.
   --------------------------------------------------------------------- */
.ui-filters {
  display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap;
  padding: var(--space-3); border: 1px solid var(--border);
  border-radius: var(--radius-md); background: var(--bg-surface);
}
.ui-filters__search { flex: 1 1 240px; min-width: 160px; }
.ui-filters__control { flex: 0 1 190px; min-width: 130px; }
.ui-filters__actions { margin-left: auto; display: flex; gap: var(--space-2); }

/* ---------------------------------------------------------------------
   BADGES — a badge REPORTS a state a record already holds. It never
   decides one, and there is no colour here for a meaning the business
   has not defined. Missing information is neutral, not dangerous.
   --------------------------------------------------------------------- */
.ui-badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px; border: 1px solid var(--border); border-radius: var(--radius-full);
  background: var(--bg-subtle); color: var(--text-secondary);
  font-size: var(--fs-meta); font-weight: var(--fw-semibold); white-space: nowrap;
}
.ui-badge--neutral { }
.ui-badge--info { background: var(--info-soft); border-color: var(--info-border); color: var(--info); }
.ui-badge--success { background: var(--success-soft); border-color: var(--success); color: var(--success-strong); }
.ui-badge--warning { background: var(--warning-soft); border-color: var(--warning-border); color: var(--warning-strong); }
.ui-badge--danger { background: var(--danger-soft); border-color: var(--danger-border); color: var(--danger); }
.ui-badge--brand { background: var(--primary-soft); border-color: var(--primary-border); color: var(--primary-hover); }
.ui-badge--outline { background: transparent; }

/* ---------------------------------------------------------------------
   ALERTS — compact, and about something. An alert that only decorates
   teaches people to read past the ones that matter.
   --------------------------------------------------------------------- */
.ui-alert {
  display: flex; gap: var(--space-3); align-items: flex-start;
  padding: var(--space-3); border: 1px solid var(--border);
  border-radius: var(--radius-md); background: var(--bg-subtle);
  font-size: var(--fs-secondary); line-height: 1.5; min-width: 0;
}
.ui-alert__title { font-weight: var(--fw-semibold); display: block; margin-bottom: 2px; }
.ui-alert__body { min-width: 0; }
.ui-alert__actions { margin-left: auto; display: flex; gap: var(--space-2); flex: none; }
.ui-alert--info { background: var(--info-soft); border-color: var(--info-border); color: var(--info-strong); }
.ui-alert--success { background: var(--success-soft); border-color: var(--success); color: var(--success-text); }
.ui-alert--warning { background: var(--warning-soft); border-color: var(--warning-border); color: var(--warning-text); }
.ui-alert--danger { background: var(--danger-soft); border-color: var(--danger-border); color: var(--danger-strong); }

/* ---------------------------------------------------------------------
   DIALOG AND DRAWER
   A confirmation is a small dialog. A drawer is for work that wants the
   page still visible behind it.
   --------------------------------------------------------------------- */
.ui-scrim {
  position: fixed; inset: 0; z-index: 70; background: rgba(16, 24, 40, .28);
  display: flex; align-items: center; justify-content: center; padding: var(--space-4);
}
.ui-dialog {
  width: 100%; max-width: 480px; max-height: 90vh; display: flex; flex-direction: column;
  background: var(--bg-surface); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-dropdown); min-width: 0;
}
.ui-dialog--wide { max-width: 720px; }
.ui-dialog__head {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--border);
}
.ui-dialog__title { margin: 0; font-size: var(--fs-section); font-weight: var(--fw-semibold); }
.ui-dialog__close { margin-left: auto; }
.ui-dialog__body { padding: var(--space-4); overflow-y: auto; min-height: 0; }
.ui-dialog__foot {
  display: flex; justify-content: flex-end; gap: var(--space-2);
  padding: var(--space-3) var(--space-4); border-top: 1px solid var(--border);
  background: var(--bg-subtle); border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.ui-scrim--drawer { align-items: stretch; justify-content: flex-end; padding: 0; }
.ui-drawer {
  width: 100%; max-width: 460px; height: 100%; display: flex; flex-direction: column;
  background: var(--bg-surface); box-shadow: var(--shadow-dropdown); min-width: 0;
}

/* ---------------------------------------------------------------------
   SYSTEM STATES
   Empty says what is missing. It offers an action only where the reader
   actually has one — a call to action somebody cannot perform is worse
   than silence.
   --------------------------------------------------------------------- */
.ui-state {
  display: flex; flex-direction: column; align-items: center; gap: var(--space-2);
  padding: var(--space-8) var(--space-4); text-align: center; color: var(--text-secondary);
}
.ui-state__icon { color: var(--text-muted); }
.ui-state__title { font-size: var(--fs-section); font-weight: var(--fw-semibold); color: var(--text-primary); }
.ui-state__body { font-size: var(--fs-secondary); max-width: 46ch; }
.ui-state--error .ui-state__title { color: var(--danger); }
.ui-state--inline { padding: var(--space-4); }

.ui-skeleton {
  background: linear-gradient(90deg, var(--bg-subtle) 25%, var(--border) 37%, var(--bg-subtle) 63%);
  background-size: 400% 100%; border-radius: var(--radius-sm);
  animation: ui-skeleton 1.4s ease infinite; height: 12px;
}
@keyframes ui-skeleton { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }
@media (prefers-reduced-motion: reduce) { .ui-skeleton { animation: none; } }

/* ---------------------------------------------------------------------
   RESPONSIVE
   Reference widths are 1366, 1280 and 820. Below 1180 the layout gives
   up columns before it gives up legibility: a form squeezed to half a
   row is not more compact, it is unreadable.
   --------------------------------------------------------------------- */
@media (max-width: 1179.98px) {
  .ui-form--two, .ui-form--three { grid-template-columns: minmax(0, 1fr); }
  .ui-page-head__actions { margin-left: 0; width: 100%; }
  .ui-filters__actions { margin-left: 0; width: 100%; }
  .ui-filters__control { flex: 1 1 160px; }
  .ui-table th, .ui-table td { padding: 8px 10px; }
  .ui-dialog { max-width: none; }
  .ui-drawer { max-width: 84vw; }
}
