:root {
  --navy: #0a1f44;
  --navy-light: #16306b;
  --gold: #c9a227;
  --gold-light: #e6c25a;
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --green: #16a34a;
  --red: #dc2626;
  --amber: #d97706;
  --warn: #ea580c;
  --blue: #2563eb;
  --shadow: 0 6px 20px rgba(10, 31, 68, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* Header */
.app-header {
  background: linear-gradient(135deg, var(--navy) 0%, var(--navy-light) 100%);
  color: #fff;
  padding: 18px 24px;
  box-shadow: var(--shadow);
}
.header-grid {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 18px;
  max-width: 1280px;
  margin: 0 auto;
}
.school-logo { width: 68px; height: 68px; object-fit: contain; }
.school-title { text-align: center; }
.school-title h1 { margin: 0; font-size: 1.6rem; letter-spacing: 0.5px; }
.school-title p { margin: 2px 0 0; color: var(--gold-light); font-size: 0.9rem; letter-spacing: 1px; text-transform: uppercase; }
.header-class {
  background: rgba(255,255,255,0.12);
  padding: 8px 14px;
  border-radius: 999px;
  font-weight: 600;
  border: 1px solid rgba(255,255,255,0.2);
  font-size: 0.95rem;
}

/* Container */
.container { max-width: 1280px; margin: 24px auto; padding: 0 20px; }

/* Toolbar */
.toolbar {
  background: var(--card);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  margin-bottom: 18px;
}
.toolbar .group {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.toolbar label { font-size: 0.85rem; color: var(--muted); margin-right: 4px; }

select, input[type="text"], input[type="date"], input[type="search"] {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
select:focus, input:focus { border-color: var(--navy-light); box-shadow: 0 0 0 3px rgba(22,48,107,0.12); }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 14px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  transition: transform .05s, opacity .15s, background .15s;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--navy-light); }
.btn-gold { background: var(--gold); color: var(--navy); }
.btn-gold:hover { background: var(--gold-light); }
.btn-ghost { background: #eef1f6; color: var(--navy); }
.btn-ghost:hover { background: #e2e7ef; }
.btn-danger { background: var(--red); color: #fff; }
.btn-success { background: var(--green); color: #fff; }
.btn-warn { background: var(--warn); color: #fff; }
.btn-warn:hover { background: #c2410c; }
.btn-sm { padding: 6px 10px; font-size: 0.82rem; }

/* Search results dropdown */
.search-group { position: relative; }
.search-results {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0; right: 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 30px rgba(10,31,68,0.15);
  max-height: 320px;
  overflow-y: auto;
  z-index: 40;
}
.search-results.open { display: block; }
.search-item {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "name actions" "meta actions";
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  gap: 2px 12px;
  align-items: center;
}
.search-item:last-child { border-bottom: none; }
.search-item:hover { background: #f8fafc; }
.si-name { grid-area: name; font-weight: 600; color: var(--navy); }
.si-meta { grid-area: meta; font-size: 0.82rem; color: var(--muted); }
.si-actions { grid-area: actions; display: flex; gap: 6px; }
.search-empty { padding: 14px; color: var(--muted); text-align: center; font-size: 0.9rem; }

/* Table card */
.table-card {
  background: var(--card);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.table-head-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 18px; border-bottom: 1px solid var(--border); flex-wrap: wrap; gap: 10px;
}
.table-head-row h2 { margin: 0; font-size: 1.1rem; color: var(--navy); }
.stats { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.88rem; }
.stat { padding: 4px 10px; border-radius: 999px; background: #eef1f6; color: var(--navy); font-weight: 600; }
.stat.p { background: #dcfce7; color: #166534; }
.stat.a { background: #fee2e2; color: #991b1b; }
.stat.l { background: #fef3c7; color: #92400e; }
.stat.lv { background: #dbeafe; color: #1e40af; }

.table-wrap { overflow-x: auto; }
table.attendance {
  width: 100%; border-collapse: collapse; min-width: 720px;
}
table.attendance th, table.attendance td {
  padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 0.93rem;
}
table.attendance th {
  background: #f8fafc; color: var(--navy); font-weight: 700; text-transform: uppercase; letter-spacing: 0.4px; font-size: 0.78rem;
}
table.attendance tr:hover td { background: #fafbff; }

/* Comments column — wider, soft tint */
.comments-col { min-width: 240px; width: 32%; color: #374151; font-style: italic; }
@media (min-width: 1024px) {
  .comments-col { min-width: 320px; }
}

/* Late column highlight */
.late-col { white-space: nowrap; color: var(--amber); font-weight: 600; }

.status-pills { display: inline-flex; gap: 4px; }
.pill {
  min-width: 72px; height: 32px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border);
  display: inline-flex; align-items: center; justify-content: center;
  background: #fff; cursor: pointer; font-weight: 700; font-size: 0.82rem; color: var(--muted);
  transition: all .15s;
}
.pill:hover { transform: scale(1.05); }
.pill.active.p { background: var(--green); color: #fff; border-color: var(--green); }
.pill.active.a { background: var(--red); color: #fff; border-color: var(--red); }
.pill.active.l { background: var(--amber); color: #fff; border-color: var(--amber); }
.pill.active.lv { background: var(--blue); color: #fff; border-color: var(--blue); }

.actions-cell { white-space: nowrap; }
.actions-cell .btn { margin-right: 4px; }
.empty {
  padding: 40px 20px; text-align: center; color: var(--muted);
}

/* Signature section */
.signature-section {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 40px;
  margin-top: 40px;
  padding: 0 18px 10px;
}
.sig { text-align: center; min-width: 0; }
.signature-preview {
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.78rem;
}
.signature-preview img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.signature-preview:not(.has-signature) { border: 1px dashed var(--border); }
.signature-controls { display: flex; justify-content: center; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.signature-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.sig-line {
  border-bottom: 2px solid var(--navy);
  margin: 0 12px 8px;
  height: 8px;
}
.sig-name {
  font-weight: 700;
  color: var(--navy);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-size: 0.9rem;
}
@media (max-width: 720px) {
  .signature-section { gap: 18px; padding: 0 4px 10px; }
  .sig-line { margin: 0 4px 6px; height: 34px; }
  .sig-name { font-size: 0.75rem; }
}

/* Row flash (after global search jump) */
@keyframes flashRow {
  0%, 100% { background: transparent; }
  30% { background: #fef9c3; }
}
tr.flash td { animation: flashRow 1.6s ease-in-out; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(10,31,68,0.45);
  display: none; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal {
  background: #fff; border-radius: 14px; padding: 22px; width: 100%; max-width: 420px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}
.modal h3 { margin: 0 0 14px; color: var(--navy); }
.modal .field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.modal .field label { font-size: 0.85rem; color: var(--muted); }
.modal .field input, .modal .field select { width: 100%; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 10px 18px; border-radius: 999px;
  box-shadow: var(--shadow); z-index: 100; opacity: 0; transition: opacity .25s;
  font-size: 0.9rem;
}
.toast.show { opacity: 1; }

/* Responsive */
@media (max-width: 720px) {
  .header-grid { grid-template-columns: auto 1fr; }
  .header-class { grid-column: 1 / -1; justify-self: start; }
  .school-title h1 { font-size: 1.2rem; }
  .school-title p { font-size: 0.75rem; }
  .school-logo { width: 52px; height: 52px; }
  .toolbar { padding: 12px; }
  table.attendance th, table.attendance td { padding: 10px 8px; font-size: 0.85rem; }
}

/* Print */
@media print {
  html, body { overflow: visible !important; }
  body { background: #fff; }
  .toolbar, .actions-cell, .app-header .header-class, .no-print, .search-results { display: none !important; }
  .container { margin: 0; padding: 0; }
  .table-card { box-shadow: none; border-radius: 0; }
  .table-wrap { overflow: visible !important; }
  table.attendance { min-width: 0; table-layout: fixed; }
  .app-header { background: #fff !important; color: var(--navy) !important; box-shadow: none; border-bottom: 2px solid var(--navy); }
  .school-title h1, .school-title p { color: var(--navy) !important; }
  .school-title p { color: #555 !important; }
  table.attendance th { background: #f1f1f1 !important; color: #000 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .status-pills { display: block; }
  .pill { min-width: 0; width: auto; height: auto; padding: 0; border: 0; color: #000 !important; background: transparent !important; }
  .pill.active.p, .pill.active.a, .pill.active.l, .pill.active.lv { background: #fff !important; color: #000 !important; font-weight: 800; }
  .pill:not(.active) { display: none; }
  .late-col { color: #000 !important; }
  .comments-col { font-style: normal; color: #000 !important; }
  @page { size: A4; margin: 14mm; }
  .print-meta { display: block !important; padding: 10px 18px; color: #333; font-size: 0.9rem; border-bottom: 1px solid #ddd; }

  /* Filtered prints: hide the Late Arrival column when printing Present-only or Absent-only */
  body.print-hide-late table.attendance th.late-col,
  body.print-hide-late table.attendance td.late-col { display: none !important; }
}
.print-meta { display: none; }

/* ===== Class column: hidden in single-class mode, shown in All-Classes mode ===== */
.class-col { display: none; }
body.all-mode .class-col { display: table-cell; }

/* Read-only status badge for All-Classes view */
.status-badge {
  display: inline-block;
  min-width: 32px;
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.85rem;
  text-align: center;
  color: #fff;
}
.status-badge.P { background: var(--green, #16a34a); }
.status-badge.A { background: var(--red, #dc2626); }
.status-badge.L { background: var(--gold, #d4a017); color: #1a1a1a; }
.status-badge.LV { background: var(--blue, #2563eb); }
.muted { color: #999; font-size: 0.8rem; font-style: italic; }

@media print {
  .status-badge { color: #000 !important; background: #fff !important; border: 1px solid #000; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .signature-section { margin-top: 30px; page-break-inside: avoid; }
  .signature-preview:not(.has-signature) { visibility: hidden; border: 0; }
  .sig-line { border-bottom: 1.5px solid #000 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .sig-name { color: #000 !important; }
}

/* Image export: show only Student Name and Status columns */
body.image-only-cols table.attendance th:nth-child(1),
body.image-only-cols table.attendance td:nth-child(1),
body.image-only-cols table.attendance th.class-col,
body.image-only-cols table.attendance td.class-col,
body.image-only-cols table.attendance th.late-col,
body.image-only-cols table.attendance td.late-col,
body.image-only-cols table.attendance th.comments-col,
body.image-only-cols table.attendance td.comments-col,
body.image-only-cols table.attendance th.actions-cell,
body.image-only-cols table.attendance td.actions-cell { display: none !important; }
