:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-soft: #1a2437;
  --border: #334155;
  --fg: #f1f5f9;
  --muted: #94a3b8;

  --free: #4ade80;
  --free-bg: rgba(34, 197, 94, 0.12);
  --free-border: rgba(34, 197, 94, 0.35);

  --busy: #fbbf24;
  --busy-bg: rgba(245, 158, 11, 0.14);
  --busy-border: rgba(245, 158, 11, 0.38);

  --off: #94a3b8;
  --off-bg: rgba(148, 163, 184, 0.12);
  --off-border: rgba(148, 163, 184, 0.3);

  --danger: #f87171;
  --danger-bg: rgba(239, 68, 68, 0.14);
  --danger-border: rgba(239, 68, 68, 0.36);

  --radius: 14px;
  --shadow: 0 1px 2px rgba(2, 6, 23, 0.6), 0 8px 24px rgba(2, 6, 23, 0.35);
}

* {
  box-sizing: border-box;
}

/* 下面几个浮层用了 display:flex，会盖掉浏览器对 [hidden] 的默认 display:none */
[hidden] {
  display: none !important;
}

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

body {
  margin: 0;
  background: radial-gradient(120% 90% at 50% 0%, #16233c 0%, var(--bg) 55%) no-repeat, var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
    "Noto Sans SC", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

h1,
h2,
h3,
p {
  margin: 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;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 40;
  padding: 10px 14px;
  background: var(--panel);
  border-radius: 0 0 var(--radius) 0;
  color: var(--fg);
}

.skip-link:focus {
  left: 0;
}

:focus-visible {
  outline: 2px solid var(--free);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ------------------------------- 顶栏 ------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: center;
  justify-content: space-between;
  padding: 14px max(20px, env(safe-area-inset-left)) 14px max(20px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.86);
  backdrop-filter: blur(12px);
}

.place-name {
  font-size: 19px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.place-meta {
  margin-top: 2px;
  color: var(--muted);
  font-size: 13px;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.live {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.live[data-state="ok"] .live-dot {
  background: var(--free);
  animation: pulse 2s ease-in-out infinite;
}

.live[data-state="error"] .live-dot {
  background: var(--danger);
}

.live[data-state="loading"] .live-dot {
  background: var(--busy);
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
    box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.45);
  }
  50% {
    opacity: 0.75;
    box-shadow: 0 0 0 6px rgba(74, 222, 128, 0);
  }
}

.select,
.input {
  min-height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
}

.select {
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 40px;
  padding: 9px 15px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  font-weight: 550;
  cursor: pointer;
  transition: background-color 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.btn:hover {
  background: #27364d;
  border-color: #475569;
}

.btn:active {
  transform: scale(0.98);
}

.btn[disabled] {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--free);
  border-color: var(--free);
  color: #05230f;
}

.btn-primary:hover {
  background: #34d374;
  border-color: #34d374;
}

.icon {
  width: 16px;
  height: 16px;
  flex: none;
}

/* ------------------------------ 提示条 ------------------------------ */

.banner {
  margin: 18px auto 0;
  max-width: 1240px;
  padding: 12px 15px;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius);
  background: var(--danger-bg);
  color: #fecaca;
  font-size: 14px;
}

.banner strong {
  display: block;
  color: #fee2e2;
  font-weight: 600;
}

.banner .banner-hint {
  margin-top: 4px;
  color: #fca5a5;
}

/* ------------------------------- 主体 ------------------------------- */

main {
  max-width: 1240px;
  margin: 0 auto;
  padding: 22px max(20px, env(safe-area-inset-left)) 40px max(20px, env(safe-area-inset-right));
  display: grid;
  gap: 26px;
}

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 12px;
  margin-bottom: 12px;
}

h2 {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #cbd5e1;
}

.hint {
  color: var(--muted);
  font-size: 13px;
}

/* ------------------------------ 总览卡 ------------------------------ */

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.stat {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.stat-value {
  font-size: 30px;
  font-weight: 650;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  margin-top: 2px;
  color: var(--muted);
  font-size: 13px;
}

.stat[data-tone="free"] .stat-value {
  color: var(--free);
}

.stat[data-tone="busy"] .stat-value {
  color: var(--busy);
}

.stat[data-tone="danger"] .stat-value {
  color: var(--danger);
}

/* ------------------------------ 工位卡 ------------------------------ */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(232px, 1fr));
  gap: 12px;
}

/* 空状态/错误说明不要挤在单列里 */
.grid > .empty,
.grid > .error-box {
  grid-column: 1 / -1;
}

.bay {
  padding: 14px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--off);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.bay[data-state="free"] {
  border-left-color: var(--free);
}

.bay[data-state="busy"] {
  border-left-color: var(--busy);
}

.bay[data-state="alert"] {
  border-left-color: var(--danger);
}

.bay-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.bay-name {
  font-size: 16px;
  font-weight: 600;
}

.badge {
  flex: none;
  padding: 3px 10px;
  border: 1px solid var(--off-border);
  border-radius: 999px;
  background: var(--off-bg);
  color: var(--off);
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
}

.badge[data-tone="free"] {
  border-color: var(--free-border);
  background: var(--free-bg);
  color: var(--free);
}

.badge[data-tone="busy"] {
  border-color: var(--busy-border);
  background: var(--busy-bg);
  color: var(--busy);
}

.badge[data-tone="danger"] {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger);
}

.bay-facts {
  margin: 10px 0 0;
  display: grid;
  gap: 5px;
  font-size: 13px;
}

.fact {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.fact dt {
  color: var(--muted);
  flex: none;
}

.fact dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: #e2e8f0;
}

.fact dd.is-alert {
  color: var(--danger);
  font-weight: 600;
}

.bars {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 12px;
  margin-right: 5px;
  vertical-align: -1px;
}

.bars i {
  width: 3px;
  border-radius: 1px;
  background: #475569;
}

.bars i:nth-child(1) {
  height: 4px;
}

.bars i:nth-child(2) {
  height: 6px;
}

.bars i:nth-child(3) {
  height: 9px;
}

.bars i:nth-child(4) {
  height: 12px;
}

.bars[data-level="4"] i,
.bars[data-level="3"] i:nth-child(-n + 3),
.bars[data-level="2"] i:nth-child(-n + 2),
.bars[data-level="1"] i:nth-child(1) {
  background: var(--free);
}

.bars[data-level="2"] i:nth-child(-n + 2) {
  background: var(--busy);
}

.bars[data-level="1"] i:nth-child(1) {
  background: var(--danger);
}

/* ------------------------------ 抓拍图 ------------------------------ */

.shots {
  display: grid;
  /* auto-fit：只有一张抓拍图时铺满整行 */
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 12px;
}

.shot {
  position: relative;
  display: block;
  /* 摄像头原图仅 640×360，限制宽度避免被拉伸放大 */
  width: 100%;
  max-width: 720px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #0b1220;
  overflow: hidden;
  cursor: zoom-in;
  transition: border-color 160ms ease, transform 160ms ease;
}

.shot:hover {
  border-color: #475569;
  transform: translateY(-1px);
}

.shot img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: contain;
  background: #0b1220;
}

.shot-cap {
  position: absolute;
  left: 10px;
  bottom: 10px;
  padding: 3px 9px;
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 999px;
  background: rgba(2, 6, 23, 0.72);
  color: #e2e8f0;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  backdrop-filter: blur(6px);
}

.empty,
.error-box {
  padding: 16px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: var(--panel-soft);
  color: var(--muted);
  font-size: 14px;
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  background: rgba(2, 6, 23, 0.92);
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 10px;
}

.lightbox-close {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--panel);
  color: var(--fg);
  cursor: pointer;
}

/* ------------------------------- 页脚 / 登录 ------------------------------- */

.foot {
  max-width: 1240px;
  margin: 0 auto;
  padding: 0 max(20px, env(safe-area-inset-left)) 32px max(20px, env(safe-area-inset-right));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.link {
  padding: 6px 2px;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
}

.link:hover {
  color: var(--fg);
}

.gate {
  position: fixed;
  inset: 0;
  z-index: 35;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(2, 6, 23, 0.94);
}

.gate-card {
  display: grid;
  gap: 12px;
  width: 100%;
  max-width: 340px;
  padding: 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.gate-card h2 {
  font-size: 17px;
  text-transform: none;
  letter-spacing: 0;
  color: var(--fg);
}

.field-block {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.gate-error {
  min-height: 18px;
  color: var(--danger);
  font-size: 13px;
}

/* ------------------------------- 响应式 ------------------------------- */

@media (max-width: 720px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stat-value {
    font-size: 26px;
  }

  .topbar {
    padding-top: 12px;
    padding-bottom: 12px;
  }

  .place-name {
    font-size: 17px;
  }

  .shots {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
