/* ═══════════════════════════════════════════════════════════════════════════
   LC-INVOICE — Modern Design System
   Glassmorphism · Gradients · Micro-interactions · Smooth animations
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
  /* Brand palette */
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --primary-light: #818cf8;
  --primary-glow: rgba(99, 102, 241, 0.4);
  --accent: #ec4899;
  --accent-glow: rgba(236, 72, 153, 0.3);
  --success: #10b981;
  --success-glow: rgba(16, 185, 129, 0.3);
  --danger: #ef4444;
  --warning: #f59e0b;
  --info: #3b82f6;

  /* Neutrals */
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-500: #6b7280;
  --gray-700: #374151;
  --gray-900: #0f172a;

  /* Glass */
  --glass-bg: rgba(255, 255, 255, 0.7);
  --glass-border: rgba(255, 255, 255, 0.3);
  --glass-shadow: 0 8px 32px rgba(31, 38, 135, 0.12);

  /* Radii */
  --radius-sm: 8px;
  --radius: 16px;
  --radius-lg: 24px;
  --radius-xl: 32px;

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow: 0 4px 16px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.12);
  --shadow-glow: 0 0 24px var(--primary-glow);

  /* Transitions */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
  --t-fast: 0.15s var(--ease);
  --t: 0.3s var(--ease);
  --t-slow: 0.5s var(--ease);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: linear-gradient(135deg, #eef2ff 0%, #faf5ff 50%, #fdf2f8 100%);
  color: var(--gray-900);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

/* ═══ ANIMATIONS ═════════════════════════════════════════════════════════════ */

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(24px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.92); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes slideInLeft {
  from { opacity: 0; transform: translateX(-30px); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 0 0 var(--primary-glow); }
  50% { box-shadow: 0 0 0 12px transparent; }
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes spin { to { transform: rotate(360deg); } }

@keyframes gradient-shift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

/* Animation utility classes */
.anim-fade-up { animation: fadeInUp 0.5s var(--ease) both; }
.anim-fade-in { animation: fadeIn 0.4s var(--ease) both; }
.anim-scale-in { animation: scaleIn 0.4s var(--ease-bounce) both; }
.anim-slide-left { animation: slideInLeft 0.4s var(--ease) both; }

/* Stagger delays */
.stagger-1 { animation-delay: 0.05s; }
.stagger-2 { animation-delay: 0.1s; }
.stagger-3 { animation-delay: 0.15s; }
.stagger-4 { animation-delay: 0.2s; }
.stagger-5 { animation-delay: 0.25s; }
.stagger-6 { animation-delay: 0.3s; }

/* ═══ SIDEBAR ═══════════════════════════════════════════════════════════════ */

.sidebar {
  width: 260px;
  background: linear-gradient(180deg, #0f172a 0%, #1e1b4b 100%);
  color: white;
  position: fixed;
  height: 100vh;
  overflow-y: auto;
  z-index: 100;
  transition: transform 0.35s var(--ease);
  border-right: 1px solid rgba(255,255,255,0.06);
}

.sidebar::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(circle at 30% 20%, rgba(99,102,241,0.15) 0%, transparent 50%),
              radial-gradient(circle at 70% 80%, rgba(236,72,153,0.1) 0%, transparent 50%);
  pointer-events: none;
}

.sidebar-header {
  padding: 28px 24px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  position: relative;
}

.sidebar-header h1 {
  font-size: 22px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-header p {
  font-size: 12px;
  color: var(--gray-500);
  margin-top: 6px;
  letter-spacing: 0.5px;
}

.nav { padding: 16px 12px; position: relative; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  color: var(--gray-300);
  font-size: 14px;
  font-weight: 500;
  transition: all var(--t);
  border-left: 3px solid transparent;
  text-decoration: none;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin-bottom: 4px;
}

.nav-item:hover {
  background: rgba(255,255,255,0.06);
  color: white;
  transform: translateX(4px);
}

.nav-item.active {
  background: linear-gradient(90deg, rgba(99,102,241,0.2) 0%, rgba(99,102,241,0.05) 100%);
  color: white;
  border-left-color: var(--primary-light);
  box-shadow: inset 0 0 20px rgba(99,102,241,0.08);
}

.nav-item svg { width: 20px; height: 20px; flex-shrink: 0; }

/* ═══ MAIN CONTENT ═══════════════════════════════════════════════════════════ */

.main-content {
  flex: 1;
  margin-left: 260px;
  padding: 32px;
  min-height: 100vh;
  max-width: 1600px;
}

/* ═══ TOPBAR ═════════════════════════════════════════════════════════════════ */

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28px;
  animation: fadeInUp 0.4s var(--ease) both;
}

.topbar h2 {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--gray-900) 0%, var(--primary-dark) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.topbar-actions { display: flex; gap: 12px; }
.topbar-title { display: flex; align-items: center; gap: 12px; }
.form-actions { display: flex; gap: 8px; margin-top: 16px; }

/* Invoice/estimate detail layout */
.detail-header-flex { display: flex; justify-content: space-between; margin-bottom: 24px; }
.invoice-totals { display: flex; justify-content: flex-end; margin-top: 16px; }
.invoice-totals-inner { min-width: 250px; }
.total-row { display: flex; justify-content: space-between; padding: 4px 0; }
.total-row-bold { font-weight: 700; border-top: 1px solid var(--gray-200); padding: 8px 0; }
.total-row-paid { color: var(--success); }
.total-row-balance { font-weight: 600; color: var(--warning); }
.integration-status { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }

.menu-btn {
  display: none;
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-sm);
  padding: 8px;
  cursor: pointer;
  color: var(--gray-700);
  backdrop-filter: blur(10px);
}

/* ═══ BUTTONS ════════════════════════════════════════════════════════════════ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--t);
  text-decoration: none;
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0; left: -100%;
  width: 100%; height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
  transition: left 0.5s var(--ease);
}

.btn:hover::before { left: 100%; }

.btn-primary {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: white;
  box-shadow: 0 4px 14px var(--primary-glow);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px var(--primary-glow);
}

.btn-primary:active { transform: translateY(0); }

.btn-secondary {
  background: var(--glass-bg);
  color: var(--gray-700);
  border: 1px solid var(--gray-200);
  backdrop-filter: blur(10px);
}

.btn-secondary:hover {
  background: var(--gray-100);
  transform: translateY(-1px);
}

.btn-sm { padding: 6px 14px; font-size: 13px; }

.btn-success {
  background: linear-gradient(135deg, var(--success) 0%, #059669 100%);
  color: white;
  box-shadow: 0 4px 14px rgba(16,185,129,0.25);
}
.btn-success:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(16,185,129,0.35); }

.btn-icon {
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: var(--radius-sm);
  padding: 6px;
  width: 32px;
  height: 32px;
  box-sizing: border-box;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: var(--danger);
  transition: all var(--t);
  display: inline-flex;
}

.btn-icon:hover {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
  transform: scale(1.05);
}
.btn-icon svg { width: 18px !important; height: 18px !important; flex-shrink: 0; }

/* ═══ CARDS ═════════════════════════════════════════════════════════════════ */

.card {
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--glass-shadow);
  margin-bottom: 20px;
  transition: all var(--t);
  animation: fadeInUp 0.5s var(--ease) both;
}

.card:hover {
  box-shadow: var(--shadow-lg);
  transform: translateY(-2px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.card-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--gray-900);
}

/* ═══ STAT CARDS ════════════════════════════════════════════════════════════ */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--glass-shadow);
  transition: all var(--t);
  position: relative;
  overflow: hidden;
  animation: fadeInUp 0.5s var(--ease) both;
}

.stat-card::after {
  content: '';
  position: absolute;
  top: 0; right: 0;
  width: 120px; height: 120px;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.15;
  pointer-events: none;
}

.stat-card:nth-child(1)::after { background: var(--primary); }
.stat-card:nth-child(2)::after { background: var(--warning); }
.stat-card:nth-child(3)::after { background: var(--accent); }
.stat-card:nth-child(4)::after { background: var(--success); }

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-lg);
}

.stat-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-700);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 32px;
  font-weight: 800;
  color: var(--gray-900);
  font-variant-numeric: tabular-nums;
}

/* ═══ TABLE ═════════════════════════════════════════════════════════════════ */

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th {
  text-align: left;
  padding: 12px 16px;
  font-size: 12px;
  font-weight: 700;
  color: var(--gray-700);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 2px solid var(--gray-200);
}

.table td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--gray-100);
  font-size: 14px;
  transition: background var(--t-fast);
}

.table tbody tr {
  transition: all var(--t-fast);
}

.table tbody tr:hover {
  background: linear-gradient(90deg, rgba(99,102,241,0.04) 0%, transparent 100%);
}

.table tbody tr:hover td {
  transform: scale(1.005);
}

/* ═══ BADGES ════════════════════════════════════════════════════════════════ */

.badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 100px;
  font-size: 12px;
  font-weight: 600;
  text-transform: capitalize;
  letter-spacing: 0.3px;
}

.badge-draft { background: var(--gray-200); color: var(--gray-700); }
.badge-sent { background: rgba(59,130,246,0.15); color: var(--info); }
.badge-paid { background: rgba(16,185,129,0.15); color: var(--success); }
.badge-overdue { background: rgba(239,68,68,0.15); color: var(--danger); }

/* ═══ FORMS ═════════════════════════════════════════════════════════════════ */

.form-group { margin-bottom: 16px; }

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 6px;
}

.form-input, .form-select, .form-textarea {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 14px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: rgba(255,255,255,0.8);
  transition: all var(--t);
  min-width: 0;
}
/* Date/time inputs have browser-intrinsic min-width that overflows grids.
   iOS Safari's native date picker has an intrinsic minimum width (~150-180px)
   that ignores standard min-width:0. We strip native appearance, force max-width,
   and clip overflow on the container to prevent card overflow on iPhone. */
.form-input[type="date"],
.form-input[type="datetime-local"],
.form-input[type="time"] {
  min-width: 0;
  width: 100%;
  max-width: 100%;
  -webkit-appearance: none;
  appearance: none;
  -webkit-min-logical-width: 0;
  min-logical-width: 0;
}

/* iOS Safari: the inner shadow-DOM control can still push wider than the input.
   Clip at the grid cell / form-group level so it never overflows the card. */
.form-row > *:has(> .form-input[type="date"]),
.form-row > *:has(> .form-input[type="datetime-local"]),
.form-row > *:has(> .form-input[type="time"]),
.form-group:has(> .form-input[type="date"]),
.form-group:has(> .form-input[type="datetime-local"]),
.form-group:has(> .form-input[type="time"]) {
  overflow: hidden;
  min-width: 0;
}

/* Fallback for browsers without :has() — clip all form-group/form-row children */
@supports not selector(:has(*)) {
  .form-row > *,
  .form-group {
    overflow: hidden;
    min-width: 0;
  }
}

.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
  background: white;
}

.form-textarea { resize: vertical; min-height: 80px; }

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.form-row > * { min-width: 0; }

/* Bottom action bar — only visible on mobile (see media query) */
.form-actions-bottom { display: none; }

/* Remove-button text label — hidden on desktop, shown on mobile */
.btn-icon-label { display: none; }

/* ═══ MODALS ═════════════════════════════════════════════════════════════════ */

.modal-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(8px);
  z-index: 200;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.modal-overlay.show {
  display: flex;
  animation: fadeIn 0.2s var(--ease) both;
}

.modal {
  background: white;
  border-radius: var(--radius-lg);
  padding: 28px;
  max-width: 500px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  animation: scaleIn 0.3s var(--ease-bounce) both;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.modal-title { font-size: 20px; font-weight: 700; }

.modal-close {
  background: var(--gray-100);
  border: none;
  border-radius: 50%;
  width: 32px; height: 32px;
  font-size: 20px;
  cursor: pointer;
  color: var(--gray-500);
  transition: all var(--t);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-close:hover {
  background: var(--danger);
  color: white;
  transform: rotate(90deg);
}

/* ═══ TOAST ══════════════════════════════════════════════════════════════════ */

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  padding: 14px 24px;
  border-radius: var(--radius-sm);
  color: white;
  font-weight: 600;
  font-size: 14px;
  box-shadow: var(--shadow-lg);
  z-index: 300;
  opacity: 0;
  transform: translateY(20px);
  transition: all var(--t);
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
  animation: float 3s ease-in-out infinite;
}

.toast.success { background: linear-gradient(135deg, var(--success) 0%, #059669 100%); }
.toast.error { background: linear-gradient(135deg, var(--danger) 0%, #dc2626 100%); }

/* ═══ LOADING ════════════════════════════════════════════════════════════════ */

.loading {
  text-align: center;
  padding: 48px;
  color: var(--gray-500);
  font-size: 14px;
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 12px;
}

/* ═══ LINE ITEMS ═════════════════════════════════════════════════════════════ */

.line-item-row { margin-bottom: 8px; }

/* ═══ RESPONSIVE UTILITY GRIDS ═══════════════════════════════════════════════ */
/* Use these classes instead of inline grid styles so they collapse on mobile.  */

/* Table-card layout is hidden by default; shown only on small mobile (≤480px) */
.table-card { display: none; }

.grid-2  { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3  { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.grid-4  { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 12px; align-items: end; }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.grid-auto-sm { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
/* Prevent grid children from overflowing */
.grid-2 > *, .grid-3 > *, .grid-4 > * { min-width: 0; }

/* Line-item editor grid (desc / qty / price / total / remove) */
.line-item-grid {
  display: grid;
  grid-template-columns: 1fr 80px 100px 100px 40px;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
/* Line-item grid with lock toggle (6 columns) */
.line-item-grid-lock {
  display: grid;
  grid-template-columns: 3fr 1fr 1fr 1fr auto auto;
  gap: 8px;
  align-items: end;
  margin-bottom: 8px;
}
/* Line-item grid without lock toggle (5 columns, wider desc) */
.line-item-grid-wide {
  display: grid;
  grid-template-columns: 3fr 1fr 1fr 1fr auto;
  gap: 8px;
  align-items: end;
  margin-bottom: 8px;
}
/* Recurring line-item grid (desc / qty / price / remove) */
.line-item-grid-4 {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr auto;
  gap: 8px;
  align-items: end;
  margin-bottom: 8px;
}

/* Detail two-column layout (client info / invoice meta) */
.detail-grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-bottom: 24px;
}

/* ═══ TABLE WRAPPER (horizontal scroll on mobile) ════════════════════════════ */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ═══ OVERLAY (mobile sidebar) ═══════════════════════════════════════════════ */

.overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  z-index: 99;
}

.overlay.show { display: block; animation: fadeIn 0.2s both; }

/* ═══ RESPONSIVE ════════════════════════════════════════════════════════════ */

/* ── Tablet (≤1024px) ──────────────────────────────────────────────────────── */
@media (max-width: 1024px) {
  .main-content { padding: 24px; }
}

/* ── Mobile (≤768px) ──────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  /* Sidebar → off-canvas drawer */
  .sidebar { transform: translateX(-100%); transition: transform 0.3s var(--ease); }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-left: 0; padding: 16px; }
  .menu-btn { display: block; }

  /* Stats */
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
  .stat-card { padding: 16px; }
  .stat-value { font-size: 24px; }

  /* Forms */
  .form-row { grid-template-columns: 1fr; }

  /* Topbar */
  .topbar { flex-wrap: wrap; gap: 12px; }
  .topbar h2 { font-size: 22px; }
  .topbar-actions { flex-wrap: wrap; gap: 8px; }

  /* Cards */
  .card { padding: 16px; }
  /* When .card wraps a table on mobile, make it transparent so individual
     row-cards stand on their own with no outer white box around them. */
  .card:has(.table) {
    background: transparent;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border: none;
    box-shadow: none;
    padding: 0;
  }

  /* Buttons — full-width on mobile for primary actions */
  .btn { padding: 10px 16px; font-size: 13px; text-align: center; justify-content: center; }
  /* Ensure button text is always centered on mobile, even inside table cells */
  .table tbody td .btn,
  .table tbody td:last-child .btn,
  .card-row .btn,
  .card .btn { text-align: center; justify-content: center; }

  /* Modals — full-width with scroll */
  .modal-overlay { padding: 12px; align-items: flex-start; overflow-y: auto; }
  .modal { max-width: 100%; padding: 20px; margin: 16px auto; }

  /* ═══ MOBILE CARD TABLES ════════════════════════════════════════════════════
     On phones (≤768px) every .table is transformed into stacked cards.
     Each <tr> becomes a card with:
       - Header:  first <td> (usually the name/number/link) — bold, prominent
       - Body:    middle <td>s rendered as label: value rows using data-label
       - Footer:  last <td> (Actions) — buttons in a row, separated by border
     Requires <td data-label="Label"> in the JS render functions.
     Tables without data-label attributes fall back to simple stacked cells. */

  /* Hide the table header on mobile — cards are self-labelling */
  .table thead { display: none; }

  /* Table becomes a vertical stack of cards */
  .table, .table tbody, .table tr, .table td { display: block; width: 100% !important; }
  .table { min-width: 0 !important; }

  /* Each row = one card — white background, separated by border + shadow + gap */
  .table tbody tr {
    background: #ffffff;
    border: 1px solid rgba(0,0,0,0.08);
    border-radius: 14px;
    padding: 0;
    margin-bottom: 16px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    animation: fadeInUp 0.4s var(--ease) both;
  }
  .table tbody tr:hover { background: #ffffff; box-shadow: 0 4px 16px rgba(0,0,0,0.10); }
  .table tbody tr:hover td { transform: none; }

  /* All <td>s are body cells by default — label: value layout */
  .table tbody td {
    padding: 10px 16px;
    border-bottom: 1px solid rgba(0,0,0,0.04);
    font-size: 14px;
    white-space: normal;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    text-align: right;
  }

  /* The data-label attribute becomes the left-side label (via ::before) */
  .table tbody td[data-label]::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 700;
    color: var(--gray-700);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    flex-shrink: 0;
    text-align: left;
  }

  /* First <td> = card header — full width, bold, no label, acts as title */
  .table tbody tr td:first-child {
    background: transparent;
    font-size: 16px;
    font-weight: 700;
    color: var(--gray-900);
    padding: 14px 16px 10px;
    border-bottom: 1px solid rgba(0,0,0,0.06);
    justify-content: flex-start;
    text-align: left;
  }
  .table tbody tr td:first-child::before { content: none; }
  .table tbody tr td:first-child a { font-weight: 700; color: var(--primary); }

  /* Last <td> = card footer — action buttons */
  .table tbody tr td:last-child {
    border-bottom: none;
    padding: 10px 16px 14px;
    background: rgba(0,0,0,0.015);
    justify-content: flex-end;
    gap: 8px;
  }
  .table tbody tr td:last-child::before { content: none; }
  .table tbody tr td:last-child .btn { padding: 8px 14px; font-size: 13px; }
  .table tbody tr td:last-child .btn-icon { width: 36px; height: 36px; }

  /* Single-cell rows (no data-label) just stack plainly */
  .table tbody td:not([data-label]):not(:first-child):not(:last-child) {
    display: block;
    text-align: left;
    padding: 10px 16px;
  }

  /* Keep .table-wrap for edge cases but don't force scroll */
  .table-wrap { overflow: visible; margin: 0; padding: 0; }

  /* Legacy .table-card system (kept for backward compat, hidden on mobile too) */
  .table-card { display: none; }

  /* Responsive grids collapse to single column */
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; gap: 12px; }
  .grid-auto { grid-template-columns: 1fr; }

  /* Line-item grids stack vertically */
  .line-item-grid {
    grid-template-columns: 1fr 80px 40px;
    grid-template-areas:
      "desc desc desc"
      "qty price total"
      "remove remove remove";
    gap: 6px;
  }
  .line-item-grid > :nth-child(1) { grid-area: desc; }
  .line-item-grid > :nth-child(2) { grid-area: qty; }
  .line-item-grid > :nth-child(3) { grid-area: price; }
  .line-item-grid > :nth-child(4) { grid-area: total; text-align: right; }
  .line-item-grid > :nth-child(5) { grid-area: remove; justify-self: stretch; }
  .line-item-grid .line-total { text-align: right; }
  /* Make remove button visible and tappable on mobile */
  .line-item-grid .btn-icon {
    width: 100%;
    height: 40px;
    border-color: var(--danger);
    color: var(--danger);
    background: rgba(220, 38, 38, 0.05);
    font-size: 13px;
    gap: 6px;
  }
  .line-item-grid .btn-icon svg { width: 18px; height: 18px; }
  .line-item-grid .btn-icon-label { display: inline; }

  .line-item-grid-lock, .line-item-grid-wide {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }
  .line-item-grid-4 {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  /* Detail two-column → single column */
  .detail-grid-2 { grid-template-columns: 1fr; gap: 16px; }

  /* Toast — position above bottom safe area */
  .toast { bottom: 16px; left: 16px; right: 16px; max-width: none; }
}

/* ── Small mobile (≤480px) ────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .stat-value { font-size: 20px; }
  .topbar h2 { font-size: 18px; }
  .card { padding: 14px; border-radius: 12px; }
  /* Topbar actions: 2-per-row grid, not full-width stacked */
  .topbar-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .topbar-actions .btn { width: 100%; justify-content: center; }
  /* If only one action, let it span both columns */
  .topbar-actions .btn:only-child { grid-column: 1 / -1; }
  /* Modal & form buttons still stack full-width */
  .modal { padding: 16px; border-radius: 12px; }
  .modal-title { font-size: 16px; }
  .form-input, .form-select, .form-textarea { font-size: 16px; /* prevent iOS zoom */ }
  /* iOS Safari date picker: force-fit into container, strip native chrome */
  .form-input[type="date"],
  .form-input[type="datetime-local"],
  .form-input[type="time"] {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    -webkit-appearance: none !important;
    appearance: none !important;
    box-sizing: border-box !important;
  }
  .form-actions { flex-direction: column; gap: 8px; }
  .form-actions .btn { width: 100%; }

  .detail-header-flex { flex-direction: column; gap: 12px; }
  .detail-header-flex > div:last-child { text-align: left; }
  .invoice-totals { justify-content: stretch; }
  .invoice-totals-inner { width: 100%; min-width: 0; }
  /* Hide topbar action buttons ONLY on form/edit pages (where bottom bar exists) */
  .topbar-actions-form { display: none !important; }
  /* Sticky bottom action bar for forms on mobile */
  .form-actions-bottom {
    display: flex !important;
    flex-direction: column;
    gap: 8px;
    position: sticky;
    bottom: 0;
    padding: 12px 0 20px;
    margin-top: 16px;
  }
  .form-actions-bottom .btn { width: 100%; justify-content: center; }
}

/* ── Language selector in sidebar ── */
.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--gray-200);
  margin-top: auto;
}
.lang-selector-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--gray-400);
  margin-bottom: 6px;
}
.lang-selector {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--gray-200);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  background: var(--white);
  color: var(--gray-700);
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}
.lang-selector:hover { border-color: var(--primary); }
.lang-selector:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,0.1); }