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

/* Calendar navigation */
.cal-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.cal-nav button {
  padding: 4px 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: white;
  color: var(--gray-600);
  font-size: 14px;
  cursor: pointer;
}
.cal-nav button:active { background: var(--gray-100); }
.cal-nav .cal-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--gray-700);
}

/* Calendar grid */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 0;
  text-align: center;
  margin-bottom: 8px;
}
.cal-header {
  font-size: 11px;
  font-weight: 600;
  color: var(--gray-400);
  padding: 2px 0 4px;
}
.cal-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 36px;
}
.cal-num {
  width: 30px;
  height: 30px;
  line-height: 30px;
  border-radius: 50%;
  font-size: 13px;
  color: var(--gray-400);
  text-align: center;
}
.cal-cell.cal-teach .cal-num {
  cursor: pointer;
  color: var(--gray-700);
  font-weight: 500;
}
.cal-cell.cal-teach:active .cal-num { opacity: 0.7; }
.cal-cell.cal-done .cal-num {
  background: #D1FAE5;
  color: #065F46;
}
.cal-cell.cal-missed .cal-num {
  background: #FEE2E2;
  color: #991B1B;
}
.cal-cell.cal-future .cal-num {
  background: transparent;
  border: 1.5px solid var(--gray-300);
  line-height: 27px;
}
.cal-cell.cal-selected .cal-num {
  background: var(--primary);
  color: white;
}
.cal-cell.cal-today .cal-num {
  text-decoration: underline;
}

/* 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: 9999;
  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;
}

/* ══════════════════════════════════════════════════════════════
   電腦版（寬度 900px 以上才生效）
   手機與平板直向完全不受影響 —— 這一段以外的規則一個字都沒改。
   底部導覽搬到左側變成選單、內容放寬；陰影改成大範圍低濃度、帶主色調。
   ══════════════════════════════════════════════════════════════ */
@media (min-width: 900px) {
  :root { --side-width: 220px; }

  body { background: #EDF1F5; }
  #app { max-width: none; margin: 0; padding-left: var(--side-width); }

  /* 底部導覽 → 左側選單 */
  #bottom-nav {
    top: 0; bottom: 0; left: 0;
    transform: none;
    width: var(--side-width); max-width: none; height: 100dvh;
    flex-direction: column; justify-content: flex-start; gap: 4px;
    padding: 0 12px;
    background: linear-gradient(180deg, #37567A 0%, #2A4059 100%);
    border-top: none;
    box-shadow: 6px 0 24px -12px rgba(30,48,70,.55);
  }
  #bottom-nav::before {
    content: "Lesson Helper";
    font-family: Georgia, "Noto Serif TC", serif;
    font-size: 19px; color: #fff; letter-spacing: .3px;
    padding: 22px 12px 18px; margin-bottom: 8px;
    border-bottom: 1px solid rgba(201,168,106,.55);
  }
  .nav-btn {
    flex: none; flex-direction: row; justify-content: flex-start; gap: 14px;
    padding: 12px 14px; border-radius: 10px; border-left: 3px solid transparent;
    color: rgba(255,255,255,.68);
  }
  .nav-btn:hover { background: rgba(255,255,255,.07); color: #fff; }
  .nav-btn.active { background: rgba(255,255,255,.14); color: #fff; border-left-color: #C9A86A; }
  .nav-icon { font-size: 18px; width: 22px; text-align: center; }
  .nav-label { font-size: 15px; }

  #header { padding: 14px 28px; box-shadow: 0 6px 18px -12px rgba(30,48,70,.6); }
  #main { padding: 24px 28px 40px; }
  #main > .page { max-width: 840px; margin: 0 auto; }
  #main > #page-attendance { max-width: 980px; }

  .card {
    border-radius: 16px;
    box-shadow: 0 1px 2px rgba(46,70,96,.05), 0 12px 28px -18px rgba(46,70,96,.45);
  }

  /* 點名頁：左日曆、右名單 */
  #page-attendance.active {
    display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 20px; align-items: start;
  }
  #att-date-selector {
    position: sticky; top: 0;
    background: #fff; border-radius: 16px; padding: 16px;
    box-shadow: 0 1px 2px rgba(46,70,96,.05), 0 12px 28px -18px rgba(46,70,96,.45);
  }

  .toast { bottom: 32px; left: calc(50% + var(--side-width) / 2); }
  .report-page { max-width: 760px; }
}
