:root {
  color-scheme: dark;
  --bg: #101419;
  --text: #e7edf3;
  --muted: #8fa0b2;
  --panel: #171d24;
  --button: #4d8dff;
  --button-text: #ffffff;
  --border: rgba(104, 119, 137, 0.22);
  --ok: #16835f;
  --alert: #c93636;
  --warn-bg: rgba(201, 54, 54, 0.1);
  --ok-bg: rgba(22, 131, 95, 0.1);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f7fb;
  --text: #17202a;
  --muted: #687789;
  --panel: #ffffff;
  --button: #2477f2;
  --button-text: #ffffff;
  --border: rgba(104, 119, 137, 0.22);
}

:root[data-theme="matrix"] {
  color-scheme: dark;
  --bg: #020703;
  --text: #b7ffbf;
  --muted: #5fa86a;
  --panel: #061308;
  --button: #00c853;
  --button-text: #001b08;
  --border: rgba(0, 200, 83, 0.32);
  --ok: #00e676;
  --alert: #ff5252;
  --warn-bg: rgba(255, 82, 82, 0.12);
  --ok-bg: rgba(0, 230, 118, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
a {
  font: inherit;
}

.app-shell {
  width: min(920px, 100%);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 14px max(24px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 13px;
}

h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.08;
  letter-spacing: 0;
}

.icon-button {
  display: grid;
  place-items: center;
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}

.icon-button:active {
  transform: translateY(1px);
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.summary-item {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 12px;
  cursor: pointer;
}

.summary-item:focus-visible {
  outline: 2px solid var(--button);
  outline-offset: 2px;
}

.summary-item strong {
  display: block;
  margin-top: 6px;
  font-size: 24px;
  line-height: 1;
}

.summary-label {
  color: var(--muted);
  font-size: 12px;
}

.summary-item.is-ok strong {
  color: var(--ok);
}

.summary-item.is-alert strong {
  color: var(--alert);
}

.toolbar {
  display: grid;
  gap: 10px;
  margin-bottom: 10px;
}

.view-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 12px;
}

.view-tab {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
}

.view-tab.is-active {
  background: var(--button);
  color: var(--button-text);
}

.settings-panel {
  display: grid;
  gap: 10px;
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 10px;
}

.settings-row {
  display: grid;
  gap: 8px;
}

.settings-row > span {
  color: var(--muted);
  font-size: 12px;
}

.segmented.compact {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel) 70%, var(--bg));
}

.segment {
  min-height: 36px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.segment.is-active {
  background: var(--button);
  color: var(--button-text);
}

input[type="search"],
input[type="text"] {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  padding: 0 12px;
  outline: none;
}

select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  padding: 0 10px;
}

.status-line {
  min-height: 20px;
  margin: 8px 2px 10px;
  color: var(--muted);
  font-size: 13px;
}

.controller-list,
.history-list {
  display: grid;
  gap: 8px;
}

.controller-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 78px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 12px;
}

.controller-card.is-offline {
  background: color-mix(in srgb, var(--warn-bg) 42%, var(--panel));
}

.controller-main {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  min-width: 0;
}

.status-dot {
  flex: 0 0 10px;
  width: 10px;
  height: 10px;
  margin-top: 7px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 4px var(--ok-bg);
}

.controller-card.is-offline .status-dot {
  background: var(--alert);
  box-shadow: 0 0 0 4px var(--warn-bg);
}

.controller-text {
  min-width: 0;
}

.controller-text h2 {
  margin: 0 0 4px;
  overflow-wrap: anywhere;
  font-size: 16px;
  line-height: 1.25;
  letter-spacing: 0;
}

.serial,
.organization,
.last-seen {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.actions {
  display: grid;
  gap: 6px;
  flex: 0 0 auto;
}

.open-link {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  border-radius: 8px;
  background: var(--button);
  color: var(--button-text);
  padding: 0 12px;
  text-decoration: none;
  cursor: pointer;
}

.empty-state {
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 24px 14px;
  color: var(--muted);
  text-align: center;
}

.denied-card {
  display: grid;
  gap: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 18px;
}

.denied-card h2 {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
}

.denied-card p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.4;
}

.denied-card code {
  display: block;
  overflow-wrap: anywhere;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  color: var(--text);
}

.denied-card button {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  background: var(--button);
  color: var(--button-text);
  cursor: pointer;
}

.denied-card button:disabled {
  cursor: default;
  opacity: 0.72;
}

.denied-card .denied-note:empty {
  display: none;
}

.history-item,
.user-row {
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 12px;
}

.history-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 54px;
  min-height: 28px;
  border-radius: 6px;
  background: var(--ok-bg);
  color: var(--ok);
  font-size: 12px;
  font-weight: 700;
}

.history-item.is-offline .history-status {
  background: var(--warn-bg);
  color: var(--alert);
}

.history-item h2 {
  margin: 0 0 3px;
  font-size: 15px;
  line-height: 1.25;
}

.history-item p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.user-form {
  display: grid;
  gap: 8px;
}

.user-form button {
  min-height: 36px;
  border: 0;
  border-radius: 8px;
  background: var(--button);
  color: var(--button-text);
  cursor: pointer;
}

.user-list {
  display: grid;
  gap: 6px;
}

@media (min-width: 720px) {
  .toolbar {
    grid-template-columns: 320px minmax(0, 1fr);
    align-items: center;
  }

  .settings-panel {
    grid-template-columns: 1fr 220px;
    align-items: end;
  }

  .controller-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .user-form {
    grid-template-columns: minmax(0, 1fr) 110px 110px;
  }
}
