:root {
  --bg: #0b1020;
  --panel: #101633;
  --ink: #e9ecff;
  --line: #20285a;
  --brand: #91a6ff;
  --btn: #2b3aa3;
  --btn-hover: #3f52d1;
}

* { box-sizing: border-box; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  display: flex; flex-direction: column; align-items: center;
  padding: 1rem; margin: 0;
}
h1 { color: var(--brand); margin: 0.25rem 0 0.5rem; }
h2 { margin: 0.5rem 0; font-size: 1.05rem; }

main { max-width: 1040px; width: 100%; }

.shipment-input, .controls {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  margin: 12px 0;
}

.item-selector {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
}
.item-chip {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0d1430;
}
.item-chip input { transform: scale(1.1); }

.selector-actions { margin-top: 10px; display: flex; gap: 8px; flex-wrap: wrap; }
.controls { display: flex; align-items: center; gap: 10px; }
button {
  background: var(--btn); color: #fff; border: none; border-radius: 8px;
  padding: 8px 14px; cursor: pointer; transition: background .15s ease;
}
button:hover { background: var(--btn-hover); }
#stats { opacity: 0.95; }

canvas {
  margin-top: 12px;
  width: 100%; height: auto;
  background: radial-gradient(900px 600px at 50% 20%, #141a3c 0%, #0f1530 50%, #0b1020 100%);
  border: 1px solid var(--line);
  border-radius: 12px;
}

/* Fixed legend */
#legend {
  position: fixed;
  right: 16px; bottom: 16px;
  background: rgba(16, 22, 51, 0.9);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.35;
  color: var(--ink);
  backdrop-filter: blur(2px);
  z-index: 500; /* ensure it stays below RL panel */
}
#legend .row { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
#legend .k {
  display: inline-block; width: 18px; height: 12px; border-radius: 3px; border: 1px solid rgba(255,255,255,.15);
}
#legend .k.bot { background: #00b3ff; }
#legend .k.item { background: rgba(0,200,0,.75); }
#legend .k.picked { background: gold; }
#legend .k.shelf { background: rgba(255,255,255,.25); }
#legend .k.person { background: rgba(180,180,195,.8); }
#legend .k.drop { background: #00d36b; }
#legend .k.dock { background: #00c7c7; }

.item-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

/* ---- Letter buttons (A–J) ---- */
.item-btn {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  border-radius: 8px;
  border: 1px solid var(--line);
  outline: 2px solid transparent;
  background: #0d1430;
  color: #e9ecff;
  cursor: pointer;
  transition: background 0.15s, outline-color 0.15s, transform 0.1s;
}
.item-btn:hover {
  background: #1b245c;
  outline-color: #3f52d1;
  transform: scale(1.005);
}
.item-btn.selected {
  background: #3f52d1;
  color: #fff;
  border-color: #7a8aff;
}

/* ================= RL Telemetry Panels ================ */
.rl-card {
  background: radial-gradient(800px 600px at 100% 0%, #141a3c 0%, #0f1530 45%, #0b1020 100%);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 12px;
  margin: 12px auto;
  max-width: 960px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.03);
}
.rl-card-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 4px 2px 6px;
}
.rl-card-header h3 { margin: 0; font-size: 1rem; color: var(--ink); }
.rl-card-body { display: block; }

.rl-btn {
  background: #1a2150; color: #e9ecff; border: 1px solid #2a3380;
  padding: 6px 10px; border-radius: 10px; cursor: pointer; font-size: .9rem;
}
.rl-btn:hover { filter: brightness(1.1); }
.rl-danger { border-color: #8a2b2b; background: #3a1212; }

.rl-metrics-grid {
  display: grid; grid-template-columns: repeat(4, minmax(0,1fr));
  gap: 10px; margin-bottom: 8px;
}
.rl-label { font-size: .75rem; color: #aab0e5; }
.rl-value { font-size: 1.1rem; font-weight: 600; color: #fff; }

.rl-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

.rl-log-stream {
  height: 180px; overflow: auto; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .85rem; padding: 6px; border-radius: 10px; background: #0b1020; border: 1px solid var(--line);
}
.rl-log-line { white-space: pre-wrap; color: #ccd2ff; }
.rl-log-dim { color: #9aa1d9; }

/* Fixed-position variant for desktop/tablet widths */
.rl-fixed {
  position: fixed;
  right: 16px;
  /* JS computes bottom to clear legend on wide screens */
  width: 340px;
  max-height: 58vh;
  overflow: hidden;
  z-index: 501; /* above legend */
}

@media (max-width: 1100px) {
  .rl-fixed { position: static; width: auto; max-width: 960px; margin: 12px auto; }
}

#signature {
  position: fixed;
  bottom: 16px;
  left: 16px;
  font-size: 13px;
  color: rgba(233, 236, 255, 0.7);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  z-index: 502; /* above legend */
  user-select: none;
}
#signature:hover { color: #91a6ff; }

/* ===== Compact horizontal toolbar (actions left, A–J right) ===== */
.shipment-input { padding: 8px; margin: 8px 0; }

.shipment-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}

/* Left cluster: actions */
.selector-actions { display: flex; gap: 8px; flex: 0 0 auto; }

/* Right cluster: A–J, aligned to the right */
.shipment-toolbar .item-buttons {
  display: flex;
  flex: 1 1 auto;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: nowrap;
  overflow-x: hidden;
  padding-bottom: 2px;
  -ms-overflow-style: none;
  scrollbar-width: none;
}
.shipment-toolbar .item-buttons::-webkit-scrollbar { display: none; }
.selector-actions .primary { box-shadow: inset 0 0 0 1px rgba(255,255,255,.05); }

.controls { margin: 8px 0; padding: 8px 12px; }
canvas { margin-top: 8px; }

/* ===== Mobile onboarding (<=768px) ===== */
@media (max-width: 768px) {
  /* Lock main UI until user passes the mobile flow */
  body.mobile-locked main,
  body.mobile-locked #rl-ui,
  body.mobile-locked #legend {
    display: none !important;
  }

  #mobileIntro,
  #mobileSetup {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(5, 7, 15, 0.85);
    z-index: 9999;
    padding: 16px;
  }
  #mobileIntro[hidden],
  #mobileSetup[hidden] { display: none !important; }
  #mobileIntro:not([hidden]),
  #mobileSetup:not([hidden]) { display: flex !important; }

  .mi-card, .ms-card {
    width: 100%;
    max-width: 520px;
    background: radial-gradient(800px 600px at 100% 0%, #141a3c 0%, #0f1530 45%, #0b1020 100%);
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 14px 16px;
    color: var(--ink);
    box-shadow: inset 0 1px 0 rgba(255,255,255,.03);
  }

  .mi-card h3, .ms-card h3 {
    margin: 0 0 8px;
    color: var(--brand);
    font-size: 1.05rem;
  }
  .mi-card ol { margin: 0 0 8px 18px; }
  .mi-hint { opacity: .85; font-size: .9rem; }

  .ms-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
  }
  .ms-actions { display: flex; gap: 8px; flex-wrap: wrap; }
  .ms-footer { display: flex; justify-content: flex-end; margin-top: 12px; }

  #mobileItemButtons {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    margin-top: 8px;
  }
  #mobileItemButtons .item-btn {
    width: 40px; height: 40px; font-size: 18px;
  }
}

/* ===== Streamlined mobile run view (only while running) ===== */
@media (max-width: 768px) {
  /* Hide desktop toolbars during the run */
  body.mobile-running .shipment-input,
  body.mobile-running .controls { display: none !important; }

  /* Canvas sizing on phones */
  body.mobile-running canvas {
    margin: 0;
    width: 100vw;
    height: auto;
    max-height: 58vh;
  }

  /* RL panel pinned above footer */
  body.mobile-running #rl-ui {
    position: fixed !important;
    left: 8px !important;
    right: 8px !important;
    bottom: 72px !important;   /* raised to clear legend */
    width: auto !important;
    max-width: none !important;
    max-height: 34vh !important;
    z-index: 501;
  }

  /* Legend stays tucked in the corner, below RL */
  body.mobile-running #legend {
    right: 8px !important;
    bottom: 8px !important;
    transform: scale(.9);
    opacity: .9;
  }

  /* Footer pinned at the bottom */
  #signature {
    position: fixed;
    left: 12px;
    bottom: 8px;
  }
}

/* ===== Mobile (not running): RL panel sits under the canvas ===== */
@media (max-width: 768px) {
  /* shrink the map a bit */
  #stage {
    width: 100%;
    height: 420px;
    max-height: 55vh;
    display: block;
    margin: 0 auto;
  }

  /* Important: only when NOT running */
  body:not(.mobile-running) #rl-ui {
    position: static !important;
    right: auto !important;
    bottom: auto !important;
    width: auto;
    max-width: none;
    margin: 8px 12px 0 12px;   /* gap under the map */
  }

  /* tighten the card and hide console on phones */
  #rl-ui .rl-card-body { padding: 8px 10px; }
  #rl-log-stream { display: none !important; }
}

/* === Mobile scroll fix for RL panel overlap === */
@media (max-width: 768px) {
  /* Allow the whole viewport to scroll when RL panel is visible */
  body.mobile-running {
    overflow-y: auto !important;
    height: auto !important;
  }

  /* Give enough space at the bottom so legend isn't hidden */
  body.mobile-running::after {
    content: "";
    display: block;
    height: 140px;  /* adjust this if legend is still partly hidden */
  }

  /* Keep RL panel visually pinned near bottom but not blocking scroll */
  body.mobile-running #rl-ui {
    position: sticky !important;
    bottom: 8px !important;
    left: 8px !important;
    right: 8px !important;
    z-index: 501;
  }
}

/* === Mobile: make page scrollable, RL sticky, legend in-flow === */
@media (max-width: 768px) {
  /* Allow scrolling while the RL panel is up */
  body.mobile-running {
    overflow-y: auto !important;
    height: auto !important;
  }

  /* Spacer so you can scroll past the sticky RL box */
  body.mobile-running::after {
    content: "";
    display: block;
    height: 220px; /* adjust if your RL panel height changes */
  }

  /* RL panel: sticky near bottom, not fixed */
  body.mobile-running #rl-ui {
    position: sticky !important;
    bottom: 12px !important;
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    max-width: none !important;
    max-height: 34vh !important;
    z-index: 501; /* above content but below overlays */
  }

  /* Legend: remove fixed positioning so it won't be covered */
  body.mobile-running #legend {
    position: static !important;
    right: auto !important;
    bottom: auto !important;
    transform: none !important;
    opacity: 1 !important;
    z-index: auto !important;

    /* Mobile-friendly sizing/spacing */
    margin: 12px 12px 80px 12px;  /* bottom margin clears your footer */
    width: auto;
  }
}
