:root {
  --primary: #3D5A80;
  --primary-dark: #2E4660;
  --success: #34C759;
  --success-bg: #E8F9ED;
  --danger: #FF3B30;
  --danger-bg: #FFE5E3;
  --warning: #FF9500;
  --warning-bg: #FFF3E0;
  --gray-50: #F9FAFB;
  --gray-100: #F3F4F6;
  --gray-200: #E5E7EB;
  --gray-300: #D1D5DB;
  --gray-400: #9CA3AF;
  --gray-500: #6B7280;
  --gray-600: #4B5563;
  --gray-700: #374151;
  --gray-800: #1F2937;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.15);
  --nav-height: 64px;
  --header-height: 52px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Header */
#header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--primary);
  color: white;
  padding: 14px 16px;
  height: var(--header-height);
  display: flex;
  align-items: center;
}

#header {
  justify-content: space-between;
}

#header h1 {
  font-size: 18px;
  font-weight: 600;
}

#settings-btn {
  background: none;
  border: none;
  color: white;
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
}

/* Manage page */
.manage-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.manage-form label {
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-600);
}

.manage-form input,
.manage-form select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: white;
}

.manage-form .form-row {
  display: flex;
  gap: 8px;
}

.manage-form .form-row > div {
  flex: 1;
}

.manage-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: white;
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  box-shadow: var(--shadow);
  gap: 12px;
}

.manage-item .student-info { flex: 1; }

.manage-actions {
  display: flex;
  gap: 6px;
}

.manage-actions button {
  padding: 6px 10px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.btn-edit { background: var(--gray-100); color: var(--gray-600); }
.btn-delete { background: var(--danger-bg); color: var(--danger); }

/* Main */
#main {
  flex: 1;
  padding: 12px;
  padding-bottom: calc(var(--nav-height) + 16px);
  overflow-y: auto;
}

.page { display: none; }
.page.active { display: block; }

/* Bottom Nav */
#bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  height: var(--nav-height);
  background: white;
  border-top: 1px solid var(--gray-200);
  display: flex;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--gray-400);
  font-size: 12px;
  cursor: pointer;
  transition: color 0.2s;
}

.nav-btn.active { color: var(--primary); }
.nav-icon { font-size: 22px; }
.nav-label { font-size: 11px; font-weight: 500; }

/* Cards */
.card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 12px;
}

.card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--gray-700);
  margin-bottom: 12px;
}

/* Date selector (horizontal scroll) */
.date-scroll {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 0 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.date-scroll::-webkit-scrollbar { display: none; }

.date-chip {
  flex-shrink: 0;
  min-width: 72px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--gray-200);
  background: white;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
}

.date-chip.active {
  border-color: var(--primary);
  background: #EAF0F6;
}

.date-chip.done {
  border-color: var(--success);
  background: var(--success-bg);
}

.date-chip .chip-day {
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-700);
}

.date-chip .chip-date {
  font-size: 11px;
  color: var(--gray-500);
  margin-top: 2px;
}

.date-chip .chip-badge {
  font-size: 10px;
  color: var(--success);
  font-weight: 600;
  margin-top: 2px;
}

/* Week navigation */
.week-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.week-nav button {
  padding: 6px 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: white;
  color: var(--gray-600);
  font-size: 14px;
  cursor: pointer;
}

.week-nav button:active { background: var(--gray-100); }

.week-nav .week-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--gray-600);
}

/* Student list */
.student-row {
  display: flex;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--gray-100);
  gap: 12px;
}

.student-row:last-child { border-bottom: none; }

.student-info {
  flex: 1;
}

.student-name {
  font-size: 16px;
  font-weight: 500;
}

.student-time {
  font-size: 13px;
  color: var(--gray-500);
  margin-top: 2px;
}

.student-meta {
  font-size: 12px;
  color: var(--gray-400);
  margin-top: 2px;
}

/* Toggle switch */
.toggle {
  position: relative;
  width: 52px;
  height: 32px;
  flex-shrink: 0;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--gray-300);
  border-radius: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 26px;
  height: 26px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform 0.3s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle input:checked + .toggle-slider {
  background: var(--success);
}

.toggle input:checked + .toggle-slider::before {
  transform: translateX(20px);
}

/* Status badges */
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
}

.badge-success { background: var(--success-bg); color: #15803D; }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-warning { background: var(--warning-bg); color: #B45309; }
.badge-neutral { background: var(--gray-100); color: var(--gray-500); }

/* Buttons */
.btn {
  display: block;
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn:active { transform: scale(0.98); }

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

.btn-primary:disabled {
  background: var(--gray-300);
  cursor: not-allowed;
}

.btn-success {
  background: var(--success);
  color: white;
}

.btn-outline {
  background: white;
  color: var(--primary);
  border: 2px solid var(--primary);
}

/* Attendance result view */
.att-result-header {
  text-align: center;
  padding: 16px 0;
}

.att-result-header .date-text {
  font-size: 18px;
  font-weight: 600;
  color: var(--gray-800);
}

.att-result-header .status-text {
  font-size: 14px;
  color: var(--success);
  margin-top: 4px;
}

.att-result-row {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--gray-100);
}

.att-result-row:last-child { border-bottom: none; }

.att-result-row .student-info { flex: 1; }

.att-result-row .status-icon {
  font-size: 18px;
  width: 32px;
  text-align: center;
}

/* Fee section */
.fee-group {
  margin-bottom: 16px;
}

.fee-group-title {
  font-size: 15px;
  font-weight: 700;
  color: #1F2937;
  margin-bottom: 8px;
  padding: 8px 12px;
  background: #EAF0F6;
  border-left: 3px solid #3D5A80;
  border-radius: 4px;
}

.fee-row {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: white;
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  box-shadow: var(--shadow);
  gap: 12px;
}

.fee-row .student-info { flex: 1; }

.fee-btn {
  padding: 6px 14px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.fee-btn-pay {
  background: var(--warning-bg);
  color: #B45309;
}

.fee-btn-paid {
  background: var(--success-bg);
  color: #15803D;
}

/* Records */
.rec-filter {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.rec-filter select {
  flex: 1;
  padding: 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: white;
  font-size: 14px;
  color: var(--gray-700);
}

.rec-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.rec-table th {
  background: var(--gray-100);
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-600);
  text-align: left;
}

.rec-table td {
  padding: 10px 12px;
  font-size: 14px;
  border-top: 1px solid var(--gray-100);
}

/* Summary */
.sum-stat {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--gray-100);
}

.sum-stat:last-child { border-bottom: none; }

.sum-label { color: var(--gray-500); font-size: 14px; }
.sum-value { font-weight: 600; font-size: 16px; }

.sum-total {
  display: flex;
  justify-content: space-between;
  padding: 16px;
  background: var(--primary);
  color: white;
  border-radius: var(--radius-sm);
  margin-top: 12px;
}

.sum-total .sum-label { color: rgba(255,255,255,0.8); }
.sum-total .sum-value { font-size: 20px; }

/* Month selector */
.month-selector {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.month-selector button {
  padding: 8px 14px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: white;
  font-size: 16px;
  cursor: pointer;
}

.month-selector .month-label {
  font-size: 18px;
  font-weight: 600;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + 20px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--gray-800);
  color: white;
  padding: 12px 24px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  z-index: 200;
  transition: opacity 0.3s;
  box-shadow: var(--shadow-lg);
}

.toast.hidden { opacity: 0; pointer-events: none; }

/* Loading */
.loading {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

.loading.hidden { display: none; }
#login-overlay.hidden { display: none; }

.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;
}

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

/* Color input circle fix (iOS Safari) */
input[type="color"] {
  -webkit-appearance: none;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  overflow: hidden;
}
input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}
input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 50%;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--gray-400);
}

.empty-state .empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 15px;
  line-height: 1.5;
}

/* Confirm dialog */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 250;
  padding: 24px;
}

.dialog {
  background: white;
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 320px;
  box-shadow: var(--shadow-lg);
}

.dialog h3 {
  font-size: 17px;
  margin-bottom: 8px;
}

.dialog p {
  font-size: 14px;
  color: var(--gray-500);
  margin-bottom: 20px;
  line-height: 1.5;
}

.dialog-actions {
  display: flex;
  gap: 8px;
}

.dialog-actions button {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.dialog-cancel {
  background: var(--gray-100);
  color: var(--gray-600);
}

.dialog-confirm {
  background: var(--primary);
  color: white;
}

/* Student Report Overlay */
.report-overlay {
  position: fixed;
  inset: 0;
  background: white;
  z-index: 300;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.report-page {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px;
  min-height: 100dvh;
}
