/* ==========================================================================
   Zain OS — Components
   All components from the inventory, named and reusable.
   ========================================================================== */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  height: 36px;
  padding: 0 var(--sp-4);
  border-radius: var(--r-sm);
  font-size: var(--fs-13);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.005em;
  white-space: nowrap;
  user-select: none;
  transition: background var(--dur-default) var(--ease),
              border-color var(--dur-default) var(--ease),
              color var(--dur-default) var(--ease),
              box-shadow var(--dur-default) var(--ease);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn .icon { width: 14px; height: 14px; }

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

.btn-secondary {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--hover); border-color: var(--brand-dim); }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover:not(:disabled) { background: var(--hover); color: var(--text); }

.btn-danger-outline {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}
.btn-danger-outline:hover:not(:disabled) { background: var(--danger-soft); }

.btn-danger-solid {
  background: var(--danger);
  color: #fff;
}
.btn-danger-solid:hover:not(:disabled) { background: var(--danger-hover); }

.btn-sm { height: 28px; padding: 0 var(--sp-3); font-size: var(--fs-12); border-radius: 6px; }
.btn-sm .icon { width: 12px; height: 12px; }

.btn-inline {
  height: auto;
  padding: 2px 6px;
  font-size: var(--fs-12);
  color: var(--brand);
  background: transparent;
  border-radius: 4px;
}
.btn-inline:hover { background: var(--brand-soft); }

.btn-icon {
  width: 32px; height: 32px;
  padding: 0;
  border-radius: var(--r-sm);
  color: var(--text-secondary);
}
.btn-icon:hover { background: var(--hover); color: var(--text); }
.btn-icon .icon { width: 16px; height: 16px; }

/* ---------- Inputs ---------- */
.input,
.select,
.textarea {
  width: 100%;
  height: 36px;
  padding: 0 var(--sp-3);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: var(--fs-14);
  transition: border-color var(--dur-default) var(--ease),
              box-shadow var(--dur-default) var(--ease);
}
.input::placeholder, .textarea::placeholder { color: var(--text-tertiary); }
.input:hover, .select:hover, .textarea:hover { border-color: var(--brand-dim); }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: var(--ring-focus);
}
.textarea {
  height: auto;
  padding: var(--sp-2) var(--sp-3);
  line-height: var(--lh-normal);
  resize: vertical;
  min-height: 80px;
}
.input.has-error, .textarea.has-error, .select.has-error {
  border-color: var(--danger);
}
.field-error {
  font-size: var(--fs-12);
  color: var(--danger);
  margin-top: var(--sp-1);
}

/* Form layout */
.field { display: flex; flex-direction: column; gap: var(--sp-2); }
.field-label {
  font-size: var(--fs-12);
  font-weight: var(--fw-medium);
  color: var(--text-secondary);
  letter-spacing: 0.01em;
}
.field-hint {
  font-size: var(--fs-12);
  color: var(--text-tertiary);
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-4);
}
.form-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: var(--sp-3);
  padding-top: var(--sp-4);
  border-top: 1px solid var(--border-light);
  margin-top: var(--sp-4);
}

/* Inline edit affordance — pencil + check/× */
.inline-edit {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
}
.inline-edit-trigger {
  width: 22px; height: 22px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  color: var(--text-tertiary);
  transition: color var(--dur-default) var(--ease), background var(--dur-default) var(--ease);
}
.inline-edit-trigger:hover { color: var(--brand); background: var(--brand-soft); }
.inline-edit-commit {
  width: 22px; height: 22px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  color: var(--brand);
  background: var(--brand-soft);
}
.inline-edit-cancel {
  width: 22px; height: 22px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  color: var(--text-secondary);
}
.inline-edit-cancel:hover { color: var(--danger); background: var(--danger-soft); }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-md);
  padding: var(--sp-5);
}
.card-section {
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-md);
}
.card-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border-light);
}
.card-section-title {
  font-size: var(--fs-15);
  font-weight: var(--fw-semibold);
  color: var(--text);
}
.card-section-body { padding: var(--sp-5); }
.card-section-body.flush { padding: 0; }

/* Metric card — used on dashboard + installments top strip */
.metric-card {
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-md);
  padding: var(--sp-5);
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  position: relative;
  overflow: hidden;
}
.metric-card .metric-label {
  font-size: var(--fs-12);
  font-weight: var(--fw-medium);
  color: var(--text-secondary);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.metric-card .metric-value {
  font-size: var(--fs-24);
  font-weight: var(--fw-semibold);
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
}
.metric-card .metric-sub {
  font-size: var(--fs-12);
  color: var(--text-secondary);
  display: flex; align-items: center; gap: var(--sp-2);
}
.metric-card.is-accent::before {
  content: "";
  position: absolute; top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--brand);
}

/* Stat card — compact horizontal */
.stat-card {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-sm);
}

/* Unit card */
.unit-card {
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-md);
  overflow: hidden;
  transition: border-color var(--dur-default) var(--ease),
              transform var(--dur-default) var(--ease),
              box-shadow var(--dur-default) var(--ease);
  cursor: pointer;
}
.unit-card:hover {
  border-color: var(--brand-dim);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}
.unit-card .unit-photo {
  aspect-ratio: 16 / 10;
  background:
    repeating-linear-gradient(135deg, #2a2725 0, #2a2725 8px, #252220 8px, #252220 16px);
  position: relative;
}
.unit-card .unit-body { padding: var(--sp-4); display: flex; flex-direction: column; gap: var(--sp-3); }

/* ---------- Tables ---------- */
.table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--fs-13);
}
.table thead th {
  background: var(--table-header);
  color: var(--text-secondary);
  font-weight: var(--fw-semibold);
  font-size: var(--fs-12);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: left;
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 2;
}
.table tbody td {
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border-light);
  color: var(--text);
  vertical-align: middle;
}
.table tbody tr { transition: background var(--dur-default) var(--ease); }
.table tbody tr:hover { background: var(--hover); }
.table.compact tbody td { padding: var(--sp-2) var(--sp-4); }
.table .num, .table [data-num] { text-align: right; font-variant-numeric: tabular-nums; }
.table .col-actions { width: 1%; white-space: nowrap; text-align: right; }
.table tbody tr.row-editing { background: var(--brand-subtle); }
.table tbody tr.row-editing td { border-bottom-color: var(--brand-dim); }

/* ---------- Status badges ----------
   Severity encoded by FILL vs OUTLINE + brand intensity, never by hue.
   Iconography reinforces meaning. */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 22px;
  padding: 0 10px;
  border-radius: var(--r-pill);
  font-size: var(--fs-11);
  font-weight: var(--fw-semibold);
  letter-spacing: 0.02em;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.badge .dot {
  width: 6px; height: 6px; border-radius: var(--r-circle);
  background: currentColor;
}

/* Outline = passive / informational */
.badge-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
}
.badge-outline-brand {
  background: transparent;
  border: 1px solid var(--brand-dim);
  color: var(--brand);
}

/* Soft fill = active / in-progress */
.badge-soft {
  background: var(--brand-soft);
  color: var(--brand);
  border: 1px solid transparent;
}

/* Solid fill = urgent / hot / requires action */
.badge-solid {
  background: var(--brand);
  color: #fff;
}

/* Danger = errors only — never status */
.badge-danger-outline {
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
}

/* Score badge — 24px orange circle */
.score-badge {
  width: 24px; height: 24px;
  border-radius: var(--r-circle);
  background: var(--brand);
  color: #fff;
  font-size: var(--fs-11);
  font-weight: var(--fw-bold);
  display: inline-flex; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}
.score-badge.cold {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

/* Delivery badge — countdown to delivery date */
.delivery-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--brand-subtle);
  border: 1px solid var(--brand-dim);
  border-radius: var(--r-sm);
  font-size: var(--fs-11);
  font-weight: var(--fw-semibold);
  color: var(--brand);
  font-variant-numeric: tabular-nums;
}

/* Health dot — server health */
.health-dot {
  width: 8px; height: 8px; border-radius: var(--r-circle);
  background: var(--brand);
  box-shadow: var(--shadow-glow);
  display: inline-block;
}
.health-dot.cold {
  background: transparent;
  border: 1px solid var(--text-tertiary);
  box-shadow: none;
}
.health-dot.bad {
  background: var(--danger);
  box-shadow: 0 0 6px rgba(161, 63, 40, 0.4);
}

/* ---------- Tab bar (pill) ---------- */
.tabs {
  display: inline-flex;
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-pill);
  padding: 4px;
  gap: 2px;
}
.tabs .tab {
  padding: 6px 14px;
  font-size: var(--fs-13);
  font-weight: var(--fw-medium);
  color: var(--text-secondary);
  border-radius: var(--r-pill);
  transition: color var(--dur-default) var(--ease), background var(--dur-default) var(--ease);
}
.tabs .tab:hover { color: var(--text); }
.tabs .tab.is-active {
  background: var(--brand);
  color: #fff;
}
.tabs .tab .tab-count {
  margin-left: 6px;
  font-size: var(--fs-11);
  opacity: 0.85;
  font-variant-numeric: tabular-nums;
}

/* Sub-route tabs (used on unit detail) — underline style */
.subtabs {
  display: flex;
  gap: var(--sp-1);
  border-bottom: 1px solid var(--border-light);
}
.subtabs .subtab {
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-13);
  font-weight: var(--fw-medium);
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color var(--dur-default) var(--ease), border-color var(--dur-default) var(--ease);
}
.subtabs .subtab:hover { color: var(--text); }
.subtabs .subtab.is-active {
  color: var(--brand);
  border-bottom-color: var(--brand);
}
.subtabs .subtab .subtab-count {
  margin-left: 6px;
  padding: 1px 6px;
  font-size: var(--fs-10);
  background: var(--hover);
  border-radius: var(--r-pill);
  color: var(--text-secondary);
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-6);
}
.modal {
  width: 100%;
  max-width: 560px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-md);
  display: flex; flex-direction: column;
  max-height: calc(100vh - 80px);
}
.modal.modal-wide { max-width: 780px; }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 1px solid var(--border-light);
}
.modal-title { font-size: var(--fs-16); font-weight: var(--fw-semibold); }
.modal-body { padding: var(--sp-5); overflow-y: auto; }
.modal-footer {
  display: flex; justify-content: flex-end; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5);
  border-top: 1px solid var(--border-light);
}

/* ---------- Toast (top-center) ---------- */
.toast-stack {
  position: fixed;
  top: var(--sp-5);
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-toast);
  display: flex; flex-direction: column; gap: var(--sp-2);
  pointer-events: none;
}
.toast {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-13);
  color: var(--text);
  box-shadow: var(--shadow-md);
  display: flex; align-items: center; gap: var(--sp-3);
  pointer-events: auto;
  min-width: 280px;
}
.toast .icon { color: var(--brand); width: 16px; height: 16px; flex-shrink: 0; }
.toast.toast-error { border-color: var(--danger); }
.toast.toast-error .icon { color: var(--danger); }
.toast .toast-msg { flex: 1; min-width: 0; }
.toast .toast-action {
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--brand-dim);
  border-radius: var(--r-pill);
  color: var(--brand);
  font-family: inherit;
  font-size: var(--fs-12);
  font-weight: 600;
  padding: 4px 12px;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease;
}
.toast .toast-action:hover {
  background: var(--brand);
  color: #fff;
}

/* ---------- Skeleton ---------- */
.skeleton {
  background: linear-gradient(90deg, var(--hover) 0%, var(--border) 50%, var(--hover) 100%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite linear;
  border-radius: 6px;
  height: 12px;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.skeleton-line { height: 12px; margin-bottom: var(--sp-2); }
.skeleton-line.w-50 { width: 50%; }
.skeleton-line.w-75 { width: 75%; }

/* ---------- Empty + Error states ---------- */
.empty-state, .error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--sp-10) var(--sp-6);
  gap: var(--sp-3);
}
.empty-state .icon, .error-state .icon {
  width: 48px; height: 48px;
  opacity: 0.3;
  color: var(--text-secondary);
}
.empty-state .title {
  font-size: var(--fs-15);
  font-weight: var(--fw-semibold);
  color: var(--text);
}
.empty-state .subtitle {
  font-size: var(--fs-13);
  color: var(--text-secondary);
  max-width: 360px;
}
.error-state .icon { color: var(--danger); opacity: 0.6; }
.error-state .title { color: var(--danger); font-size: var(--fs-15); font-weight: var(--fw-semibold); }

/* ---------- Search dropdown ---------- */
.search-results {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  box-shadow: var(--shadow-md);
  max-height: 480px;
  overflow-y: auto;
  padding: var(--sp-2);
}
.search-group-label {
  font-size: var(--fs-10);
  font-weight: var(--fw-semibold);
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: var(--sp-2) var(--sp-3) var(--sp-1);
}
.search-result-row {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: 6px;
  cursor: pointer;
  transition: background var(--dur-default) var(--ease);
}
.search-result-row:hover, .search-result-row.is-active { background: var(--hover); }
.search-result-row .result-meta { color: var(--text-secondary); font-size: var(--fs-12); margin-left: auto; }
.search-result-row mark { background: var(--brand-soft); color: var(--brand); padding: 0 2px; border-radius: 2px; }

/* ---------- Alert banner (in-page, not toast) ---------- */
.alert-banner {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border: 1px solid var(--brand-dim);
  background: var(--brand-soft);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: var(--fs-13);
}
.alert-banner.alert-danger {
  border-color: var(--danger);
  background: var(--danger-soft);
}
.alert-banner .icon { color: var(--brand); flex-shrink: 0; margin-top: 2px; }
.alert-banner.alert-danger .icon { color: var(--danger); }

/* ---------- Chat bubbles ---------- */
.chat-bubble {
  max-width: 70%;
  padding: var(--sp-3) var(--sp-4);
  border-radius: 14px;
  font-size: var(--fs-14);
  line-height: var(--lh-normal);
  word-wrap: break-word;
}
.chat-bubble-lead {
  background: #332F2B;
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.chat-bubble-agent {
  background: var(--brand-soft);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
  border-left: 3px solid var(--brand);
  padding-left: calc(var(--sp-4) - 3px);
}
.chat-bubble-zain {
  background: var(--brand);
  color: #fff;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}
.chat-meta {
  font-size: var(--fs-11);
  color: var(--text-tertiary);
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

/* Voice + PDF bubble variants */
.chat-voice {
  display: flex; align-items: center; gap: var(--sp-3);
  min-width: 220px;
}
.chat-voice .play {
  width: 32px; height: 32px;
  border-radius: var(--r-circle);
  background: rgba(0,0,0,0.2);
  display: inline-flex; align-items: center; justify-content: center;
  color: inherit;
}
.chat-voice .waveform {
  flex: 1;
  height: 18px;
  background: repeating-linear-gradient(90deg,
    currentColor 0, currentColor 2px,
    transparent 2px, transparent 5px);
  opacity: 0.5;
  border-radius: 2px;
}
.chat-voice .duration { font-size: var(--fs-11); font-variant-numeric: tabular-nums; opacity: 0.8; }

.chat-pdf {
  display: flex; align-items: center; gap: var(--sp-3);
  min-width: 240px;
  cursor: pointer;
}
.chat-pdf .pdf-icon {
  width: 36px; height: 44px;
  background: rgba(0,0,0,0.2);
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: var(--fs-10);
  font-weight: var(--fw-bold);
  color: inherit;
}

/* Voice recording UI */
.voice-recording {
  display: inline-flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-4);
  background: var(--brand-soft);
  border: 1px solid var(--brand-dim);
  border-radius: var(--r-pill);
  color: var(--brand);
  font-size: var(--fs-13);
  font-weight: var(--fw-medium);
}
.voice-recording .pulse {
  width: 8px; height: 8px;
  border-radius: var(--r-circle);
  background: var(--brand);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.8); }
}
.voice-recording .timer { font-variant-numeric: tabular-nums; }

/* Typing indicator (3 dots, dot-bounce 1.4s) */
.typing { display: inline-flex; gap: 4px; padding: var(--sp-2) var(--sp-3); }
.typing span {
  width: 6px; height: 6px;
  border-radius: var(--r-circle);
  background: var(--text-secondary);
  animation: dot-bounce 1.4s infinite ease-in-out both;
}
.typing span:nth-child(1) { animation-delay: -0.32s; }
.typing span:nth-child(2) { animation-delay: -0.16s; }
@keyframes dot-bounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* Spin (loading) */
.spin { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Shake (login wrong-password) */
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
.shake { animation: shake 400ms ease-in-out; }

/* ---------- Background sync strip (bottom-left) ---------- */
.sync-strip {
  position: fixed;
  bottom: var(--sp-4);
  left: var(--sp-4);
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 6px 12px;
  background: var(--card);
  border: 1px solid var(--border-light);
  border-radius: var(--r-pill);
  font-size: var(--fs-11);
  color: var(--text-secondary);
  z-index: 30;
  box-shadow: var(--shadow-md);
}
.sync-strip .health-dot { width: 6px; height: 6px; }

/* ---------- Helper utilities ---------- */
.row { display: flex; align-items: center; }
.row.gap-1 { gap: var(--sp-1); } .row.gap-2 { gap: var(--sp-2); } .row.gap-3 { gap: var(--sp-3); }
.row.gap-4 { gap: var(--sp-4); } .row.gap-5 { gap: var(--sp-5); } .row.gap-6 { gap: var(--sp-6); }
.col { display: flex; flex-direction: column; }
.col.gap-1 { gap: var(--sp-1); } .col.gap-2 { gap: var(--sp-2); } .col.gap-3 { gap: var(--sp-3); }
.col.gap-4 { gap: var(--sp-4); } .col.gap-5 { gap: var(--sp-5); } .col.gap-6 { gap: var(--sp-6); }
.spacer { flex: 1; }
.divider { height: 1px; background: var(--border-light); width: 100%; }
.muted { color: var(--text-secondary); }

/* ==========================================================================
   Mobile / Touch — additive overrides for the shared component layer
   (modals go full-screen on phone, inputs grow to 16px to suppress iOS zoom,
   chat bubbles widen, hover-only affordances stay visible on touch).
   Desktop (>1024px) is untouched.
   ========================================================================== */

/* ---------- Tablet (≤1024px) ---------- */
@media (max-width: 1024px) {
  /* Form rows always 1-col on tablet to give labels room */
  .form-row { grid-template-columns: 1fr; gap: var(--sp-3); }

  /* Modal: smaller backdrop padding so the dialog can be wider */
  .modal-backdrop { padding: var(--sp-4); }

  /* Stat / unit-card grids that callers leave alone */
  .unit-card .unit-body { gap: var(--sp-2); padding: var(--sp-3); }
}

/* ---------- Phone (≤640px) ---------- */
@media (max-width: 640px) {
  /* Inputs: 16px font defeats iOS focus-zoom; 44px height = comfortable tap */
  .input, .select, .textarea {
    font-size: 16px;
    height: 44px;
  }
  .textarea { height: auto; min-height: 96px; }

  /* Buttons: enlarge for thumbs */
  .btn { height: 40px; padding: 0 var(--sp-3); font-size: var(--fs-13); }
  .btn-sm { height: 32px; padding: 0 var(--sp-2); font-size: var(--fs-12); }
  .btn-icon { width: 36px; height: 36px; }

  /* Modal goes full-bleed: a sheet, not a centered dialog */
  .modal-backdrop {
    padding: 0;
    align-items: stretch;
    justify-content: stretch;
  }
  .modal,
  .modal.modal-wide {
    width: 100%;
    max-width: none;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    border-top: 0;
    height: 100vh;
    height: 100dvh;
  }
  .modal-header {
    padding: var(--sp-3) var(--sp-4);
    position: sticky; top: 0; z-index: 2;
    background: var(--card);
  }
  .modal-title { font-size: var(--fs-15); }
  .modal-body { padding: var(--sp-4); }
  .modal-footer {
    padding: var(--sp-3) var(--sp-4);
    padding-bottom: calc(var(--sp-3) + env(safe-area-inset-bottom, 0px));
    position: sticky; bottom: 0; z-index: 2;
    background: var(--card);
    flex-wrap: wrap;
    gap: var(--sp-2);
  }
  .modal-footer > .btn { flex: 1 1 auto; min-width: 44%; }

  /* Forms */
  .form-row { grid-template-columns: 1fr; gap: var(--sp-3); }
  .form-actions {
    flex-wrap: wrap;
    gap: var(--sp-2);
    padding-top: var(--sp-3);
    margin-top: var(--sp-3);
  }
  .form-actions > .btn { flex: 1 1 auto; }

  /* Cards */
  .card { padding: var(--sp-4); }
  .card-section-header { padding: var(--sp-3) var(--sp-4); }
  .card-section-body { padding: var(--sp-4); }
  .metric-card { padding: var(--sp-4); gap: var(--sp-2); }
  .metric-card .metric-value { font-size: var(--fs-22); }

  /* Tabs scroll horizontally rather than wrap awkwardly */
  .tabs,
  .subtabs {
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
    white-space: nowrap;
  }
  .tabs::-webkit-scrollbar,
  .subtabs::-webkit-scrollbar { display: none; }
  .tabs .tab,
  .subtabs .subtab { flex-shrink: 0; }

  /* Chat bubbles get more room and slightly tighter padding */
  .chat-bubble {
    max-width: 88%;
    padding: var(--sp-2) var(--sp-3);
    font-size: var(--fs-14);
  }
  .chat-voice,
  .chat-pdf { min-width: 0; width: 100%; }

  /* Toasts: full-width with side margins, anchored above safe area */
  .toast-stack {
    top: auto;
    bottom: calc(var(--sp-4) + env(safe-area-inset-bottom, 0px));
    left: var(--sp-3);
    right: var(--sp-3);
    transform: none;
    align-items: stretch;
  }
  .toast {
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
  }

  /* Search dropdown fills the screen width */
  .search-results {
    max-height: min(60vh, 420px);
  }

  /* Empty / error states tighter padding so they don't push content off */
  .empty-state,
  .error-state {
    padding: var(--sp-6) var(--sp-4);
  }
  .empty-state .icon,
  .error-state .icon { width: 40px; height: 40px; }
  .empty-state .subtitle { font-size: var(--fs-12); }

  /* Alert banner stacks icon above text if needed */
  .alert-banner { padding: var(--sp-3); gap: var(--sp-2); font-size: var(--fs-12); }

  /* Sync strip — keep above safe area, slightly smaller */
  .sync-strip {
    bottom: calc(var(--sp-3) + env(safe-area-inset-bottom, 0px));
    left: var(--sp-3);
    padding: 4px 10px;
    font-size: var(--fs-11);
  }

  /* Inline-edit triggers are easier to tap */
  .inline-edit-trigger,
  .inline-edit-commit,
  .inline-edit-cancel {
    width: 28px; height: 28px;
  }

  /* Tables (legacy un-namespaced .table): if rendered without a wrapper,
     still give them horizontal scroll inside their parent so they don't
     blow out the page. */
  .table { font-size: var(--fs-12); }
  .table thead th,
  .table tbody td { padding: var(--sp-2) var(--sp-3); }
}

/* ---------- Touch devices (no hover, coarse pointer) ---------- */
@media (hover: none) and (pointer: coarse) {
  /* Always show hover-revealed inline-edit affordance */
  .inline-edit-trigger { color: var(--text-secondary); }

  /* Buttons get a 44px hit target on phones with hands */
  .btn { min-height: 40px; }
  .btn-sm { min-height: 32px; }
  .btn-icon { min-width: 36px; min-height: 36px; }

  /* Disable scale-on-hover that can leave cards "stuck" after a tap */
  .unit-card:hover { transform: none; box-shadow: none; }

  /* Filter chip + tab tap targets */
  .tabs .tab { min-height: 32px; }
  .subtabs .subtab { min-height: 40px; }
}

/* ---------- iPhone in landscape with notches — extra safe-area love ---------- */
@supports (padding: max(0px)) {
  .main { padding-left: max(var(--canvas-pad-x), env(safe-area-inset-left)); padding-right: max(var(--canvas-pad-x), env(safe-area-inset-right)); }
}
