:root {
  color-scheme: dark;
  --background: #080b0d;
  --surface: #111619;
  --surface-raised: #171d21;
  --border: #283136;
  --text: #f4f7f5;
  --muted: #8f9da3;
  --green: #52e39a;
  --green-dark: #123d2a;
  --red: #ff5d5d;
  --red-dark: #441b1f;
  --amber: #f3b956;
  --offline: #778389;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--background);
  color: var(--text);
}

button, input { font: inherit; }

.topbar,
main {
  width: min(1120px, calc(100% - 40px));
  margin-inline: auto;
}

.topbar {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.topbar-actions { display: flex; align-items: center; gap: 5px; }

.brand { display: flex; align-items: center; gap: 11px; }
.brand h1, .overview h2, .auth-panel h2 { margin: 0; letter-spacing: -0.035em; }
.brand h1 { font-size: 1.25rem; }

.brand-mark {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 1px solid #35694f;
  border-radius: 50%;
  background: var(--green-dark);
}

.brand-mark span {
  width: 14px;
  height: 14px;
  border: 3px solid var(--green);
  border-top-color: transparent;
  border-radius: 50%;
  transform: rotate(45deg);
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 750;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.connection-badge {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 7px 10px;
  border: 0;
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.diagnostics-button,
.icon-button {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.diagnostics-button:hover, .icon-button:hover { background: var(--surface-raised); color: var(--text); }
.diagnostics-button:focus-visible, .icon-button:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
.diagnostics-button svg, .icon-button svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); }
.connection-badge.is-connected .status-dot { background: var(--green); box-shadow: 0 0 12px rgba(82, 227, 154, 0.75); }
.connection-badge.is-disconnected .status-dot { background: var(--red); }

main { padding: 36px 0 64px; }

.overview {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  margin-bottom: 28px;
}

.overview h2 { font-size: clamp(1.8rem, 4vw, 2.5rem); line-height: 1; }

.auth-panel {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  align-items: end;
  gap: 40px;
  margin-bottom: 48px;
  padding: 26px;
  border: 1px solid #584420;
  border-radius: 14px;
  background: #211b11;
}

.auth-panel[hidden] { display: none; }
.auth-panel p { color: #c5b99f; }
.auth-panel label { display: block; margin-bottom: 8px; font-size: 0.8rem; font-weight: 700; }
.token-row { display: flex; gap: 9px; }
.token-row input { flex: 1; min-width: 0; padding: 11px 13px; border: 1px solid #5d5038; border-radius: 8px; background: #0d1012; color: var(--text); }
.token-row input:focus { outline: 2px solid var(--amber); outline-offset: 2px; }
.token-row button { padding: 11px 17px; border: 0; border-radius: 8px; background: var(--amber); color: #1b1408; font-weight: 800; cursor: pointer; }
.form-error { min-height: 1.2em; margin: 8px 0 0; color: #ff8d8d !important; font-size: 0.8rem; }

.device-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 12px; }
.device-grid.is-server-unavailable .device-card { opacity: 0.46; filter: grayscale(1); }

.device-card {
  position: relative;
  overflow: hidden;
  min-height: 152px;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: linear-gradient(145deg, var(--surface-raised), var(--surface));
  transition: opacity 160ms ease, filter 160ms ease;
}

.device-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 3px; background: var(--green); }
.device-card.limiting { border-color: #6d2d32; background: linear-gradient(145deg, #281719, #171214); }
.device-card.limiting::before { width: 4px; background: var(--red); }
.device-card.offline { opacity: 0.66; }
.device-card.offline::before { background: var(--offline); }

.device-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.device-id { margin: 0; font-size: 1.15rem; }
.state-label { padding: 6px 9px; border-radius: 6px; background: var(--green-dark); color: var(--green); font-size: 0.72rem; font-weight: 850; letter-spacing: 0.08em; }
.limiting .state-label { background: var(--red-dark); color: #ff8585; }
.offline .state-label { background: #252c30; color: #aab3b7; }
.history-panel { margin-top: 17px; padding: 8px 10px 6px; border: 1px solid rgba(40, 49, 54, 0.58); border-radius: 8px; background: rgba(4, 7, 8, 0.28); box-shadow: inset 0 1px 8px rgba(0, 0, 0, 0.14); }
.state-history { display: block; width: 100%; height: 40px; overflow: visible; }
.history-scale { display: flex; justify-content: space-between; margin-top: 1px; color: rgba(143, 157, 163, 0.58); font-size: 0.62rem; font-weight: 650; letter-spacing: 0.02em; }
.history-ok { fill: var(--green); }
.history-limiting { fill: var(--red); }

.device-details {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px 24px;
  margin-top: 16px;
  padding-top: 4px;
}
.detail-group:first-child { grid-column: 1 / -1; }
.detail-group h4 {
  margin: 0 0 9px;
  color: var(--muted);
  font-size: 0.67rem;
  font-weight: 800;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}
.detail-group dl { display: grid; grid-template-columns: minmax(84px, auto) 1fr; gap: 6px 12px; margin: 0; font-size: 0.76rem; }
.detail-group dt { color: var(--muted); }
.detail-group dd { min-width: 0; margin: 0; overflow-wrap: anywhere; color: var(--text); text-align: right; font-variant-numeric: tabular-nums; }

.diagnostics-dialog {
  width: min(820px, calc(100% - 32px));
  max-height: min(760px, calc(100% - 48px));
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: #0d1215;
  color: var(--text);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.55);
}
.settings-dialog {
  width: min(620px, calc(100% - 32px));
  max-height: min(700px, calc(100% - 48px));
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: #0d1215;
  color: var(--text);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.55);
}
.settings-dialog::backdrop { background: rgba(2, 5, 6, 0.76); backdrop-filter: blur(3px); }
.diagnostics-dialog::backdrop { background: rgba(2, 5, 6, 0.76); backdrop-filter: blur(3px); }
.diagnostics-dialog-header { display: flex; align-items: center; justify-content: space-between; gap: 24px; padding: 22px 24px; border-bottom: 1px solid var(--border); }
.diagnostics-dialog-header h2 { margin: 0; font-size: 1.35rem; letter-spacing: -0.025em; }
.dialog-close { width: 34px; height: 34px; flex: 0 0 auto; display: grid; place-items: center; padding: 0; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--muted); cursor: pointer; }
.dialog-close:hover { color: var(--text); border-color: #3a474d; }
.dialog-close:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
.dialog-close svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.diagnostics-list { max-height: calc(min(760px, 100vh - 48px) - 91px); padding: 16px; overflow-y: auto; }
.diagnostics-device { padding: 20px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.diagnostics-device + .diagnostics-device { margin-top: 12px; }
.diagnostics-device-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
.diagnostics-device-header h3 { margin: 0; font-size: 1rem; }
.diagnostics-online-state { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.72rem; font-weight: 750; }
.diagnostics-online-state::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--offline); }
.diagnostics-online-state.is-online::before { background: var(--green); }
.diagnostics-empty { margin: 0; padding: 40px 20px; color: var(--muted); text-align: center; }
.settings-list { max-height: calc(min(700px, 100vh - 48px) - 91px); padding: 16px; overflow-y: auto; }
.settings-help { margin: 0 2px 14px; color: var(--muted); font-size: 0.78rem; line-height: 1.45; }
.device-setting { padding: 18px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.device-setting + .device-setting { margin-top: 12px; }
.device-setting-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.device-setting label { display: block; margin-bottom: 6px; font-size: 0.78rem; font-weight: 750; }
.device-setting-id { margin: 0 0 13px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.7rem; overflow-wrap: anywhere; }
.order-controls { display: flex; flex: 0 0 auto; gap: 5px; }
.device-setting .order-button { width: 32px; height: 32px; padding: 0; border: 1px solid #3a474d; border-radius: 7px; background: #0b1012; color: var(--text); font-weight: 850; cursor: pointer; }
.device-setting .order-button:hover:not(:disabled) { border-color: var(--green); color: var(--green); }
.device-setting .order-button:disabled { opacity: 0.28; cursor: default; }
.device-setting-row { display: flex; gap: 8px; }
.device-setting input { flex: 1; min-width: 0; padding: 10px 12px; border: 1px solid #3a474d; border-radius: 8px; background: #090d0f; color: var(--text); }
.device-setting input:focus { outline: 2px solid var(--green); outline-offset: 2px; }
.device-setting button[type="submit"] { padding: 10px 15px; border: 0; border-radius: 8px; background: var(--green); color: #082015; font-weight: 800; cursor: pointer; }
.device-setting button[type="submit"]:disabled { opacity: 0.55; cursor: wait; }
.setting-message { min-height: 1.15em; margin: 7px 0 0; color: var(--muted); font-size: 0.72rem; }
.setting-message.is-error { color: #ff8d8d; }

.empty-state {
  padding: 56px 24px;
  border: 1px dashed var(--border);
  border-radius: 14px;
  text-align: center;
  color: var(--muted);
}
.empty-state h3 { margin: 16px 0 7px; color: var(--text); }
.empty-state p { margin: 0; }
.empty-pulse { display: inline-block; width: 16px; height: 16px; border: 3px solid var(--green); border-radius: 50%; box-shadow: 0 0 0 8px rgba(82, 227, 154, 0.08); }

@media (max-width: 760px) {
  .topbar, main { width: min(100% - 28px, 1120px); }
  main { padding-top: 28px; }
  .overview { display: block; }
  .auth-panel { grid-template-columns: 1fr; gap: 18px; }
  .device-details { grid-template-columns: 1fr; }
  .detail-group:first-child { grid-column: auto; }
  .diagnostics-dialog-header { padding: 18px; }
  .diagnostics-list { padding: 10px; }
  .diagnostics-device { padding: 16px; }
  .settings-list { padding: 10px; }
  .device-setting-row { align-items: stretch; flex-direction: column; }
}

@media (prefers-reduced-motion: no-preference) {
  .is-connecting .status-dot, .empty-pulse { animation: pulse 1.6s ease-in-out infinite; }
  @keyframes pulse { 50% { opacity: 0.42; transform: scale(0.86); } }
}
