:root {
  color: #16263d;
  background: #f4f7fb;
  font-family: Tahoma, "Segoe UI", Arial, sans-serif;
  font-synthesis: none;
  --navy: #123a5a;
  --navy-dark: #0a2942;
  --teal: #087f72;
  --teal-soft: #e7f6f3;
  --blue-soft: #eaf2fb;
  --green: #16815e;
  --red: #c83c4c;
  --amber: #a76708;
  --border: #d8e2ec;
  --muted: #5d6c7e;
  --shadow: 0 16px 38px rgba(20, 51, 77, 0.08);
  --page-top: #edf3f8;
  --page-bottom: #f8fafc;
  --surface: #ffffff;
  --surface-subtle: #fbfcfe;
  --surface-muted: #f6f8fb;
  --surface-hover: #fafcff;
  --text: #16263d;
  --text-strong: #0a2942;
  --field-border: #bac8d6;
  --detail-border: #e5ebf1;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --navy: #2f7da5;
  --navy-dark: #236789;
  --teal: #45c7b8;
  --teal-soft: #123b3b;
  --blue-soft: #172f46;
  --green: #4bd29c;
  --red: #ff7f8d;
  --amber: #f2b556;
  --border: #2c4051;
  --muted: #a8b8c8;
  --shadow: 0 18px 42px rgba(0, 0, 0, .28);
  --page-top: #08131e;
  --page-bottom: #0d1925;
  --surface: #111f2c;
  --surface-subtle: #142433;
  --surface-muted: #182938;
  --surface-hover: #1a2d3d;
  --text: #e8eef5;
  --text-strong: #f4f8fb;
  --field-border: #486074;
  --detail-border: #2b4051;
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; min-height: 100vh; color: var(--text); background: linear-gradient(180deg, var(--page-top) 0, var(--page-bottom) 420px); }
button, input, select { font: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(100%, 440px); padding: 38px; border: 1px solid var(--border); border-radius: 24px; background: var(--surface); box-shadow: var(--shadow); }
.login-card h1 { margin: 8px 0 10px; font-size: 29px; }
.login-card label { display: grid; gap: 8px; margin-top: 28px; font-weight: 700; }
.privacy-note { margin: 15px 0 0; color: var(--muted); font-size: 12px; text-align: center; }
.brand-mark { width: 52px; height: 52px; display: grid; place-items: center; color: white; border-radius: 16px; background: var(--navy); }
.brand-mark svg { width: 29px; height: 29px; }
.brand-mark.small { width: 44px; height: 44px; border-radius: 13px; }
.brand-mark.small svg { width: 24px; height: 24px; }

.app-shell { width: min(1440px, calc(100% - 40px)); margin: 0 auto; padding: 24px 0 40px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 12px 0 26px; }
.topbar-title, .topbar-actions { display: flex; align-items: center; gap: 14px; }
.topbar h1 { margin: 3px 0 0; font-size: 25px; }
.eyebrow { margin: 0; color: var(--teal); font-size: 12px; font-weight: 800; letter-spacing: .03em; }
.muted { color: var(--muted); line-height: 1.7; }
.sync-state { padding: 8px 12px; border-radius: 999px; color: var(--green); background: #e9f7f1; font-size: 13px; font-weight: 700; }

.button, .icon-button { border: 1px solid transparent; border-radius: 11px; min-height: 42px; transition: background-color .18s ease, border-color .18s ease, color .18s ease, box-shadow .18s ease; }
.button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 9px 16px; font-weight: 750; }
.button.primary { color: white; background: var(--navy); }
.button.primary:hover { background: var(--navy-dark); }
.button.secondary { color: var(--navy); background: white; border-color: var(--border); }
.button.secondary:hover { border-color: #9fb1c3; background: #f8fafc; }
.button.danger { color: var(--red); background: #fff4f5; border-color: #f1c5cb; }
.button.wide { width: 100%; margin-top: 8px; }
.icon-button { width: 42px; padding: 0; display: inline-grid; place-items: center; color: var(--navy); background: white; border-color: var(--border); }
.icon-button:hover { background: var(--blue-soft); border-color: #abc1d6; }
.sun-icon { display: none; }
html[data-theme="dark"] .sun-icon { display: block; }
html[data-theme="dark"] .moon-icon { display: none; }
.button:focus-visible, .icon-button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid rgba(59,130,246,.28); outline-offset: 2px; }

.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.metric-card { position: relative; overflow: hidden; display: grid; gap: 8px; min-height: 136px; padding: 21px; background: var(--surface); border: 1px solid var(--border); border-radius: 17px; box-shadow: 0 8px 24px rgba(20,51,77,.05); }
.metric-card::before { content: ""; position: absolute; inset: 0 0 auto; height: 4px; background: var(--navy); }
.metric-card.success::before { background: var(--green); }
.metric-card.info::before { background: #2773b8; }
.metric-card.warning::before { background: #d28a1c; }
.metric-card span { color: var(--muted); font-size: 13px; font-weight: 700; }
.metric-card strong { color: var(--text-strong); font-size: 34px; line-height: 1; font-variant-numeric: tabular-nums; direction: ltr; justify-self: start; }
.metric-card small { color: var(--muted); }

.panel { margin-top: 20px; padding: 24px; background: var(--surface); border: 1px solid var(--border); border-radius: 19px; box-shadow: 0 10px 30px rgba(20,51,77,.05); }
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 20px; }
.panel-heading h2 { margin: 4px 0; font-size: 23px; }
.panel-heading .muted { margin: 0; }
.device-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.device-card { padding: 18px; border: 1px solid var(--border); border-radius: 15px; background: var(--surface-subtle); }
.device-card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; }
.device-title { display: flex; gap: 12px; min-width: 0; }
.device-icon { flex: 0 0 auto; width: 40px; height: 40px; display: grid; place-items: center; color: var(--navy); background: var(--blue-soft); border-radius: 11px; }
.device-title h3 { margin: 0 0 5px; font-size: 17px; }
.serial { color: var(--muted); font-family: Consolas, monospace; font-size: 12px; direction: ltr; text-align: right; overflow-wrap: anywhere; }
.status { display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; padding: 6px 9px; border-radius: 999px; font-size: 12px; font-weight: 800; background: #edf1f5; color: #536274; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status.connected { color: var(--green); background: #e8f7f0; }
.status.streaming { color: #236da9; background: #e8f2fb; }
.status.disabled { color: var(--red); background: #fff0f2; }
.device-details { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin: 18px 0; }
.detail { padding: 10px; border-radius: 10px; background: var(--surface); border: 1px solid var(--detail-border); min-width: 0; }
.detail span { display: block; color: var(--muted); font-size: 11px; margin-bottom: 5px; }
.detail strong { display: block; font-size: 13px; overflow-wrap: anywhere; }
.card-actions { display: flex; justify-content: flex-end; gap: 8px; padding-top: 13px; border-top: 1px solid var(--border); }
.card-actions .button { min-height: 36px; padding: 6px 11px; font-size: 12px; }

.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--border); border-radius: 14px; }
table { width: 100%; border-collapse: collapse; min-width: 820px; }
th, td { padding: 14px; text-align: right; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); background: var(--surface-muted); font-size: 12px; }
td { font-size: 13px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-hover); }
.url { max-width: 300px; direction: ltr; text-align: left; overflow-wrap: anywhere; color: #345a78; }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 5px 8px; border-radius: 8px; font-size: 11px; font-weight: 800; }
.badge.good { color: var(--green); background: #e8f7f0; }
.badge.warn { color: var(--amber); background: #fff7e9; }
.badge.neutral { color: #536274; background: #edf1f5; }
.row-actions { display: flex; gap: 7px; justify-content: flex-end; }
.row-actions .button { min-height: 34px; padding: 5px 9px; font-size: 12px; }
.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); border: 1px dashed var(--field-border); border-radius: 13px; background: var(--surface-hover); }
footer { padding: 24px 4px 0; color: var(--muted); text-align: center; font-size: 12px; }

input, select { width: 100%; min-height: 44px; padding: 9px 11px; color: var(--text); background: var(--surface); border: 1px solid var(--field-border); border-radius: 10px; }
input::placeholder { color: var(--muted); opacity: .85; }
input:hover, select:hover { border-color: #8098ae; }
dialog { width: min(680px, calc(100% - 28px)); padding: 0; color: var(--text); background: var(--surface); border: 0; border-radius: 20px; box-shadow: 0 28px 80px rgba(4,24,42,.28); }
dialog::backdrop { background: rgba(9,31,50,.62); backdrop-filter: blur(3px); }
dialog form { padding: 24px; }
.dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; padding-bottom: 18px; border-bottom: 1px solid var(--border); }
.dialog-heading h2 { margin: 4px 0 0; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 17px; margin: 20px 0; }
.form-grid label { display: grid; gap: 8px; color: var(--text); font-size: 13px; font-weight: 750; }
.form-grid label > span { color: var(--red); }
.switch-row { grid-column: 1 / -1; display: flex !important; align-items: center; gap: 10px !important; padding: 13px; background: var(--surface-muted); border-radius: 11px; }
.switch-row input { width: 19px; min-height: 19px; margin: 0; accent-color: var(--teal); }
.switch-row span { color: var(--text) !important; }
.button.secondary, .icon-button { color: var(--text); background: var(--surface); border-color: var(--border); }
html[data-theme="dark"] .button.secondary:hover, html[data-theme="dark"] .icon-button:hover { border-color: #5c7890; background: var(--surface-hover); }
html[data-theme="dark"] .sync-state { color: #61d9aa; background: #11352d; }
html[data-theme="dark"] .status { color: #a8b8c8; background: #243644; }
html[data-theme="dark"] .status.connected, html[data-theme="dark"] .badge.good { color: #61d9aa; background: #11352d; }
html[data-theme="dark"] .status.streaming { color: #75bff2; background: #15334b; }
html[data-theme="dark"] .status.disabled { color: #ff93a0; background: #46242d; }
html[data-theme="dark"] .badge.warn, html[data-theme="dark"] .callout { color: #f3c875; background: #392f1d; border-color: #5b4927; }
html[data-theme="dark"] .badge.neutral { color: #bdc9d4; background: #243644; }
html[data-theme="dark"] .button.danger { color: #ff93a0; background: #3b2028; border-color: #6b3540; }
html[data-theme="dark"] .url { color: #9ac7e8; }
.callout { padding: 13px 15px; color: #68501d; background: #fff8e9; border: 1px solid #f0ddb2; border-radius: 11px; font-size: 12px; line-height: 1.7; }
.dialog-actions { display: flex; justify-content: flex-start; gap: 9px; padding-top: 18px; border-top: 1px solid var(--border); }
.form-error { min-height: 20px; margin: 10px 0; color: var(--red); font-size: 13px; }
.toast { position: fixed; z-index: 20; inset: auto 50% 24px auto; transform: translateX(50%); min-width: 260px; padding: 12px 18px; color: white; text-align: center; background: #142f47; border-radius: 11px; box-shadow: var(--shadow); }
.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; }

@media (max-width: 980px) {
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .device-grid { grid-template-columns: 1fr; }
}

@media (max-width: 680px) {
  .app-shell { width: min(100% - 24px, 1440px); padding-top: 12px; }
  .topbar, .panel-heading { align-items: stretch; flex-direction: column; }
  .topbar-actions { flex-wrap: wrap; }
  .topbar-actions .secondary { margin-inline-start: auto; }
  .summary-grid { grid-template-columns: 1fr; }
  .panel { padding: 17px; }
  .panel-heading .button { width: 100%; }
  .device-details, .form-grid { grid-template-columns: 1fr; }
  .login-card { padding: 28px 22px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
