:root {
  --red: #E0161F;
  --red-dark: #b3121a;
  --ink: #141414;
  --bg: #f3f3f4;
  --card: #fff;
  --line: #e2e2e5;
  --muted: #62626a;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font); }
[hidden] { display: none !important; }
body.drawer-open { overflow: hidden; }
.wrap { max-width: 1040px; margin: 0 auto; padding: 0 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error, .error-text { color: var(--red-dark); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
.inactive { opacity: .55; }

.test-banner { background: #ffd400; color: #111; text-align: center; font-weight: 700; padding: 6px; font-size: 13px; }

/* ---- Header -------------------------------------------------------------- */
.top { background: var(--ink); color: #fff; border-bottom: 4px solid var(--red); }
.top-inner { display: flex; align-items: center; gap: 14px; padding-top: 10px; padding-bottom: 10px; }
.logo { height: 48px; width: auto; display: block; }
.title { flex: 1; min-width: 0; }
.title h1 { margin: 0; font-size: 20px; letter-spacing: .3px; }
.title .muted { color: #b9b9c0; margin: 2px 0 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.home-link { color: #fff; }
.top-actions { display: flex; gap: 8px; }
.top-actions button { background: transparent; color: #fff; border: 1px solid #555; border-radius: 6px; padding: 6px 12px; }
.top-actions button:hover { border-color: #fff; }
.subnav { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 0; }
.subnav button { background: none; border: 0; color: #cfcfd4; padding: 8px 12px 10px; border-bottom: 3px solid transparent; white-space: nowrap; }
.subnav button.on { color: #fff; border-bottom-color: var(--red); font-weight: 700; }

/* ---- Home ---------------------------------------------------------------- */
.section-title { font-size: 17px; margin: 20px 0 10px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.tile { display: grid; gap: 6px; background: var(--card); border: 1px solid var(--line); border-top: 4px solid var(--red); border-radius: 10px; padding: 18px; color: inherit; text-decoration: none; }
.tile strong { font-size: 18px; }
.tile span { color: var(--muted); font-size: 14px; }
.tile:hover { border-color: #b8b8be; border-top-color: var(--red); }

/* ---- Common pieces ------------------------------------------------------- */
.panel { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px; margin: 16px 0; }
.panel h2 { margin: 0 0 8px; font-size: 17px; }
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin: 16px 0 4px; }
.control-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs button { background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; color: var(--ink); }
.tabs button.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.count { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--red); color: #fff; font-size: 12px; text-align: center; }
input[type=search] { border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; min-width: 200px; background: #fff; }
.check { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; }
.plain { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.empty { padding: 24px; text-align: center; }
.big-count { font-size: 32px; font-weight: 800; margin: 12px 0; }

.pill { display: inline-block; font-size: 12px; font-weight: 700; padding: 1px 9px; border-radius: 999px; background: #eee; color: #333; }
.pill.bad, .s-new { background: #fde3e4; color: #9e0f16; }
.s-in_progress { background: #dfe9ff; color: #1e3f9a; }
.s-waiting_parts { background: #fff1c7; color: #7a5600; }
.s-done { background: #dcf3e2; color: #17602f; }
.s-cancelled { background: #ececee; color: #55555c; }

.btn-sm { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 5px 12px; color: var(--ink); }
.btn-primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn-primary:hover { background: var(--red-dark); }
.link-btn { background: none; border: 0; color: var(--red-dark); padding: 4px 6px; text-decoration: underline; }
.link-btn.danger { margin-left: auto; }
.link-plain { background: none; border: 0; padding: 0; color: inherit; font-weight: 700; text-align: left; text-decoration: underline; text-decoration-color: #c9c9cf; }

/* ---- Work orders --------------------------------------------------------- */
.collecting-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.collecting-list li { border-left: 3px solid #f0b400; padding: 2px 0 2px 10px; }
.wo-list, .unit-list, .inv-list { list-style: none; margin: 12px 0 24px; padding: 0; display: grid; gap: 10px; }
.wo-btn, .unit-btn { display: block; width: 100%; text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; color: inherit; }
.wo-btn:hover, .unit-btn:hover { border-color: #b8b8be; }
.wo-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.wo-num { font-weight: 700; }
.wo-age { margin-left: auto; }
.wo-chassis { font-size: 20px; font-weight: 800; letter-spacing: .5px; margin: 4px 0 2px; }
.wo-what { font-weight: 600; }

/* ---- Inventory ----------------------------------------------------------- */
.inv-item { display: grid; grid-template-columns: 1fr auto; grid-template-areas: "main qty" "actions actions" "editor editor"; gap: 8px 12px; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; }
.inv-main { grid-area: main; min-width: 0; }
.inv-qty { grid-area: qty; font-size: 26px; font-weight: 800; min-width: 48px; text-align: right; }
.inv-qty.low { color: #a36b00; }
.inv-qty.off { color: var(--red-dark); }
.inv-actions { grid-area: actions; display: flex; flex-wrap: wrap; gap: 6px; }
.inv-editor { grid-area: editor; }
@media (min-width: 760px) {
  .inv-item { grid-template-columns: 1fr auto auto; grid-template-areas: "main actions qty" "editor editor editor"; align-items: center; }
}

/* ---- Units --------------------------------------------------------------- */
.unit-btn { display: flex; justify-content: space-between; gap: 12px; align-items: center; }
.unit-main { min-width: 0; }
.unit-due { flex-shrink: 0; text-align: right; }
.inspection { border-left: 3px solid var(--line); padding-left: 10px; }

/* ---- Shop team ----------------------------------------------------------- */
.team-list { list-style: none; margin: 10px 0; padding: 0; display: grid; gap: 8px; }
.team-add { display: flex; flex-wrap: wrap; gap: 8px; }
.team-add input { border: 1px solid var(--line); border-radius: 6px; padding: 7px 8px; min-width: 180px; }

/* ---- Drawer -------------------------------------------------------------- */
.drawer { position: fixed; inset: 0; z-index: 20; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .35); }
.drawer-panel { position: absolute; top: 0; right: 0; bottom: 0; width: min(560px, 100%); background: var(--card); overflow-y: auto; padding: 16px 20px 40px; box-shadow: -8px 0 24px rgba(0, 0, 0, .15); }
.drawer-panel h2 { margin: 0; font-size: 22px; }
.drawer-panel h3 { font-size: 15px; margin: 22px 0 8px; }
.d-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; border-bottom: 3px solid var(--red); padding-bottom: 10px; }
.d-status { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0; }
.st { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; color: var(--ink); }
.st.on { font-weight: 700; border-color: transparent; }
/* The current status keeps its color; .st's white background would otherwise win. */
.st.on.s-new { background: #fde3e4; color: #9e0f16; }
.st.on.s-in_progress { background: #dfe9ff; color: #1e3f9a; }
.st.on.s-waiting_parts { background: #fff1c7; color: #7a5600; }
.st.on.s-done { background: #dcf3e2; color: #17602f; }
.st.on.s-cancelled { background: #ececee; color: #55555c; }
.d-fields { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 12px 0 0; }
.d-fields dt { color: var(--muted); }
.d-fields dd { margin: 0; font-weight: 600; }
.d-fields a { color: var(--red-dark); }
.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; margin-top: 6px; }
.photos img { width: 100%; height: 110px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: #eee; }
.thread { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.msg { max-width: 85%; padding: 8px 12px; border-radius: 12px; }
.msg.in { background: #f0f0f2; justify-self: start; }
.msg.out { background: #fde9ea; justify-self: end; }
.msg-body { white-space: pre-wrap; }
.events { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
.events li { border-left: 3px solid var(--line); padding-left: 10px; }
.note-form { display: grid; gap: 8px; }
.note-form textarea { min-height: 64px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; resize: vertical; }
.note-form button { justify-self: start; }

/* ---- Forms --------------------------------------------------------------- */
.edit-form, .inline-form { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-top: 8px; background: #fafafa; }
.panel > .edit-form { margin: 0; border: 0; padding: 0; background: none; }
.edit-form .grid, .inline-form .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.edit-form label, .inline-form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.edit-form .wide { grid-column: 1 / -1; }
.edit-form input, .edit-form select, .edit-form textarea, .inline-form input, .inline-form select { border: 1px solid var(--line); border-radius: 6px; padding: 7px 8px; color: var(--ink); background: #fff; }
.edit-form textarea { width: 100%; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.tires { margin-top: 12px; }
.tire-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.tire-row select, .tire-row input { border: 1px solid var(--line); border-radius: 6px; padding: 6px; background: #fff; }
.tire-row input { flex: 1; min-width: 120px; }
.edit-actions { display: flex; gap: 10px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.team-add.inline-form { border: 0; padding: 0; background: none; }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; z-index: 30; max-width: calc(100% - 32px); }
.toast.bad { background: var(--red-dark); }

@media (max-width: 640px) {
  .logo { height: 36px; }
  .title h1 { font-size: 17px; }
  .top-actions button { padding: 5px 9px; font-size: 13px; }
  /* All four Maintenance tabs stay visible on a phone instead of scrolling off the edge. */
  .subnav { flex-wrap: wrap; overflow-x: visible; gap: 0; }
  .subnav button { padding: 8px 7px 9px; font-size: 13px; }
  input[type=search] { min-width: 0; width: 100%; }
  .control-row { width: 100%; }
  .edit-form .grid, .inline-form .grid { grid-template-columns: 1fr; }
  .drawer-panel { padding: 12px 14px 40px; }
  .unit-btn { display: block; }
  .unit-due { text-align: left; margin-top: 4px; }
}
