:root {
  color-scheme: dark;
  font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
  --page: #080d11;
  --surface: #0e171d;
  --surface-2: #121e25;
  --line: #2b3b44;
  --line-strong: #3a5059;
  --text: #dce7e5;
  --muted: #82979e;
  --accent: #54ddc5;
  --danger: #ff6666;
  --warning: #f0ba57;
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; margin: 0; background: var(--page); color: var(--text); }
button, input, select { font: inherit; letter-spacing: 0; }
button { color: inherit; }
[hidden] { display: none !important; }

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #080d11;
}

.login-panel {
  width: min(100%, 360px);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  padding: 24px;
  display: grid;
  gap: 10px;
}

.brand { color: var(--accent); font-weight: 700; font-size: 16px; margin-bottom: 8px; }
.login-panel label { color: var(--muted); font-size: 12px; }
.login-panel input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: #091218;
  color: var(--text);
  padding: 0 11px;
  outline: none;
}
.login-panel input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgb(84 221 197 / 15%); }
.command-button {
  min-height: 40px;
  border: 1px solid #3d9688;
  border-radius: 4px;
  background: #176659;
  color: #f0fffc;
  cursor: pointer;
}
.command-button:hover { background: #1c7768; }
.command-button:disabled { opacity: .55; cursor: wait; }
.form-error { min-height: 18px; margin: 0; color: var(--danger); font-size: 12px; }

.dashboard { min-height: 100vh; display: flex; flex-direction: column; }
.app-bar {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: #0b1318;
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-title { color: var(--accent); font-size: 13px; font-weight: 700; white-space: nowrap; }
.connection-summary { display: flex; align-items: center; justify-content: flex-end; gap: 8px; color: var(--muted); font-size: 12px; min-width: 0; }
.metric { border-left: 1px solid var(--line); padding-left: 8px; white-space: nowrap; }
.metric strong { color: var(--text); font-variant-numeric: tabular-nums; }
.status-dot { width: 8px; height: 8px; flex: 0 0 8px; border-radius: 50%; background: var(--muted); }
.status-dot.live { background: var(--accent); box-shadow: 0 0 8px rgb(84 221 197 / 50%); }
.status-dot.connecting { background: var(--warning); }
.status-dot.warning { background: var(--warning); box-shadow: 0 0 7px rgb(240 186 87 / 40%); }
.status-dot.offline { background: var(--danger); }

.icon-button {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-2);
  cursor: pointer;
}
.icon-button:hover { border-color: var(--accent); }

.empty-state { flex: 1; min-height: 60vh; display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--muted); }
.empty-state p { margin: 0; }

.radar-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
  gap: 8px;
  padding: 8px;
  align-items: start;
}

.radar-panel {
  container-type: inline-size;
  min-width: 0;
  height: calc(100vh - 64px);
  min-height: 520px;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) 30px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  overflow: hidden;
}

.panel-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-bottom: 1px solid var(--line);
  background: #0c151a;
  min-width: 0;
}
.panel-status { flex: 0 0 100%; min-width: 0; display: flex; align-items: center; gap: 10px; }
.panel-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.panel-count { color: var(--muted); font-size: 11px; white-space: nowrap; }
.panel-thermal { color: #c080ff; font-size: 12px; font-weight: 600; white-space: nowrap; }
.panel-spacer { flex: 1; }
.quick-toggle { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; white-space: nowrap; cursor: pointer; }
.quick-toggle input { accent-color: var(--accent); margin: 0; }

.merchant-toggle, .merchant-close {
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-2);
  padding: 5px 7px;
  font-size: 11px;
  white-space: nowrap;
  cursor: pointer;
}
.merchant-toggle { flex: 0 0 auto; }
.merchant-toggle:enabled { color: var(--accent); border-color: #34796e; }
.merchant-toggle[aria-pressed="true"] { background: #17443c; border-color: var(--accent); }
.merchant-toggle:disabled { color: #63757d; opacity: .6; cursor: default; }
.merchant-close:hover { border-color: var(--accent); }
.radar-panel.video-open .panel-main { grid-template-columns: minmax(0, 1fr) minmax(170px, 28%); }
.radar-panel.merchant-open .panel-main { grid-template-columns: minmax(0, 1fr) minmax(270px, 42%); }
.media-rail { display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; border-left: 1px solid var(--line); }
.media-rail > .merchant-sidebar { flex: 1; border-left: 0; }
.video-sync { position: relative; flex: 1; min-width: 0; min-height: 0; overflow: hidden; background: #05090c; }
.video-sync video { display: block; width: 100%; height: 100%; object-fit: contain; }
.media-rail.both .video-sync { flex: 0 0 36%; border-bottom: 1px solid var(--line); }
.video-enable { position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); max-width: calc(100% - 16px); padding: 8px 12px; border: 1px solid var(--accent); border-radius: 6px; background: #0c151aee; color: var(--text); cursor: pointer; }
.merchant-sidebar { min-width: 0; min-height: 0; display: flex; flex-direction: column; border-left: 1px solid var(--line); background: #0c151a; overflow: hidden; }
.merchant-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.merchant-header strong { font-size: 13px; }
.merchant-status, .merchant-legend { margin: 0; padding: 7px 10px 0; white-space: pre-line; overflow-wrap: anywhere; color: #b8c8c5; font-size: 11px; line-height: 1.5; }
.merchant-legend { color: var(--muted); padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.merchant-content { flex: 1; min-height: 0; min-width: 0; overflow: auto; }
.merchant-shelf { min-width: 0; padding: 10px; border-bottom: 1px solid var(--line); }
.merchant-shelf-heading { display: flex; flex-direction: column; gap: 4px; font-size: 11px; margin-bottom: 8px; }
.merchant-age, .merchant-note { color: var(--muted); font-size: 10px; }
.merchant-note { margin: 6px 0 0; }
.merchant-empty { margin: 16px 10px; color: var(--muted); font-size: 12px; }
.merchant-board-scroll { max-width: 100%; overflow-x: auto; padding-bottom: 5px; }
.merchant-matrix { --cell: 40px; display: grid; grid-template-columns: 22px repeat(var(--columns), var(--cell)); grid-template-rows: 20px repeat(var(--rows), var(--cell)); width: max-content; }
.merchant-axis { display: grid; place-items: center; font-size: 10px; color: var(--muted); }
.merchant-board { display: grid; grid-template-columns: repeat(var(--columns), var(--cell)); grid-template-rows: repeat(var(--rows), var(--cell)); background-color: #1b222c; background-image: linear-gradient(to right, #374350 1px, transparent 1px), linear-gradient(to bottom, #374350 1px, transparent 1px); background-size: var(--cell) var(--cell); }
.merchant-item { min-width: 0; min-height: 0; margin: 1px; padding: 4px; border: 1px solid #dce1e7; border-radius: 3px; background: #404750; color: #fff; font-size: 12px; line-height: 1.25; overflow: hidden; overflow-wrap: anywhere; white-space: pre-line; text-align: left; cursor: pointer; }
.merchant-item.tier-red { background: #69252a; border-color: #ff6969; }
.merchant-item.tier-yellow { background: #644e1a; border-color: #ffd55d; }
.merchant-item.tier-outline { background: #282f39; border-color: #6e7c8c; color: #9daab9; }
.merchant-item:hover, .merchant-item.selected { outline: 2px solid var(--accent); outline-offset: -3px; }
.merchant-details { flex: 0 1 auto; max-height: 38%; padding: 10px; border-top: 1px solid var(--line-strong); overflow: auto; overflow-wrap: anywhere; background: #122029; font-size: 11px; line-height: 1.5; }
.merchant-details p { margin: 5px 0; }
.merchant-details small { display: block; color: var(--muted); }
.merchant-candidates { margin: 6px 0; padding-left: 16px; }
.merchant-candidates li { margin-bottom: 5px; }

.panel-main { min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); position: relative; }
.canvas-wrap { min-width: 0; min-height: 0; display: grid; place-items: center; background: #071116; }
.radar-canvas { display: block; width: 100%; height: 100%; min-height: 0; }

.settings-popover {
  position: absolute;
  top: 8px;
  right: 8px;
  width: min(250px, calc(100% - 16px));
  z-index: 5;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: #101a20;
  box-shadow: 0 10px 28px rgb(0 0 0 / 45%);
  padding: 10px;
}
.setting-row { min-height: 34px; display: grid; grid-template-columns: 95px minmax(0, 1fr); align-items: center; gap: 8px; border-bottom: 1px solid #27363e; }
.setting-row:last-child { border-bottom: 0; }
.setting-row label { color: var(--muted); font-size: 11px; }
.setting-row input[type="range"] { width: 100%; accent-color: var(--accent); }
.setting-row input[type="checkbox"] { justify-self: end; accent-color: var(--accent); }
.setting-row select { width: 100%; min-width: 0; border: 1px solid var(--line-strong); border-radius: 3px; background: #091218; color: var(--text); padding: 4px; }

.panel-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 9px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  min-width: 0;
}
.binding-state { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.binding-state.unbound { color: var(--warning); }

.panel-sidebar { display: none; min-width: 240px; border-left: 1px solid var(--line); background: #0c151a; overflow: auto; }
.sidebar-section { padding: 10px; border-bottom: 1px solid var(--line); }
.sidebar-section h3 { margin: 0 0 8px; color: var(--muted); font-size: 11px; font-weight: 600; }
.sidebar-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 5px; }
.sidebar-list li { min-width: 0; display: flex; justify-content: space-between; gap: 8px; color: #b8c8c5; font-size: 11px; }
.sidebar-list li span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-list li span:last-child { color: var(--muted); white-space: nowrap; }

.radar-panel:fullscreen { width: 100vw; height: 100vh; min-height: 0; border: 0; border-radius: 0; }
.radar-panel:fullscreen .panel-main { grid-template-columns: minmax(0, 1fr) minmax(240px, 28vw); }
.radar-panel:fullscreen .panel-sidebar { display: block; }
.radar-panel.merchant-open:fullscreen .panel-main { grid-template-columns: minmax(0, 1fr) minmax(300px, 32vw); }
.radar-panel.merchant-open:fullscreen .panel-sidebar, .radar-panel.video-open:fullscreen .panel-sidebar { display: none; }

@container (max-width: 640px) {
  .radar-panel.merchant-open .panel-main, .radar-panel.video-open .panel-main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(140px, 42%) minmax(0, 1fr); }
  .media-rail { border-left: 0; border-top: 1px solid var(--line); }
  .merchant-open .canvas-wrap, .video-open .canvas-wrap { grid-column: 1; grid-row: 1; }
  .media-rail { grid-column: 1; grid-row: 2; }
}

@container (max-width: 520px) {
  .panel-header { gap: 5px; padding-inline: 6px; }
  .panel-spacer { display: none; }
  .panel-title { flex: 1; }
  .quick-toggle { gap: 3px; font-size: 10px; }
}

@media (min-width: 1400px) {
  .radar-grid[data-count="3"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .app-bar { min-height: 44px; padding: 6px 8px; }
  .metric { padding-left: 6px; }
  .radar-grid { grid-template-columns: 1fr; padding: 6px; gap: 6px; }
  .radar-panel { height: min(72vh, 620px); min-height: 410px; }
  .quick-toggle { font-size: 10px; }
  .radar-panel:fullscreen .panel-main { grid-template-columns: minmax(0, 1fr); }
  .radar-panel:fullscreen .panel-sidebar { display: none; }
  .radar-panel.merchant-open:fullscreen .panel-main, .radar-panel.video-open:fullscreen .panel-main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(140px, 42%) minmax(0, 1fr); }
}

@media (max-width: 460px) {
  .app-title { display: none; }
  .connection-summary { width: 100%; justify-content: space-between; }
}

@media (orientation: landscape) and (max-height: 600px) {
  .radar-grid { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
  .radar-panel { height: calc(100vh - 58px); min-height: 330px; }
}

.watch-picker { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.watch-picker select { width: min(100%, 260px); min-width: 0; height: 24px; padding: 1px 5px; border: 1px solid var(--line-strong); border-radius: 3px; background: #091218; color: var(--text); font-size: 11px; }
.watch-picker select:focus-visible { outline: 2px solid var(--accent); }
.watch-picker select:disabled { opacity: .6; }
