/* CSS Variables / Theme */
:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #16213e;
  --bg-card: #1e1e35;
  --border: #2a2a4a;
  --border-light: #333355;
  --text-primary: #e8e8f0;
  --text-secondary: #9090b0;
  --text-muted: #606080;
  --accent: #6366f1;
  --accent-hover: #5558e8;
  --accent-light: rgba(99, 102, 241, 0.15);
  --success: #22c55e;
  --success-light: rgba(34, 197, 94, 0.15);
  --warning: #f59e0b;
  --warning-light: rgba(245, 158, 11, 0.15);
  --danger: #ef4444;
  --danger-light: rgba(239, 68, 68, 0.15);
  --info: #3b82f6;
  --info-light: rgba(59, 130, 246, 0.15);
  --sidebar-width: 240px;
  --radius: 8px;
  --radius-sm: 4px;
  --radius-lg: 12px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

/* Reset and base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 16px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Inter', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
a:hover { opacity: 0.8; }

code {
  font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  background: var(--bg-secondary);
  padding: 0.15em 0.4em;
  border-radius: var(--radius-sm);
  font-size: 0.875em;
  color: #a78bfa;
}

/* App layout */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-brand {
  padding: 1.5rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.brand-link {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--accent);
  text-decoration: none;
  letter-spacing: -0.02em;
}

.nav-menu {
  list-style: none;
  padding: 1rem 0;
  flex: 1;
}

.nav-menu li { margin: 0.15rem 0.75rem; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.15s;
}

.nav-item:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  opacity: 1;
}

.nav-item.active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
}

.nav-icon { font-size: 1rem; flex-shrink: 0; }

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}

/* Main content */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  padding: 2rem;
  max-width: calc(100vw - var(--sidebar-width));
}

/* Page header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.page-title {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.page-actions { display: flex; align-items: center; gap: 0.75rem; }

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
}

.card-action {
  font-size: 0.85rem;
  color: var(--accent);
  text-decoration: none;
}

.card-body { padding: 1.5rem; }

/* Stats grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  transition: border-color 0.2s;
}

.stat-card:hover { border-color: var(--border-light); }
.stat-card.success { border-left: 3px solid var(--success); }
.stat-card.accent { border-left: 3px solid var(--accent); }
.stat-card.warning { border-left: 3px solid var(--warning); }

.stat-card-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 500;
  margin-bottom: 0.5rem;
}

.stat-card-value {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.stat-card-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

/* Two column grid */
.two-col-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}

/* Tables */
.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th {
  text-align: left;
  padding: 0.85rem 1.25rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
}

.data-table td {
  padding: 1rem 1.25rem;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.data-table tr:last-child td { border-bottom: none; }

.data-table tbody tr { transition: background 0.15s; }
.data-table tbody tr:hover { background: var(--bg-secondary); cursor: pointer; }

/* Status badges */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.badge-failed { background: var(--danger-light); color: var(--danger); }
.badge-retrying { background: var(--warning-light); color: var(--warning); }
.badge-recovered { background: var(--success-light); color: var(--success); }
.badge-expired { background: rgba(100,100,120,0.2); color: var(--text-muted); }
.badge-active { background: var(--success-light); color: var(--success); }
.badge-past_due { background: var(--warning-light); color: var(--warning); }
.badge-cancelled { background: rgba(100,100,120,0.2); color: var(--text-muted); }

/* Filter tabs */
.filter-tabs {
  display: flex;
  gap: 0.25rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.25rem;
}

.filter-tab {
  padding: 0.4rem 0.85rem;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
}

.filter-tab:hover { color: var(--text-primary); }
.filter-tab.active { background: var(--accent); color: white; font-weight: 600; }

/* Forms */
.form-group { margin-bottom: 1.25rem; }
.form-row { display: flex; gap: 1rem; }
.form-row .form-group { flex: 1; }

.form-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.4rem;
}

.form-input, .form-textarea, .form-select {
  width: 100%;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  padding: 0.6rem 0.85rem;
  font-size: 0.9rem;
  font-family: inherit;
  transition: border-color 0.15s;
  outline: none;
}

.form-input:focus, .form-textarea:focus, .form-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

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

.form-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  padding-top: 0.75rem;
}

/* Buttons */
.btn-primary {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.btn-primary:hover { background: var(--accent-hover); opacity: 1; transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }

.btn-secondary {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.btn-secondary:hover { border-color: var(--text-secondary); color: var(--text-primary); opacity: 1; }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
  padding: 0.4rem 0.85rem;
  border-radius: var(--radius);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-danger:hover { background: var(--danger-light); }

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

/* Alerts */
.alert {
  padding: 0.85rem 1.1rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  line-height: 1.5;
}

.alert-warning { background: var(--warning-light); color: var(--warning); border: 1px solid rgba(245,158,11,0.3); }
.alert-success { background: var(--success-light); color: var(--success); border: 1px solid rgba(34,197,94,0.3); }
.alert-danger { background: var(--danger-light); color: var(--danger); border: 1px solid rgba(239,68,68,0.3); }
.alert-info { background: var(--info-light); color: var(--info); border: 1px solid rgba(59,130,246,0.3); }

/* Search input */
.search-input {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  padding: 0.55rem 0.85rem;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s;
}

.search-input:focus { border-color: var(--accent); }

/* Modal */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal-large { max-width: 720px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.modal-title { font-size: 1.1rem; font-weight: 600; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  transition: color 0.15s;
}

.modal-close:hover { color: var(--text-primary); }

.modal-body { padding: 1.5rem; }

/* Loading and empty states */
.loading-state {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.empty-state {
  text-align: center;
  padding: 3rem;
  color: var(--text-muted);
}

.empty-state p { font-size: 0.9rem; }

/* Template cards */
.template-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 1rem;
  overflow: hidden;
}

.template-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.template-card-body { padding: 1.25rem 1.5rem; }

.template-meta {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
}

.template-meta span { color: var(--text-secondary); }
.template-meta strong { color: var(--text-primary); }

.template-preview {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 1rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
  white-space: pre-wrap;
  max-height: 150px;
  overflow: hidden;
  font-family: inherit;
  line-height: 1.6;
  position: relative;
}

.template-preview::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 40px;
  background: linear-gradient(transparent, var(--bg-secondary));
}

/* Retry schedule */
.retry-schedule { display: flex; flex-direction: column; gap: 1rem; }

.retry-step {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.retry-step-num {
  width: 32px; height: 32px;
  background: var(--accent-light);
  color: var(--accent);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.9rem;
  flex-shrink: 0;
}

.retry-step-label { font-weight: 600; margin-bottom: 0.2rem; }
.retry-step-desc { font-size: 0.85rem; color: var(--text-secondary); }

/* Utility */
.text-muted { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }
.text-danger { color: var(--danger); }
.text-accent { color: var(--accent); }

.amount { font-weight: 700; font-variant-numeric: tabular-nums; }
.monospace { font-family: 'JetBrains Mono', 'Fira Code', monospace; font-size: 0.85em; }

.mini-list { list-style: none; }
.mini-list li {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.mini-list li:last-child { border-bottom: none; }

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.detail-item label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.25rem;
}

.detail-item span {
  font-size: 0.9rem;
  font-weight: 500;
}

/* Responsive */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    z-index: 200;
    transition: transform 0.3s;
  }
  .main-content { margin-left: 0; padding: 1rem; }
  .two-col-grid { grid-template-columns: 1fr; }
  .detail-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .page-header { flex-direction: column; align-items: flex-start; }
  .form-row { flex-direction: column; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
