:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-border: #262d36;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #2DA3DA;
  --accent-dark: #2182AE;
  --green: #3fb950;
  --red: #f85149;
  --red-dim: #582929;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

h1, h2 { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.error { color: var(--red); margin-top: 0.5rem; }

/* ---- Login ---- */
.login-page {
  position: relative;
  min-height: 100vh;
  background: linear-gradient(to bottom, #0b1320, #0f1b2d);
}
.login-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}
.login-overlay {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr;
  min-height: 100vh;
  width: 100%;
  padding: 1.5rem;
  background: linear-gradient(to bottom, rgba(11,19,32,0.25), rgba(15,27,45,0.30));
  align-items: center;
  align-content: center;
  justify-items: center;
  gap: 0;
}
.login-marketing { display: none; }
.login-form-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
}
.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.18);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border: none;
  border-radius: 12px;
  padding: 2rem;
}
.login-logo { margin-bottom: 1rem; }
.login-logo img { max-width: 200px; height: auto; display: block; margin: 0 auto; }
.login-card .subtitle { margin: 0 0 1.5rem; color: rgba(255,255,255,0.85); }
.login-card label { display: block; font-size: 0.85rem; margin-bottom: 0.75rem; color: rgba(255,255,255,0.90); }
.login-card input {
  display: block;
  width: 100%;
  padding: 0.6rem 0.75rem;
  margin-top: 0.25rem;
  background: rgba(255,255,255,0.80);
  border: 1px solid rgba(255,255,255,0.30);
  border-radius: 4px;
  color: #161b22;
  font-size: 1rem;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  width: 100%;
  padding: 0.75rem;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 4px;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 0.5rem;
}
.login-card button:hover { background: var(--accent-dark); }
.login-footer-note {
  position: fixed;
  bottom: 0.5rem;
  left: 50%;
  transform: translateX(-50%);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  opacity: 0.70;
  pointer-events: none;
  user-select: none;
  white-space: nowrap;
  margin: 0;
}

@media (min-width: 768px) {
  .login-overlay {
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    padding: 1.5rem 6rem;
    justify-items: stretch;
    align-content: normal;
  }
  .login-marketing {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    color: #fff;
    max-width: 480px;
    margin: auto;
  }
  .login-heading {
    font-size: 3rem;
    font-weight: 700;
    line-height: 1.2;
    margin: 0;
  }
  .login-subtext {
    color: rgba(255,255,255,0.80);
    font-size: 1.05rem;
    margin: 0;
  }
}

/* ---- Top bar ---- */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.25rem;
  background: #ffffff;
  border-bottom: 1px solid #e0e0e0;
}
.brand { display: flex; align-items: center; }
.brand img { height: 36px; width: auto; }
.top-right { display: flex; gap: 0.5rem; }
.top-right button {
  padding: 0.45rem 0.9rem;
  background: #21262d;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
}
.top-right button:hover { background: #30363d; }
#logout-btn { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 600; }
#logout-btn:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.manual-btn {
  padding: 0.45rem 0.9rem;
  background: var(--accent);
  color: #111;
  border: 1px solid var(--accent);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  line-height: normal;
  white-space: nowrap;
}
.manual-btn:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.alerts-btn.has-alerts { background: var(--red-dim); border-color: var(--red); }

/* ---- Alerts panel ---- */
.alerts-panel {
  background: var(--red-dim);
  border-bottom: 1px solid var(--red);
  padding: 0.75rem 1.25rem;
}
.alerts-panel ul { margin: 0; padding-left: 1.25rem; }
.alerts-panel li { margin: 0.25rem 0; }

.fatal-banner {
  background: var(--red);
  color: white;
  text-align: center;
  padding: 0.5rem;
  font-weight: 600;
}

/* ---- Forklift bar ---- */
.forklift-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--panel-border);
}
.forklift-name { font-weight: 600; font-size: 1.1rem; color: var(--text); }
.auto-refresh-note { color: var(--muted); font-size: 0.9rem; margin-left: auto; }

/* ---- Stacked layout for iMOV + Map ---- */
.grid-main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  padding: 1rem 1.25rem 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 1rem;
}
.panel h2 { font-size: 1rem; color: var(--muted); font-weight: 500; margin-bottom: 0.75rem; }

.imov-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem 1rem;
}
.imov-panel .imov-grid,
.detail-panel .imov-grid {
  display: flex;
  gap: 1.5rem;
  padding: 0 2rem;
  justify-content: center;
}
.imov-col {
  flex: 1;
  min-width: 0;
  max-width: 440px;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
}
.imov-col .kv:last-child { border-bottom: none; }
.imov-grid .kv { display: flex; gap: 0.75rem; align-items: baseline; border-bottom: 1px dashed var(--panel-border); padding: 0.25rem 0; min-width: 0; }
.imov-grid .kv .k { color: var(--muted); flex-shrink: 0; }
.imov-grid .kv .v { font-family: "JetBrains Mono", Menlo, Consolas, monospace; font-size: 0.9rem; flex: 1; min-width: 0; overflow-wrap: break-word; word-break: break-word; text-align: right; }
.imov-col .kv .k { min-width: 135px; }
.imov-col .kv .v { text-align: center; }

/* ---- Map ---- */
.map-panel { display: flex; flex-direction: column; }
.map-wrap {
  position: relative;
  width: 100%;
  height: 520px;          /* use more of the panel; viewBox adapts to aspect */
  margin: 0 auto;
}
/* Greyed-out forklift while the last-known fix is stale. */
.stale-fix { opacity: 0.35; filter: grayscale(1); }
#map-svg {
  width: 100%;
  height: 100%;
  background: #0b0e13;
  border-radius: 4px;
}
.floor { fill: #161f2e; stroke: #3a4a68; stroke-width: 0.02; }
.anchor-dot { fill: var(--accent); stroke: #222; stroke-width: 0.01; }
.anchor-dot.stale { fill: #555; }
.anchor-label { fill: var(--text); font-size: 0.18px; font-family: monospace; }
.map-overlay {
  position: absolute;
  top: 0.5rem; left: 50%;
  transform: translateX(-50%);
  padding: 0.25rem 0.75rem;
  background: rgba(0, 0, 0, 0.75);
  color: var(--text);
  border-radius: 4px;
  font-size: 0.85rem;
  white-space: nowrap;
  pointer-events: none;
}
.map-tooltip {
  position: absolute;
  padding: 0.15rem 0.45rem;
  background: rgba(15, 23, 42, 0.92);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 3px;
  font-family: monospace;
  font-size: 0.75rem;
  white-space: nowrap;
  pointer-events: none;
  transform: translate(8px, -50%);
  z-index: 2;
}
.map-tooltip.flip { transform: translate(calc(-100% - 8px), -50%); }

/* ---- Anchors panel ---- */
.anchors-row { display: flex; align-items: center; justify-content: center; gap: 1.5rem; flex-wrap: wrap; }
.anchor-group {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
}
.anchors-label { font-weight: 600; color: var(--muted); min-width: 3rem; }
.dot-row { display: inline-flex; gap: 0.6rem; align-items: center; }
.dot-row .dot {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  font-family: monospace;
}
.dot .circle {
  width: 14px; height: 14px; border-radius: 50%;
  background: #555;
  border: 1px solid #222;
}
.dot.fresh .circle { background: var(--green); box-shadow: 0 0 4px var(--green); }

/* ---- Toggle row ---- */
.toggle-row {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  padding: 1rem;
}
.algo-btn {
  padding: 0.6rem 2rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  background: #21262d;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  cursor: pointer;
  min-width: 100px;
}
.algo-btn.active {
  background: var(--accent);
  color: #111;
  border-color: var(--accent);
  font-weight: 600;
}

.detail-panel { margin: 0 1.25rem 0; }

/* ---- Footer ---- */
.app-footer {
  margin-top: 1.5rem;
  padding: 0.75rem 1.25rem;
  background: #ffffff;
  border-top: 1px solid #e0e0e0;
  text-align: center;
  font-size: 1.1rem;
  font-weight: 600;
  color: #111;
}

/* ---- Admin button + modal ---- */
.admin-btn {
  font-size: 1.1rem;
  line-height: 1;
  min-width: 2.25rem;
}

.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex; align-items: flex-start; justify-content: center;
  z-index: 100;
  overflow-y: auto;
  padding: 2rem 1rem;
}
/* The HTML `hidden` attribute normally means `display: none`, but the .modal
   rule above sets an explicit display which overrides it. Re-assert that
   hidden wins for the modal. */
.modal[hidden] { display: none; }
.modal-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  width: min(720px, 100%);
  color: var(--text);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--panel-border);
}
.modal-header h2 { margin: 0; color: var(--accent); }
.modal-close {
  background: transparent; border: 0; color: var(--muted);
  font-size: 1.5rem; cursor: pointer; line-height: 1;
}
.modal-section {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--panel-border);
}
.modal-section:last-child { border-bottom: 0; }
.modal-section h3 {
  margin: 0 0 0.5rem; color: var(--text); font-size: 1rem;
}
.modal-section label.inline {
  display: inline-flex; align-items: center; gap: 0.5rem;
  margin-right: 0.75rem; color: var(--muted); font-size: 0.9rem;
}
.modal-section input[type="number"],
.modal-section input[type="text"] {
  background: #0b0e13;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 0.3rem 0.5rem;
  width: 90px;
  font-family: monospace;
}
.modal-btn {
  padding: 0.4rem 0.9rem;
  margin-right: 0.5rem;
  background: #21262d;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  cursor: pointer;
}
.modal-btn:hover { background: #30363d; }
.modal-btn.primary { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 600; }
.modal-btn.primary:hover { background: var(--accent-dark); }

.alert-row {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.4rem 0;
  border-bottom: 1px dashed var(--panel-border);
  flex-wrap: wrap;
}
.alert-row:last-child { border-bottom: 0; }
.alert-row .alert-name { min-width: 180px; font-family: monospace; font-size: 0.85rem; }
.alert-row .alert-fields { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.alert-row .alert-fields label { font-size: 0.75rem; color: var(--muted); }

.data-usage-bar {
  height: 14px;
  background: #0b0e13;
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  overflow: hidden;
  margin: 0.25rem 0 0.75rem;
}
.data-usage-bar > div { height: 100%; background: var(--accent); transition: width 0.3s ease; }
.data-usage-bar.warn > div { background: #d99b00; }
.data-usage-bar.critical > div { background: var(--red); }

/* ---- Responsive logo sizing ---- */
@media (max-width: 480px) {
  .brand img { height: 26px; }
  .login-logo img { max-width: 160px; }
  .imov-grid { grid-template-columns: 1fr; }
  .imov-panel .imov-grid,
  .detail-panel .imov-grid { flex-direction: column; gap: 1rem; padding: 0; justify-content: flex-start; }
  .imov-col { max-width: none; }
  .anchor-group { width: 100%; }

  /* Logo centred on its own line, buttons row below */
  .top-bar {
    flex-wrap: wrap;
    gap: 0.4rem 0;
    padding: 0.6rem 1rem;
  }
  .brand {
    flex: 1 1 100%;
    justify-content: center;
  }
  .top-right {
    flex: 1 1 100%;
    justify-content: center;
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  /* Dynamic font across all header buttons — clamp prevents Setup Manual overflow */
  .top-right button,
  .top-right .manual-btn {
    font-size: clamp(0.62rem, 3vw, 0.78rem);
    padding: 0.3rem 0.5rem;
  }

  /* Shrink UWB/FTM toggle buttons */
  .algo-btn {
    padding: 0.35rem 1.25rem;
    font-size: 0.78rem;
    min-width: 70px;
  }

  /* Shrink forklift bar text */
  .forklift-name { font-size: 0.9rem; }
  .auto-refresh-note { font-size: 0.75rem; }

  /* Shrink alerts panel text */
  .alerts-panel li { font-size: 0.78rem; }

  .app-footer { font-size: 0.82rem; }
}

/* ---- Initial load spinner ---- */
#dash {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
#dash[hidden] { display: none; }
#initial-loader {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}
#initial-loader[hidden] { display: none; }
.loader-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(230, 237, 243, 0.12);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spinner-spin 0.8s linear infinite;
}
@keyframes spinner-spin { to { transform: rotate(360deg); } }
.loader-text {
  color: var(--muted);
  font-size: 1rem;
  letter-spacing: 0.02em;
}

/* ---- Desktop content width constraint ---- */
@media (min-width: 481px) {
  .grid-main {
    max-width: 1100px;
    margin-left: auto;
    margin-right: auto;
  }
  .toggle-row {
    max-width: 1100px;
    margin-left: auto;
    margin-right: auto;
  }
  /* detail-panel sits outside grid-main; subtract grid-main's 1.25rem side padding
     so it aligns flush with the panels above it */
  .detail-panel {
    max-width: calc(1100px - 2.5rem);
    margin-left: auto;
    margin-right: auto;
  }

  /* Anchors — two inner boxes mirroring iMOV column layout */
  .anchors-row {
    padding: 0 2rem;
    flex-wrap: nowrap;
  }
  .anchor-group {
    flex: 1;
    max-width: 440px;
  }
}
