/* ================= Reset & Base ================= */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --color-bg: #f5f6fb;
  --color-surface: #ffffff;
  --color-border: #e2e6ef;
  --color-text: #14172c;
  --color-text-muted: #5f6580;
  --color-primary: #3454d1;
  --color-primary-dark: #26389e;
  --color-primary-light: #eaefff;
  --color-success: #1a9e63;
  --color-success-light: #e4f8ee;
  --color-danger: #d6373d;
  --color-danger-light: #fceaea;
  --color-warning: #c97a12;
  --color-warning-light: #fdf1e0;
  --color-sidebar-bg: #12152a;
  --color-sidebar-text: #9aa0bd;
  --color-sidebar-active: #1d2140;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 12px 32px rgba(12, 15, 35, 0.16);
  --ease-standard: cubic-bezier(0.2, 0.7, 0.2, 1);
  --font-body: "Inter", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
  --font-display: "Sora", "Inter", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
}

html, body { height: 100%; overflow-x: hidden; }
html { scroll-behavior: smooth; }

body {
  font-family: var(--font-body);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 14px;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  width: 100%;
}

a { color: inherit; text-decoration: none; }
ul { list-style: none; }
img { max-width: 100%; }
button { font-family: inherit; cursor: pointer; }

h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 700; letter-spacing: -0.015em; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ================= Auth (Login) Page ================= */
.auth-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-sidebar-bg) 100%);
  padding: 24px;
}

.auth-footer {
  margin-top: 20px;
  color: rgba(255, 255, 255, 0.65);
  font-size: 12px;
  text-align: center;
}

.auth-card {
  width: 100%;
  max-width: 460px;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 40px 36px;
  animation: authCardIn 480ms var(--ease-standard);
}

@keyframes authCardIn {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 28px;
}

.auth-brand .brand-logo-lg {
  width: 46px;
  height: 46px;
  object-fit: contain;
  flex-shrink: 0;
}

.auth-brand .brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
  min-width: 0;
}

.auth-brand .brand-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14.5px;
  white-space: nowrap;
}

.auth-brand .brand-sub {
  font-size: 11.5px;
  color: var(--color-text-muted);
}

.auth-card h1 { font-size: 22px; margin-bottom: 4px; }
.auth-card .subtitle { color: var(--color-text-muted); font-size: 13px; margin-bottom: 20px; }

.auth-tabs {
  display: flex;
  gap: 4px;
  background: #f0f2f7;
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 22px;
}

.auth-tab {
  flex: 1;
  padding: 8px 10px;
  border: none;
  background: transparent;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-muted);
  transition: background 0.18s var(--ease-standard), color 0.18s var(--ease-standard);
}

.auth-tab.active { background: #fff; color: var(--color-primary-dark); box-shadow: var(--shadow-sm); }

#login-form, #pin-form { animation: authFormIn 220ms var(--ease-standard); }

@keyframes authFormIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.pin-box-row { display: flex; gap: 10px; }

.pin-box {
  width: 100%;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0;
  padding: 10px 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--color-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.pin-box:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.pin-box.pin-pop { animation: pinPop 220ms var(--ease-standard); }

@keyframes pinPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); border-color: var(--color-primary); }
  100% { transform: scale(1); }
}

.auth-hint {
  margin-top: 20px;
  padding: 12px 14px;
  background: var(--color-primary-light);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: var(--color-primary-dark);
}
.auth-hint strong { display: block; margin-bottom: 4px; }
.auth-hint code { background: rgba(52, 84, 209, 0.12); padding: 1px 5px; border-radius: 4px; }

/* ================= Form Elements ================= */
.field { margin-bottom: 16px; }

.field label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--color-text);
}

.field .req { color: var(--color-danger); }

.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-family: inherit;
  background: #fff;
  color: var(--color-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.input-with-action { position: relative; display: flex; align-items: center; }

/* Leaves room for the trailing button so a long value never runs underneath it. */
.field .input-with-action input { padding-right: 42px; }

.input-action {
  position: absolute;
  right: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-text-muted);
  transition: color 0.16s var(--ease-standard), background 0.16s var(--ease-standard);
}

.input-action:hover { color: var(--color-text); background: #eef0f5; }
.input-action:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 1px; }
.input-action svg { width: 16px; height: 16px; }

/* aria-pressed is the single source of truth for which icon shows. */
.input-action .icon-eye-off { display: none; }
.input-action[aria-pressed="true"] .icon-eye { display: none; }
.input-action[aria-pressed="true"] .icon-eye-off { display: block; }

.field .error-text {
  display: none;
  color: var(--color-danger);
  font-size: 12px;
  margin-top: 5px;
}

.field.has-error input,
.field.has-error select { border-color: var(--color-danger); }
.field.has-error .error-text { display: block; }

.field-row { display: flex; gap: 14px; }
.field-row .field { flex: 1; }

/* ================= Buttons ================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 13.5px;
  font-weight: 600;
  transition: background 0.16s var(--ease-standard), border-color 0.16s var(--ease-standard),
    color 0.16s var(--ease-standard), opacity 0.16s var(--ease-standard), transform 0.1s var(--ease-standard);
}

.btn:active:not(:disabled) { transform: scale(0.97); }

.btn-block { width: 100%; }

.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-dark); }

.btn-secondary { background: #fff; color: var(--color-text); border-color: var(--color-border); }
.btn-secondary:hover { background: #f2f4f9; }

.btn-danger { background: var(--color-danger); color: #fff; }
.btn-danger:hover { background: #b7333d; }

.btn-success { background: var(--color-success); color: #fff; }
.btn-success:hover { background: #157e51; }

.btn-ghost { background: transparent; color: var(--color-text-muted); }
.btn-ghost:hover { color: var(--color-text); background: #eef0f5; }

.btn-sm { padding: 6px 12px; font-size: 12.5px; }

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.icon-btn { padding: 0; width: 30px; height: 30px; flex-shrink: 0; }
.icon-btn svg { width: 15px; height: 15px; }

.spinner {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

/* Fills the empty app shell while the session check and first render are still in flight. */
.app-loading {
  width: 100%;
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
}

.app-loading .spinner { width: 26px; height: 26px; border-width: 3px; }

/* ================= App Shell ================= */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--color-sidebar-bg);
  color: var(--color-sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  z-index: 10;
  height: 100vh;
  overflow-y: auto;
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 22px;
  color: #fff;
}

.sidebar .brand .brand-logo-wrap {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
}

.sidebar .brand .brand-logo-sm { width: 100%; height: 100%; object-fit: contain; }

.sidebar .brand .brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.sidebar .brand .brand-name { font-family: var(--font-display); font-weight: 700; font-size: 13.5px; color: #fff; }
.sidebar .brand .brand-sub { font-size: 10.5px; color: var(--color-sidebar-text); }

.sidebar nav { flex: 1; }

.sidebar .nav-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px 10px 15px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 500;
  margin-bottom: 2px;
  color: var(--color-sidebar-text);
  transition: background 0.16s var(--ease-standard), color 0.16s var(--ease-standard);
}

.sidebar .nav-link .icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  flex-shrink: 0;
}
.sidebar .nav-link .icon svg { width: 16px; height: 16px; }

.sidebar .nav-link:hover { background: var(--color-sidebar-active); color: #fff; }

.sidebar .nav-link.active { background: var(--color-sidebar-active); color: #fff; }

.sidebar .nav-link.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 8px;
  bottom: 8px;
  width: 3px;
  border-radius: 3px;
  background: var(--color-danger);
}

.sidebar .sidebar-footer {
  border-top: 1px solid rgba(255,255,255,0.08);
  padding-top: 14px;
  margin-top: 14px;
}

.main-area { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.topbar h1 { font-size: 20px; }
.topbar .page-subtitle { font-size: 12.5px; color: var(--color-text-muted); margin-top: 2px; }

.topbar .user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 6px 5px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  font: inherit;
  color: inherit;
  transition: background 0.16s var(--ease-standard), border-color 0.16s var(--ease-standard);
}

.topbar .user-chip:hover { background: #f2f4f9; border-color: var(--color-border); }
.topbar .user-chip:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 1px; }

.topbar .avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 13px;
}

.topbar .user-meta { text-align: right; }
.topbar .user-meta .name { font-weight: 600; font-size: 13px; }
.topbar .user-meta .role { font-size: 11.5px; color: var(--color-text-muted); text-transform: capitalize; }

.content { padding: 26px 28px; flex: 1; min-width: 0; animation: contentIn 420ms var(--ease-standard); }

@keyframes contentIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.app-footer {
  padding: 16px 28px;
  border-top: 1px solid var(--color-border);
  color: var(--color-text-muted);
  font-size: 12px;
  text-align: center;
}

/* ================= Cards / Stats ================= */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 18px;
  margin-bottom: 24px;
}

.stat-card {
  position: relative;
  min-width: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 18px 20px 20px;
  overflow: hidden;
}

.stat-card::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: var(--color-primary);
}

.stat-card--success::before { background: var(--color-success); }
.stat-card--warning::before { background: var(--color-warning); }
.stat-card--accent::before { background: var(--color-danger); }

.stat-card .stat-label {
  font-size: 12.5px;
  color: var(--color-text-muted);
  font-weight: 500;
  margin-bottom: 10px;
}

.stat-card .stat-value { font-family: var(--font-display); font-size: 28px; font-weight: 700; }
.stat-card .stat-hint { font-size: 12px; color: var(--color-text-muted); margin-top: 4px; }

.panel {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  margin-bottom: 22px;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--color-border);
}

.panel-header h2 { font-size: 15.5px; }
.panel-body { padding: 20px; }

.two-col { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; }

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

table { width: 100%; border-collapse: collapse; font-size: 13.2px; }

thead th {
  text-align: left;
  padding: 10px 14px;
  background: #f8f9fc;
  color: var(--color-text-muted);
  font-weight: 600;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

thead th:not(:first-child) { text-align: center; }

tbody td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

tbody td:not(:first-child) { text-align: center; }

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f9fafc; }

.cell-name { font-weight: 600; }
.cell-muted { color: var(--color-text-muted); }
.actions-cell { display: flex; justify-content: center; gap: 8px; }

.empty-row td {
  text-align: center;
  padding: 34px 14px;
  color: var(--color-text-muted);
}

/* ================= Badges ================= */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
}

.badge-success { background: var(--color-success-light); color: var(--color-success); }
.badge-danger { background: var(--color-danger-light); color: var(--color-danger); }
.badge-warning { background: var(--color-warning-light); color: var(--color-warning); }
.badge-neutral { background: #eef0f5; color: var(--color-text-muted); }
.badge-primary { background: var(--color-primary-light); color: var(--color-primary-dark); }

/* ================= Filters / Toolbar ================= */
.toolbar {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.toolbar .field { margin-bottom: 0; min-width: 170px; }
.toolbar .field.grow { flex: 1; min-width: 200px; }

/* ================= Attendance Punch Card ================= */
.punch-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
}

.punch-clock { font-family: var(--font-display); font-size: 34px; font-weight: 700; letter-spacing: 0.02em; }
.punch-date { color: var(--color-text-muted); font-size: 13px; margin-top: 2px; }

.punch-status { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.punch-actions { display: flex; gap: 12px; }

.status-dot { position: relative; width: 9px; height: 9px; border-radius: 50%; background: var(--color-text-muted); }
.status-dot.off { background: var(--color-danger); }

.status-dot.on {
  background: var(--color-success);
}

.status-dot.on::after {
  content: "";
  position: absolute;
  inset: -5px;
  border-radius: 50%;
  border: 1.5px solid var(--color-success);
  animation: statusPulse 1.8s ease-out infinite;
}

@keyframes statusPulse {
  0% { transform: scale(0.6); opacity: 0.9; }
  100% { transform: scale(1.6); opacity: 0; }
}

/* ================= Modal ================= */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(15, 17, 34, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s var(--ease-standard), visibility 0.2s var(--ease-standard);
}

.modal-overlay.open { opacity: 1; visibility: visible; pointer-events: auto; }

.modal {
  background: #fff;
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-md);
  transform: translateY(16px) scale(0.97);
  transition: transform 0.22s var(--ease-standard);
}

.modal-overlay.open .modal { transform: translateY(0) scale(1); }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--color-border);
}

.modal-header h2 { font-size: 16px; }
.modal-body { padding: 22px; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--color-border);
}

.modal-intro { color: var(--color-text-muted); font-size: 13px; line-height: 1.5; margin-bottom: 18px; }

.confirm-modal { max-width: 400px; }
.confirm-modal .modal-body p { color: var(--color-text-muted); font-size: 13.5px; line-height: 1.5; }

/* ================= Toast ================= */
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: min(440px, calc(100vw - 32px));
  padding: 12px 16px;
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  font-size: 13.5px;
  font-weight: 500;
  line-height: 1.45;
  opacity: 0;
  /* Slides down from above; the -50% must stay in every transform or it drifts off-centre. */
  transform: translate(-50%, -12px) scale(0.98);
  transition: opacity 0.22s var(--ease-standard), transform 0.22s var(--ease-standard);
  pointer-events: none;
  z-index: 200;
}

.toast.show { opacity: 1; transform: translate(-50%, 0) scale(1); }

.toast .toast-icon { display: flex; flex-shrink: 0; color: var(--color-primary); }
.toast .toast-icon svg { width: 18px; height: 18px; }
.toast.toast-success .toast-icon { color: var(--color-success); }
.toast.toast-danger .toast-icon { color: var(--color-danger); }

/* ================= Utility ================= */
.hidden { display: none !important; }
.text-muted { color: var(--color-text-muted); }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.access-denied {
  text-align: center;
  padding: 80px 20px;
  color: var(--color-text-muted);
}

/* ================= Responsive ================= */
@media (max-width: 960px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
  .app-shell { flex-direction: column; }

  .sidebar {
    width: 100%;
    height: auto;
    flex-direction: row;
    align-items: center;
    overflow-x: auto;
    overflow-y: visible;
    padding: 8px 12px;
  }

  .sidebar .brand { padding: 4px 10px 4px 0; flex-shrink: 0; }
  .sidebar .brand .brand-text { display: none; }

  .sidebar nav { display: flex; flex: 1; justify-content: center; }
  .sidebar .nav-link { padding: 8px 10px; margin-bottom: 0; }
  .sidebar .nav-link .nav-label { display: none; }
  .sidebar .nav-link.active::before { left: 4px; right: 4px; top: auto; bottom: 0; height: 2px; width: auto; }

  .sidebar .sidebar-footer {
    display: flex;
    flex: none;
    border-top: none;
    padding-top: 0;
    margin-top: 0;
    margin-left: auto;
    padding-left: 8px;
  }

  .topbar { padding: 12px 16px; flex-wrap: wrap; gap: 10px; }
  .topbar h1 { font-size: 18px; }
  .topbar .user-meta { display: none; }

  .content { padding: 16px; }
  .app-footer { padding: 14px 16px; }

  .stat-grid { grid-template-columns: 1fr; }
  .two-col { grid-template-columns: 1fr; }

  .field-row { flex-direction: column; gap: 0; }

  .toolbar { gap: 12px; }
  .toolbar .field { min-width: 0; flex-basis: 100%; }
  .toolbar .field.grow { flex-basis: 100%; }

  .punch-card { flex-direction: column; align-items: flex-start; }
  .punch-actions { width: 100%; }
  .punch-actions .btn { flex: 1; }

  .modal-body { padding: 18px; }
  .modal-header, .modal-footer { padding: 14px 18px; }
}

@media (max-width: 480px) {
  .auth-card { padding: 28px 22px; }
  .auth-brand .brand-name { white-space: normal; font-size: 13.5px; }
  .pin-box { font-size: 18px; padding: 8px 0; }
}
