:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --primary: #2563eb;
  --primary-d: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
  --amber: #d97706;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --radius: 10px;
  --topbar-h: 52px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: 'Segoe UI', Tahoma, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
button { font-family: inherit; }

/* ===================== Thanh trên (mobile) ===================== */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--topbar-h);
  background: #0f172a; color: #fff; display: flex; align-items: center; gap: 10px;
  padding: 0 12px; z-index: 40;
}
.topbar #menu-toggle { background: none; border: none; color: #fff; font-size: 24px; cursor: pointer; padding: 4px 8px; }
.topbar-title { font-size: 16px; font-weight: 600; }

.nav-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 44; }

/* ===================== Bố cục ===================== */
#app { display: block; padding-top: var(--topbar-h); }

.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0; width: 230px; z-index: 45;
  background: #0f172a; color: #cbd5e1; display: flex; flex-direction: column;
  transform: translateX(-100%); transition: transform .22s ease;
}
.sidebar.open { transform: translateX(0); box-shadow: 4px 0 24px rgba(0,0,0,.3); }
.brand { font-size: 20px; font-weight: 700; color: #fff; padding: 18px; }
.brand span { font-size: 16px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; padding: 6px; flex: 1; overflow-y: auto; }
.sidebar nav button {
  text-align: left; background: none; border: none; color: #cbd5e1;
  padding: 13px 14px; border-radius: 8px; cursor: pointer; font-size: 15px;
}
.sidebar nav button:hover { background: #1e293b; color: #fff; }
.sidebar nav button.active { background: var(--primary); color: #fff; }
.sidebar-foot { padding: 14px; border-top: 1px solid #1e293b; }
.link { background: none; border: none; color: #93c5fd; cursor: pointer; font-size: 14px; display: block; text-align: left; padding: 6px 0; }
.user-box { color: #cbd5e1; font-size: 14px; margin-bottom: 8px; }
.user-box b { color: #fff; display: block; }
.user-box .role { font-size: 12px; color: #94a3b8; }

#main { padding: 16px 14px 40px; max-width: 1200px; margin: 0 auto; }

h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 22px 0 10px; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ===================== Thẻ thống kê ===================== */
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
}
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.stat .value.green { color: var(--green); }
.stat .value.amber { color: var(--amber); }
.stat .value.blue { color: var(--primary); }

/* ===================== Nút ===================== */
button.btn, .btn {
  background: var(--primary); color: #fff; border: none;
  padding: 11px 16px; border-radius: 9px; cursor: pointer; font-size: 15px;
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
}
.btn:hover { background: var(--primary-d); }
.btn.ghost { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: #f3f4f6; }
.btn.danger { background: var(--red); }
.btn.green { background: var(--green); }
.btn.sm { padding: 7px 11px; font-size: 13px; border-radius: 8px; }

/* ===================== Bảng ===================== */
.table-wrap { border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: var(--card); }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: #f9fafb; font-weight: 600; font-size: 13px; color: #374151; }
tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f9fafb; }
td.right, th.right { text-align: right; }
td.center, th.center { text-align: center; }

/* ===================== Badge ===================== */
.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.green { background: #dcfce7; color: #166534; }
.badge.red { background: #fee2e2; color: #991b1b; }
.badge.amber { background: #fef3c7; color: #92400e; }
.badge.gray { background: #f3f4f6; color: #4b5563; }
.badge.blue { background: #dbeafe; color: #1e40af; }

/* ===================== Biểu mẫu ===================== */
.form-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.full { grid-column: 1 / -1; }
label { font-size: 13px; color: #374151; font-weight: 500; }
input, select, textarea {
  padding: 11px 12px; border: 1px solid var(--border); border-radius: 9px;
  font-size: 16px; /* >=16px để iOS không tự zoom */ font-family: inherit; background: #fff; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
textarea { resize: vertical; min-height: 60px; }
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar input, .toolbar select { width: auto; flex: 0 1 auto; }
.toolbar .grow, .grow { flex: 1; }

/* ===================== Hộp thoại ===================== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.5);
  display: flex; align-items: flex-start; justify-content: center; padding: 16px;
  z-index: 50; overflow: auto;
}
.modal {
  background: #fff; border-radius: 14px; width: 100%; max-width: 640px;
  padding: 20px; box-shadow: 0 20px 50px rgba(0,0,0,.25); margin: auto;
}
.modal h3 { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.hidden { display: none !important; }

/* ===================== Toast ===================== */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 12px 20px; border-radius: 9px;
  z-index: 100; font-size: 14px; max-width: 90vw; text-align: center;
}
.toast.err { background: var(--red); }

/* ===================== Phụ huynh ===================== */
.parent-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; align-items: center; }
.parent-row input:first-child { grid-column: 1 / -1; }

/* ===================== Ảnh khuôn mặt ===================== */
.name-cell { display: inline-flex; align-items: center; gap: 9px; text-decoration: none; color: var(--primary); }
.avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; border: 1px solid var(--border); flex-shrink: 0; }
.avatar-empty { display: inline-flex; align-items: center; justify-content: center; background: #e0e7ff; color: #3730a3; font-weight: 700; font-size: 16px; }
.photo-list { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; align-items: center; min-height: 24px; }
.photo-thumb { position: relative; width: 90px; height: 90px; }
.photo-thumb img { width: 90px; height: 90px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }
.photo-thumb button { position: absolute; top: -7px; right: -7px; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; background: var(--red); color: #fff; cursor: pointer; font-size: 11px; line-height: 1; padding: 0; }
.photo-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.camera-panel { margin-top: 10px; }
.camera-panel video { width: 100%; max-width: 320px; border-radius: 10px; background: #000; display: block; }
.camera-panel video.mirror { transform: scaleX(-1); }
.cam-actions { display: flex; gap: 8px; margin-top: 8px; }
.gallery { display: flex; gap: 10px; flex-wrap: wrap; margin: 4px 0 14px; }
.gallery img { width: 100px; height: 100px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); cursor: pointer; }

/* ===================== Điểm danh (thẻ) ===================== */
.att-list { display: flex; flex-direction: column; gap: 10px; }
.att-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.att-id { display: flex; align-items: center; gap: 10px; }
.att-name { flex: 1; min-width: 0; }
.att-name b { display: block; }
.face-btn { flex-shrink: 0; }
.att-controls { display: flex; flex-direction: column; gap: 8px; }
.att-btns { display: flex; gap: 6px; }
.att-btns button { flex: 1; border: 1px solid var(--border); background: #fff; padding: 11px 6px; border-radius: 9px; cursor: pointer; font-size: 14px; }
.att-btns button.on-present { background: var(--green); color: #fff; border-color: var(--green); }
.att-btns button.on-absent { background: var(--red); color: #fff; border-color: var(--red); }
.att-btns button.on-excused { background: var(--amber); color: #fff; border-color: var(--amber); }
.att-info { min-height: 16px; color: var(--green); }

/* ===================== Đăng nhập ===================== */
.login-wrap { display: flex; align-items: center; justify-content: center; min-height: 100vh; width: 100%; background: var(--bg); padding: 16px; }
.login-card { background: #fff; border: 1px solid var(--border); border-radius: 14px; padding: 28px 24px; width: 100%; max-width: 360px; box-shadow: 0 12px 40px rgba(0,0,0,.1); }
.login-card h1 { font-size: 20px; text-align: center; margin-bottom: 4px; }
.login-card p { text-align: center; color: var(--muted); margin: 0 0 20px; font-size: 13px; }
.login-card .field { margin-bottom: 12px; }
.login-card .remember-row { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); margin: 4px 0 2px; cursor: pointer; }
.login-card .remember-row input { width: auto; }
.login-card .btn { width: 100%; margin-top: 6px; padding: 13px; }
.login-err { color: var(--red); font-size: 13px; text-align: center; margin-top: 10px; min-height: 18px; }

/* ===================== Trình xem ảnh toàn màn hình ===================== */
.img-viewer { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 200; display: flex; align-items: center; justify-content: center; }
.img-viewer .iv-img { max-width: 96vw; max-height: 90vh; object-fit: contain; border-radius: 6px; box-shadow: 0 8px 40px rgba(0,0,0,.5); }
.iv-close { position: fixed; top: 14px; right: 16px; width: 48px; height: 48px; border-radius: 50%; border: none; background: rgba(255,255,255,.18); color: #fff; font-size: 22px; cursor: pointer; z-index: 2; }
.iv-close:hover { background: rgba(255,255,255,.34); }
.iv-nav { position: fixed; top: 50%; transform: translateY(-50%); width: 52px; height: 64px; border: none; background: rgba(255,255,255,.14); color: #fff; font-size: 34px; cursor: pointer; border-radius: 10px; }
.iv-nav:hover { background: rgba(255,255,255,.28); }
.iv-prev { left: 10px; }
.iv-next { right: 10px; }
.iv-cap { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); color: #fff; font-size: 14px; background: rgba(0,0,0,.45); padding: 5px 14px; border-radius: 20px; max-width: 90vw; }
.iv-count { opacity: .8; }

/* ===================== Lịch điểm danh ===================== */
.ah-summary { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; align-items: center; }
.calendar { margin-top: 4px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.cal-head { margin-bottom: 5px; }
.cal-h { text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; padding: 2px 0; }
.cal-cell { aspect-ratio: 1 / 1; display: flex; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; background: #fff; }
.cal-cell.empty { border: none; background: transparent; }
.cal-cell.p { background: var(--green); color: #fff; border-color: var(--green); font-weight: 600; }
.cal-cell.a { background: var(--red); color: #fff; border-color: var(--red); font-weight: 600; }
.cal-cell.e { background: var(--amber); color: #fff; border-color: var(--amber); font-weight: 600; }
.cal-legend { display: flex; gap: 14px; margin-top: 12px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.cal-legend span { display: inline-flex; align-items: center; gap: 6px; }
.cal-legend .dot { width: 14px; height: 14px; border-radius: 4px; display: inline-block; }
.cal-legend .dot.p { background: var(--green); }
.cal-legend .dot.a { background: var(--red); }
.cal-legend .dot.none { background: #fff; border: 1px solid var(--border); }

.empty { text-align: center; color: var(--muted); padding: 36px 16px; }
.row-actions { display: flex; gap: 6px; }
.print-header { display: none; }

/* ===================== Màn hình lớn (>= 820px) ===================== */
@media (min-width: 820px) {
  body { font-size: 14px; }
  .topbar, .nav-overlay { display: none !important; }
  #app { display: flex; padding-top: 0; min-height: 100vh; }
  .sidebar { position: static; transform: none; width: 210px; flex-shrink: 0; transition: none; }
  .brand { padding: 20px 18px; }
  .sidebar nav button { padding: 11px 14px; font-size: 14px; }
  #main { padding: 24px 28px; }
  h1 { font-size: 22px; }
  .cards { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
  .form-grid { grid-template-columns: 1fr 1fr; gap: 12px 16px; }
  .parent-row { grid-template-columns: 1.4fr 1fr 1.2fr auto; }
  .parent-row input:first-child { grid-column: auto; }
  th, td { white-space: normal; }
  input, select, textarea { font-size: 14px; padding: 8px 10px; }
  /* Điểm danh: hàng ngang trên màn rộng */
  .att-card { flex-direction: row; align-items: center; }
  .att-id { flex: 1; min-width: 220px; }
  .att-controls { flex-direction: row; align-items: center; flex: 2; }
  .att-btns { flex: 0 0 auto; }
  .att-btns button { flex: 0 0 auto; padding: 8px 14px; }
  .att-note { flex: 1; }
  .att-info { flex: 0 0 auto; min-width: 150px; text-align: right; }
}
