:root {
  /* QuickBooks inspired Light Theme Palette */
  --color-primary: #00b074;
  --color-primary-light: #9bfdc1;
  --color-primary-dark: #02875a;
  
  --color-surface: #ffffff;
  --color-background: #f4f5f8;
  
  --color-text-main: #333333;
  --color-text-muted: #6b7280;
  
  --color-border: #e8e8e8;
  --color-border-focus: #00b074;
  
  --color-danger: #ef4444;
  --color-warning: #f59e0b;
  --color-success: #10b981;
  --color-info: #3b82f6;

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  
  --font-sans: 'Inter', sans-serif;
  --sidebar-width: 260px;
  --header-height: 60px;
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--color-background);
  color: var(--color-text-main);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { color: var(--color-primary-dark); }

h1, h2, h3, h4, h5, h6 { font-weight: 600; color: var(--color-text-main); }

.hidden { display: none !important; }

/* typography */
.text-muted { color: var(--color-text-muted); }
.text-danger { color: var(--color-danger); }
.text-success { color: var(--color-success); }
.text-right { text-align: right; }
.text-center { text-align: center; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.2s;
  background-color: var(--color-surface);
  color: var(--color-text-main);
  border-color: var(--color-border);
}

.btn:hover { background-color: #f9fafb; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-primary {
  background-color: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
}
.btn-primary:hover { background-color: var(--color-primary-dark); }

.btn-danger {
  background-color: var(--color-danger);
  color: white;
  border-color: var(--color-danger);
}
.btn-danger:hover { background-color: #dc2626; }

.btn-icon {
  background: none;
  border: none;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
}
.btn-icon:hover { color: var(--color-primary); background: #f3f4f6; }

.btn-block { width: 100%; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--color-text-muted);
}
.form-control, input:not([type="checkbox"]):not([type="radio"]), select, textarea {
  width: 100%;
  padding: 8px 12px;
  font-size: 14px;
  font-family: inherit;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background-color: var(--color-surface);
  transition: border-color 0.2s, box-shadow 0.2s;
}
.form-control:focus, input:not([type="checkbox"]):not([type="radio"]):focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(0, 176, 116, 0.1);
}
input[type="checkbox"], input[type="radio"] {
  width: auto;
  margin: 0;
  cursor: pointer;
  accent-color: var(--color-primary);
}
.form-row {
  display: flex;
  gap: 16px;
}
.form-row .form-group { flex: 1; }

.card {
  background: var(--color-surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 24px;
  margin-bottom: 24px;
  border: 1px solid var(--color-border);
}

/* Tables */
.table-responsive { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}
th {
  font-size: 12px;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background-color: #f9fafb;
}
tr:hover td { background-color: #f9fafb; }
.td-money { text-align: right; font-variant-numeric: tabular-nums; }
th.th-money { text-align: right; }

/* Status Badges */
.badge {
  display: inline-flex;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 9999px;
}
.badge-posted { background-color: #def7ec; color: #03543f; }
.badge-void { background-color: #fde8e8; color: #9b1c1c; }
.badge-draft { background-color: #f3f4f6; color: #374151; }

/* Login */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f4f5f8 0%, #e5e7eb 100%);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  padding: 40px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
.login-header {
  text-align: center;
  margin-bottom: 32px;
}
.login-header img { width: 64px; height: 64px; margin-bottom: 16px; }
.login-header h2 { font-size: 24px; }
.login-header p { color: var(--color-text-muted); font-size: 14px; }

/* App Layout */
#app-main {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: var(--sidebar-width);
  background-color: #111827; /* Dark sidebar for contrast like some modern QB variants */
  color: white;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.3s;
}
.sidebar-header {
  padding: 20px 24px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar-header h2 { color: white; font-size: 20px; }
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 16px 0;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 24px;
  color: #9ca3af;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
}
.nav-item:hover { color: white; background: rgba(255,255,255,0.05); }
.nav-item.active {
  color: var(--color-primary-light);
  background: rgba(0, 176, 116, 0.15);
  border-right: 3px solid var(--color-primary);
}
.nav-item i { font-size: 18px; }
.nav-divider {
  height: 1px;
  background: rgba(255,255,255,0.1);
  margin: 16px 24px;
}
.sidebar-footer {
  padding: 16px 24px;
  border-top: 1px solid rgba(255,255,255,0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.user-info { display: flex; align-items: center; gap: 12px; overflow: hidden; }
.user-avatar {
  width: 32px; height: 32px;
  background: var(--color-primary);
  color: white;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 14px;
}
.user-name { font-size: 14px; font-weight: 500; white-space: nowrap; text-overflow: ellipsis; overflow: hidden; }
.user-role { font-size: 12px; color: #9ca3af; }

/* Main Layout Centering */
.app-main { display: flex; min-height: 100vh; }

.sidebar {
  width: 260px;
  background: #111827;
  color: white;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; bottom: 0; left: 0;
  z-index: 50;
  transition: transform 0.3s ease;
}

.main-content {
  flex: 1;
  margin-left: 260px;
  background-color: #f3f4f6;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.top-header {
  height: 64px;
  background: white;
  border-bottom: 1px solid var(--color-border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.top-header-inner, .content-wrapper {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  box-sizing: border-box;
}

.top-header-inner {
  height: 100%;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.content-wrapper {
  padding: 32px 24px;
  flex: 1;
}

/* Visibility Control */
.admin-only, .finance-only, .admin-or-finance { display: none; }
.role-admin .admin-only { display: flex; }
.role-finance .finance-only { display: flex; }
.role-admin .admin-or-finance, .role-finance .admin-or-finance { display: flex; }

/* Dashboard KPIs */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}
.kpi-card {
  background: white;
  padding: 24px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
  transition: transform 0.2s, box-shadow 0.2s;
}
.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}
.kpi-title { font-size: 14px; color: var(--color-text-muted); font-weight: 500; margin-bottom: 8px; }
.kpi-value { font-size: 28px; font-weight: 700; color: var(--color-text-main); font-variant-numeric: tabular-nums;}
.kpi-value.text-success { color: #10b981; }
.kpi-value.text-danger { color: #ef4444; }

.charts-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
  margin-bottom: 24px;
}
.chart-card {
  background: white;
  padding: 20px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
}
.chart-card h3 { font-size: 16px; margin-bottom: 16px; border-bottom: 1px solid var(--color-border); padding-bottom: 8px;}

/* Voucher Forms */
.voucher-form-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
}

/* Sub-tables in forms */
.lines-table { width: 100%; }
.lines-table input, .lines-table select { padding: 6px 8px; }

/* Modals */
.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(2px);
}
.modal {
  background: white;
  border-radius: var(--radius-md);
  width: 90%; max-width: 500px;
  box-shadow: var(--shadow-lg);
  max-height: 90vh;
  display: flex; flex-direction: column;
}
.modal-lg { max-width: 800px; }
.modal-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--color-border);
  display: flex; justify-content: space-between; align-items: center;
}
.modal-header h3 { font-size: 18px; margin: 0; }
.btn-close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--color-text-muted); line-height: 1;}
.modal-body { padding: 24px; overflow-y: auto; }
.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--color-border);
  display: flex; justify-content: flex-end; gap: 12px;
}

/* Toasts */
#toast-container {
  position: fixed; top: 20px; right: 20px; z-index: 9999;
  display: flex; flex-direction: column; gap: 10px;
}
.toast {
  background: white; padding: 12px 20px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md); display: flex; align-items: center; gap: 12px;
  animation: slideInRight 0.3s forwards;
  border-left: 4px solid var(--color-primary);
}
.toast.error { border-left-color: var(--color-danger); }
@keyframes slideInRight {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Loader */
#loading {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(255,255,255,0.7);
  z-index: 9999;
  display: flex; align-items: center; justify-content: center;
}
.spinner {
  width: 40px; height: 40px;
  border: 3px solid rgba(0, 176, 116, 0.2);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Print Media */
@media print {
  .sidebar, .top-header, .header-actions { display: none !important; }
  .main-content { margin: 0; padding: 0; background: white; }
  .content-wrapper { padding: 0; overflow: visible; }
  body { background: white; }
  .card { border: none; box-shadow: none; padding: 0; }
  /* Print voucher format */
  .print-voucher { max-width: 800px; margin: 0 auto; outline: 1px solid #ccc; padding: 20px; }
}

.mobile-only { display: none; }
@media (max-width: 768px) {
  .mobile-only { display: block; }
  .sidebar { position: fixed; height: 100vh; transform: translateX(-100%); z-index: 100; }
  .sidebar.open { transform: translateX(0); box-shadow: 2px 0 10px rgba(0,0,0,0.1); }
  .charts-grid { grid-template-columns: 1fr; }
  .voucher-form-grid { grid-template-columns: 1fr; }
}

/* Print Media Optimizations */
@media print {
  .sidebar, 
  .top-header, 
  .header-actions, 
  .report-tabs, 
  .report-controls,
  .no-print { 
    display: none !important; 
  }
  
  .main-content { 
    margin: 0 !important; 
    padding: 0 !important; 
    width: 100% !important;
  }
  
  .content-wrapper { 
    padding: 0 !important; 
    overflow: visible !important; 
  }
  
  body { 
    background: white !important; 
    padding: 0;
    margin: 0;
  }
  
  .card { 
    border: none !important; 
    box-shadow: none !important; 
    padding: 0 !important; 
  }
  
  .print-area {
    display: block !important;
    width: 100% !important;
  }

  table {
    border: 1px solid #eee !important;
  }
}

.no-print { display: none; } /* Default is hidden, but primarily used for @media print override */
@media screen {
  .no-print { display: flex; } /* Or block, depending on context */
}

/* Animations & Interactions */

@keyframes blink-red {
  0% { color: var(--color-danger); transform: scale(1); }
  50% { color: #fecaca; transform: scale(1.1); }
  100% { color: var(--color-danger); transform: scale(1); }
}
.blink-red {
  animation: blink-red 0.5s infinite;
  color: var(--color-danger) !important;
}

/* Report Header & Navigation */
.report-tabs {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
  border-bottom: 1px solid var(--color-border);
  padding-bottom: 8px;
}
.report-tabs {
  display: flex;
  gap: 24px;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 24px;
  padding-bottom: 2px;
}
.nav-item {
  display: flex;
  align-items: center;
  padding: 0.85rem 1.25rem;
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: 0.95rem;
  font-weight: 500;
  border-radius: var(--radius-md);
  margin-bottom: 0.25rem;
  transition: all 0.2s;
  position: relative;
  gap: 12px;
}

.nav-item span {
  flex: 1;
}

.drag-handle {
  opacity: 0;
  cursor: grab;
  font-size: 1.1rem;
  color: var(--color-text-muted);
  transition: opacity 0.2s;
}

.nav-item:hover .drag-handle {
  opacity: 0.5;
}

.drag-handle:active {
  cursor: grabbing;
}

.nav-item.dragging {
  opacity: 0.4;
  background: var(--color-bg-alt);
  border: 1px dashed var(--color-primary-light);
}

.nav-item.drag-over {
  border-top: 2px solid var(--color-primary);
}
.report-tab {
  padding: 8px 4px;
  color: var(--color-text-muted);
  text-decoration: none;
  transition: all 0.2s;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  font-size: 15px;
}
.report-tab:hover {
  color: var(--color-primary);
}
.report-tab.active {
  color: var(--color-primary);
  border-bottom-color: var(--color-primary);
}

.report-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  background: white;
  padding: 24px;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  margin-bottom: 24px;
  align-items: flex-end;
  border: 1px solid var(--color-border);
}
.report-controls .form-group {
  margin-bottom: 0;
}

.report-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--color-border);
}
.report-table th, .report-table td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}
.report-table th {
  background: var(--color-background);
  font-weight: 600;
  font-size: 13px;
  text-transform: uppercase;
  color: var(--color-text-muted);
}
.report-table tr:last-child td {
  border-bottom: none;
}
.report-table .text-right { text-align: right; }
.report-table .font-bold { font-weight: 700; }

.trash-btn {
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: all 0.2s;
  color: var(--color-text-muted);
  border: none;
  background: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.trash-btn:hover {
  background: #fee2e2;
  color: var(--color-danger);
}
.trash-btn i { font-size: 18px; }


/* --- Floating Chat Widget --- */
#chat-widget-root {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 1000;
  display: block; /* Universally shown for authenticated users */
}

.chat-trigger {
  width: 56px;
  height: 56px;
  background: var(--color-primary);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  cursor: pointer;
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.chat-trigger:hover { transform: scale(1.1); }
.chat-trigger i { font-size: 24px; }

.chat-popup {
  width: 360px;
  height: 500px;
  background: white;
  position: absolute;
  bottom: 72px;
  right: 0;
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(20px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.3s ease, opacity 0.3s ease;
  border: 1px solid var(--color-border);
}
.chat-popup.active {
  transform: translateY(0);
  opacity: 1;
  pointer-events: all;
}

.chat-popup-header {
  padding: 16px;
  background: var(--color-primary);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.chat-popup-header h4 { margin: 0; font-size: 14px; }

#chat-popup-history {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #f9fafb;
}

.chat-popup-input {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--color-border);
  background: white;
  align-items: flex-end;
}
.chat-popup-input textarea {
  flex: 1;
  resize: none;
  border: none;
  background: #f3f4f6;
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 13px;
  max-height: 80px;
}
.chat-popup-input textarea:focus { box-shadow: none; }

.btn-popup-send, .btn-popup-media {
  background: none;
  border: none;
  color: var(--color-primary);
  cursor: pointer;
  padding: 4px;
}
.btn-popup-media { color: var(--color-text-muted); }

/* Global Centering Polish */
.modal-lg { max-width: 800px; width: 95%; }
.student-form-large { display: flex; flex-direction: column; height: 100%; }
.form-sections-container { 
  display: flex; 
  flex-direction: column; 
  gap: 24px; 
  max-height: 60vh; 
  overflow-y: auto; 
  padding-right: 8px;
  margin-top: 16px;
}
.form-section {
  padding: 16px;
  background: #f9fafb;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
}
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--color-primary);
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--color-border);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
}
.checkbox-label input {
  width: auto;
}

/* Student Ledger Styles */
.ledger-table {
  border-collapse: separate;
  border-spacing: 0;
}
.ledger-table th, .ledger-table td {
  white-space: nowrap;
  padding: 8px 12px;
}
.ledger-table thead th {
  position: sticky;
  top: 0;
  z-index: 5;
  background-color: #f8f9fa;
  border-bottom: 2px solid var(--color-border);
}
.ledger-table .sticky-left {
  position: sticky;
  left: 0;
  z-index: 10;
  background-color: #ffffff;
  border-right: 1px solid var(--color-border);
}
.ledger-table thead tr:first-child th.sticky-left {
  z-index: 15;
}
.font-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
.font-bold { font-weight: 700; }
.font-semibold { font-weight: 600; }
.font-bold.text-danger { color: #dc2626; }

/* ── Sliding Drawer UI ── */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(2px);
  z-index: 1000;
  display: flex;
  justify-content: flex-end;
  opacity: 1;
  transition: opacity 0.3s ease;
}
.drawer-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
.drawer {
  width: 440px;
  max-width: 90vw;
  background: #fff;
  height: 100%;
  box-shadow: -10px 0 25px rgba(0,0,0,0.1);
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.drawer-overlay.hidden .drawer {
  transform: translateX(100%);
}
.drawer-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #f8fafc;
}
.drawer-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text);
  display: flex;
  align-items: center;
  gap: 8px;
}
.drawer-header h3 i { color: var(--color-primary); }
.drawer-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

/* Skeleton Loaders */
.skeleton {
  background: #e2e8f0;
  border-radius: 4px;
  animation: pulse 1.5s infinite ease-in-out;
}
.skeleton-text { height: 16px; margin-bottom: 8px; width: 100%; }
.skeleton-title { height: 24px; margin-bottom: 16px; width: 60%; }
.skeleton-box { height: 80px; width: 100%; border-radius: 8px; margin-bottom: 16px; }

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.5; }
  100% { opacity: 1; }
}

/* Clickable Student Row */
.clickable-student {
  cursor: pointer;
  color: var(--color-primary);
  font-weight: 500;
  transition: color 0.15s;
}
.clickable-student:hover {
  color: var(--color-primary-dark);
  text-decoration: underline;
}
