:root {
  --color-primary: #0e5814;
  --color-primary-hover: #176f23;
  --color-bg: #f7f8f7;
  --color-panel: #ffffff;
  --color-border: #e0e0e0;
  --color-text: #1a1a1a;
  --color-text-muted: #6b6b6b;

  --color-expired: #ffcccc;
  --color-expiring30: #ffe5cc;
  --color-expiring60: #ffffcc;
  --color-expiring90: #cce6ff;
  --color-active: #e7f4e7;

  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.06);
}

* { box-sizing: border-box; }

/* The hidden attribute always wins, even over class rules that set display
   (the cause of the modal / Created-Modified box always-visible bugs). */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--color-primary);
  color: #fff;
  padding: 14px 20px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 1.15em;
  letter-spacing: 0.5px;
}

.brand-mark { color: #9ee6a8; }

.menu-toggle {
  background: none;
  border: none;
  cursor: pointer;
  width: 34px;
  height: 34px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.menu-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: #fff;
  border-radius: 2px;
}

/* ---------- Slide-out menu (right side) ---------- */

.side-menu {
  position: fixed;
  top: 0;
  right: -280px;
  width: 280px;
  height: 100%;
  background: var(--color-panel);
  box-shadow: -2px 0 12px rgba(0,0,0,0.15);
  z-index: 40;
  transition: right 0.25s ease;
  display: flex;
  flex-direction: column;
}

.side-menu.open { right: 0; }

.side-menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--color-border);
  font-weight: 700;
  color: var(--color-primary);
}

.menu-close {
  background: none;
  border: none;
  font-size: 1.6em;
  cursor: pointer;
  color: var(--color-text-muted);
  line-height: 1;
}

.side-menu-list {
  list-style: none;
  margin: 0;
  padding: 8px 0;
  flex: 1;
}

.side-menu-link {
  display: block;
  padding: 14px 20px;
  color: var(--color-text);
  text-decoration: none;
  font-size: 1.02em;
  border-left: 3px solid transparent;
}

.side-menu-link:hover { background: #f2f2f2; }

.side-menu-link.active {
  border-left-color: var(--color-primary);
  color: var(--color-primary);
  font-weight: 600;
  background: #eef6ee;
}

.side-menu-scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 30;
  display: none;
}

.side-menu-scrim.open { display: block; }

/* ---------- Main layout ---------- */

.app-main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.view-header h1 {
  margin: 0;
  font-size: 1.5em;
  color: var(--color-primary);
}

.header-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.import-status {
  margin: -10px 0 16px;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 0.88em;
  background: #eef6ee;
  color: var(--color-primary);
}

.import-status-warn { background: #fff3e0; color: #b06a00; }
.import-status-error { background: #fde8e8; color: #c0392b; }

/* ---------- Buttons ---------- */

.btn {
  border: none;
  border-radius: 6px;
  padding: 10px 18px;
  font-size: 0.95em;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

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

.btn-secondary { background: #eee; color: var(--color-text); }
.btn-secondary:hover { background: #e0e0e0; }

.btn:disabled { opacity: 0.5; cursor: default; }

.btn-danger { background: #c0392b; color: #fff; }
.btn-danger:hover { background: #a93226; }

.btn-danger-outline { background: none; color: #c0392b; border: 1px solid #c0392b; }
.btn-danger-outline:hover { background: #fde8e8; }

a.btn { display: inline-block; text-decoration: none; }

.btn-link {
  background: none;
  border: none;
  color: var(--color-primary);
  cursor: pointer;
  font-weight: 600;
  padding: 4px 8px;
}

/* ---------- Stat tiles ---------- */

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.stat-tile {
  background: var(--color-panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border-top: 4px solid var(--color-primary);
}

.stat-tile.stat-expired { border-top-color: #c0392b; }
.stat-tile.stat-30 { border-top-color: #d68a1f; }
.stat-tile.stat-60 { border-top-color: #c7b515; }
.stat-tile.stat-90 { border-top-color: #3178c6; }

.stat-num { font-size: 1.8em; font-weight: 700; }
.stat-label { font-size: 0.85em; color: var(--color-text-muted); }

/* ---------- Panel ---------- */

.panel {
  background: var(--color-panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.panel-header h2 {
  margin: 0 0 14px;
  font-size: 1.1em;
}

.panel + .panel { margin-top: 16px; }

.panel-danger { border: 1px solid #f0b4ad; }
.panel-danger p { margin-top: 0; font-size: 0.92em; }

.subhead { font-size: 0.95em; margin: 22px 0 8px; }

.back-link {
  display: inline-block;
  margin-bottom: 8px;
  color: var(--color-primary);
  text-decoration: none;
  font-size: 0.9em;
}

.cert-table a { color: var(--color-primary); }

.select, .search {
  padding: 9px 10px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font-size: 0.95em;
  font-family: inherit;
  background: #fff;
}

.button-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 10px; }

.email-list {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.9em;
}

/* ---------- Table ---------- */

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

.cert-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92em;
}

.cert-table th {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 2px solid var(--color-border);
  color: var(--color-text-muted);
  font-weight: 600;
  white-space: nowrap;
}

.cert-table td {
  padding: 10px 8px;
  border-bottom: 1px solid var(--color-border);
}

.cert-table tr.row-expired td { background: var(--color-expired); }
.cert-table tr.row-expiring30 td { background: var(--color-expiring30); }
.cert-table tr.row-expiring60 td { background: var(--color-expiring60); }
.cert-table tr.row-expiring90 td { background: var(--color-expiring90); }

.status-chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 0.82em;
  font-weight: 600;
  background: var(--color-active);
}

.status-chip.status-expired { background: #f5a3a3; }
.status-chip.status-expiring30 { background: #f5c98a; }
.status-chip.status-expiring60 { background: #eae08a; }
.status-chip.status-expiring90 { background: #a3c9f5; }
.status-chip.status-badDate { background: #d9b3f0; }
.status-chip.status-renewed { background: #e4e4e4; color: var(--color-text-muted); }

.cert-table tr.row-badDate td { background: #f3e6fb; }
.cert-table tr.row-renewed td { color: var(--color-text-muted); }

.empty-state {
  text-align: center;
  color: var(--color-text-muted);
  padding: 30px 10px;
}

.placeholder-note {
  color: var(--color-text-muted);
  font-style: italic;
}

/* ---------- Facet lists (Training Types / Holder Names) ---------- */

.facet-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.facet-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 6px;
  border-bottom: 1px solid var(--color-border);
}

.facet-item:last-child { border-bottom: none; }

.facet-count {
  background: var(--color-bg);
  border-radius: 10px;
  padding: 2px 10px;
  font-size: 0.85em;
  color: var(--color-text-muted);
}

.facet-sub { color: var(--color-text-muted); font-size: 0.85em; margin-left: 6px; }

.facet-empty { color: var(--color-text-muted); padding: 10px 6px; }

.facet-link { cursor: pointer; }
.facet-link:hover { background: #f4f8f4; }

.side-menu-divider { border-top: 1px solid var(--color-border); margin: 8px 0; }

/* ---------- Holder page ---------- */

.device-list { margin: 0; padding-left: 18px; font-size: 0.88em; }
.device-list .facet-empty { list-style: none; margin-left: -18px; }

.history-list { list-style: none; margin: 0; padding: 0; font-size: 0.88em; }

.history-item {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  padding: 8px 0;
  border-bottom: 1px solid var(--color-border);
}

.history-item:last-child { border-bottom: none; }
.history-when { color: var(--color-text-muted); white-space: nowrap; min-width: 160px; }
.history-what { flex: 1; min-width: 200px; }
.history-who { color: var(--color-text-muted); }

/* ---------- Settings ---------- */

.settings-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
}

.settings-row .meta-label { min-width: 90px; font-size: 0.88em; }

/* ---------- Grid View ---------- */

.grid-table th.sortable { cursor: pointer; user-select: none; }
.grid-table th.sortable:hover { color: var(--color-text); }
.print-only { display: none; }

/* ---------- Modal ---------- */

.modal-scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 50;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: 30px 16px;
}

.modal-scrim:not([hidden]) {
  display: flex;
}

.modal-scrim[hidden] {
  display: none;
}

.modal {
  background: var(--color-panel);
  border-radius: var(--radius);
  width: 100%;
  max-width: 560px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.25);
}

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

.modal-header h2 { margin: 0; font-size: 1.2em; color: var(--color-primary); }

.modal-close {
  background: none;
  border: none;
  font-size: 1.6em;
  cursor: pointer;
  color: var(--color-text-muted);
  line-height: 1;
}

.modal-body { padding: 20px 22px; }

.form-group { margin-bottom: 16px; }

.form-row {
  display: flex;
  gap: 12px;
}

.form-row .form-group { flex: 1; }

.form-group-narrow { flex: 0 0 70px; }

.form-group-inline { display: flex; align-items: center; }

.form-group label {
  display: block;
  font-size: 0.88em;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--color-text);
}

.req { color: #c0392b; }

.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="url"],
.form-group input[type="date"],
.form-group input[type="file"],
.form-group textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font-size: 0.95em;
  font-family: inherit;
}

.form-group textarea { resize: vertical; }

.field-hint {
  font-size: 0.8em;
  color: var(--color-text-muted);
  margin: 5px 0 0;
}

.field-hint-error { color: #c0392b; }

.attachment-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
}

.attachment-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.88em;
}

.attachment-item:last-child { border-bottom: none; }

.attachment-thumb {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75em;
  font-weight: 700;
  color: #c0392b;
}

.attachment-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--color-primary);
}

.attachment-size { color: var(--color-text-muted); white-space: nowrap; }

.readonly-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  background: var(--color-bg);
  border-radius: 6px;
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 0.82em;
}

.meta-label {
  display: block;
  color: var(--color-text-muted);
  font-weight: 600;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 8px;
}

.modal-actions .spacer { flex: 1; }

.renews-note {
  background: #eef6ee;
  color: var(--color-primary);
  border-radius: 6px;
  padding: 8px 12px;
  margin: 0 0 16px;
  font-size: 0.88em;
}

/* ---------- Mobile ---------- */

@media (max-width: 600px) {
  .side-menu { width: 82%; right: -82%; }
  .form-row { flex-direction: column; }
  .form-group-narrow { flex: 1; }
  .view-header h1 { font-size: 1.25em; }
}

/* ---------- Print (Grid View report → paper or PDF) ---------- */

@media print {
  @page { size: landscape; margin: 12mm; }

  body { background: #fff; font-size: 10pt; }
  .topbar, .side-menu, .side-menu-scrim, .modal-scrim, .no-print, .back-link { display: none !important; }
  .app-main { max-width: none; padding: 0; }
  .panel { box-shadow: none; padding: 0; }
  .table-wrap { overflow: visible; }
  .print-only { display: block; font-size: 13pt; margin: 0 0 8px; }

  /* Keep row colors, repeat the header on every page, never split a row. */
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  thead { display: table-header-group; }
  tr { break-inside: avoid; page-break-inside: avoid; }
  .cert-table th, .cert-table td { padding: 4px 6px; }
  .grid-table th.sortable { cursor: default; }
}
