@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;900&display=swap');

:root {
  --ink: #173432;
  --ink-soft: #4c615e;
  --paper: #f2f6f5;
  --surface: #ffffff;
  --border: #d9e4e1;
  --accent: #087f76;
  --accent-dim: #e1f2ef;
  --accent-deep: #075d57;
  --copper: #bd7942;

  --present: #1e7a56;
  --present-bg: #e4f2ec;
  --absent: #a63a2c;
  --absent-bg: #f7e6e3;
  --late: #b9862a;
  --late-bg: #f8edda;
  --unmarked: #6b7280;
  --unmarked-bg: #eceeec;

  --radius: 6px;
  --shadow: 0 1px 3px rgba(23, 52, 50, 0.07);
  --font: 'Tajawal', 'Segoe UI', Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  direction: rtl;
  font-family: var(--font);
  background: var(--paper);
  color: var(--ink);
  min-height: 100vh;
  line-height: 1.55;
}

h1, h2, h3 { font-weight: 900; margin: 0 0 .4em; color: var(--ink); }
p { margin: 0 0 .6em; }
a { color: var(--accent); }

button {
  font-family: var(--font);
  cursor: pointer;
}

.hidden { display: none !important; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  background: var(--ink);
  color: #fff;
  padding: .65rem 1.25rem;
  border-bottom: 3px solid var(--copper);
  box-shadow: 0 2px 10px rgba(23, 52, 50, .12);
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: .65rem;
  font-weight: 900;
  font-size: 1.05rem;
}

.topbar .brand .brand-title {
  font-size: 1rem;
  font-weight: 900;
  line-height: 1.2;
}

.topbar .brand .institute-sub {
  display: block;
  font-weight: 400;
  color: #b9c4d2;
  font-size: .72rem;
  margin-top: 2px;
}

.topbar .brand .institute-logo {
  display: block;
  width: 64px;
  height: 64px;
  flex: 0 0 64px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  object-fit: contain;
}

.topbar .meta {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: .85rem;
  color: #d7dee6;
  flex-wrap: wrap;
}

.topbar .role-pill {
  background: rgba(255,255,255,.12);
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 999px;
  padding: .2rem .7rem;
  font-weight: 500;
  color: #fff;
}

.topbar .clock {
  font-variant-numeric: tabular-nums;
  color: #fff;
  font-weight: 500;
}

.topbar nav {
  display: flex;
  flex-wrap: wrap;
  min-width: 0;
  gap: .4rem;
}

.topbar nav a {
  color: #d7dee6;
  text-decoration: none;
  font-size: .85rem;
  padding: .35rem .6rem;
  border-radius: 4px;
}

.topbar nav a:hover, .topbar nav a.active {
  background: rgba(255,255,255,.16);
  color: #fff;
}

.btn-logout {
  background: transparent;
  border: 1px solid rgba(255,255,255,.35);
  color: #fff;
  border-radius: 4px;
  padding: .35rem .8rem;
  font-size: .82rem;
}

.btn-logout:hover { background: rgba(255,255,255,.12); }

/* ---------- layout ---------- */
main.container {
  max-width: 1240px;
  margin: 0 auto;
  padding: 1.25rem 1.25rem 3rem;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem;
  margin-bottom: 1.1rem;
}

.panel h2 {
  font-size: 1.05rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: .6rem;
  margin-bottom: .9rem;
}

.controls-row {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: flex-end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: .3rem;
}

.field label {
  font-size: .78rem;
  color: var(--ink-soft);
  font-weight: 500;
}

select, input[type="date"], input[type="text"], input[type="password"], textarea {
  font-family: var(--font);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .5rem .65rem;
  font-size: .9rem;
  background: #fff;
  color: var(--ink);
  min-width: 180px;
}

select:focus, input:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 4px;
  padding: .55rem 1rem;
  font-size: .88rem;
  font-weight: 500;
}

.btn:hover { background: var(--accent-deep); }
.btn.secondary { background: #fff; color: var(--accent); }
.btn.secondary:hover { background: var(--accent-dim); }
.btn.danger { background: #fff; color: var(--absent); border-color: var(--absent); }
.btn.danger:hover { background: var(--absent-bg); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- status banner ---------- */
.status-banner {
  display: flex;
  align-items: center;
  gap: .6rem;
  font-size: .85rem;
  padding: .55rem .8rem;
  border-radius: 6px;
  margin: .9rem 0;
}
.status-banner.open { background: var(--present-bg); color: var(--present); }
.status-banner.locked { background: var(--unmarked-bg); color: #4b5563; }
.status-banner.off { background: var(--late-bg); color: var(--late); }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}

thead th {
  text-align: right;
  font-size: .76rem;
  color: var(--ink-soft);
  font-weight: 700;
  border-bottom: 2px solid var(--border);
  padding: .5rem .6rem;
  white-space: nowrap;
}

tbody td {
  padding: .55rem .6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:hover { background: #fafbf9; }

.student-name { font-weight: 500; }
.boarder-tag {
  font-size: .68rem;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 0 .3rem;
  margin-inline-start: .35rem;
}

/* status segmented control */
.status-group {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.status-group button {
  border: none;
  background: #fff;
  color: var(--ink-soft);
  padding: .35rem .7rem;
  font-size: .82rem;
  font-weight: 500;
  border-inline-start: 1px solid var(--border);
}

.status-group button:first-child { border-inline-start: none; }

.status-group button.active.present { background: var(--present); color: #fff; }
.status-group button.active.absent { background: var(--absent); color: #fff; }
.status-group button.active.late { background: var(--late); color: #fff; }

.status-group button:disabled { cursor: not-allowed; opacity: .55; }

.pill {
  display: inline-block;
  font-size: .74rem;
  font-weight: 500;
  border-radius: 999px;
  padding: .15rem .6rem;
}
.pill.present { background: var(--present-bg); color: var(--present); }
.pill.absent { background: var(--absent-bg); color: var(--absent); }
.pill.late { background: var(--late-bg); color: var(--late); }
.pill.unmarked { background: var(--unmarked-bg); color: var(--unmarked); }
.pill.justified { background: var(--present-bg); color: var(--present); }
.pill.unjustified { background: var(--absent-bg); color: var(--absent); }

.muted { color: #7a8899; font-size: .78rem; }
.small { font-size: .8rem; }

/* overview cards */
.overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: .9rem;
}

.overview-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem 1rem;
  background: #fff;
  cursor: pointer;
  border-radius: 6px;
  box-shadow: var(--shadow);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.overview-card:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(23, 52, 50, .1); }
.overview-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-dim); }

.overview-card h3 { font-size: .95rem; margin-bottom: .6rem; }

.overview-stats {
  display: flex;
  gap: .8rem;
  flex-wrap: wrap;
}

.overview-stat { text-align: center; }
.overview-stat .n { font-size: 1.3rem; font-weight: 900; line-height: 1.1; }
.overview-stat .l { font-size: .68rem; color: var(--ink-soft); }
.overview-stat.present .n { color: var(--present); }
.overview-stat.absent .n { color: var(--absent); }
.overview-stat.late .n { color: var(--late); }
.overview-stat.unmarked .n { color: var(--unmarked); }

/* login page */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  border-top: 4px solid var(--accent);
  box-shadow: 0 12px 36px rgba(23,52,50,.1);
  padding: 2.2rem 2rem;
  width: 100%;
  max-width: 380px;
}

.login-hero {
  text-align: center;
  margin-bottom: 1.6rem;
}

.login-hero .badge {
  display: inline-block;
  width: 124px;
  height: 124px;
  border-radius: 0;
  background: #fff;
  margin-bottom: .8rem;
}
.login-hero .badge img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.login-hero h1 { font-size: 1.15rem; margin-bottom: .2rem; }
.login-hero .clock {
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  font-size: .85rem;
}

.login-card form { display: flex; flex-direction: column; gap: .8rem; }
.login-card input { width: 100%; min-width: 0; }
.login-error {
  background: var(--absent-bg);
  color: var(--absent);
  border-radius: 6px;
  padding: .55rem .7rem;
  font-size: .82rem;
}


/* modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(20,39,62,.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.modal {
  background: #fff;
  border-radius: 10px;
  padding: 1.3rem 1.4rem;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.modal h3 { margin-bottom: .9rem; }
.modal .field { margin-bottom: .8rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }

.toast {
  position: fixed;
  bottom: 1.2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: .6rem 1.1rem;
  border-radius: 8px;
  font-size: .85rem;
  z-index: 100;
  box-shadow: 0 6px 20px rgba(0,0,0,.2);
}
.toast.error { background: var(--absent); }
.toast.success { background: var(--present); }

/* simple management list */
.mgmt-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
@media (max-width: 760px) {
  .mgmt-grid { grid-template-columns: 1fr; }
}

.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .5rem .1rem;
  border-bottom: 1px solid var(--border);
  font-size: .88rem;
  gap: .5rem;
}
.list-row:last-child { border-bottom: none; }
.list-row .tag {
  font-size: .7rem;
  color: var(--ink-soft);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 .35rem;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: .3rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}
.tabs button {
  flex: 1 1 150px;
  min-width: 0;
  background: none;
  border: none;
  padding: .6rem 1rem;
  font-size: .88rem;
  white-space: normal;
  overflow-wrap: anywhere;
  color: var(--ink-soft);
  border-bottom: 2px solid transparent;
  font-weight: 500;
}
.tabs button.active { color: var(--ink); border-bottom-color: var(--accent); }

/* quick reason buttons in permit modal */
.quick-reasons {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  margin-top: .3rem;
}

.reason-btn {
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 6px;
  padding: .3rem .65rem;
  font-size: .8rem;
  font-family: var(--font);
}

.reason-btn:hover,
.reason-btn.active {
  background: var(--accent);
  color: #fff;
}

/* print */
@media print {
  .topbar, .controls-row, .no-print, .status-group button:not(.active), .btn { display: none !important; }
  body { background: #fff; }
  .panel { box-shadow: none; border: none; }
  .status-group button.active { border: 1px solid #000 !important; color: #000 !important; background: #fff !important; }
  main.container { max-width: 100%; padding: 0; }
}

@media (max-width: 640px) {
  .controls-row { gap: .6rem; }
  select, input[type="date"] { min-width: 140px; }
  .topbar { padding: .6rem .8rem; }
}

/* Real-time live SSE status badge */
.live-pill {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  font-size: .75rem;
  font-weight: 700;
  padding: .25rem .6rem;
  border-radius: 999px;
  background: #e6f7ed;
  color: #1b7a43;
  border: 1px solid #a3e0b9;
}
.live-pill::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #1b7a43;
  box-shadow: 0 0 0 0 rgba(27, 122, 67, 0.7);
  animation: pulse-dot 1.8s infinite;
}
.live-pill.disconnected {
  background: #fef2f2;
  color: #991b1b;
  border-color: #fca5a5;
}
.live-pill.disconnected::before {
  background: #991b1b;
  animation: none;
}
@keyframes pulse-dot {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(27, 122, 67, 0.7); }
  70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(27, 122, 67, 0); }
  100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(27, 122, 67, 0); }
}

/* QR Code Scanner Modal */
.qr-scanner-viewport {
  position: relative;
  width: 100%;
  max-width: 380px;
  height: 280px;
  margin: 0 auto 1rem;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.qr-scanner-viewport video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.qr-reticle {
  position: absolute;
  top: 15%;
  left: 15%;
  width: 70%;
  height: 70%;
  border: 2px solid rgba(255, 255, 255, 0.7);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}
.qr-scan-line {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: #10b981;
  box-shadow: 0 0 8px #10b981;
  animation: scan-vertical 2s infinite ease-in-out;
}
@keyframes scan-vertical {
  0%, 100% { top: 0%; opacity: 0.8; }
  50% { top: 100%; opacity: 1; }
}

/* Visual Analytics & Charts */
.analytics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.25rem;
  margin-top: 1rem;
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
  box-shadow: var(--shadow);
}
.chart-card h3 {
  margin: 0 0 .8rem;
  font-size: .95rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.chart-container {
  width: 100%;
  height: 220px;
  position: relative;
}
.chart-svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}
.chart-legend {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: .6rem;
  font-size: .78rem;
  flex-wrap: wrap;
}
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
}
.legend-box {
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

/* Student ID Badges Print Sheet */
.badge-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.2rem;
  margin-top: 1rem;
}
.student-badge {
  border: 2px solid #204060;
  border-radius: 8px;
  padding: 0.9rem;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  page-break-inside: avoid;
}
.badge-header {
  text-align: center;
  border-bottom: 1px solid #ddd;
  padding-bottom: .4rem;
  margin-bottom: .5rem;
}
.badge-header .rep { font-size: .68rem; font-weight: 700; color: #444; }
.badge-header .inst { font-size: .8rem; font-weight: 900; color: #204060; }
.badge-body {
  display: flex;
  gap: .8rem;
  align-items: center;
}
.badge-qr {
  width: 90px;
  height: 90px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #eee;
  padding: 4px;
  border-radius: 6px;
}
.badge-info {
  flex-grow: 1;
  font-size: .82rem;
}
.badge-info .student-name {
  font-size: 1rem;
  font-weight: 800;
  color: #14273e;
  margin-bottom: .25rem;
}
.badge-info .badge-field {
  margin-bottom: .2rem;
  color: #475569;
}
.badge-footer {
  margin-top: .5rem;
  padding-top: .3rem;
  border-top: 1px dashed #ccc;
  display: flex;
  justify-content: space-between;
  font-size: .7rem;
  color: #64748b;
}
@media print {
  .badge-grid { grid-template-columns: repeat(2, 1fr) !important; gap: .8rem !important; }
  .student-badge { border: 1.5px solid #000 !important; }
}

