/* ============================================================
   components.css — reusable UI pieces, styled with real intent
   rather than flat gray boxes: gradient CTA, color-coded stat
   cards, softer shadows, pill badges with tinted backgrounds.
   ============================================================ */

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  padding: 10px var(--sp-4); border-radius: var(--radius-sm); border: 1px solid transparent;
  font-weight: 600; font-size: 14px; cursor: pointer; min-height: 44px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn-primary { background: var(--gradient-brand); color: #fff; box-shadow: var(--shadow-brand); }
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 10px 24px rgba(76,58,227,0.32); }
.btn-primary:active { transform: translateY(0); }
.btn-secondary { background: var(--color-surface); color: var(--color-text); border-color: var(--color-border); }
.btn-secondary:hover { background: var(--color-bg-alt); border-color: var(--color-primary-light); }
.btn-danger { background: var(--color-danger-bg); color: var(--color-danger); }
.btn-danger:hover { background: #f8d7dc; }
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 6px var(--sp-3); font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none !important; box-shadow: none !important; }

/* ── Cards ───────────────────────────────────────────────── */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: var(--sp-4);
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: var(--sp-4); }

.stat-card {
  display: flex; flex-direction: column; gap: var(--sp-2);
  position: relative; overflow: hidden;
}
.stat-card::before {
  content: ''; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: var(--gradient-brand);
}
.stat-card .stat-label {
  font-size: 11px; color: var(--color-text-dim); text-transform: uppercase;
  letter-spacing: 0.06em; font-weight: 700;
}
.stat-card .stat-value {
  font-size: 26px; font-weight: 800; font-family: var(--font-display);
  overflow-wrap: anywhere;
  line-height: 1.15;
}

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-3); }
@media (min-width: 640px) { .stat-grid { grid-template-columns: repeat(4, 1fr); } }

a.stat-card {
  text-decoration: none;
  color: inherit;
}
a.stat-card:hover {
  box-shadow: var(--shadow-md);
}

/* ── Forms ───────────────────────────────────────────────── */
.field { margin-bottom: var(--sp-4); }
.field label { display: block; font-size: 13px; font-weight: 700; margin-bottom: var(--sp-1); color: var(--color-text); }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px var(--sp-3);
  border: 1.5px solid var(--color-border); border-radius: var(--radius-sm);
  background: var(--color-surface); min-height: 44px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--color-primary); box-shadow: 0 0 0 3px var(--color-primary-light); outline: none;
}
.field textarea { min-height: 88px; resize: vertical; }
.field-row { display: flex; flex-direction: column; gap: var(--sp-3); }
@media (min-width: 640px) {
  .field-row { flex-direction: row; }
  .field-row .field { flex: 1; }
}

/* ── Tables ──────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--color-border); background: var(--color-surface); }
table.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data-table th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; font-weight: 700;
  color: var(--color-text-dim); padding: var(--sp-3); background: var(--color-bg-alt);
  border-bottom: 1px solid var(--color-border);
}
table.data-table td { padding: var(--sp-3); border-bottom: 1px solid var(--color-border); }
table.data-table tbody tr:hover { background: var(--color-bg-alt); }
table.data-table tbody tr:last-child td { border-bottom: none; }

@media (max-width: 640px) {
  .table-wrap { border: none; background: none; }
  table.data-table.stack-on-mobile thead { display: none; }
  table.data-table.stack-on-mobile, table.data-table.stack-on-mobile tbody,
  table.data-table.stack-on-mobile tr, table.data-table.stack-on-mobile td { display: block; width: 100%; }
  table.data-table.stack-on-mobile tr {
    margin-bottom: var(--sp-3); border: 1px solid var(--color-border); border-radius: var(--radius-sm);
    padding: var(--sp-2); background: var(--color-surface); box-shadow: var(--shadow-sm);
  }
  table.data-table.stack-on-mobile td { border: none; padding: var(--sp-1) var(--sp-2); }
  table.data-table.stack-on-mobile td::before { content: attr(data-label); display: block; font-size: 11px; color: var(--color-text-dim); text-transform: uppercase; font-weight: 700; }

  /* Action-button cells — consistently data-label="" across every page
     (Invoices, Clients, admin lists, etc.). Several of them set an
     inline style="white-space:nowrap" for the desktop table view,
     which otherwise always beats a class-based CSS rule regardless of
     the block/stacking rules above — that's what was forcing a whole
     row of buttons wider than the phone screen instead of letting them
     wrap. !important here is deliberate and narrowly scoped: it exists
     specifically to override that inline style, only inside this one
     mobile breakpoint, only for actions cells. */
  table.data-table.stack-on-mobile td[data-label=""] {
    display: flex !important;
    flex-wrap: wrap;
    gap: var(--sp-2);
    white-space: normal !important;
    margin-top: var(--sp-2);
  }
}

/* ── Badges — tinted pills ───────────────────────────────── */
.badge { display: inline-block; padding: 4px 11px; border-radius: 999px; font-size: 12px; font-weight: 700; text-transform: capitalize; }
.badge.draft   { background: #F1EFEA; color: #5C5647; }
.badge.sent    { background: var(--color-info-bg); color: var(--color-primary); }
.badge.viewed  { background: #F4EBFF; color: #7C3AED; }
.badge.paid    { background: var(--color-success-bg); color: var(--color-success); }
.badge.overdue { background: var(--color-danger-bg); color: var(--color-danger); }
.badge.open    { background: var(--color-info-bg); color: var(--color-primary); }
.badge.in_progress { background: var(--color-warn-bg); color: var(--color-warn); }
.badge.resolved, .badge.closed { background: var(--color-success-bg); color: var(--color-success); }

/* ── Toast ───────────────────────────────────────────────── */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--bottomnav-h) + var(--sp-3));
  transform: translateX(-50%); background: var(--color-text); color: #fff;
  padding: 12px 22px; border-radius: 999px; font-size: 14px; font-weight: 600;
  box-shadow: var(--shadow-md); z-index: 1000; max-width: 90vw; text-align: center;
}
.toast.error { background: var(--color-danger); }
.toast.success { background: var(--color-success); }

/* ── Modal ───────────────────────────────────────────────── */
/* Restructured so the action buttons (Cancel/Save) are ALWAYS visible,
   pinned to the bottom of the modal, instead of scrolling away with the
   fields — on a long form (the template editor especially, ~20 toggles)
   the old version meant scrolling all the way down just to find Save. */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(28,21,51,0.55); backdrop-filter: blur(2px);
  display: flex; align-items: flex-end; justify-content: center; z-index: 900;
}
@media (min-width: 640px) { .modal-overlay { align-items: center; } }
.modal-box {
  background: var(--color-surface); border-radius: var(--radius) var(--radius) 0 0;
  width: 100%; max-width: 560px; max-height: 90vh;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-md);
}
@media (min-width: 640px) { .modal-box { border-radius: var(--radius); } }
.modal-box > h2 { padding: var(--sp-5) var(--sp-5) 0; margin: 0 0 var(--sp-2); flex-shrink: 0; }
.modal-box > form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
/* min-height: 0 above is required — without it, a flex child ignores its
   parent's height constraint and grows to fit its content instead of
   scrolling, which would silently undo the whole fix. */
.modal-fields { overflow-y: auto; padding: 0 var(--sp-5); flex: 1; min-height: 0; }
.modal-actions {
  display: flex; gap: var(--sp-2);
  padding: var(--sp-4) var(--sp-5);
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
}

/* ── Empty state ─────────────────────────────────────────── */
.empty-state { text-align: center; padding: var(--sp-6) var(--sp-4); color: var(--color-text-dim); }
.empty-state h3 { color: var(--color-text); margin-bottom: var(--sp-2); }
