:root {
  color-scheme: dark;
  --bg: #061321;
  --surface: #0b2034;
  --surface-2: #102a43;
  --line: #1d4867;
  --line-bright: #2b6f96;
  --text: #ecf7ff;
  --muted: #88a9bf;
  --cyan: #32c7dd;
  --green: #35d07f;
  --amber: #f4b740;
  --red: #ff5b64;
  --blue: #4f97ff;
  --shadow: 0 16px 38px rgba(0, 0, 0, .22);
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  background:
    linear-gradient(rgba(27, 69, 97, .11) 1px, transparent 1px),
    linear-gradient(90deg, rgba(27, 69, 97, .11) 1px, transparent 1px),
    #061321;
  background-size: 44px 44px, 44px 44px, auto;
}

button, input, select { font: inherit; }
button, select { cursor: pointer; }

.app-shell { min-height: 100vh; }

.topbar {
  min-height: 82px;
  padding: 14px clamp(16px, 3vw, 42px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  background: rgba(8, 27, 46, .96);
  border-bottom: 1px solid var(--line);
}

.brand-block, .topbar-actions, .gas-card-head, .panel-head, .section-heading,
.status-band, .threshold-row, .chart-legend, .page-title {
  display: flex;
  align-items: center;
}

.brand-block { gap: 13px; min-width: 0; }
.brand-mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid #3b8cad;
  border-radius: 6px;
  color: #bff9ff;
  background: linear-gradient(145deg, #17476a, #0c2a42);
  box-shadow: inset 0 0 18px rgba(50, 199, 221, .13);
  font-weight: 800;
}

h1, h2, h3, p { margin: 0; }
.brand-block h1 { font-size: 26px; letter-spacing: 0; }
.brand-block p { margin-top: 4px; color: var(--muted); font-size: 13px; }
.topbar-actions { gap: 16px; }

.device-picker { display: grid; gap: 4px; color: var(--muted); font-size: 11px; }
.device-picker select, .filter-bar input, .filter-bar select {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 34px 0 11px;
  color: var(--text);
  background: #0b2238;
  outline: none;
}
.device-picker select:focus, .filter-bar input:focus, .filter-bar select:focus { border-color: var(--cyan); }

.sound-toggle {
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--muted);
  background: #0b2238;
}
.sound-toggle strong { color: var(--text); }
.sound-toggle[aria-pressed="true"] { border-color: #27996a; color: #a8f0cc; }
.sound-dot { display: inline-block; width: 8px; height: 8px; margin-right: 7px; border-radius: 50%; background: #607a8d; }
.sound-toggle[aria-pressed="true"] .sound-dot { background: var(--green); box-shadow: 0 0 10px rgba(53, 208, 127, .75); }

.clock-block { min-width: 94px; text-align: right; }
.clock-block strong { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.clock-block span { color: var(--muted); font-size: 11px; }

.main-nav {
  height: 54px;
  padding: 8px clamp(16px, 3vw, 42px);
  display: flex;
  gap: 6px;
  background: #0a1c30;
  border-bottom: 1px solid var(--line);
}
.nav-tab {
  min-width: 132px;
  padding: 0 24px;
  border: 1px solid transparent;
  border-radius: 4px;
  color: #94b2c6;
  background: transparent;
  font-weight: 700;
}
.nav-tab:hover { color: var(--text); background: rgba(34, 83, 116, .28); }
.nav-tab.is-active { color: white; border-color: #3a87b1; background: #174a70; box-shadow: inset 0 0 14px rgba(58, 167, 211, .15); }

main { width: min(1880px, 100%); margin: 0 auto; padding: 18px clamp(14px, 2vw, 30px) 32px; }
.view { display: none; }
.view.is-active { display: block; }

.status-band {
  justify-content: space-between;
  min-height: 48px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-left: 3px solid #617c91;
  border-radius: 4px;
  background: rgba(11, 32, 52, .78);
  color: var(--muted);
  font-size: 13px;
}
.status-band > div:first-child { display: flex; align-items: center; gap: 9px; }
.status-band strong { color: var(--text); }
.status-light { width: 9px; height: 9px; border-radius: 50%; background: #617c91; }
.status-band.is-online { border-left-color: var(--green); }
.status-band.is-online .status-light { background: var(--green); box-shadow: 0 0 10px rgba(53, 208, 127, .75); }
.status-band.is-alarm { border-color: rgba(255, 91, 100, .7); border-left-color: var(--red); background: rgba(93, 24, 34, .4); }
.status-band.is-alarm .status-light { background: var(--red); box-shadow: 0 0 10px rgba(255, 91, 100, .8); }
.status-meta { display: flex; gap: 25px; }
.status-meta span { white-space: nowrap; }

.section-heading { justify-content: space-between; margin: 24px 2px 12px; }
.section-heading h2, .panel-head h2, .page-title h2 { font-size: 20px; letter-spacing: 0; }
.eyebrow { color: #5aa6c8; font-size: 10px; font-weight: 700; letter-spacing: .12em; margin-bottom: 4px; }
.overall-state { min-width: 88px; padding: 7px 12px; border: 1px solid; border-radius: 4px; text-align: center; font-size: 13px; font-weight: 700; }
.state-offline { color: #9cb0bf; border-color: #536a79; background: rgba(83, 106, 121, .13); }
.state-normal { color: #8eedbb; border-color: #2b9d68; background: rgba(43, 157, 104, .14); }
.state-alarm { color: #ffb4b8; border-color: var(--red); background: rgba(255, 91, 100, .14); }

.gas-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.gas-card {
  min-width: 0;
  min-height: 206px;
  padding: 16px;
  border: 1px solid var(--line);
  border-top: 3px solid var(--gas-color, var(--cyan));
  border-radius: 6px;
  background: linear-gradient(155deg, rgba(17, 48, 75, .98), rgba(9, 29, 48, .98));
  box-shadow: var(--shadow);
}
.gas-card[data-gas="o2"] { --gas-color: #43c4e4; }
.gas-card[data-gas="co"] { --gas-color: #4f97ff; }
.gas-card[data-gas="h2s"] { --gas-color: #35d07f; }
.gas-card[data-gas="ch4"] { --gas-color: #f4b740; }
.gas-card.is-alarm { border-color: var(--red); border-top-color: var(--red); background: linear-gradient(155deg, rgba(81, 27, 39, .97), rgba(36, 19, 31, .98)); animation: alarmPulse 1.6s ease-in-out infinite; }
@keyframes alarmPulse { 50% { box-shadow: 0 0 24px rgba(255, 91, 100, .22); } }

.gas-card-head { justify-content: space-between; gap: 10px; }
.gas-card-head > div { display: flex; align-items: baseline; gap: 9px; min-width: 0; }
.gas-code { color: var(--gas-color); font-size: 20px; font-weight: 800; }
.gas-card h3 { font-size: 13px; color: var(--muted); font-weight: 500; }
.gas-status { padding: 4px 8px; border: 1px solid #476275; border-radius: 3px; color: #aabeca; font-size: 11px; white-space: nowrap; }
.gas-card.is-normal .gas-status { border-color: #277a56; color: #78dda8; }
.gas-card.is-alarm .gas-status { border-color: var(--red); color: #ffc3c6; }
.gas-reading { margin: 22px 0 19px; display: flex; align-items: baseline; gap: 7px; }
.gas-reading strong { font-size: 42px; line-height: 1; font-weight: 700; letter-spacing: 0; font-variant-numeric: tabular-nums; }
.gas-reading span { color: var(--muted); font-size: 12px; }

.range-track { position: relative; height: 7px; border-radius: 2px; background: linear-gradient(90deg, #318866, #3eaa7d 60%, #b7892d 80%, #aa3c48); }
.gas-card[data-gas="o2"] .range-track { background: linear-gradient(90deg, #aa3c48 0 21%, #3eaa7d 34% 70%, #b7892d 82%, #aa3c48); }
.range-track i { position: absolute; inset: -3px auto -3px 0; width: 2px; background: white; box-shadow: 0 0 7px white; transform: translateX(-1px); transition: left .35s ease; }
.range-track b { position: absolute; inset: -3px 0 -3px auto; width: 1px; background: rgba(255,255,255,.45); }
.threshold-row { justify-content: space-between; margin-top: 12px; color: var(--muted); font-size: 11px; }
.threshold-row strong { color: #d8e9f3; font-variant-numeric: tabular-nums; }

.monitor-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(320px, .85fr); gap: 12px; margin-top: 12px; }
.panel { border: 1px solid var(--line); border-radius: 6px; background: rgba(10, 31, 51, .94); box-shadow: var(--shadow); }
.panel-head { justify-content: space-between; min-height: 61px; padding: 12px 15px; border-bottom: 1px solid var(--line); }
.panel-head h2 { font-size: 17px; }
.gas-switch { display: flex; padding: 3px; border: 1px solid var(--line); border-radius: 4px; background: #081a2c; }
.gas-switch button { width: 52px; height: 30px; border: 0; border-radius: 3px; color: var(--muted); background: transparent; font-size: 12px; font-weight: 700; }
.gas-switch button.is-active { color: white; background: #1d597f; }

.chart-wrap { position: relative; height: 278px; padding: 10px 12px 2px; }
#trendChart { display: block; width: 100%; height: 100%; }
.chart-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 13px; pointer-events: none; }
.chart-legend { justify-content: flex-start; gap: 18px; min-height: 42px; padding: 8px 15px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px; }
.chart-legend span { display: flex; align-items: center; gap: 6px; }
.chart-legend i { width: 14px; height: 2px; display: inline-block; }
.legend-current { background: var(--cyan); }
.legend-low { border-top: 1px dashed var(--amber); }
.legend-high { border-top: 1px dashed var(--red); }
.chart-legend strong { margin-left: auto; color: #bcd1df; font-weight: 500; }

.alarm-count { min-width: 34px; height: 28px; display: grid; place-items: center; border-radius: 4px; color: white; background: #30526a; font-weight: 800; }
.alarm-count.has-alarm { background: var(--red); }
.alarm-list { min-height: 320px; max-height: 320px; overflow: auto; }
.empty-state { min-height: 258px; display: grid; place-content: center; text-align: center; color: var(--muted); }
.empty-state strong { color: #c5d9e6; font-size: 16px; }
.empty-state span { margin-top: 7px; font-size: 12px; }
.alarm-row { padding: 14px 15px; display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; border-bottom: 1px solid rgba(29, 72, 103, .7); }
.alarm-row:last-child { border-bottom: 0; }
.alarm-gas { width: 42px; height: 42px; display: grid; place-items: center; border: 1px solid var(--red); border-radius: 4px; color: #ffc4c8; background: rgba(255, 91, 100, .12); font-weight: 800; }
.alarm-detail { min-width: 0; }
.alarm-detail strong { display: block; font-size: 13px; }
.alarm-detail span { color: var(--muted); font-size: 11px; }
.alarm-value { text-align: right; color: #ffbec2; font-size: 13px; font-variant-numeric: tabular-nums; }
.alarm-value strong { display: block; font-size: 20px; }

.latest-panel { margin-top: 12px; }
.sample-count, .panel-head > span:not(.alarm-count) { color: var(--muted); font-size: 12px; }
.table-scroll { overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th { height: 39px; padding: 0 14px; color: #8fb0c5; background: rgba(18, 54, 81, .65); text-align: left; white-space: nowrap; font-weight: 600; }
td { height: 42px; padding: 0 14px; border-top: 1px solid rgba(29, 72, 103, .54); color: #d8e8f2; white-space: nowrap; font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: rgba(31, 80, 112, .19); }
.table-empty { height: 100px; color: var(--muted); text-align: center; }
.status-tag { display: inline-block; min-width: 54px; padding: 4px 7px; border: 1px solid #287a56; border-radius: 3px; color: #7ce0aa; text-align: center; }
.status-tag.alarm { border-color: var(--red); color: #ffadb2; background: rgba(255, 91, 100, .09); }

.page-title { justify-content: space-between; min-height: 92px; padding: 10px 2px 17px; }
.page-title h2 { font-size: 24px; }
.page-title > div > p:last-child { margin-top: 7px; color: var(--muted); font-size: 12px; }
.primary-button { height: 38px; padding: 0 18px; border: 1px solid #3187b2; border-radius: 4px; color: white; background: #17638d; font-weight: 700; }
.primary-button:hover { background: #1d739f; }

.filter-bar { margin-bottom: 12px; padding: 13px; display: grid; grid-template-columns: repeat(3, minmax(180px, 260px)) auto; gap: 11px; align-items: end; border: 1px solid var(--line); border-radius: 6px; background: rgba(10, 31, 51, .94); }
.filter-bar.compact { grid-template-columns: minmax(180px, 260px) auto; justify-content: start; }
.filter-bar label { display: grid; gap: 6px; color: var(--muted); font-size: 11px; }
.record-panel { min-height: 520px; }
.tall-table { max-height: calc(100vh - 335px); min-height: 420px; }
.threshold-snapshot { color: var(--muted); }

.alarm-summary { margin-bottom: 12px; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.alarm-summary > div { min-height: 86px; padding: 14px 16px; display: flex; align-items: center; justify-content: space-between; border: 1px solid var(--line); border-radius: 6px; background: rgba(10, 31, 51, .94); }
.alarm-summary span { color: var(--muted); font-size: 12px; }
.alarm-summary strong { font-size: 27px; font-variant-numeric: tabular-nums; }
.alarm-summary > div:first-child strong { color: var(--red); }
.alarm-direction { color: var(--amber); }
.alarm-direction.high { color: var(--red); }

.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 10; display: grid; gap: 8px; width: min(360px, calc(100vw - 36px)); }
.toast { padding: 12px 14px; border: 1px solid var(--line-bright); border-radius: 5px; color: var(--text); background: #11314d; box-shadow: var(--shadow); font-size: 12px; animation: toastIn .2s ease-out; }
.toast.is-error { border-color: var(--red); background: #49222e; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 1050px) {
  .gas-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .monitor-grid { grid-template-columns: 1fr; }
  .alarm-list { min-height: 210px; max-height: 280px; }
  .empty-state { min-height: 200px; }
  .status-meta span:first-child { display: none; }
}

@media (max-width: 720px) {
  .topbar { padding: 12px 14px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .brand-mark { width: 40px; height: 40px; }
  .brand-block h1 { font-size: 18px; }
  .clock-block, .device-picker span { display: none; }
  .topbar-actions { width: 100%; display: grid; grid-template-columns: minmax(0, 1fr) 42px; gap: 7px; }
  .device-picker select { width: 100%; height: 36px; }
  .sound-toggle { width: 42px; overflow: hidden; padding: 0 12px; color: transparent; white-space: nowrap; }
  .sound-toggle strong { display: none; }
  .sound-dot { margin-right: 20px; }
  .main-nav { height: 50px; padding: 7px 10px; }
  .nav-tab { flex: 1; min-width: 0; padding: 0 7px; font-size: 13px; }
  main { padding: 12px 10px 24px; }
  .status-band { align-items: flex-start; gap: 8px; }
  .status-band > div:first-child { flex-wrap: wrap; }
  #lastSeenText { width: 100%; padding-left: 18px; }
  .status-meta { display: none; }
  .gas-grid { grid-template-columns: 1fr; }
  .gas-card { min-height: 185px; }
  .gas-reading { margin: 17px 0; }
  .gas-reading strong { font-size: 38px; }
  .panel-head { align-items: flex-start; gap: 10px; }
  .trend-panel .panel-head { display: grid; }
  .gas-switch button { width: 48px; }
  .chart-wrap { height: 235px; }
  .chart-legend { gap: 10px; flex-wrap: wrap; }
  .chart-legend strong { width: 100%; margin-left: 0; }
  .page-title { align-items: flex-end; gap: 12px; }
  .page-title h2 { font-size: 21px; }
  .filter-bar, .filter-bar.compact { grid-template-columns: 1fr; }
  .alarm-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tall-table { max-height: none; min-height: 360px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
