/* ─────────────────────────────────────────────────────────────────────────
   components.css — reusable UI patterns: buttons, badges, cards, forms, tabs,
   page action bars, danger zone, dialog, toasts, global search, empty state,
   upload dropzone. var(--token) only. Quiet, data-dense internal-tool tone:
   hover states, focus-visible rings, real :active feedback.
   ───────────────────────────────────────────────────────────────────────── */

/* ═══════════════ Buttons ══════════════════════════════════════════════════*/
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--control-min); padding: 0 var(--space-4);
  border: 1px solid transparent; border-radius: var(--radius-sm);
  font-weight: 600; font-size: var(--fs-sm); white-space: nowrap; line-height: 1;
  transition: filter var(--transition), background var(--transition), border-color var(--transition);
}
.btn .ico { width: 18px; height: 18px; flex: none; }
.btn-primary   { background: var(--color-primary); color: var(--color-on-primary); }
.btn-secondary { background: var(--bg-page); color: var(--text); border-color: var(--border-strong); }
.btn-ghost     { background: transparent; color: var(--text-soft); }
.btn-danger    { background: var(--st-danger-bg); color: var(--st-danger-fg); border-color: color-mix(in srgb, var(--st-danger-fg) 25%, transparent); }
.btn:hover     { filter: brightness(.97); text-decoration: none; }
.btn-ghost:hover, .btn-secondary:hover { background: var(--bg-sunken); }
.btn:active    { filter: brightness(.93); transform: translateY(1px); }   /* tactile press (tap-highlight is off) */
.btn:disabled, .btn[aria-disabled="true"] { opacity: .55; pointer-events: none; }
.btn-sm { min-height: 36px; padding: 0 var(--space-3); font-size: var(--fs-xs); }
.btn-block { width: 100%; }

/* ═══════════════ Badges ═══════════════════════════════════════════════════*/
.badge {
  display: inline-flex; align-items: center; gap: var(--space-1);
  padding: 2px var(--space-2); border-radius: var(--radius-pill);
  font-size: var(--fs-xs); font-weight: 600; white-space: nowrap;
}
.badge-success { background: var(--st-success-bg); color: var(--st-success-fg); }
.badge-warning { background: var(--st-warning-bg); color: var(--st-warning-fg); }
.badge-danger  { background: var(--st-danger-bg);  color: var(--st-danger-fg); }
.badge-accent  { background: var(--st-info-bg);    color: var(--st-info-fg); }
.badge-neutral { background: var(--slate-100);     color: var(--text-soft); }
.badge-muted   { background: var(--slate-100);     color: var(--text-muted); }

/* ═══════════════ Cards ════════════════════════════════════════════════════
   Container-query ready: children can adapt to the CARD's width, not the
   viewport's (a card in a narrow column restacks even on a wide screen). */
.card {
  background: var(--bg-raised); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-sm);
  container-type: inline-size;
}
.card-header {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.card-header h2, .card-header h3 { font-size: var(--fs-lg); margin: 0; }
.card-body { padding: var(--space-5); }
.card-body > * + * { margin-top: var(--space-4); }

/* ═══════════════ Forms ════════════════════════════════════════════════════*/
.form-group { margin-bottom: var(--space-5); }
.form-label { display: block; font-weight: 600; font-size: var(--fs-sm); color: var(--text); margin-bottom: var(--space-2); }
.form-label-required::after { content: " *"; color: var(--st-danger-fg); }
.form-input, .form-select, .form-textarea {
  display: block; width: 100%;
  min-height: var(--control-min);
  padding: var(--space-2) var(--space-3);
  font-size: var(--fs-md);              /* ≥16px — iOS zoom guard */
  line-height: 1.5; color: var(--text);
  background: var(--bg-page); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: 2px solid var(--color-accent); outline-offset: 1px; border-color: var(--color-accent);
}
.form-textarea { min-height: 96px; resize: vertical; }
.form-input[aria-invalid="true"], .form-select[aria-invalid="true"], .form-textarea[aria-invalid="true"] {
  border-color: var(--st-danger-fg);
}
.form-help { margin-top: var(--space-2); font-size: var(--fs-xs); color: var(--text-muted); }
.form-error { margin-top: var(--space-2); font-size: var(--fs-sm); color: var(--st-danger-fg); }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: var(--space-4); }
.form-actions { display: flex; justify-content: flex-end; gap: var(--space-3); margin-top: var(--space-5); }
.form-actions .secondary-left { margin-right: auto; }

/* ═══════════════ Tabs (status buckets on one line) ════════════════════════*/
.tabs {
  display: flex; gap: var(--space-1);
  border-bottom: 1px solid var(--border); margin-bottom: var(--space-4);
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: var(--space-2) var(--space-3);
  color: var(--text-soft); font-weight: 600; font-size: var(--fs-sm);
  border-bottom: 2px solid transparent; white-space: nowrap; flex-shrink: 0;
  transition: color var(--transition), border-color var(--transition);
}
.tab:hover { color: var(--text); text-decoration: none; }
.tab.active, .tab.is-active { color: var(--color-primary); border-bottom-color: var(--color-primary); }
.tab-count { margin-left: var(--space-1); font-size: var(--fs-xs); color: var(--text-muted); }

/* ═══════════════ Page action bar (sticky Back + Save) ═════════════════════
   Ported from farmbelt §6b. On mobile it sits inline below the mobile topbar;
   at ≥768px it sticks to the top of the viewport so Save is never scrolled
   away. Secondary chrome (badges, print) goes in .page-meta-actions below. */
.page-action-bar {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap;
  gap: var(--space-3);
  padding: var(--space-3) 0;
  margin: 0 0 var(--space-5);
  border-bottom: 1px solid var(--border);
  background: var(--bg-sunken);
}
.page-action-bar h1 { font-size: var(--fs-h2); line-height: 1.15; }
.page-action-bar-actions { display: flex; align-items: center; gap: var(--space-2); margin-left: auto; }
@media (min-width: 768px) {
  .page-action-bar { position: sticky; top: 0; z-index: 30; padding: var(--space-4) 0; }
}
.back-link {
  display: inline-flex; align-items: center; gap: var(--space-1);
  min-height: var(--control-min); padding: var(--space-2) var(--space-2);
  color: var(--color-accent); font-weight: 500; font-size: var(--fs-sm);
  border-radius: var(--radius-sm); transition: background var(--transition);
}
.back-link:hover { background: var(--bg-page); text-decoration: none; }
.back-link .ico { width: 1.05em; height: 1.05em; flex: none; }

/* page-meta-actions (farmbelt §6d) — secondary badges + buttons under the
   title; wraps instead of overflowing. Do NOT duplicate the back link here. */
.page-meta-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); margin-bottom: var(--space-4); }
.page-meta-actions .badge { font-size: var(--fs-xs); padding: 3px var(--space-3); }

/* ═══════════════ Danger zone (farmbelt §6c) ═══════════════════════════════
   Destructive actions live below the form in a visually-separated, hard-to-
   misclick block. */
.danger-zone {
  margin-top: var(--space-7);
  padding: var(--space-4) var(--space-5);
  border: 1px solid color-mix(in srgb, var(--st-danger-fg) 45%, transparent);
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--st-danger-bg) 40%, transparent);
}
.danger-zone-title {
  font-size: var(--fs-sm); font-weight: 600; color: var(--st-danger-fg);
  text-transform: uppercase; letter-spacing: .05em; margin-bottom: var(--space-2);
}
.danger-zone-text { color: var(--text-soft); font-size: var(--fs-sm); margin-bottom: var(--space-4); }

/* ═══════════════ Dialog (native <dialog>) ═════════════════════════════════*/
dialog {
  border: none; padding: 0; margin: auto;   /* restore UA centering (reset zeroed margin) */
  max-width: 460px; width: calc(100vw - 2 * var(--space-4));
  max-height: 90vh; max-height: 90dvh;
  border-radius: var(--radius-lg); background: var(--bg-raised); color: var(--text);
  box-shadow: var(--shadow-lg); overflow: auto;
}
dialog::backdrop { background: rgba(20, 25, 32, .45); }
.dialog-card { display: flex; flex-direction: column; }
/* A <dialog class="dialog-card"> is closed by default: the class's display:flex is
   an author rule that would otherwise override the UA dialog:not([open]) hide, so
   the confirm modal would render on every page load. Re-hide it when not open. */
.dialog-card:not([open]) { display: none; }
.dialog-card > header { padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--border); font-weight: 600; font-size: var(--fs-lg); }
.dialog-card > .dialog-body { padding: var(--space-5); }
.dialog-card > footer { display: flex; justify-content: flex-end; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-top: 1px solid var(--border); }
.dialog-card > footer .secondary-left { margin-right: auto; }

/* ═══════════════ Toasts ═══════════════════════════════════════════════════
   .toast-region is the fixed, aria-live host (bottom-right on desktop, bottom-
   center on phones). Toasts enter, then get .is-leaving to animate out. */
.toast-region {
  position: fixed; z-index: 80;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--bottomnav-h) + var(--space-4) + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: var(--space-2);
  width: max-content; max-width: calc(100vw - 2 * var(--space-4));
  pointer-events: none;
}
@media (min-width: 768px) {
  .toast-region { left: auto; right: var(--space-5); bottom: var(--space-5); transform: none; }
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border: 1px solid; border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  font-size: var(--fs-sm); font-weight: 500;
  background: var(--bg-raised); color: var(--text); border-color: var(--border-strong);
  animation: toast-in .22s ease;
}
.toast--success { background: var(--st-success-bg); color: var(--st-success-fg); border-color: color-mix(in srgb, var(--st-success-fg) 30%, transparent); }
.toast--error   { background: var(--st-danger-bg);  color: var(--st-danger-fg);  border-color: color-mix(in srgb, var(--st-danger-fg) 30%, transparent); }
.toast.is-leaving { animation: toast-out .2s ease forwards; }
@keyframes toast-in  { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { from { opacity: 1; transform: none; } to { opacity: 0; transform: translateY(10px); } }

/* ═══════════════ Empty state (TEXT ONLY — house doctrine: no icon) ════════*/
.empty-state {
  text-align: center; padding: var(--space-7) var(--space-4);
  color: var(--text-soft);
}
.empty-state h2, .empty-state h3 { font-size: var(--fs-lg); color: var(--text); margin-bottom: var(--space-2); }
.empty-state p { font-size: var(--fs-sm); max-width: 42ch; margin-inline: auto; }
.empty-state .btn { margin-top: var(--space-4); }

/* ═══════════════ Upload dropzone ══════════════════════════════════════════*/
.upload-dropzone {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--space-1); text-align: center;
  min-height: 7rem; padding: var(--space-5);
  border: 2px dashed var(--border-strong); border-radius: var(--radius-md);
  background: var(--bg-sunken);
  transition: border-color var(--transition), background var(--transition);
}
.upload-dropzone:focus-within,
.upload-dropzone.drag-over { border-color: var(--color-accent); background: color-mix(in srgb, var(--color-accent) 6%, transparent); }
.upload-dropzone input[type="file"] { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.upload-dropzone strong { font-weight: 600; }
.upload-dropzone .hint { font-size: var(--fs-xs); color: var(--text-muted); }

/* Echoes back what was dropped, pasted or picked. Empty until then — an aria-live
   region that starts with placeholder text announces the placeholder. */
.upload-dropzone .selected-filename:not(:empty) {
  margin-top: var(--space-2);
  font-size: var(--fs-xs); font-weight: 600;
  color: var(--color-accent);
  max-width: 100%; overflow-wrap: anywhere;
}
.upload-dropzone kbd {
  font: inherit; font-size: .95em;
  padding: .05em .35em;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--bg-base);
}
/* The file input covers the zone at opacity 0 to make the whole area a click and
   drop target, which also means it swallows the drag events — so the zone must
   still highlight while a file is dragged over the input itself. */
.upload-dropzone input[type="file"]:focus-visible { outline: none; }

/* ═══════════════ Dialog confirm actions ═══════════════════════════════════
   The simple confirm <dialog class="dialog-card"> has a direct <p> + this
   right-aligned button row; padding is carried by the children so it doesn't
   double up on the structured header/body/footer variant above. */
.dialog-card > p { margin: 0; padding: var(--space-5) var(--space-5) 0; }
.dialog-card-actions { display: flex; justify-content: flex-end; gap: var(--space-3); padding: var(--space-4) var(--space-5) var(--space-5); }

/* ═══════════════ Activity feed (compact list) ═════════════════════════════*/
.activity-feed { display: flex; flex-direction: column; }
.activity-feed li { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--space-2); padding: var(--space-2) 0; border-bottom: 1px solid var(--border); }
.activity-feed li:last-child { border-bottom: none; }
.activity-who  { font-weight: 500; font-size: var(--fs-sm); color: var(--text); }
.activity-what { font-size: var(--fs-sm); color: var(--text-soft); }
.activity-when { margin-left: auto; font-size: var(--fs-xs); color: var(--text-muted); white-space: nowrap; }

/* ═══════════════ Dirty pill ("Unsaved changes") ═══════════════════════════
   Lives in .page-action-bar-actions; the [hidden] rule wins over inline-flex
   so it shows only when JS clears the attribute. */
.dirty-pill { display: inline-flex; align-items: center; padding: 2px var(--space-2); border-radius: var(--radius-pill); background: var(--st-warning-bg); color: var(--st-warning-fg); font-size: var(--fs-xs); font-weight: 600; white-space: nowrap; }
.dirty-pill[hidden] { display: none; }

/* Checkbox + its label on one line, for a single boolean in a form. */
.form-check { display: flex; align-items: center; gap: var(--space-2); font-size: var(--fs-sm); cursor: pointer; }
.form-check input[type="checkbox"] { width: 1rem; height: 1rem; flex: 0 0 auto; }
