/* =====================================================================
   CAEMS design system
   Industrial / quality-lab theme. Deliberately high contrast: this is read
   on a phone under factory lighting, often through a scratched screen
   protector, sometimes with gloves on.

   No CDN. The whole stylesheet is cached by the service worker so the app
   still renders on a dead shop-floor Wi-Fi connection.
   ===================================================================== */

:root {
  /* Brand - steel blue, overridable per factory from settings */
  --brand:        #0B4A6F;
  --brand-dark:   #073751;
  --brand-tint:   #E7EFF5;
  --brand-ink:    #FFFFFF;

  /* Surfaces */
  --bg:           #EEF2F6;
  --card:         #FFFFFF;
  --line:         #D8E0E8;
  --line-strong:  #B8C4D0;

  /* Text */
  --ink:          #10202F;
  --ink-soft:     #47576A;
  --ink-faint:    #6B7C90;

  /* Calibration status - these carry meaning, never decoration */
  --ok:           #14653B;  --ok-bg:      #E4F4EA;  --ok-line:      #A7D8BC;
  --warn:         #8A4B08;  --warn-bg:    #FDF1DF;  --warn-line:    #EFC891;
  --soon:         #92400E;  --soon-bg:    #FEF6E7;  --soon-line:    #F2D9A8;
  --danger:       #9B1C1C;  --danger-bg:  #FCE9E9;  --danger-line:  #F0B4B4;
  --neutral:      #47576A;  --neutral-bg: #EDF1F5;  --neutral-line: #CFD9E3;

  --radius:       12px;
  --radius-sm:    8px;
  --tap:          44px;      /* minimum touch target */
  --header-h:     56px;
  --nav-h:        62px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}

a { color: var(--brand); }

/* ---------------------------------------------------------------- header */
.appbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  min-height: var(--header-h);
  padding: 8px 14px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: var(--brand);
  color: var(--brand-ink);
  box-shadow: 0 1px 0 rgba(0,0,0,.12);
}
.appbar h1 { font-size: 17px; font-weight: 650; margin: 0; letter-spacing: .1px; }
.appbar .sub { font-size: 12px; opacity: .82; margin: 0; }
.appbar .spacer { margin-left: auto; }
.appbar a, .appbar button { color: var(--brand-ink); }

/* Buttons keep their own colours inside the app bar. Without :not(.btn) the
   rule above wins on specificity and paints a white .btn-secondary's label
   white on white, so the Sign out button reads as blank. */
.appbar a.btn, .appbar button.btn { color: inherit; }
.appbar .btn-secondary { background: #fff; color: var(--brand); border-color: rgba(255,255,255,.6); }
.appbar .btn-primary   { background: var(--brand-dark); color: #fff; }
.appbar .btn-ghost     { background: rgba(255,255,255,.16); color: #fff; }

/* Sign out must be reachable from every page. On phones the bottom tab bar
   has no room for it and the sidebar user block is hidden, so it lives here;
   from 900px up the sidebar carries it instead. */
.appbar-signout {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; flex: 0 0 auto; margin-left: 8px;
  border-radius: 8px; background: rgba(255,255,255,.14);
  color: var(--brand-ink); text-decoration: none;
}
.appbar-signout svg { width: 19px; height: 19px; }
.appbar-signout:active { background: rgba(255,255,255,.28); }
@media (min-width: 900px) { .appbar-signout { display: none; } }

.appbar-back {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; margin-left: -6px;
  border-radius: 8px; text-decoration: none; font-size: 20px; line-height: 1;
  background: rgba(255,255,255,.12);
}

/* ------------------------------------------------------------------ page */
.page { padding: 14px; max-width: 1100px; margin: 0 auto; }
.page-title { font-size: 20px; font-weight: 700; margin: 4px 0 14px; }
.section-title {
  font-size: 12px; font-weight: 700; letter-spacing: .8px;
  text-transform: uppercase; color: var(--ink-faint);
  margin: 22px 0 8px;
}

/* ------------------------------------------------------------------ card */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card-tight { padding: 10px 12px; }
.card-link { display: block; text-decoration: none; color: inherit; }
.card-link:active { background: #F6F9FC; }

/* ------------------------------------------------------------------ grid */
.grid { display: grid; gap: 10px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
@media (min-width: 720px) {
  .grid-2, .grid-3 { grid-template-columns: repeat(4, minmax(0,1fr)); }
}

/* ------------------------------------------------------------------ tile */
.tile {
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 4px solid var(--neutral-line);
  border-radius: var(--radius);
  padding: 12px;
  text-decoration: none; color: inherit;
  display: block;
}
.tile .num { font-size: 28px; font-weight: 750; line-height: 1.1; font-variant-numeric: tabular-nums; }
.tile .lbl { font-size: 12px; color: var(--ink-soft); margin-top: 3px; }
.tile.ok      { border-left-color: var(--ok); }
.tile.ok      .num { color: var(--ok); }
.tile.soon    { border-left-color: var(--soon); }
.tile.soon    .num { color: var(--soon); }
.tile.warn    { border-left-color: var(--warn); }
.tile.warn    .num { color: var(--warn); }
.tile.danger  { border-left-color: var(--danger); }
.tile.danger  .num { color: var(--danger); }
.tile.brand   { border-left-color: var(--brand); }
.tile.brand   .num { color: var(--brand); }

/* ----------------------------------------------------------------- badge */
.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid;
  font-size: 11px; font-weight: 700;
  letter-spacing: .3px; text-transform: uppercase;
  white-space: nowrap;
}
.badge.ok      { color: var(--ok);      background: var(--ok-bg);      border-color: var(--ok-line); }
.badge.soon    { color: var(--soon);    background: var(--soon-bg);    border-color: var(--soon-line); }
.badge.warn    { color: var(--warn);    background: var(--warn-bg);    border-color: var(--warn-line); }
.badge.danger  { color: var(--danger);  background: var(--danger-bg);  border-color: var(--danger-line); }
.badge.neutral { color: var(--neutral); background: var(--neutral-bg); border-color: var(--neutral-line); }

/* ------------------------------------------------------------------ list */
.row {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px; background: var(--card);
  border: 1px solid var(--line); border-radius: var(--radius);
  margin-bottom: 8px; text-decoration: none; color: inherit;
}
.row:active { background: #F6F9FC; }
.row .grow { min-width: 0; flex: 1; }
.row .title { font-weight: 650; }
.row .meta { font-size: 13px; color: var(--ink-soft); }
.row .meta-faint { font-size: 12px; color: var(--ink-faint); margin-top: 2px; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --------------------------------------------------------- definition list */
.dl { display: grid; grid-template-columns: 40% 60%; gap: 0; font-size: 14.5px; }
.dl dt { color: var(--ink-faint); padding: 7px 0; border-bottom: 1px solid var(--line); }
.dl dd { margin: 0; padding: 7px 0; border-bottom: 1px solid var(--line); font-weight: 550; word-break: break-word; }
.dl dt:last-of-type, .dl dd:last-of-type { border-bottom: none; }

/* ----------------------------------------------------------------- forms */
label.field { display: block; margin-bottom: 12px; }
label.field > span {
  display: block; font-size: 13px; font-weight: 650;
  color: var(--ink-soft); margin-bottom: 5px;
}
label.field > span .req { color: var(--danger); }

input[type=text], input[type=email], input[type=password], input[type=date],
input[type=number], input[type=search], input[type=file], select, textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  font: inherit; font-size: 16px;   /* 16px stops iOS zooming on focus */
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  appearance: none;
}
textarea { min-height: 84px; resize: vertical; }
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath fill='%2347576A' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
  background-size: 12px; padding-right: 34px;
}
input:focus, select:focus, textarea:focus {
  outline: 3px solid rgba(11,74,111,.25);
  border-color: var(--brand);
}
.field-row { display: grid; gap: 0 12px; grid-template-columns: 1fr; }
@media (min-width: 640px) { .field-row { grid-template-columns: 1fr 1fr; } }

.checkline { display: flex; align-items: center; gap: 10px; min-height: var(--tap); }
.checkline input { width: 20px; height: 20px; }

/* --------------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 10px 18px;
  font: inherit; font-size: 15px; font-weight: 650;
  border-radius: var(--radius-sm); border: 1px solid transparent;
  text-decoration: none; cursor: pointer;
}
.btn-primary  { background: var(--brand); color: #fff; }
.btn-primary:active { background: var(--brand-dark); }
.btn-secondary{ background: #fff; color: var(--brand); border-color: var(--line-strong); }
.btn-danger   { background: var(--danger); color: #fff; }
.btn-ghost    { background: transparent; color: var(--brand); }
.btn-block    { display: flex; width: 100%; }
.btn-sm       { min-height: 36px; padding: 6px 12px; font-size: 13.5px; }
.btn:focus-visible { outline: 3px solid rgba(11,74,111,.35); outline-offset: 2px; }
.btn-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 6px; }

/* --------------------------------------------------------------- alerts */
.notice {
  border-radius: var(--radius-sm); padding: 11px 13px;
  font-size: 14.5px; margin-bottom: 12px; border: 1px solid;
}
.notice.error   { color: var(--danger); background: var(--danger-bg); border-color: var(--danger-line); }
.notice.success { color: var(--ok);     background: var(--ok-bg);     border-color: var(--ok-line); }
.notice.info    { color: var(--brand);  background: var(--brand-tint);border-color: #BBD3E3; }
.notice.warn    { color: var(--warn);   background: var(--warn-bg);   border-color: var(--warn-line); }

/* --------------------------------------------------------- search / filter */
.searchbar { display: flex; gap: 8px; margin-bottom: 12px; }
.searchbar input { flex: 1; }
.chips { display: flex; gap: 7px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 10px; -webkit-overflow-scrolling: touch; }
.chip {
  flex: 0 0 auto; padding: 7px 13px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: #fff;
  font-size: 13px; font-weight: 600; color: var(--ink-soft); text-decoration: none;
}
.chip.active { background: var(--brand); border-color: var(--brand); color: #fff; }

/* --------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; background: #fff; }
table.data th, table.data td { padding: 9px 11px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data th { font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--ink-faint); background: #F7FAFC; }
table.data tr:last-child td { border-bottom: none; }

/* -------------------------------------------------------------- history */
.timeline { list-style: none; margin: 0; padding: 0 0 0 18px; border-left: 2px solid var(--line); }
.timeline li { position: relative; padding: 0 0 16px 14px; }
.timeline li::before {
  content: ''; position: absolute; left: -25px; top: 5px;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--brand); border: 2px solid #fff;
}
.timeline .when { font-size: 12px; color: var(--ink-faint); }
.timeline .what { font-weight: 620; font-size: 14.5px; }
.timeline .who  { font-size: 12.5px; color: var(--ink-soft); }

/* ------------------------------------------------------------------- QR */
.qr-card { text-align: center; }
.qr-card svg { width: 100%; max-width: 210px; height: auto; }
.qr-asset { font-family: var(--mono); font-size: 15px; font-weight: 700; letter-spacing: 1px; margin-top: 6px; }

/* Printable label sheet */
.label-sheet { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6mm; }
.label {
  border: 1px dashed var(--line-strong); border-radius: 6px;
  padding: 4mm; text-align: center; break-inside: avoid;
  background: #fff;
}
.label svg { width: 32mm; height: 32mm; }
.label .asset { font-family: var(--mono); font-weight: 700; font-size: 11pt; margin-top: 2mm; }
.label .name  { font-size: 8pt; color: #333; line-height: 1.2; }
.label .plant { font-size: 6.5pt; color: #666; margin-top: 1mm; }

@media print {
  .appbar, .mainnav, .no-print { display: none !important; }
  body { background: #fff; padding: 0; }
  .page { padding: 0; max-width: none; }
  .label-sheet { grid-template-columns: repeat(3, 1fr); gap: 4mm; }
  .label { border: 1px solid #999; }
  @page { margin: 10mm; }
}

/* ---------------------------------------------------------------- misc */
.muted   { color: var(--ink-faint); }
.small   { font-size: 13px; }
.mono    { font-family: var(--mono); }
.nowrap  { white-space: nowrap; }
.center  { text-align: center; }
.empty   { text-align: center; color: var(--ink-faint); padding: 40px 16px; }
.stack > * + * { margin-top: 10px; }
.flex-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.hr { height: 1px; background: var(--line); border: 0; margin: 14px 0; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.offline-banner {
  display: none; background: var(--warn-bg); color: var(--warn);
  border-bottom: 1px solid var(--warn-line);
  padding: 7px 14px; font-size: 13px; font-weight: 600; text-align: center;
}
body.is-offline .offline-banner { display: block; }


/* ================================================================
   NAVIGATION
   One markup, two positions: a left sidebar from 900px up, a bottom
   tab bar below that. Bottom tabs stay on phones on purpose - this is
   used one-handed on the shop floor, and a top-left hamburger is the
   hardest target to reach on a large phone.
   ================================================================ */

.mainnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  background: #fff; border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}

.nav-items { display: flex; justify-content: space-around; }

.nav-item {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; min-height: var(--nav-h); padding: 6px 2px;
  font-size: 10.5px; font-weight: 600; letter-spacing: .2px;
  color: var(--ink-faint); text-decoration: none;
}
.nav-item svg { width: 22px; height: 22px; stroke: var(--ink-faint); fill: none; stroke-width: 1.9; }
.nav-item.active { color: var(--brand); }
.nav-item.active svg { stroke: var(--brand); }
.nav-item:active { background: #F2F6FA; }

/* The signed-in user block belongs to the sidebar only */
.nav-user { display: none; }

@media (min-width: 900px) {
  :root { --sidebar-w: 232px; }

  body { padding-bottom: 0; padding-left: var(--sidebar-w); }

  .appbar {
    position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  }

  .mainnav {
    top: var(--header-h);
    bottom: 0; right: auto;
    width: var(--sidebar-w);
    border-top: none; border-right: 1px solid var(--line);
    display: flex; flex-direction: column; justify-content: space-between;
    padding: 12px 0 0;
    overflow-y: auto;
  }

  .nav-items { display: block; }

  .nav-item {
    flex: none;
    flex-direction: row; justify-content: flex-start; align-items: center;
    gap: 12px;
    min-height: 46px; padding: 10px 18px; margin: 0 8px 2px;
    border-radius: var(--radius-sm);
    font-size: 14.5px; font-weight: 600; letter-spacing: 0;
    color: var(--ink-soft);
  }
  .nav-item svg { width: 20px; height: 20px; flex: 0 0 auto; }
  .nav-item:hover { background: var(--bg); color: var(--ink); }
  .nav-item.active {
    background: var(--brand-tint); color: var(--brand);
  }
  .nav-item.active svg { stroke: var(--brand); }

  .nav-user {
    display: block;
    border-top: 1px solid var(--line);
    padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
    margin-top: 12px;
  }
  .nav-user-name { font-size: 14px; font-weight: 650; color: var(--ink); }
  .nav-user-role { font-size: 12px; color: var(--ink-faint); line-height: 1.35; margin-top: 2px; }
  .nav-user-out {
    display: inline-block; margin-top: 8px;
    font-size: 13px; font-weight: 600; color: var(--brand); text-decoration: none;
  }
  .nav-user-out:hover { text-decoration: underline; }

  /* The fixed header no longer occupies flow space */
  body { padding-top: var(--header-h); }
  .page { max-width: 980px; margin: 0 auto; padding: 18px 22px 40px; }
}

@media print {
  .mainnav { display: none !important; }
  body { padding-left: 0; padding-top: 0; }
}

/* ================================================================
   BRANDING
   ================================================================ */

/* Uploaded factory logo in the app bar.
   Both dimensions are capped and the element cannot grow: a tall or square
   logo would otherwise set the height of the header on every page. */
.appbar-logo {
  height: 30px !important;
  max-height: 30px;
  width: auto !important;
  max-width: 110px;
  object-fit: contain;
  flex: 0 0 auto;
  background: rgba(255,255,255,.92);
  border-radius: 5px;
  padding: 2px 5px;
  display: block;
}
@media (max-width: 380px) { .appbar-logo { max-width: 78px; } }

/* The header must never be taller than its content */
.appbar { max-height: 72px; overflow: hidden; }
.appbar > div { min-width: 0; }

.logo-preview {
  max-width: 220px; max-height: 90px; width: auto;
  background: var(--brand-tint); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 10px; display: block;
}

/* Test-mode banner. Deliberately loud: a password-bypass session must never
   be mistaken for a normal one. */
.test-mode-banner {
  background: #9B1C1C; color: #fff;
  padding: 7px 14px; font-size: 12.5px; font-weight: 700;
  text-align: center; letter-spacing: .3px;
}
.test-mode-banner a { color: #fff; text-decoration: underline; }
