:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e6e8ee;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef0ff;
  --green: #16a34a;
  --green-soft: #e8f7ee;
  --amber: #d97706;
  --amber-soft: #fdf3e3;
  --red: #ef4444;
  --red-soft: #fdecec;
  --blue: #0ea5e9;
  --blue-soft: #e7f6fd;
  --orange: #f97316;
  --orange-soft: #fef0e6;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 4px 16px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 12px 40px rgba(16, 24, 40, 0.18);
}

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

html, body { height: 100%; }

body {
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

[hidden] { display: none !important; }

button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; font-size: 14px; }

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  transition: background .15s, border-color .15s, box-shadow .15s;
}
.btn svg { width: 16px; height: 16px; flex: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); box-shadow: 0 4px 12px rgba(79, 70, 229, 0.35); }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-ghost:hover { background: #f0f1f4; color: var(--text); }
.btn-block { width: 100%; justify-content: center; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  transition: background .15s, color .15s;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { background: var(--primary-soft); color: var(--primary); }

/* ============ Login ============ */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(60% 60% at 15% 10%, rgba(79, 70, 229, 0.14), transparent 60%),
    radial-gradient(50% 50% at 90% 90%, rgba(14, 165, 233, 0.12), transparent 60%),
    var(--bg);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 36px 32px;
}
.login-logo { text-align: center; margin-bottom: 26px; }
.logo-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff;
  box-shadow: 0 8px 20px rgba(79, 70, 229, 0.35);
}
.logo-mark svg { width: 24px; height: 24px; }
.login-logo h1 { font-size: 22px; margin-top: 12px; letter-spacing: -0.02em; }
.login-logo p { color: var(--muted); font-size: 13px; }
.login-hint {
  margin-top: 18px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

/* ============ Topbar ============ */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo-mark { width: 36px; height: 36px; border-radius: 10px; }
.brand .logo-mark svg { width: 18px; height: 18px; }
.brand-name { font-size: 18px; font-weight: 700; letter-spacing: -0.02em; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-chip { display: flex; align-items: center; gap: 10px; }
.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-transform: uppercase;
}
.user-name { font-weight: 600; color: var(--text); }
@media (max-width: 520px) {
  .user-name { display: none; }
}

/* ============ Layout ============ */
.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px;
}

/* ============ Stats ============ */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.stat-card {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.stat-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  flex: none;
}
.stat-icon svg { width: 20px; height: 20px; }
.stat-icon-blue { background: var(--blue-soft); color: var(--blue); }
.stat-icon-amber { background: var(--amber-soft); color: var(--amber); }
.stat-icon-green { background: var(--green-soft); color: var(--green); }
.stat-label { color: var(--muted); font-size: 12px; }
.stat-value { font-size: 20px; font-weight: 700; letter-spacing: -0.02em; }

/* ============ Card / Toolbar ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.toolbar-group { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.search-box {
  position: relative;
  display: flex;
  align-items: center;
}
.search-box svg {
  position: absolute;
  left: 11px;
  width: 16px;
  height: 16px;
  color: var(--muted);
  pointer-events: none;
}
.search-box input {
  width: 220px;
  padding: 9px 12px 9px 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.search-box input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
  background: #fff;
}

.select-box select {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  outline: none;
  cursor: pointer;
  transition: border-color .15s;
}
.select-box select:focus { border-color: var(--primary); }

/* ============ Table ============ */
.table-wrap { overflow-x: auto; }
.orders-table { width: 100%; border-collapse: collapse; min-width: 780px; }
.orders-table thead th {
  text-align: left;
  padding: 12px 16px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  background: #fafbfc;
  border-bottom: 1px solid var(--border);
}
.orders-table tbody td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.orders-table tbody tr:last-child td { border-bottom: none; }
.orders-table tbody tr { transition: background .15s; }
.orders-table tbody tr:hover { filter: brightness(0.97); }
.th-num, .th-actions, .th-age { width: 1%; white-space: nowrap; }
.th-actions { text-align: right; }

/* Age tinted rows */
.age-l0 { --age: var(--green); --age-bg: rgba(22, 163, 74, 0.05); }
.age-l1 { --age: var(--blue); --age-bg: rgba(14, 165, 233, 0.06); }
.age-l2 { --age: var(--amber); --age-bg: rgba(217, 119, 6, 0.07); }
.age-l3 { --age: var(--orange); --age-bg: rgba(249, 115, 22, 0.08); }
.age-l4 { --age: var(--red); --age-bg: rgba(239, 68, 68, 0.09); }
.orders-table tbody tr { background: var(--age-bg); }

.age-cell { display: inline-flex; align-items: center; justify-content: center; }
.age-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: color-mix(in srgb, var(--age) 12%, #fff);
  color: var(--age);
  white-space: nowrap;
}
.age-badge svg { width: 13px; height: 13px; }

.client-cell { display: flex; flex-direction: column; }
.client-name { font-weight: 600; }
.client-email { color: var(--muted); font-size: 12px; }

.monto { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-pending { background: var(--amber-soft); color: var(--amber); }
.badge-processing { background: var(--blue-soft); color: var(--blue); }
.badge-paid { background: var(--green-soft); color: var(--green); }
.badge-overdue { background: var(--red-soft); color: var(--red); }

.date-cell { color: var(--muted); font-size: 12.5px; white-space: nowrap; }

.actions-cell {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}
.actions-cell .icon-btn:hover { background: var(--primary-soft); color: var(--primary); }
.icon-btn.mail-btn:hover { background: var(--green-soft); color: var(--green); }

.empty-state {
  padding: 56px 24px;
  text-align: center;
  color: var(--muted);
}
.empty-state svg { width: 44px; height: 44px; margin-bottom: 10px; color: #c3c9d4; }
.empty-state p { font-size: 14px; }

/* ============ Forms ============ */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; flex: 1; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.field input, .field select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}
.form-row { display: flex; gap: 12px; }
@media (max-width: 480px) { .form-row { flex-direction: column; gap: 0; } }
.input-prefix { position: relative; display: flex; align-items: center; }
.input-prefix span {
  position: absolute;
  left: 12px;
  color: var(--muted);
  font-weight: 600;
  pointer-events: none;
}
.input-prefix input { padding-left: 28px; }
.form-error { color: var(--red); font-size: 12px; margin-top: -6px; }

/* ============ Modal ============ */
.modal-overlay, .lightbox-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 33, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
  animation: fadeIn .15s ease-out;
}
.modal {
  width: 100%;
  max-width: 520px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 22px;
  animation: popIn .18s ease-out;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.modal-head h2 { font-size: 17px; letter-spacing: -0.02em; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

/* ============ Lightbox ============ */
.lightbox {
  position: relative;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  animation: popIn .2s ease-out;
}
.lightbox-close { position: absolute; top: 10px; right: 10px; z-index: 2; }
.lightbox-head { padding: 22px 24px 0; }
.lightbox-head h2 { font-size: 17px; letter-spacing: -0.02em; }
.lightbox-body { padding: 14px 24px 24px; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.detail-item {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 12px;
}
.detail-item.wide { grid-column: 1 / -1; }
.detail-item .d-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 3px; }
.detail-item .d-value { font-weight: 600; font-size: 14px; word-break: break-word; }
.detail-item .d-value.monto { font-size: 16px; }
.detail-amount { display: flex; align-items: baseline; gap: 10px; }

/* Email preview */
.email-preview {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  margin-top: 16px;
}
.email-preview .ep-head {
  background: #fafbfc;
  padding: 10px 14px;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}
.email-preview .ep-body { padding: 14px; font-size: 13.5px; white-space: pre-wrap; }
.email-preview .ep-subject { font-weight: 600; color: var(--text); margin-top: 8px; }

.lightbox-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
  align-items: center;
}

.send-state { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.send-state .spinner {
  width: 16px; height: 16px;
  border: 2px solid rgba(79,70,229,.25);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
.send-state.success { color: var(--green); }
.send-state.error { color: var(--red); }

/* ============ Toast ============ */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2430;
  color: #fff;
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 100;
  animation: slideUp .2s ease-out;
  display: flex;
  align-items: center;
  gap: 8px;
}
.toast::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--green); }

/* ============ Animations ============ */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn {
  from { opacity: 0; transform: scale(0.96) translateY(8px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
@keyframes slideUp { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes spin { to { transform: rotate(360deg); } }
