/* ============================================================================
   components.css — reusable UI primitives.
   ========================================================================== */

/* ---- Page header -------------------------------------------------------- */
.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--sp-4);
  margin-bottom: var(--sp-6);
}
.page-head__text { flex: 1 1 260px; min-width: 0; }
.page-head__text p { color: var(--text-muted); font-size: var(--fs-base); margin-top: var(--sp-1); }
.page-head__actions { display: flex; flex-wrap: wrap; gap: var(--sp-2); }

/* ---- Buttons ------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  min-height: 40px; padding: 0 var(--sp-4);
  border-radius: var(--r-md); border: 1px solid transparent;
  font-size: var(--fs-base); font-weight: var(--fw-semi); white-space: nowrap;
  transition: background var(--dur-fast), border-color var(--dur-fast),
              color var(--dur-fast), transform var(--dur-instant) var(--ease-out);
}
.btn svg { width: 17px; height: 17px; flex: none; }
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; }

.btn--primary { background: var(--primary); color: var(--primary-fg); }
.btn--primary:hover:not(:disabled) { background: color-mix(in srgb, var(--primary) 86%, var(--text)); }

.btn--accent { background: var(--accent); color: var(--accent-fg); }
.btn--accent:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 86%, var(--text)); }

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

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

.btn--danger { background: var(--danger); color: var(--danger-fg); }
.btn--danger:hover:not(:disabled) { background: color-mix(in srgb, var(--danger) 86%, black); }

.btn--quiet { background: transparent; color: var(--text-muted); padding: 0 var(--sp-3); }
.btn--quiet:hover:not(:disabled) { background: var(--surface-hover); color: var(--text); }

.btn--sm { min-height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-sm); }
.btn--icon { width: 40px; padding: 0; }
.btn--icon.btn--sm { width: 32px; }
.btn--block { width: 100%; }

/* ---- Cards -------------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--pad-card);
}
.card--flush { padding: 0; overflow: hidden; }
.card__head {
  display: flex; align-items: center; gap: var(--sp-3);
  margin-bottom: var(--sp-4);
}
.card__head h2, .card__head h3 { font-size: var(--fs-lg); }
.card__head .spacer { flex: 1; }

.card--link {
  display: block; text-decoration: none; color: inherit;
  transition: border-color var(--dur-fast), background var(--dur-fast), transform var(--dur-instant);
}
.card--link:hover { border-color: var(--border-strong); background: var(--surface-hover); }
.card--link:active { transform: scale(0.995); }

.grid { display: grid; gap: var(--gap-grid); }
.grid--2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid--3 { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }
/* 150px lets four stat tiles sit two-up at 375px instead of stacking into a
   long scroll, while auto-fit still gives four across on a laptop. */
.grid--4 { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: var(--gap-grid); }
.row { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }

/* ---- Stat tiles --------------------------------------------------------- */
.stat { display: flex; flex-direction: column; gap: var(--sp-1); }
.stat__label { font-size: var(--fs-sm); color: var(--text-muted); font-weight: var(--fw-medium); }
.stat__value {
  font-size: var(--fs-2xl); font-weight: var(--fw-bold); letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums; line-height: 1.1;
}
.stat__meta { font-size: var(--fs-sm); color: var(--text-subtle); font-variant-numeric: tabular-nums; }

/* ---- Chips / badges ----------------------------------------------------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 24px; padding: 2px var(--sp-2);
  border-radius: var(--r-sm);
  background: var(--surface-2); color: var(--text-muted);
  font-size: var(--fs-xs); font-weight: var(--fw-semi);
  line-height: 1.3;
}
.chip svg { width: 13px; height: 13px; flex: none; }
.chip--primary { background: var(--primary-soft); color: var(--primary-soft-fg); }
.chip--danger  { background: var(--danger-soft);  color: var(--danger); }
.chip--warn    { background: var(--warn-soft);    color: var(--warn); }
.chip--success { background: var(--success-soft); color: var(--success); }
.chip--info    { background: var(--info-soft);    color: var(--info); }
.chip--accent  { background: var(--accent-soft);  color: var(--accent); }
.chip--outline { background: transparent; border: 1px solid var(--border); }

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; min-width: 0; }

.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--dot-color, var(--primary)); }

/* ---- Segmented control -------------------------------------------------- */
.segmented {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--r-md);
}
.segmented button {
  min-height: 30px; padding: 0 var(--sp-3);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm); font-weight: var(--fw-medium); color: var(--text-muted);
  transition: background var(--dur-fast), color var(--dur-fast);
  white-space: nowrap;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] {
  background: var(--surface); color: var(--text); font-weight: var(--fw-semi);
  box-shadow: var(--shadow-sm);
}

/* ---- Forms -------------------------------------------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field__label { font-size: var(--fs-sm); font-weight: var(--fw-semi); color: var(--text); }
.field__req { color: var(--danger); margin-left: 2px; }
.field__hint { font-size: var(--fs-xs); color: var(--text-subtle); line-height: var(--lh-snug); }
.field__error {
  font-size: var(--fs-xs); font-weight: var(--fw-medium); color: var(--danger);
  display: flex; align-items: flex-start; gap: 5px; line-height: var(--lh-snug);
}
.field__error svg { width: 13px; height: 13px; flex: none; margin-top: 2px; }

.input, .select, .textarea {
  width: 100%; min-height: 44px;         /* touch-friendly (Apple HIG) */
  padding: 10px var(--sp-3);
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r-md);
  font-size: var(--fs-md);               /* 16px stops iOS zoom-on-focus */
  color: var(--text);
  transition: border-color var(--dur-fast), box-shadow var(--dur-fast), background var(--dur-fast);
}
.input::placeholder, .textarea::placeholder { color: var(--text-subtle); }
.input:hover, .select:hover, .textarea:hover { border-color: var(--border-strong); }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--ring);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 22%, transparent);
}
.input[aria-invalid="true"], .select[aria-invalid="true"], .textarea[aria-invalid="true"] { border-color: var(--danger); }
.input[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 22%, transparent); }
.input:disabled, .select:disabled, .textarea:disabled { background: var(--surface-2); color: var(--text-subtle); cursor: not-allowed; }
.input[readonly] { background: var(--surface-2); }

.textarea { min-height: 88px; resize: vertical; line-height: var(--lh-body); }

.select {
  appearance: none; padding-right: var(--sp-8); cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235c6f72' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--sp-3) center; background-size: 18px;
}

.form-grid { display: grid; gap: var(--sp-4); grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.form-grid .span-2 { grid-column: 1 / -1; }

.checkbox { display: flex; align-items: center; gap: var(--sp-3); cursor: pointer; padding: var(--sp-2) 0; min-height: 44px; }
/* A compact variant still keeps a 44px hit area — it just stops adding
   padding around it, rather than shrinking the target itself. */
.checkbox--inline { padding: 0; }
.checkbox input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--primary); flex: none; cursor: pointer; }
.checkbox__text { font-size: var(--fs-base); }
.checkbox__text small { display: block; color: var(--text-subtle); font-size: var(--fs-xs); }

.swatches { display: flex; flex-wrap: wrap; gap: var(--sp-2); }
.swatch {
  width: 34px; height: 34px; border-radius: var(--r-sm);
  border: 2px solid transparent; position: relative;
  transition: transform var(--dur-instant) var(--ease-spring);
}
.swatch::after {
  content: ""; position: absolute; inset: 3px; border-radius: 3px; background: var(--sw);
}
.swatch:hover { transform: scale(1.08); }
.swatch[aria-pressed="true"] { border-color: var(--text); }

/* ---- Error summary (WCAG 2.2 focus management) -------------------------- */
.error-summary {
  border: 1px solid var(--danger); background: var(--danger-soft);
  border-radius: var(--r-md); padding: var(--sp-4); margin-bottom: var(--sp-4);
}
.error-summary:focus-visible { outline: 2px solid var(--danger); outline-offset: 2px; }
.error-summary h3 { font-size: var(--fs-base); color: var(--danger); margin-bottom: var(--sp-2); }
.error-summary ul { display: flex; flex-direction: column; gap: 4px; }
.error-summary a { color: var(--danger); font-size: var(--fs-sm); font-weight: var(--fw-medium); }

/* ---- Modal / dialog ----------------------------------------------------- */
.modal-scrim {
  position: fixed; inset: 0; z-index: var(--z-modal);
  display: grid; place-items: end center;
  background: var(--scrim-bg);
  animation: fade-in var(--dur-base) var(--ease-out);
  overscroll-behavior: contain;
}
@media (min-width: 640px) { .modal-scrim { place-items: center; padding: var(--sp-6); } }

.modal {
  position: relative;
  width: 100%; max-width: 620px; max-height: min(88dvh, 900px);
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  box-shadow: var(--shadow-lg);
  animation: sheet-up var(--dur-slow) var(--ease-out);
}
@media (min-width: 640px) {
  .modal { border-radius: var(--r-xl); animation: modal-in var(--dur-base) var(--ease-out); }
}
.modal--wide { max-width: 820px; }

@keyframes sheet-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes modal-in { from { transform: scale(0.97); opacity: 0; } to { transform: none; opacity: 1; } }

.modal__head {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-5) var(--sp-5) var(--sp-4);
  border-bottom: 1px solid var(--border);
}
.modal__head h2 { flex: 1; font-size: var(--fs-lg); min-width: 0; }
.modal__body { padding: var(--sp-5); overflow-y: auto; overscroll-behavior: contain; flex: 1; }
.modal__foot {
  display: flex; gap: var(--sp-2); justify-content: flex-end; flex-wrap: wrap;
  padding: var(--sp-4) var(--sp-5);
  padding-bottom: max(var(--sp-4), env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--surface-2);
  border-radius: 0 0 var(--r-xl) var(--r-xl);
}
.modal__foot .spacer { flex: 1; }

/* ---- Toasts ------------------------------------------------------------- */
.toast-region {
  position: fixed; z-index: var(--z-toast);
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--sp-3) + env(safe-area-inset-bottom));
  display: flex; flex-direction: column-reverse; gap: var(--sp-2);
  width: min(440px, calc(100vw - var(--sp-6)));
  pointer-events: none;
}
@media (min-width: 1024px) {
  .toast-region { left: auto; right: var(--sp-6); transform: none; bottom: var(--sp-6); }
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-3) var(--sp-3) var(--sp-4);
  border-radius: var(--r-md);
  background: var(--text); color: var(--bg);
  box-shadow: var(--shadow-lg);
  font-size: var(--fs-base); font-weight: var(--fw-medium);
  animation: rise var(--dur-base) var(--ease-out);
}
.toast.is-leaving { animation: fade-out var(--dur-fast) var(--ease-in) forwards; }
@keyframes fade-out { to { opacity: 0; transform: translateY(6px); } }
.toast__text { flex: 1; min-width: 0; }
.toast__action {
  flex: none; padding: 6px var(--sp-3); border-radius: var(--r-sm);
  font-weight: var(--fw-bold); font-size: var(--fs-sm);
  color: var(--a-400); background: rgb(255 255 255 / 0.08);
}
.toast__action:hover { background: rgb(255 255 255 / 0.16); }
.toast--danger  { background: var(--danger); color: var(--danger-fg); }
.toast--success { background: var(--success); color: var(--success-fg); }
.toast--danger .toast__action, .toast--success .toast__action { color: inherit; }

/* ---- Command palette ---------------------------------------------------- */
.palette-scrim {
  position: fixed; inset: 0; z-index: var(--z-palette);
  display: grid; place-items: start center; padding: var(--sp-4);
  padding-top: min(14vh, 120px);
  background: var(--scrim-bg);
  animation: fade-in var(--dur-fast) var(--ease-out);
}
.palette {
  width: min(600px, 100%); max-height: min(70dvh, 560px);
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--r-lg); box-shadow: var(--shadow-lg);
  animation: modal-in var(--dur-base) var(--ease-out);
}
.palette__search { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-bottom: 1px solid var(--border); }
.palette__search svg { width: 18px; height: 18px; color: var(--text-subtle); flex: none; }
.palette__search input { flex: 1; min-width: 0; border: 0; background: none; font-size: var(--fs-md); outline: none; }
.palette__list { overflow-y: auto; padding: var(--sp-2); overscroll-behavior: contain; }
.palette__group { padding: var(--sp-3) var(--sp-3) var(--sp-1); font-size: var(--fs-xs); font-weight: var(--fw-bold); color: var(--text-subtle); text-transform: uppercase; letter-spacing: 0.05em; }
.palette__item {
  display: flex; align-items: center; gap: var(--sp-3); width: 100%;
  min-height: 42px; padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md); text-align: left; color: var(--text);
  font-size: var(--fs-base);
}
.palette__item svg { width: 17px; height: 17px; color: var(--text-subtle); flex: none; }
.palette__item .sub { color: var(--text-subtle); font-size: var(--fs-xs); }
.palette__item .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.palette__item[aria-selected="true"] { background: var(--primary-soft); color: var(--primary-soft-fg); }
.palette__item[aria-selected="true"] svg { color: currentColor; }
.palette__foot { display: flex; gap: var(--sp-4); padding: var(--sp-2) var(--sp-4); border-top: 1px solid var(--border); font-size: var(--fs-xs); color: var(--text-subtle); background: var(--surface-2); }
.palette__foot kbd { font-family: var(--font-sans); font-weight: var(--fw-semi); padding: 1px 5px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface); }

/* ---- Empty & error states ----------------------------------------------- */
.empty {
  display: grid; place-items: center; gap: var(--sp-3);
  padding: var(--sp-12) var(--sp-5); text-align: center;
  color: var(--text-muted);
}
.empty__icon {
  display: grid; place-items: center; width: 52px; height: 52px;
  border-radius: var(--r-lg); background: var(--surface-2); color: var(--text-subtle);
}
.empty__icon svg { width: 24px; height: 24px; }
.empty h3 { font-size: var(--fs-lg); color: var(--text); }
.empty p { max-width: 42ch; font-size: var(--fs-base); }

/* ---- Progress ----------------------------------------------------------- */
.bar { height: 8px; border-radius: var(--r-full); background: var(--surface-2); overflow: hidden; }
.bar__fill { height: 100%; border-radius: inherit; background: var(--bar-color, var(--primary)); transition: width var(--dur-slow) var(--ease-out); }

.ring { display: block; }
.ring__track { stroke: var(--surface-2); }
.ring__value { stroke: var(--ring-color, var(--primary)); transition: stroke-dashoffset var(--dur-slow) var(--ease-out); }

/* ---- Tables ------------------------------------------------------------- */
.table-wrap { overflow-x: auto; overscroll-behavior-x: contain; min-width: 0; max-width: 100%; }
.table { width: 100%; border-collapse: collapse; font-size: var(--fs-base); }
.table th, .table td { padding: var(--sp-3) var(--sp-3); text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-size: var(--fs-sm); font-weight: var(--fw-semi); color: var(--text-muted); white-space: nowrap; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr { transition: background var(--dur-fast); }
.table tbody tr:hover { background: var(--surface-hover); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table__sort { display: inline-flex; align-items: center; gap: 4px; color: inherit; font: inherit; }
.table__sort svg { width: 13px; height: 13px; opacity: 0.4; }
.table__sort[data-active="true"] { color: var(--text); }
.table__sort[data-active="true"] svg { opacity: 1; color: var(--primary); }

/* ---- Misc --------------------------------------------------------------- */
.divider { height: 1px; background: var(--border); border: 0; margin: var(--sp-4) 0; }
.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); }
.tnum { font-variant-numeric: tabular-nums; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.wrap-any { overflow-wrap: anywhere; }
.sr-only { position:absolute!important; width:1px; height:1px; padding:0; margin:-1px; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap; border:0; }
.hide-sm { display: none; }
@media (min-width: 640px) { .hide-sm { display: revert; } .show-sm { display: none; } }

.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-hover) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
  border-radius: var(--r-sm);
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }
