:root {
  --indigo: #4F46E5;
  --indigo-hover: #4338CA;
  --indigo-soft: #EEF2FF;
  --indigo-100: #E0E7FF;
  --white: #FFFFFF;
  --bg: #F6F7F9;
  --g100: #F3F4F6;
  --g200: #E5E7EB;
  --g300: #D1D5DB;
  --g400: #9CA3AF;
  --g500: #6B7280;
  --g700: #374151;
  --g900: #111827;
  --green: #059669;
  --green-soft: #ECFDF5;
  --red: #DC2626;
  --red-soft: #FEF2F2;
  --amber: #D97706;
  --amber-soft: #FFFBEB;
  --r-card: 20px;
  --r-ctrl: 14px;
  --r-pill: 999px;
  --sh-card: 0 4px 16px rgba(17, 24, 39, 0.05);
  --sh-hover: 0 8px 24px rgba(17, 24, 39, 0.09);
  --sh-pop: 0 12px 32px rgba(79, 70, 229, 0.22);
  --font: 'Segoe UI', system-ui, -apple-system, 'Inter', sans-serif;
}

.dark {
  --indigo: #6366F1;
  --indigo-hover: #818CF8;
  --indigo-soft: #1E1B4B;
  --indigo-100: #312E81;
  --white: #1C1F26;
  --bg: #0F1115;
  --g100: #252830;
  --g200: #2E323C;
  --g300: #3D424E;
  --g400: #8B92A0;
  --g500: #9CA3AF;
  --g700: #D1D5DB;
  --g900: #F3F4F6;
  --green: #34D399;
  --green-soft: #064E3B;
  --red: #F87171;
  --red-soft: #450A0A;
  --amber: #FBBF24;
  --amber-soft: #78350F;
  --sh-card: 0 4px 16px rgba(0, 0, 0, 0.40);
  --sh-hover: 0 8px 24px rgba(0, 0, 0, 0.55);
  --sh-pop: 0 12px 32px rgba(99, 102, 241, 0.35);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--g900);
  font-size: 14px;
  line-height: 1.45;
  min-height: 100vh;
}

button {
  font-family: inherit;
  border: none;
  cursor: pointer;
  background: none;
}

.app {
  max-width: 1600px;
  margin: 0 auto;
  padding: 24px 28px 48px;
}

/* Welcome (disconnected) */
.welcome {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 140px);
  padding: 24px 0 48px;
}

.welcome[hidden],
.session[hidden] {
  display: none !important;
}

.welcome-card {
  width: 100%;
  max-width: 420px;
  text-align: center;
  padding: 36px 32px;
  margin-bottom: 0;
}

.welcome-logo {
  width: 56px;
  height: 56px;
  border-radius: var(--r-ctrl);
  background: var(--indigo);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 20px;
  margin: 0 auto 18px;
  box-shadow: var(--sh-pop);
}

.welcome-card h2 {
  font-size: 22px;
  font-weight: 750;
  letter-spacing: -0.5px;
  margin-bottom: 8px;
}

.welcome-card > p {
  font-size: 13px;
  color: var(--g500);
  margin-bottom: 22px;
  line-height: 1.5;
}

.welcome-btn {
  width: 100%;
  justify-content: center;
  padding: 13px 22px;
}

.welcome-status {
  margin-top: 14px;
  margin-bottom: 0 !important;
  min-height: 1.2em;
  font-size: 13px;
  font-weight: 600;
  color: var(--indigo);
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 28px;
}

.logo {
  width: 40px;
  height: 40px;
  border-radius: var(--r-ctrl);
  background: var(--indigo);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 14px;
  flex-shrink: 0;
  box-shadow: var(--sh-pop);
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.brand h1 {
  font-size: 24px;
  font-weight: 750;
  letter-spacing: -0.6px;
  color: var(--g900);
}

.brand p {
  font-size: 12.5px;
  color: var(--g400);
  font-weight: 500;
}

.topbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-connection {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--white);
  border-radius: var(--r-pill);
  padding: 5px 5px 5px 14px;
  box-shadow: var(--sh-card);
  max-width: min(360px, 50vw);
}

.nav-connection[hidden] {
  display: none !important;
}

.nav-connection-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  flex-shrink: 0;
}

.nav-connection-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--g900);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.nav-disconnect {
  padding: 8px 14px;
  flex-shrink: 0;
}

@media (max-width: 600px) {
  .nav-connection-name {
    max-width: 90px;
  }
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: var(--r-ctrl);
  background: var(--white);
  box-shadow: var(--sh-card);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--g500);
  transition: 0.15s;
}

.icon-btn:hover {
  box-shadow: var(--sh-hover);
  color: var(--g900);
}

.icon-btn svg {
  width: 18px;
  height: 18px;
}

.icon-btn .icon-sun {
  display: none;
}

.dark .icon-btn .icon-moon {
  display: none;
}

.dark .icon-btn .icon-sun {
  display: block;
}

/* Cards */
.card {
  background: var(--white);
  border-radius: var(--r-card);
  padding: 22px;
  box-shadow: var(--sh-card);
  transition: 0.15s;
  margin-bottom: 16px;
}

.card:hover {
  box-shadow: var(--sh-hover);
}

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

.card h2 {
  font-size: 14px;
  font-weight: 700;
  color: var(--g900);
}

.card-sub {
  font-size: 12.5px;
  color: var(--g400);
  font-weight: 500;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 20px;
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 600;
  transition: 0.15s;
  color: var(--g700);
}

.btn.primary {
  background: var(--indigo);
  color: #fff;
  box-shadow: var(--sh-pop);
}

.btn.primary:hover {
  background: var(--indigo-hover);
}

.btn.ghost {
  background: var(--white);
  color: var(--g700);
  box-shadow: var(--sh-card);
}

.btn.ghost:hover {
  box-shadow: var(--sh-hover);
  color: var(--g900);
}

.btn.danger {
  background: var(--red-soft);
  color: var(--red);
}

.btn.danger:hover {
  filter: brightness(0.97);
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Form bits */
label {
  font-size: 12.5px;
  color: var(--g500);
  font-weight: 500;
}

input[type="text"],
input[type="number"],
input[type="password"] {
  border: 1.5px solid var(--g200);
  border-radius: var(--r-ctrl);
  padding: 10px 14px;
  font: inherit;
  font-size: 12.5px;
  outline: none;
  transition: 0.15s;
  background: var(--white);
  color: var(--g900);
  width: 100%;
}

input[type="number"] {
  width: 72px;
}

input:focus {
  border-color: var(--indigo);
  box-shadow: 0 0 0 3px var(--indigo-soft);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}

.field-row .field {
  flex: 0 1 auto;
}

.divider {
  height: 1px;
  background: var(--g100);
  margin: 16px 0;
}

/* Device */
.device-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

#device-info {
  font-size: 13px;
  font-weight: 600;
  color: var(--indigo);
  min-height: 1.2em;
}

.auth-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}

.auth-row .field {
  flex: 0 0 140px;
}

/* Status grid */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}

.status-item {
  background: var(--bg);
  border-radius: var(--r-ctrl);
  padding: 14px 16px;
  transition: 0.15s;
}

.status-item.feature {
  background: var(--indigo);
  box-shadow: var(--sh-pop);
  grid-column: span 1;
}

.status-item .label {
  display: block;
  font-size: 12.5px;
  color: var(--g400);
  font-weight: 500;
  margin-bottom: 6px;
}

.status-item.feature .label {
  color: rgba(255, 255, 255, 0.65);
}

.status-item .value {
  font-size: 22px;
  font-weight: 750;
  letter-spacing: -0.8px;
  color: var(--g900);
}

.status-item.feature .value {
  color: #fff;
  font-size: 27px;
  letter-spacing: -1px;
}

.status-item .unit {
  font-size: 12px;
  color: var(--g400);
  font-weight: 500;
  margin-left: 2px;
}

.status-item.feature .unit {
  color: rgba(255, 255, 255, 0.65);
}

.status-item .value.on {
  color: var(--green);
}

.status-item .value.off {
  color: var(--g500);
}

.status-item .value.locked {
  color: var(--red);
}

.status-item .value.unlocked {
  color: var(--green);
}

.status-item .value.active {
  color: var(--indigo);
}

button.status-item.status-action {
  font: inherit;
  text-align: left;
  cursor: pointer;
  position: relative;
  border: none;
  width: 100%;
  transition: 0.15s;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
}

.status-action-top {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-bottom: 2px;
}

.status-action-top .label {
  margin-bottom: 0;
}

.status-icon {
  width: 28px;
  height: 28px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--g100);
  color: var(--g500);
}

.status-icon svg {
  width: 15px;
  height: 15px;
  display: block;
}

button.status-item.status-action:hover:not(:disabled) {
  box-shadow: var(--sh-card);
}

button.status-item.status-action:disabled {
  cursor: not-allowed;
  opacity: 0.7;
}

button.status-item.status-action.busy {
  cursor: wait;
  opacity: 1;
}

button.status-item.status-action.tone-danger {
  background: var(--red-soft);
}
button.status-item.status-action.tone-danger .status-icon {
  background: rgba(220, 38, 38, 0.12);
  color: var(--red);
}
button.status-item.status-action.tone-danger .value {
  color: var(--red);
}

button.status-item.status-action.tone-ok {
  background: var(--green-soft);
}
button.status-item.status-action.tone-ok .status-icon {
  background: rgba(5, 150, 105, 0.12);
  color: var(--green);
}
button.status-item.status-action.tone-ok .value {
  color: var(--green);
}

button.status-item.status-action.tone-active {
  background: var(--indigo-soft);
}
button.status-item.status-action.tone-active .status-icon {
  background: rgba(79, 70, 229, 0.12);
  color: var(--indigo);
}
button.status-item.status-action.tone-active .value {
  color: var(--indigo);
}

button.status-item.status-action.tone-warn {
  background: var(--amber-soft);
}
button.status-item.status-action.tone-warn .status-icon {
  background: rgba(217, 119, 6, 0.14);
  color: var(--amber);
}
button.status-item.status-action.tone-warn .value {
  color: var(--amber);
}

button.status-item.status-action.tone-muted {
  background: var(--g100);
}
button.status-item.status-action.tone-muted .status-icon {
  background: var(--g200);
  color: var(--g500);
}
button.status-item.status-action.tone-muted .value {
  color: var(--g500);
}

button.status-item.status-action.tone-danger:hover:not(:disabled),
button.status-item.status-action.tone-ok:hover:not(:disabled),
button.status-item.status-action.tone-active:hover:not(:disabled),
button.status-item.status-action.tone-warn:hover:not(:disabled),
button.status-item.status-action.tone-muted:hover:not(:disabled) {
  filter: brightness(0.98);
}

button.status-item.status-action .btn-spinner {
  position: absolute;
  top: 12px;
  right: 12px;
}

.btn-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--g300);
  border-top-color: var(--indigo);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%) translateY(12px);
  z-index: 1100;
  min-width: 220px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px 18px;
  border-radius: var(--r-pill);
  background: var(--white);
  color: var(--g900);
  box-shadow: var(--sh-hover);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}

.toast.err {
  background: var(--red-soft);
  color: var(--red);
}

/* Command groups */
.cmd-group {
  margin-bottom: 14px;
}

.cmd-group:last-child {
  margin-bottom: 0;
}

.cmd-label {
  font-size: 11.5px;
  color: var(--g400);
  font-weight: 500;
  margin-bottom: 8px;
}

/* Messages */
.collapse-btn {
  flex-shrink: 0;
}

.collapse-btn svg {
  transition: transform 0.2s;
}

.card.collapsed .collapse-btn svg {
  transform: rotate(-90deg);
}

.card.collapsed .card-head {
  margin-bottom: 0;
}

.collapse-body {
  display: block;
}

.card.collapsed .collapse-body {
  display: none;
}

.log-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--g500);
  font-weight: 500;
  cursor: pointer;
  user-select: none;
}

.check input {
  accent-color: var(--indigo);
  width: 15px;
  height: 15px;
}

#messages {
  background: var(--bg);
  color: var(--g700);
  padding: 14px 16px;
  border-radius: var(--r-ctrl);
  min-height: 280px;
  max-height: 520px;
  overflow-y: auto;
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

#messages-panel {
  display: flex;
  flex-direction: column;
}

#messages-panel #messages {
  flex: 1;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  inset: 0;
  background: rgba(17, 24, 39, 0.35);
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.dark .modal {
  background: rgba(0, 0, 0, 0.55);
}

.modal-content {
  background: var(--white);
  padding: 28px;
  width: 100%;
  max-width: 360px;
  border-radius: var(--r-card);
  box-shadow: var(--sh-hover);
  text-align: center;
}

.modal-content .bubble {
  width: 48px;
  height: 48px;
  border-radius: var(--r-ctrl);
  background: var(--red-soft);
  color: var(--red);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 14px;
  font-size: 20px;
  font-weight: 700;
}

.modal-content h3 {
  font-size: 16px;
  font-weight: 700;
  color: var(--g900);
  margin-bottom: 8px;
}

.modal-content p {
  font-size: 13px;
  color: var(--g500);
  margin-bottom: 16px;
  line-height: 1.5;
}

.modal-content input {
  margin-bottom: 16px;
  text-align: center;
  max-width: 160px;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

/* Layout helpers */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1.9fr;
  gap: 16px;
  margin-bottom: 16px;
}

.grid-2 > .card {
  margin-bottom: 0;
}

@media (max-width: 1000px) {
  .status-grid {
    grid-template-columns: repeat(3, 1fr);
  }
  .grid-2 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 600px) {
  .app {
    padding: 16px;
  }

  .brand h1 {
    font-size: 20px;
  }

  .status-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .status-item .value {
    font-size: 18px;
  }

  .status-item.feature .value {
    font-size: 22px;
  }
}
