:root {
  /* Base dark palette */
  --bg: #0a0a0d;
  --bg-texture: #0c0c10;
  --card: #15151a;
  --card-alt: #1a1a20;
  --border: #2a2a32;
  --border-soft: #202026;
  --text: #eceef1;
  --muted: #8b8b96;
  --muted-dim: #5c5c66;

  /* Tally-light accent system */
  --red: #ff3b3f;
  --red-soft: #3a1517;
  --amber: #ffb020;
  --amber-soft: #362509;
  --green: #2ecc71;
  --green-soft: #0f2c1c;

  /* Primary brand accent (tally record red) */
  --accent: #ff3b3f;
  --accent-hover: #ff5a5d;

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

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background-color: var(--bg);
  background-image:
    radial-gradient(rgba(255,255,255,0.035) 1px, transparent 1px);
  background-size: 14px 14px;
  color: var(--text);
}

.topbar {
  background: #0d0d11;
  background-image: linear-gradient(90deg, rgba(255,59,63,0.08), transparent 40%);
  border-bottom: 1px solid var(--border);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  flex-wrap: wrap;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.2px;
  display: flex;
  align-items: center;
}
.brand::before {
  content: "";
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 8px 1px rgba(255,59,63,0.7);
  margin-right: 10px;
}
.brand span {
  font-weight: 400;
  color: var(--muted);
  margin-left: 6px;
}

.search-form { flex: 1; max-width: 320px; margin: 0 12px; }
.search-form input {
  width: 100%;
  background: var(--card-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 7px 12px;
  font-size: 13px;
}
.search-form input:focus { outline: none; border-color: var(--accent); }

#nav { display: flex; gap: 4px; flex-wrap: wrap; }
#nav a {
  color: var(--muted);
  text-decoration: none;
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  border: 1px solid transparent;
}
#nav a:hover { background: rgba(255,255,255,0.05); color: var(--text); }
#nav a.active { background: var(--red-soft); color: var(--red); border-color: rgba(255,59,63,0.3); }

.help-menu { position: relative; }
#helpBtn { border-radius: 50%; width: 30px; height: 30px; padding: 0; font-weight: 700; }
.help-popover {
  position: absolute; top: 40px; right: 0; z-index: 60;
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 14px; width: 240px; box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}

/* Small inline "?" help icons with a CSS-only hover tooltip — no JS needed. */
.help-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 15px; height: 15px; border-radius: 50%;
  background: var(--card-alt); border: 1px solid var(--border);
  color: var(--muted); font-size: 10px; font-weight: 700;
  cursor: help; margin-left: 6px; position: relative; vertical-align: middle;
}
.help-icon:hover, .help-icon:focus { color: var(--accent); border-color: var(--accent); }
.help-icon:hover::after, .help-icon:focus::after {
  content: attr(data-tip);
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 130%;
  background: var(--card-alt); border: 1px solid var(--border); color: var(--text);
  padding: 8px 10px; border-radius: 6px; font-size: 12px; font-weight: 400;
  text-transform: none; letter-spacing: normal; white-space: normal; width: 220px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.55); z-index: 80; line-height: 1.4;
}

/* Guided tutorial */
.tour-highlight { outline: 3px solid var(--accent) !important; outline-offset: 3px; border-radius: 8px; }
#tourBar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 200;
  background: #0d0d11; border-top: 2px solid var(--accent);
  padding: 14px 24px; display: flex; align-items: center; gap: 18px;
  box-shadow: 0 -8px 24px rgba(0,0,0,0.5);
}
#tourBar .tour-text { flex: 1; min-width: 0; }
#tourBar .tour-step-count { color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; }
#tourBar .tour-title { font-size: 15px; font-weight: 700; margin: 2px 0 4px; }
#tourBar .tour-desc { color: var(--muted); font-size: 13px; line-height: 1.4; }
#tourBar .tour-actions { display: flex; gap: 8px; flex: 0 0 auto; }

#app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 28px 24px 80px;
}

h1 { font-size: 22px; margin: 0 0 4px; color: var(--text); font-weight: 700; }
h2 { font-size: 15px; margin: 28px 0 10px; color: var(--text); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.subtitle { color: var(--muted); margin: 0 0 22px; font-size: 14px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 18px;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  position: relative;
  overflow: hidden;
}
.stat::before {
  content: "";
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 3px;
  background: var(--red);
  opacity: 0.6;
}
.stat .num { font-size: 26px; font-weight: 700; color: var(--text); font-family: var(--mono); }
.stat .label { color: var(--muted); font-size: 13px; margin-top: 2px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border-soft); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; }
tr:last-child td { border-bottom: none; }
td.actions { white-space: nowrap; text-align: right; }
td.task-done { color: var(--muted-dim); text-decoration: line-through; }

a.link { color: var(--red); text-decoration: none; font-weight: 500; }
a.link:hover { text-decoration: underline; color: var(--accent-hover); }

button, .btn {
  background: var(--accent);
  color: #14060a;
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
button.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
button.danger { background: transparent; color: var(--red); border: 1px solid var(--border); }
button.small { padding: 5px 10px; font-size: 12px; margin-left: 6px; }
button:hover { opacity: 0.88; }
a.btn { display: inline-block; text-decoration: none; }
a.btn.secondary, a.btn.small.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; flex-wrap: wrap; gap: 10px; }

form.inline-form { display: grid; gap: 10px; grid-template-columns: repeat(2, 1fr); margin-top: 8px; }
form.inline-form.single-col { grid-template-columns: 1fr; }
form.inline-form label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); font-weight: 600; gap: 4px; }
form.inline-form input, form.inline-form select, form.inline-form textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  background: var(--card-alt);
}
form.inline-form input:focus, form.inline-form select:focus, form.inline-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}
form.inline-form textarea { resize: vertical; min-height: 70px; }
form.inline-form .full { grid-column: 1 / -1; }
form.inline-form .form-actions { grid-column: 1 / -1; display: flex; gap: 8px; margin-top: 4px; }
form.inline-form .hint { font-weight: 400; color: var(--muted); font-size: 11px; margin-top: 2px; }
.hint { color: var(--muted); font-size: 11px; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 100px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  font-family: var(--mono);
}
/* Unified semantic tiers — every status across every module (rig kits, RMA,
   subrentals, billing, logistics, Notion jobs, eng-check, prep) maps to one
   of these four color meanings, not a one-off color per literal string.
   neutral = not started / informational, warning = in progress / needs eyes,
   good = done / healthy, bad = flagged / blocked / overdue. */
.badge.notPrepped, .badge.active, .badge.inService, .badge.planned, .badge.adjusted,
.badge.incoming, .badge.inPlanning, .badge.unpaid, .badge.notFound { background: var(--card-alt); color: var(--muted); border: 1px solid var(--border); }
.badge.prepped, .badge.rmaSubmitted, .badge.confirmed, .badge.matched, .badge.dueSoon { background: var(--amber-soft); color: var(--amber); }
.badge.tested, .badge.backInService, .badge.rmaReturned, .badge.done, .badge.added, .badge.paid { background: var(--green-soft); color: var(--green); }
.badge.flagged, .badge.returned, .badge.gone, .badge.unmatched, .badge.overdue, .badge.dead { background: var(--red-soft); color: var(--red); }

/* Slightly higher-contrast badge text for quick reads in low light (truck
   ramp, dim warehouse) — bumped weight/tracking already existed; this adds a
   touch more background separation so color isn't the only cue. */
.badge { border: 1px solid transparent; }
.badge.prepped, .badge.rmaSubmitted, .badge.confirmed, .badge.matched, .badge.dueSoon { border-color: rgba(255,176,32,0.35); }
.badge.tested, .badge.backInService, .badge.rmaReturned, .badge.done, .badge.added, .badge.paid { border-color: rgba(46,204,113,0.35); }
.badge.flagged, .badge.returned, .badge.gone, .badge.unmatched, .badge.overdue, .badge.dead { border-color: rgba(255,59,63,0.35); }

.last-pulled { color: var(--muted); font-size: 11px; font-family: var(--mono); margin-top: 4px; display: inline-block; }
.error-text { color: var(--red); font-size: 13px; padding: 10px; background: var(--red-soft); border-radius: 6px; border: 1px solid rgba(255,59,63,0.3); }

.empty {
  color: var(--muted);
  font-size: 14px;
  padding: 14px 10px;
  background-image: radial-gradient(rgba(255,255,255,0.04) 1px, transparent 1px);
  background-size: 8px 8px;
  border-radius: 6px;
}

.detail-back { display: inline-block; margin-bottom: 14px; color: var(--muted); text-decoration: none; font-size: 13px; font-weight: 600; }
.detail-back:hover { color: var(--text); }

.log-item { border-bottom: 1px solid var(--border-soft); padding: 10px 0; }
.log-item:last-child { border-bottom: none; }
.log-item .ts { color: var(--muted); font-size: 12px; font-family: var(--mono); }

.pill-row { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.pill { background: var(--red-soft); color: var(--red); border-radius: 100px; padding: 4px 12px; font-size: 12px; font-weight: 600; }

.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }

/* Crew view: big glanceable time rows and pinned alert */
.big-time-row { display: flex; align-items: baseline; gap: 16px; padding: 10px 0; border-bottom: 1px solid var(--border-soft); }
.big-time-row:last-child { border-bottom: none; }
.big-time { font-family: var(--mono); font-size: 30px; font-weight: 700; color: var(--text); min-width: 120px; }
.big-time-label { font-size: 16px; color: var(--muted); }
.pinned-alert {
  background: var(--red-soft); border: 2px solid var(--red); color: var(--text);
  font-size: 20px; font-weight: 700; text-align: center; padding: 18px 16px;
}
.qr-preview { margin-top: 10px; display: inline-block; background: #fff; padding: 10px; border-radius: 8px; }
.qr-preview img { display: block; width: 160px; height: 160px; }

/* Flex vs Engineering Doc live comparison */
.compare-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 12px; }
.compare-grid .compare-col { display: flex; flex-direction: column; gap: 6px; }
.compare-grid .compare-col-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
}
.compare-grid iframe {
  width: 100%; height: 420px; border: 1px solid var(--border); border-radius: 8px; background: #fff;
}
.compare-grid .compare-empty {
  border: 1px dashed var(--border); border-radius: 8px; padding: 18px; text-align: center;
  color: var(--muted-dim); font-size: 13px; height: 420px; display: flex; align-items: center; justify-content: center;
}

/* My scratchpad — clean checklist */
.scratch-list { display: flex; flex-direction: column; }
.scratch-row {
  display: flex; align-items: center; gap: 10px; padding: 9px 2px;
  border-bottom: 1px solid var(--border-soft);
}
.scratch-row:last-child { border-bottom: none; }
.scratch-row input[type="checkbox"] { flex: 0 0 auto; width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.scratch-row .scratch-text { flex: 1; font-size: 14px; }
.scratch-row.done .scratch-text { color: var(--muted-dim); text-decoration: line-through; }
.scratch-row .scratch-remove {
  flex: 0 0 auto; opacity: 0; transition: opacity 0.12s ease;
  background: transparent; border: none; color: var(--muted); font-size: 18px; line-height: 1;
  cursor: pointer; padding: 0 4px;
}
.scratch-row:hover .scratch-remove, .scratch-row:focus-within .scratch-remove { opacity: 1; }
.scratch-row .scratch-remove:hover { color: var(--red); }
.scratch-done-toggle {
  display: block; background: none; border: none; color: var(--muted); font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.03em; font-weight: 600; cursor: pointer;
  padding: 10px 2px 0; text-align: left;
}
.scratch-done-toggle:hover { color: var(--text); }
.scratch-add { display: flex; gap: 8px; margin-top: 12px; }
.scratch-add input {
  flex: 1; font-family: inherit; font-size: 14px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 6px; color: var(--text); background: var(--card-alt);
}
.scratch-add input:focus { outline: none; border-color: var(--accent); }

/* Timecode-style monospace for times/dates in tables */
td:first-child { font-family: var(--mono); }

/* Dashboard needs-attention groups — plain <details>/<summary>, no JS needed. */
.attention-group { margin-bottom: 6px; }
.attention-group > summary { cursor: pointer; padding: 8px 4px; display: flex; align-items: center; gap: 8px; list-style: none; }
.attention-group > summary::-webkit-details-marker { display: none; }
.attention-group > summary::before { content: "▸"; color: var(--muted); font-size: 11px; transition: transform 0.15s ease; }
.attention-group[open] > summary::before { transform: rotate(90deg); }
.attention-group .group-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.attention-group .group-count { color: var(--muted); font-weight: 400; }

.search-form-with-btn { display: flex; gap: 6px; align-items: stretch; }
.search-form-with-btn input { flex: 1; }
.search-form-with-btn button { flex: 0 0 auto; padding: 7px 12px; }

/* Show Detail: collapsible cards, applied via JS (makeCardsCollapsible) so
   the existing card markup doesn't need restructuring. Collapsed by
   default nothing changes visually — click the heading to toggle. */
.card.collapsible > h2 { cursor: pointer; user-select: none; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-toggle-arrow { color: var(--muted); font-size: 12px; flex: 0 0 auto; transition: transform 0.15s ease; }
.card.collapsed .card-toggle-arrow { transform: rotate(-90deg); }
.card.collapsed .details-body-auto { display: none; }

/* Camera Builds: same collapsible pattern, but the clickable header is a
   toolbar with its own Edit/Delete buttons rather than a plain h2, so it's
   wired up directly in rigkits.js instead of via makeCardsCollapsible.
   Made deliberately loud (colored "Show/Hide details" text + hover fill,
   not just a small gray arrow) since a bare arrow wasn't reading as
   clickable. */
.kit-card-header { cursor: pointer; user-select: none; padding: 4px 8px; margin: -4px 0 0 -8px; border-radius: 6px; transition: background 0.12s ease; }
.kit-card-header:hover { background: var(--card-alt); }
.kit-toggle-hint { display: inline-flex; align-items: center; gap: 3px; margin-left: 10px; font-size: 12px; font-weight: 600; color: var(--accent); }
.kit-toggle-hint .card-toggle-arrow { color: var(--accent); font-size: 13px; }
.kit-toggle-label::before { content: "Hide details"; }
.card.collapsed .kit-toggle-label::before { content: "Show details"; }

/* Camera Build BOM reordering (SortableJS) — drag handle used both in the
   checklist table (list view) and the row-based editor (New/Edit form). */
.drag-handle { cursor: grab; color: var(--muted); font-size: 16px; text-align: center; user-select: none; touch-action: none; }
.drag-handle:active { cursor: grabbing; }
td.drag-handle { width: 28px; }
.sortable-ghost { opacity: 0.4; }
.sortable-chosen { background: var(--card-alt); }

.kit-items-rows { display: flex; flex-direction: column; gap: 6px; }
.kit-item-row { display: flex; align-items: center; gap: 8px; background: var(--card-alt); border: 1px solid var(--border-soft); border-radius: 6px; padding: 6px 8px; }
.kit-item-row .ki-category { flex: 0 0 140px; }
.kit-item-row .ki-item { flex: 1 1 auto; }
.kit-item-row .ki-notes { flex: 1 1 auto; }
.kit-item-row .ki-remove { flex: 0 0 auto; }

/* ---------- Mobile layout ---------- */
@media (max-width: 640px) {
  .kit-item-row { flex-wrap: wrap; }
  .kit-item-row .ki-category,
  .kit-item-row .ki-item,
  .kit-item-row .ki-notes { flex: 1 1 100%; }
  .kit-item-row .ki-remove { flex: 0 0 auto; margin-left: auto; }

  .topbar { padding: 10px 14px; }
  .search-form { order: 3; max-width: none; margin: 0; flex: 1 1 100%; }
  #app { padding: 18px 14px 60px; }

  /* Too many nav links to wrap cleanly now — scroll horizontally as one row instead. */
  #nav { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
  #nav a { white-space: nowrap; flex: 0 0 auto; padding: 8px 10px; }

  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar > div:first-child { margin-bottom: 4px; }

  .card-grid { grid-template-columns: repeat(2, 1fr); }
  .card { padding: 14px 14px; }
  h1 { font-size: 19px; }

  form.inline-form { grid-template-columns: 1fr; }

  /* Dense admin tables: allow horizontal scroll inside the card rather than clipping/squeezing */
  .card { overflow-x: auto; }

  /* Crew-view tables: collapse into stacked label/value cards instead of scrolling. */
  table.crew-table thead { display: none; }
  table.crew-table, table.crew-table tbody, table.crew-table tr, table.crew-table td { display: block; width: 100%; }
  table.crew-table tr {
    border: 1px solid var(--border-soft);
    border-radius: 8px;
    padding: 8px 10px;
    margin-bottom: 10px;
    background: var(--card-alt);
  }
  table.crew-table td {
    border-bottom: none;
    padding: 4px 0;
    display: flex;
    justify-content: space-between;
    gap: 10px;
    text-align: right;
  }
  table.crew-table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    text-align: left;
    font-family: var(--sans);
  }
  table.crew-table td:first-child { font-family: var(--sans); }

  .compare-grid { grid-template-columns: 1fr; }
  .compare-grid iframe, .compare-grid .compare-empty { height: 280px; }

  /* #nav scrolls horizontally on mobile — an absolutely-positioned popover
     anchored inside it gets clipped by that same scroll container, so pin
     it to the viewport instead of the nav item on small screens. */
  .help-popover { position: fixed; top: 54px; right: 14px; left: 14px; width: auto; }
}

/* Very small phones: two stat columns get cramped — drop to one. */
@media (max-width: 400px) {
  .card-grid { grid-template-columns: 1fr; }
}
