/* =====================================================================
   HSE Field Calculator — visual theme
   Safe Work Australia-style: navy/white, authoritative, mobile-first.
   Light + dark themes via [data-theme] on <html> (set before first paint
   by the inline script in index.html). All colours chosen for WCAG AA.
   ===================================================================== */

:root,
:root[data-theme="light"] {
  --navy: #0b2e4f;
  --navy-2: #103a63;
  --accent: #1d6fb8;
  --accent-strong: #155a97;

  --bg: #eef2f6;
  --bg-2: #e3eaf1;
  --surface: #ffffff;
  --surface-2: #f6f9fc;
  --border: #d3dce5;
  --border-strong: #b7c3cf;

  --text: #16232f;
  --text-muted: #566575;
  --text-onnavy: #ffffff;
  --text-onnavy-dim: #c3d4e3;

  --pass: #1a7a3a;
  --pass-bg: #e6f4ea;
  --pass-border: #9ed3b0;
  --fail: #b3261e;
  --fail-bg: #fbeae9;
  --fail-border: #eab3af;
  --caution: #8a5a00;
  --caution-bg: #fdf3e0;
  --caution-border: #f0cf97;
  --neutral: #2f4a63;
  --neutral-bg: #eaf0f5;
  --neutral-border: #c0cfdd;

  --shadow-sm: 0 1px 2px rgba(11, 46, 79, 0.06), 0 1px 3px rgba(11, 46, 79, 0.08);
  --shadow-md: 0 2px 8px rgba(11, 46, 79, 0.08), 0 6px 20px rgba(11, 46, 79, 0.06);
  --focus: #2a86d8;

  --mono: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;

  --radius: 12px;
  --radius-sm: 8px;
  --maxw: 780px;
  --tap: 44px;
}

/* Dark theme — deep navy surfaces, lifted text, AA-contrast states. */
:root[data-theme="dark"] {
  --navy: #071a2c;
  --navy-2: #0e2a44;
  --accent: #4b9fe6;
  --accent-strong: #6fb4ee;

  --bg: #0a1622;
  --bg-2: #0e1e2e;
  --surface: #12263a;
  --surface-2: #16304a;
  --border: #234156;
  --border-strong: #315471;

  --text: #e7eef5;
  --text-muted: #9db2c6;
  --text-onnavy: #ffffff;
  --text-onnavy-dim: #a9c2da;

  --pass: #5cc07f;
  --pass-bg: #123626;
  --pass-border: #2f6f4b;
  --fail: #f2857e;
  --fail-bg: #3a1a1a;
  --fail-border: #7a3630;
  --caution: #f0b95a;
  --caution-bg: #3a2c12;
  --caution-border: #755a24;
  --neutral: #a9c2da;
  --neutral-bg: #16304a;
  --neutral-border: #315471;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
  --focus: #6fb4ee;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  * { transition: none !important; animation: none !important; }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

/* ---- Utilities ---- */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}
.skip-link {
  position: absolute;
  left: 12px; top: -48px;
  z-index: 100;
  background: var(--surface);
  color: var(--accent-strong);
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  box-shadow: var(--shadow-md);
  transition: top 0.15s ease;
}
.skip-link:focus { top: 12px; }

:where(a) { color: var(--accent); }

:where(button, input, select, a):focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---- App bar ---- */
.app-bar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--navy);
  color: var(--text-onnavy);
  box-shadow: var(--shadow-sm);
}
.app-bar-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  background: none;
  border: none;
  color: var(--text-onnavy);
  font-family: var(--sans);
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: 0.2px;
  cursor: pointer;
  padding: 8px 6px;
  border-radius: var(--radius-sm);
  margin-right: auto;
}
.brand:hover { color: #fff; }
.brand-mark { color: #8fd0ff; flex: 0 0 auto; }

.app-bar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: var(--tap);
  padding: 6px 12px 6px 8px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-onnavy);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.back-btn:hover { background: rgba(255, 255, 255, 0.16); }

.quick-switch { display: inline-flex; }
.quick-switch select {
  min-height: var(--tap);
  max-width: 170px;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.10);
  color: var(--text-onnavy);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: var(--radius-sm);
  font-family: var(--sans);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.quick-switch select option,
.quick-switch select optgroup { color: var(--text); background: var(--surface); }

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--tap);
  height: var(--tap);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-onnavy);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 50%;
  cursor: pointer;
  flex: 0 0 auto;
}
.theme-toggle:hover { background: rgba(255, 255, 255, 0.18); }
:root[data-theme="light"] .theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* Contextual bar controls: only inside a calculator, not on home. */
body[data-view="home"] .back-btn,
body[data-view="home"] .quick-switch { display: none; }

/* ---- Views / panels visibility ---- */
main {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 20px 16px 44px;
}
.view, .panel { display: none; }
.view.active, .panel.active { display: block; animation: fade 0.18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---- Home ---- */
.hero { padding: 8px 2px 20px; }
.hero h1 {
  margin: 0 0 8px;
  font-size: 1.6rem;
  line-height: 1.2;
  color: var(--text);
  letter-spacing: -0.2px;
}
.hero-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.96rem;
  max-width: 60ch;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
  margin: 4px 0 8px;
}
.search-icon {
  position: absolute;
  left: 14px;
  color: var(--text-muted);
  pointer-events: none;
}
#calc-search {
  width: 100%;
  min-height: 52px;
  padding: 12px 44px 12px 44px;
  font-size: 1rem;
  font-family: var(--sans);
  color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
#calc-search::placeholder { color: var(--text-muted); }
.search-clear {
  position: absolute;
  right: 8px;
  width: 36px; height: 36px;
  display: inline-flex;
  align-items: center; justify-content: center;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 50%;
}
.search-clear:hover { color: var(--text); background: var(--bg-2); }

.no-results {
  margin: 16px 2px;
  color: var(--text-muted);
  font-size: 0.95rem;
}
.linkish {
  background: none; border: none; padding: 0;
  color: var(--accent); font: inherit; font-weight: 600;
  cursor: pointer; text-decoration: underline;
}

.cat-group { margin-top: 22px; }
.cat-heading {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.9px;
  color: var(--accent);
  font-weight: 700;
  margin: 0 0 10px;
  padding-left: 2px;
}

.card-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.calc-card {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  text-align: left;
  padding: 15px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  color: var(--text);
  font-family: var(--sans);
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}
.calc-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--accent);
}
.card-icon {
  flex: 0 0 auto;
  width: 46px; height: 46px;
  display: inline-flex;
  align-items: center; justify-content: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--accent);
}
.card-icon svg { width: 25px; height: 25px; }
.card-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.card-title { font-size: 1.02rem; font-weight: 700; color: var(--text); }
.card-desc { font-size: 0.82rem; color: var(--text-muted); line-height: 1.4; }
.calc-card.card-references .card-icon { color: var(--neutral); }
.calc-card[hidden] { display: none; }

/* ---- Calculator panel ---- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 18px 22px;
  box-shadow: var(--shadow-sm);
}
.panel h2 {
  margin: 0 0 8px;
  font-size: 1.3rem;
  line-height: 1.22;
  color: var(--text);
}
.panel h2:focus { outline: none; }
.std-ref {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin: 0 0 18px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
  line-height: 1.55;
}
.std-ref em { color: var(--text); font-style: italic; }

.section-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--accent);
  font-weight: 700;
  margin: 18px 0 8px;
}

.field {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 4px 0;
  margin-bottom: 8px;
}
.field label {
  font-size: 0.92rem;
  font-weight: 500;
  flex: 1;
  min-width: 0;
}
.field .sub {
  display: block;
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 3px;
  font-weight: 400;
}
.field input,
.field select {
  width: 148px;
  flex: 0 0 auto;
  min-height: var(--tap);
  padding: 9px 11px;
  border: 1.5px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.95rem;
  text-align: right;
  border-radius: var(--radius-sm);
}
.field select {
  text-align: left;
  font-family: var(--sans);
  cursor: pointer;
}
.field input:focus,
.field select:focus {
  border-color: var(--accent);
  background: var(--surface);
}

.divider {
  margin: 16px 0;
  border: none;
  border-top: 1px dashed var(--border-strong);
}

/* ---- Result status card ---- */
.result {
  margin-top: 20px;
  padding: 16px 16px 14px;
  background: var(--neutral-bg);
  border: 1px solid var(--neutral-border);
  border-left: 5px solid var(--neutral);
  border-radius: var(--radius-sm);
}
.result:empty { display: none; }
.result.pass { background: var(--pass-bg); border-color: var(--pass-border); border-left-color: var(--pass); }
.result.fail { background: var(--fail-bg); border-color: var(--fail-border); border-left-color: var(--fail); }
.result.caution { background: var(--caution-bg); border-color: var(--caution-border); border-left-color: var(--caution); }
.result.neutral { background: var(--neutral-bg); border-color: var(--neutral-border); border-left-color: var(--neutral); }

.result .headline {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: var(--sans);
  font-size: 1.02rem;
  font-weight: 800;
  letter-spacing: 0.2px;
  margin-bottom: 12px;
  color: var(--text);
}
.result .status-dot {
  flex: 0 0 auto;
  width: 22px; height: 22px;
  display: inline-flex;
  align-items: center; justify-content: center;
  border-radius: 50%;
  color: #fff;
  font-size: 14px;
  font-weight: 900;
}
.result.pass .headline { color: var(--pass); }
.result.pass .status-dot { background: var(--pass); }
.result.fail .headline { color: var(--fail); }
.result.fail .status-dot { background: var(--fail); }
.result.caution .headline { color: var(--caution); }
.result.caution .status-dot { background: var(--caution); }
.result.neutral .headline { color: var(--neutral); }
.result.neutral .status-dot { background: var(--neutral); }

.result .line {
  font-size: 0.9rem;
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 6px 0;
  border-top: 1px solid color-mix(in srgb, var(--border) 60%, transparent);
}
.result .line:first-of-type { border-top: none; }
.result .line span:first-child { color: var(--text-muted); }
.result .line span:last-child {
  font-family: var(--mono);
  text-align: right;
  font-weight: 600;
  color: var(--text);
}

.note {
  margin-top: 16px;
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.55;
  padding: 12px 14px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

/* ---- Panel actions ---- */
.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}
.btn-secondary {
  min-height: var(--tap);
  padding: 8px 16px;
  background: var(--surface-2);
  color: var(--accent-strong);
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-family: var(--sans);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-secondary:hover { background: var(--bg-2); border-color: var(--accent); }

/* ---- References ---- */
.ref-entry {
  font-size: 0.9rem;
  margin: 0 0 6px;
  line-height: 1.55;
  color: var(--text);
}
.ref-entry em { font-style: italic; }

/* ---- Footer ---- */
.site-footer {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 8px 16px 40px;
}
.disclaimer {
  font-size: 0.76rem;
  color: var(--text-muted);
  line-height: 1.6;
  border-top: 1px solid var(--border);
  padding-top: 16px;
}
.footer-links { margin-top: 10px; font-size: 0.85rem; }
.footer-links a { color: var(--accent); font-weight: 600; }

/* ---- Toast ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(12px);
  background: var(--navy);
  color: #fff;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  z-index: 90;
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.18s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- Narrow screens: keep the app bar within the viewport ---- */
@media (max-width: 560px) {
  /* Inside a calculator the bar must fit brand + back + switch + toggle. */
  body[data-view="calculator"] .brand-text { display: none; }
  .back-btn span { display: none; }
  .back-btn { width: var(--tap); padding: 0; justify-content: center; gap: 0; }
  .quick-switch select { max-width: 132px; }
  .app-bar-actions { min-width: 0; }
}

/* ---- Wider screens ---- */
@media (min-width: 620px) {
  main { padding: 28px 20px 52px; }
  .panel { padding: 26px 28px 28px; }
  .card-grid { grid-template-columns: 1fr 1fr; }
  .hero h1 { font-size: 1.9rem; }
  .field input, .field select { width: 168px; }
}
@media (min-width: 900px) {
  .card-grid { grid-template-columns: 1fr 1fr 1fr; }
}

/* ---- Print (browser print-to-PDF of the active calculator) ---- */
@media print {
  .app-bar, .panel-actions, .site-footer .footer-links, .toast, .skip-link { display: none !important; }
  body { background: #fff; color: #000; }
  .panel { box-shadow: none; border: none; padding: 0; }
  .result { break-inside: avoid; }
}
