:root {
  color-scheme: light;
  font-family: Inter, "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  color: #17202a;
  background: #f3f5f7;
  --surface: #ffffff;
  --border: #d8dde3;
  --muted: #66717d;
  --primary: #1769aa;
  --primary-hover: #105487;
  --success: #16845b;
  --danger: #b73832;
  --shadow: 0 10px 28px rgba(25, 38, 52, 0.12);
}

* {
  box-sizing: border-box;
  letter-spacing: 0;
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: #f3f5f7;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
.open-kvm {
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--primary);
  border-radius: 6px;
  color: #ffffff;
  background: var(--primary);
  cursor: pointer;
  font-weight: 650;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button:hover,
.open-kvm:hover {
  background: var(--primary-hover);
}

button:disabled {
  cursor: wait;
  opacity: 0.6;
}

button.secondary {
  color: #26323d;
  border-color: #b9c1ca;
  background: #ffffff;
}

button.secondary:hover {
  background: #edf1f4;
}

button.danger {
  color: var(--danger);
  border-color: #e0b6b3;
  background: #ffffff;
}

button.danger:hover {
  background: #fff0ef;
}

button.compact {
  min-width: 72px;
  padding: 0 12px;
}

.icon-button {
  width: 40px;
  min-width: 40px;
  padding: 0;
  border-color: transparent;
  color: #2f3943;
  background: transparent;
  font-size: 26px;
  line-height: 1;
}

.icon-button:hover {
  background: #edf1f4;
}

.topbar {
  height: 64px;
  padding: 0 max(20px, calc((100vw - 1180px) / 2));
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand,
.account {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  color: #ffffff;
  background: #202b35;
  font-weight: 800;
}

.brand div {
  display: grid;
  gap: 1px;
}

.brand span:last-child {
  color: var(--muted);
  font-size: 12px;
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 32px 20px 56px;
}

.workspace-tabs {
  min-height: 45px;
  margin-bottom: 26px;
  display: flex;
  align-items: stretch;
  gap: 4px;
  border-bottom: 1px solid var(--border);
}

button.workspace-tab {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  color: var(--muted);
  background: transparent;
  font-weight: 650;
}

button.workspace-tab:hover {
  color: #26323d;
  background: #e9edf1;
}

button.workspace-tab.active {
  border-bottom-color: var(--primary);
  color: var(--primary);
  background: transparent;
}

.workspace-view {
  min-width: 0;
}

.page-heading {
  margin-bottom: 24px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
}

.heading-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

h1,
h2,
p {
  margin-top: 0;
}

.page-heading h1 {
  margin-bottom: 4px;
  font-size: 26px;
}

.page-heading p {
  margin-bottom: 0;
  color: var(--muted);
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 16px;
}

.device-card {
  min-width: 0;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}

.device-card > header,
.device-card > footer,
.device-title {
  display: flex;
  align-items: center;
}

.device-card > header {
  min-height: 49px;
  justify-content: space-between;
  gap: 16px;
}

.device-title {
  min-width: 0;
  gap: 11px;
}

.device-title > div {
  min-width: 0;
}

.device-name {
  margin-bottom: 4px;
  overflow: hidden;
  font-size: 17px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-uuid {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-dot {
  width: 11px;
  height: 11px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #aab2ba;
}

.device-card.online .status-dot {
  background: var(--success);
  box-shadow: 0 0 0 4px rgba(22, 132, 91, 0.12);
}

.device-state {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 13px;
}

.device-card.online .device-state {
  color: var(--success);
}

.device-facts,
.provision-values {
  margin: 20px 0;
}

.device-facts div,
.provision-values div {
  min-height: 32px;
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid #edf0f2;
}

dt {
  color: var(--muted);
  font-size: 13px;
}

dd {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
  text-align: right;
}

.device-card > footer {
  flex-wrap: wrap;
  gap: 8px;
}

.data-table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.data-table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
  table-layout: fixed;
}

.data-table th,
.data-table td {
  padding: 13px 14px;
  border-bottom: 1px solid #e7ebee;
  overflow-wrap: anywhere;
  text-align: left;
  vertical-align: middle;
}

.data-table th {
  color: var(--muted);
  background: #f8f9fa;
  font-size: 12px;
  font-weight: 700;
}

.data-table tbody tr:last-child td {
  border-bottom: 0;
}

.data-table tbody tr:hover {
  background: #f8fafb;
}

.data-table .primary-cell {
  color: #17202a;
  font-weight: 700;
}

.data-table .actions-heading {
  width: 150px;
  text-align: right;
}

.data-table .table-actions {
  width: 150px;
  text-align: right;
  white-space: nowrap;
}

button.table-action {
  min-width: 0;
  min-height: 32px;
  margin-left: 6px;
  padding: 0 10px;
  font-size: 12px;
}

.data-table .table-empty {
  height: 150px;
  color: var(--muted);
  text-align: center;
}

.empty-state {
  min-height: 240px;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 18px;
  border: 1px dashed #bac2ca;
  color: var(--muted);
  background: #fafbfc;
}

.login-view {
  min-height: calc(100vh - 64px);
  padding: 28px 20px;
  display: grid;
  place-items: center;
}

.auth-card {
  width: min(390px, 100%);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.auth-tabs {
  margin: -8px 0 24px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  border-bottom: 1px solid var(--border);
}

button.auth-tab {
  min-height: 42px;
  padding: 0 12px;
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  color: var(--muted);
  background: transparent;
}

button.auth-tab:hover {
  color: #26323d;
  background: #edf1f4;
}

button.auth-tab.active {
  border-bottom-color: var(--primary);
  color: var(--primary);
  background: transparent;
}

.auth-card form h1 {
  margin-bottom: 24px;
  font-size: 22px;
}

label {
  display: grid;
  gap: 7px;
  margin-bottom: 16px;
  color: #35414c;
  font-size: 13px;
  font-weight: 650;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid #b8c1ca;
  border-radius: 5px;
  color: #18222c;
  background: #ffffff;
  outline: none;
}

input,
select {
  height: 42px;
  padding: 0 11px;
}

textarea {
  min-height: 76px;
  padding: 9px 11px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(23, 105, 170, 0.14);
}

input[type="checkbox"] {
  width: 18px;
  height: 18px;
  padding: 0;
  accent-color: var(--primary);
}

.check-row {
  min-height: 32px;
  display: flex;
  align-items: center;
  gap: 9px;
}

fieldset {
  min-width: 0;
  margin: 4px 0 20px;
  padding: 0;
  border: 0;
}

legend {
  margin-bottom: 8px;
  color: #35414c;
  font-size: 13px;
  font-weight: 650;
}

.choice-list {
  max-height: 180px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 5px;
}

.choice-option {
  min-height: 42px;
  margin: 0;
  padding: 8px 11px;
  display: flex;
  align-items: center;
  gap: 9px;
  border-bottom: 1px solid #edf0f2;
  font-weight: 500;
}

.choice-option:last-child {
  border-bottom: 0;
}

.choice-option:hover {
  background: #f5f7f9;
}

.choice-empty {
  min-height: 70px;
  margin: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
}

.auth-card form > button {
  width: 100%;
}

.message {
  min-height: 20px;
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.message.error {
  color: var(--danger);
}

.message.success {
  color: var(--success);
}

dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: #17202a;
  background: var(--surface);
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(20, 28, 36, 0.48);
}

dialog form,
dialog > section {
  margin: 0;
  padding: 22px;
}

#user-dialog,
#profile-dialog,
#user-group-dialog,
#device-group-dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 32px));
  overflow-y: auto;
}

.profile-summary {
  margin-bottom: 24px;
}

.profile-form {
  margin: 0;
  padding-top: 22px;
  border-top: 1px solid var(--border);
}

.profile-form + .profile-form {
  margin-top: 24px;
}

.profile-form h3 {
  margin: 0 0 16px;
  font-size: 16px;
}

.dialog-header {
  min-height: 40px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.dialog-header h2 {
  margin: 0;
  font-size: 19px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.dialog-intro {
  margin: -6px 0 16px;
  color: var(--muted);
}

.binding-summary {
  margin: 0 0 18px;
}

.binding-summary > div {
  min-height: 34px;
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--border);
}

.binding-summary dt {
  color: var(--muted);
  font-size: 13px;
}

.binding-summary dd {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
  text-align: right;
}

.link-binding-actions .action-spacer {
  flex: 1 1 auto;
}

.token-input {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.binding-code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 22px;
  font-weight: 750;
  text-align: center;
  text-transform: uppercase;
}

.provision-values dd {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
}

[hidden] {
  display: none !important;
}

@media (max-width: 620px) {
  .topbar {
    height: auto;
    min-height: 64px;
    padding: 10px 14px;
  }

  .brand span:last-child,
  #account-name {
    display: none;
  }

  .account {
    gap: 6px;
  }

  #profile-button,
  #logout-button {
    padding: 0 10px;
  }

  main {
    padding: 24px 14px 40px;
  }

  .workspace-tabs {
    margin: 0 -14px 22px;
    padding: 0 10px;
    overflow-x: auto;
  }

  button.workspace-tab {
    flex: 0 0 auto;
    padding: 0 12px;
  }

  .page-heading {
    align-items: center;
  }

  .page-heading .heading-actions {
    flex: 0 0 auto;
  }

  .device-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .device-card {
    padding: 16px;
  }

  .device-card > footer > * {
    flex: 1 1 130px;
  }

  dialog form,
  dialog > section {
    padding: 18px;
  }
}

@media (max-width: 420px) {
  .brand > div {
    display: none;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    color: #e7ebee;
    background: #12171c;
    --surface: #1b2229;
    --border: #35404a;
    --muted: #a7b0b9;
    --primary: #3d91d5;
    --primary-hover: #56a5e2;
    --shadow: 0 12px 30px rgba(0, 0, 0, 0.36);
  }

  body {
    background: #12171c;
  }

  button.secondary,
  button.danger,
  input,
  select,
  textarea {
    color: #e7ebee;
    background: #202830;
  }

  button.secondary:hover,
  .icon-button:hover,
  button.auth-tab:hover,
  button.workspace-tab:hover {
    background: #2b353f;
  }

  button.danger:hover {
    background: #3a2526;
  }

  .empty-state {
    background: #171d23;
  }

  label {
    color: #d4d9de;
  }

  legend {
    color: #d4d9de;
  }

  .data-table th {
    background: #202830;
  }

  .data-table th,
  .data-table td,
  .device-facts div,
  .provision-values div,
  .choice-option {
    border-color: #303941;
  }

  .data-table tbody tr:hover,
  .choice-option:hover {
    background: #232c34;
  }

  .data-table .primary-cell {
    color: #e7ebee;
  }
}
