:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-alt: #f9fafb;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --secondary: #6b7280;
  --secondary-hover: #4b5563;
  --success: #16a34a;
  --warning: #ea580c;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-alt: #1d2128;
    --border: #2a2e37;
    --text: #e5e7eb;
    --text-muted: #9ca3af;
    --primary: #6366f1;
    --primary-hover: #818cf8;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
}

h1 { margin: 0; font-size: 20px; font-weight: 700; }
h2 { margin: 0 0 12px; font-size: 15px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }

/* ---- App shell: sidebar + main ---- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 30;
}

.brand { font-size: 16px; font-weight: 800; padding: 4px 10px 16px; }

.nav { display: flex; flex-direction: column; gap: 4px; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
}
.nav-link svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-link:hover { background: var(--surface-alt); color: var(--text); }
.nav-link.active { background: rgba(79, 70, 229, 0.12); color: var(--primary); }

.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 8px; }
.sidebar-footer button { width: 100%; white-space: normal; }

/* Grows with the screen up to a readable width, then centers in the space
   next to the sidebar instead of hugging the left edge on wide monitors. */
.main { flex: 1; min-width: 0; max-width: 1600px; margin: 0 auto; padding: 0 32px 40px; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 0;
  margin-bottom: 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.menu-btn { display: none; padding: 6px; }
.menu-btn svg { width: 22px; height: 22px; display: block; }

.sidebar-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); z-index: 25; }

.view > h2 { margin-top: 28px; }
.view > h2:first-child { margin-top: 0; }

.inline-fields { display: flex; gap: 6px; }
.inline-fields input { flex: 1; min-width: 0; }
.inline-fields select { width: auto; }

.form-hint { grid-column: 1 / -1; margin: 0; }
.form-actions { display: flex; flex-direction: row; gap: 8px; justify-content: flex-end; }

table.compact { min-width: 0; }
.plan-label { margin-top: 3px; }

@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    left: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: inline-flex; }
  .main { padding: 0 16px 40px; }
}

button {
  cursor: pointer;
  border: none;
  border-radius: 7px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  background: var(--primary);
  color: white;
  transition: background 0.15s ease, opacity 0.15s ease;
  white-space: nowrap;
}

button:hover { background: var(--primary-hover); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

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

button.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
button.secondary:hover { background: var(--surface-alt); }

button.ghost { background: transparent; color: var(--text-muted); padding: 6px 10px; }
button.ghost:hover { background: var(--surface-alt); color: var(--text); }

.icon-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.icon-btn svg { width: 16px; height: 16px; pointer-events: none; }
a.icon-btn { text-decoration: none; }
.icon-btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--border); }
.icon-btn.danger:hover { background: var(--danger); color: white; }
.icon-btn.primary { background: var(--primary); color: white; }
.icon-btn.primary:hover { background: var(--primary-hover); }

section { margin-bottom: 28px; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 12px;
}

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.card-icon svg { width: 20px; height: 20px; }

.card-icon.active { background: rgba(22, 163, 74, 0.12); color: var(--success); }
.card-icon.expiring { background: rgba(234, 88, 12, 0.12); color: var(--warning); }
.card-icon.expired { background: rgba(220, 38, 38, 0.12); color: var(--danger); }
.card-icon.revenue { background: rgba(79, 70, 229, 0.12); color: var(--primary); }

.card-value { font-size: 22px; font-weight: 700; line-height: 1.1; }
.card-label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field label { font-size: 12px; font-weight: 600; color: var(--text-muted); }

input, select {
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
  width: 100%;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.15);
}

#add-student-form,
#plan-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  align-items: end;
}

#add-student-form .submit-field,
#plan-form .submit-field { grid-column: -2 / -1; }

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
  align-items: center;
}

.toolbar .field { flex: 1; min-width: 180px; }
.toolbar select { min-width: 160px; }

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
}

table { width: 100%; border-collapse: collapse; min-width: 860px; }

th, td {
  padding: 11px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  vertical-align: middle;
}

th {
  background: var(--surface-alt);
  font-weight: 600;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

tbody tr:hover { background: var(--surface-alt); }
tbody tr:last-child td { border-bottom: none; }

.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

.badge-ACTIVE { background: rgba(22, 163, 74, 0.14); color: var(--success); }
.badge-EXPIRING_SOON { background: rgba(234, 88, 12, 0.14); color: var(--warning); }
.badge-EXPIRED_BANNED { background: rgba(220, 38, 38, 0.14); color: var(--danger); }
.badge-REACTIVATION_PENDING { background: rgba(124, 58, 237, 0.14); color: #7c3aed; }
.badge-DEACTIVATED { background: rgba(107, 114, 128, 0.16); color: var(--text-muted); }

.actions { display: flex; gap: 6px; flex-wrap: nowrap; align-items: center; }

.muted { color: var(--text-muted); font-size: 12px; }
.hidden-note { margin: 10px 2px 0; }

.cell-main { font-weight: 600; line-height: 1.3; }
.cell-sub { font-size: 11px; color: var(--text-muted); line-height: 1.3; }

.truncate {
  display: block;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stack { display: flex; flex-direction: column; gap: 4px; }

.mini-field { display: flex; align-items: center; gap: 6px; }
.mini-field span { font-size: 10px; color: var(--text-muted); width: 34px; flex-shrink: 0; text-transform: uppercase; letter-spacing: 0.03em; }

td input[type="text"], td input[type="email"], td input[type="date"] {
  min-width: 110px;
  padding: 6px 8px;
  font-size: 12px;
}

/* Toasts */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
  max-width: calc(100vw - 32px);
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: var(--shadow);
  font-size: 13px;
  max-width: 360px;
  animation: toast-in 0.2s ease;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.loading-row td { text-align: center; padding: 30px; color: var(--text-muted); }

/* Responsive table -> stacked cards on narrow screens */
@media (max-width: 720px) {
  .table-wrap { overflow-x: visible; border: none; box-shadow: none; background: transparent; }
  table { min-width: 0; }
  thead { display: none; }
  tbody { display: flex; flex-direction: column; gap: 12px; }
  tbody tr {
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 4px 0;
  }
  tbody tr:hover { background: var(--surface); }
  td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    border-bottom: 1px dashed var(--border);
    padding: 9px 14px;
  }
  tbody tr td:last-child { border-bottom: none; }
  td::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    flex-shrink: 0;
  }
  td.actions { justify-content: flex-start; flex-wrap: wrap; }
  td.actions::before { display: none; }

  /* Two-line cells and truncated text get the full width to the right of the label */
  .cell-group { text-align: right; }
  .truncate { max-width: none; text-align: right; }
  .stack { flex: 1; }
}
