:root {
  --johns-blue: #004590;
  --johns-blue-dark: #00336b;
  --johns-red: #e31a12;
  --surface-page: #f2f4f7;
  --surface-card: #ffffff;
  --surface-muted: #f7f8fa;
  --text-primary: #1a1a1a;
  --text-secondary: #5f5e5a;
  --text-muted: #8b8a86;
  --border: #e1e3e8;
  --radius: 8px;
  --radius-card: 12px;
  --success-bg: #eaf3de; --success-text: #2f5c11;
  --warning-bg: #faeeda; --warning-text: #8a5307;
  --danger-bg: #fcebeb; --danger-text: #8a2020;
  --accent-bg: #e6f1fb; --accent-text: #0c447c;
  --font: Calibri, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--surface-page);
  color: var(--text-primary);
  line-height: 1.5;
}

a { color: var(--johns-blue); }

/* ---------- Header ---------- */
.app-header {
  background: var(--johns-blue);
  color: #fff;
  padding: 0.6rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-header .brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-header .logo-img {
  width: 150px;
  height: auto;
  display: block;
}

.app-header .title {
  font-size: 15px;
  font-weight: bold;
  color: #fff;
}

.app-header .subtitle {
  font-size: 12px;
  color: rgba(255,255,255,0.75);
}

.app-header .user-menu {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13px;
  color: rgba(255,255,255,0.85);
}

.app-header .user-menu a {
  color: #fff;
  text-decoration: none;
  opacity: 0.9;
}

.app-header .user-menu a:hover { opacity: 1; text-decoration: underline; }

/* ---------- Layout ---------- */
.page-wrap {
  max-width: 960px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

.page-wrap.narrow { max-width: 480px; }
.page-wrap.wide { max-width: 1200px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 1rem 1.25rem;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 1.25rem;
}

.metric-card {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  text-align: center;
}

.metric-card .value { font-size: 22px; font-weight: bold; margin: 0; }
.metric-card .label { font-size: 12px; color: var(--text-secondary); margin: 2px 0 0; }
.metric-card.value-success .value { color: var(--success-text); }
.metric-card.value-warning .value { color: var(--warning-text); }
.metric-card.value-danger .value { color: var(--danger-text); }
.metric-card.value-accent .value { color: var(--accent-text); }

/* ---------- Status badges ---------- */
.badge {
  display: inline-block;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 20px;
  white-space: nowrap;
}
.badge-valid { background: var(--success-bg); color: var(--success-text); }
.badge-expiring-6 { background: var(--warning-bg); color: var(--warning-text); }
.badge-expiring-3 { background: var(--warning-bg); color: var(--warning-text); font-weight: bold; }
.badge-expired { background: var(--danger-bg); color: var(--danger-text); }
.badge-booked { background: var(--accent-bg); color: var(--accent-text); }

/* ---------- Alert banner ---------- */
.banner {
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  font-size: 13.5px;
  margin-bottom: 1.25rem;
  border: 1px solid transparent;
}
.banner-warning { background: var(--warning-bg); color: var(--warning-text); border-color: #f0d9b5; }

/* ---------- Training list (employee view) ---------- */
.section-heading { font-size: 14px; font-weight: bold; color: var(--text-secondary); margin: 1.5rem 0 0.6rem; }
.record-list { background: var(--surface-card); border: 1px solid var(--border); border-radius: var(--radius-card); overflow: hidden; }
.upcoming-list .record-row { border-left: 3px solid var(--johns-blue); }
.upcoming-list .notes { font-size: 12.5px; color: var(--text-secondary); margin: 6px 0 0; line-height: 1.5; }
.record-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.8rem 1.1rem;
  border-bottom: 1px solid var(--border);
  gap: 10px;
}
.record-row:last-child { border-bottom: none; }
.record-row .name { font-weight: bold; font-size: 14px; margin: 0; }
.record-row .meta { font-size: 12.5px; color: var(--text-secondary); margin: 2px 0 0; }
.record-row .right { display: flex; align-items: center; gap: 12px; }

.cert-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 18px;
  padding: 2px;
}
.cert-btn:hover { color: var(--johns-blue); }
.cert-btn:disabled { color: #cfcfcf; cursor: not-allowed; }

/* ---------- Forms ---------- */
label { font-size: 13px; color: var(--text-secondary); display: block; margin-bottom: 4px; }
input, select, textarea {
  width: 100%;
  font-family: var(--font);
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text-primary);
  resize: vertical;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--johns-blue); outline-offset: 1px; }

.field { margin-bottom: 12px; }

button.btn {
  font-family: var(--font);
  font-size: 14px;
  border-radius: var(--radius);
  padding: 9px 16px;
  border: 1px solid var(--border);
  background: #fff;
  cursor: pointer;
}
button.btn:hover { background: var(--surface-muted); }
button.btn-primary {
  background: var(--johns-blue);
  color: #fff;
  border-color: var(--johns-blue);
  font-weight: bold;
}
button.btn-primary:hover { background: var(--johns-blue-dark); }
button.btn-block { width: 100%; }

.error-text { color: var(--danger-text); font-size: 12.5px; margin-top: 4px; display: none; }
.error-text.show { display: block; }

/* ---------- Toolbar / filters ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.1rem;
}
.toolbar .filters { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar input, .toolbar select { width: auto; min-width: 150px; }

/* ---------- Training grid (manager view) ---------- */
/* When the whole matrix fits the screen width (script adds .fits), the box
   doesn't scroll itself, so the header row sticks to the PAGE as you scroll
   down. On narrow screens it falls back to scrolling inside the box. */
.grid-scroll.fits { overflow: visible; max-height: none; }
.grid-scroll {
  overflow: auto;
  /* Scrolls inside its own box so the training-name header row can stay
     frozen at the top while the rows move underneath it. */
  max-height: calc(100vh - 24px);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  background: var(--surface-card);
}
table.training-grid {
  border-collapse: collapse;
  width: 100%;
  font-size: 12.5px;
}
table.training-grid th, table.training-grid td {
  border-bottom: 1px solid var(--border);
  padding: 6px 6px;
  text-align: left;
  white-space: nowrap;
}
table.training-grid thead th {
  background: var(--surface-muted);
  color: var(--text-secondary);
  font-weight: bold;
  font-size: 11px;
  vertical-align: bottom;
  padding-top: 8px;
  padding-bottom: 8px;
  position: sticky;
  top: 0;
  z-index: 3;
  box-shadow: 0 2px 3px rgba(0,0,0,0.08);
}
table.training-grid .emp-col {
  position: sticky;
  left: 0;
  background: var(--surface-card);
  z-index: 1;
  box-shadow: 2px 0 4px rgba(0,0,0,0.05);
  min-width: 148px;
}
table.training-grid thead .emp-col { background: var(--surface-muted); z-index: 4; }
table.training-grid .emp-name { font-weight: bold; font-size: 13px; display: block; }
table.training-grid .emp-pos { font-size: 11.5px; color: var(--text-secondary); }

.cell-btn {
  border: none;
  border-radius: 5px;
  padding: 5px 7px;
  font-size: 11.5px;
  font-weight: bold;
  cursor: pointer;
  min-width: 58px;
  text-align: center;
}
.cell-valid { background: var(--success-bg); color: var(--success-text); }
.cell-expiring { background: #fdf1c8; color: #7a5b04; }
.cell-expired { background: var(--danger-bg); color: var(--danger-text); }
.cell-booked { background: var(--accent-bg); color: var(--accent-text); }
.cell-none { background: transparent; color: var(--text-muted); font-weight: normal; }

.grid-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--text-secondary); margin: 10px 2px 0; }
.grid-legend span { display: inline-flex; align-items: center; gap: 6px; }
.grid-legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }

/* ---------- Card list (mobile alternative to the grid) ---------- */
.card-list { display: none; }
.emp-card {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: 0.85rem 1rem;
  margin-bottom: 10px;
}
.emp-card .emp-card-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.emp-card .emp-name { font-weight: bold; font-size: 14.5px; display: block; }
.emp-card .emp-pos { font-size: 12.5px; color: var(--text-secondary); }
.emp-card .chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.chip { display: inline-block; font-size: 12px; padding: 3px 10px; border-radius: 20px; }
.chip-expiring { background: #fdf1c8; color: #7a5b04; }
.chip-expired { background: var(--danger-bg); color: var(--danger-text); }
.chip-booked { background: var(--accent-bg); color: var(--accent-text); }
.chip-ok { font-size: 12.5px; color: var(--text-muted); }

@media (max-width: 720px) and (orientation: portrait) {
  .grid-scroll { display: none; }
  .card-list { display: block; }
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: none;
  align-items: center; justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: #fff;
  border-radius: var(--radius-card);
  padding: 1.25rem;
  width: 100%;
  max-width: 380px;
}
.modal h2 { font-size: 16px; margin: 0 0 4px; }
.modal .sub { font-size: 13px; color: var(--text-secondary); margin: 0 0 1rem; }
.modal-actions { display: flex; gap: 8px; margin-top: 1rem; }
.modal-actions button { flex: 1; }

.modal-large { max-width: 90vw; width: 700px; height: 85vh; display: flex; flex-direction: column; }
.modal-large-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.75rem; }
.modal-large-header h2 { margin: 0; }
.cert-viewer-content { flex: 1; overflow: auto; background: var(--surface-muted); border-radius: var(--radius); display: flex; align-items: center; justify-content: center; }
.cert-viewer-content iframe { width: 100%; height: 100%; border: none; }
.cert-viewer-content img { max-width: 100%; max-height: 100%; display: block; }
.cert-viewer-content .cert-error { padding: 2rem; color: var(--text-secondary); text-align: center; }

/* ---------- Login ---------- */
.login-card { background: var(--surface-card); border: 1px solid var(--border); border-radius: 16px; padding: 1.5rem; max-width: 360px; margin: 3rem auto 0; }
.login-header { text-align: center; margin-bottom: 1.25rem; }
.login-header .logo-img { width: 220px; height: auto; display: block; margin: 0 auto; }
.login-header .accent-line { height: 3px; width: 48px; background: var(--johns-red); margin: 12px auto 0; }
.login-header p { color: var(--johns-blue); font-weight: bold; font-size: 13px; margin: 10px 0 0; }

.tabs { display: flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 1.25rem; }
.tabs button { flex: 1; border: none; border-radius: 0; background: var(--surface-card); padding: 10px 0; font-size: 13px; font-weight: bold; cursor: pointer; color: var(--text-secondary); }
.tabs button.active { background: var(--surface-muted); color: var(--text-primary); }

.hint { font-size: 12px; color: var(--text-muted); text-align: center; margin-top: 12px; }

.sso-btn { display: flex; align-items: center; justify-content: center; gap: 8px; }

.saved-text { color: var(--success-text); font-size: 13px; font-weight: bold; line-height: 1.4; margin: 8px 0 0; }
.saved-text:empty { display: none; }
.cert-section { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 12px; }
.cert-status { font-size: 13px; margin: 0 0 6px; word-break: break-word; }
.cert-links { font-size: 13px; margin: 0 0 8px; }
.cert-upload { display: flex; gap: 8px; align-items: center; }
.cert-upload input { flex: 1; min-width: 0; }
.cert-upload button { flex-shrink: 0; }

.pin-title { font-weight: bold; margin: 0 0 4px; }
.pin-note { font-size: 13px; color: var(--text-secondary); margin: 0 0 12px; line-height: 1.5; }

.demo-note {
  background: var(--accent-bg); color: var(--accent-text);
  font-size: 12.5px; padding: 8px 12px; border-radius: var(--radius);
  margin: 0 0 1rem;
}

/* ---------- Info icon + popover (course explanations) ---------- */
.info-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid var(--johns-blue);
  color: var(--johns-blue);
  background: #fff;
  font-size: 11px;
  font-weight: bold;
  font-style: italic;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  vertical-align: middle;
  margin-left: 4px;
}
.info-icon:hover { background: var(--accent-bg); }

.info-popover {
  position: absolute;
  z-index: 100;
  width: 260px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  padding: 10px 12px;
  font-size: 12.5px;
  line-height: 1.5;
}
.info-popover strong { display: block; font-size: 13px; margin-bottom: 3px; }
.info-popover p { margin: 0; color: var(--text-secondary); }

.field-hint { font-size: 12px; color: var(--text-secondary); margin: -6px 0 12px; line-height: 1.5; }

/* Subcontractors table (manager view) */
.sub-section { margin-top: 2rem; }
.section-title { font-size: 16px; margin: 0 0 2px; color: var(--primary, #004590); }
.section-count { font-weight: normal; color: var(--text-muted); font-size: 13px; }
.section-note { margin: 0 0 10px; font-size: 13px; color: var(--text-muted); }
@media (max-width: 720px) and (orientation: portrait) {
  .sub-section .grid-scroll { display: none; }
  .sub-section .card-list { display: block; }
}

/* Settings — user permissions */
.perm-add { margin: 1rem 0; }
.perm-add-row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.perm-add-row .field { flex: 1 1 200px; margin-bottom: 0; }
.perm-add-row .field:nth-child(2) { flex: 0 1 170px; }
.perm-list .right select { width: auto; min-width: 140px; }
.perm-list .name { word-break: break-all; }
.perm-legend { margin-top: 1rem; font-size: 12.5px; color: var(--text-secondary); line-height: 1.5; }
.perm-legend p { margin: 0 0 4px; }
.settings-section-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 2.25rem 0 0.75rem; }
.settings-section-head .section-note { margin-bottom: 0; }
.settings-section-actions { display: flex; gap: 8px; flex-shrink: 0; }
.import-preview { font-size: 13px; line-height: 1.5; }
.import-summary { margin: 0 0 6px; }
.import-note { margin: 0 0 6px; color: var(--text-secondary); }
.import-problems { margin: 0; padding-left: 18px; max-height: 180px; overflow: hidden auto; overflow-wrap: anywhere; color: var(--danger-text); }
.import-problems li { margin-bottom: 4px; }
button.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.settings-section-head.email-test-head { margin: 0 0 1.75rem; }
.check-label { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text-primary); cursor: pointer; }
.check-label input { width: auto; margin: 0; }
@media (max-width: 520px) {
  .perm-add-row button { width: 100%; }
  .settings-section-head { flex-direction: column; }
  .settings-section-actions { width: 100%; }
  .settings-section-actions button { flex: 1; }
  .perm-list .record-row { flex-wrap: wrap; gap: 8px; }
}
