/* UBlue Admin Web UI */

:root {
  --bg: #f4f6fa;
  --bg-elevated: #ffffff;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --surface-hover: #eef4ff;
  --border: rgba(15, 23, 42, 0.10);
  --border-strong: rgba(15, 23, 42, 0.16);

  --text-primary: #0f172a;
  --text-secondary: #334155;
  --text-muted: #64748b;

  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --info: #0284c7;
  --jidian: #7c3aed;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  --font-mono: "SF Mono", "Cascadia Code", Consolas, monospace;
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 0.9375rem;
  --text-lg: 1.125rem;
  --text-xl: 1.5rem;
  --text-2xl: 2rem;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-full: 999px;
  --shadow-popover: 0 18px 60px rgba(15, 23, 42, 0.14);
  --transition-fast: 150ms ease;
  --transition-normal: 200ms ease;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-sans);
  line-height: 1.5;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button,
input,
select,
textarea {
  font: inherit;
  letter-spacing: 0;
}

a {
  color: var(--accent-hover);
  text-decoration: none;
}

a:hover {
  color: var(--text-primary);
}

code {
  font-family: var(--font-mono);
}

.bg-gradient {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    linear-gradient(180deg, rgba(59, 130, 246, 0.08), transparent 320px),
    var(--bg);
}

.page-wrapper {
  position: relative;
  z-index: 0;
  min-height: 100vh;
}

.page-container {
  width: min(1240px, calc(100% - 48px));
  margin: 0 auto;
  padding: var(--space-5) 0 var(--space-6);
}

.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;
}

.mono,
.device-id,
.time,
.ip-value,
.rank-device-id {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.time,
.ip-value {
  color: var(--text-muted);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.muted {
  color: var(--text-muted);
}

/* Navigation */
.nav-bar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 58px;
  padding: 0 var(--space-5);
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.nav-left,
.nav-right {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

.nav-title {
  margin: 0;
  color: var(--text-primary);
  font-size: var(--text-base);
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-back,
.logout-link {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.nav-back:hover,
.logout-link:hover {
  color: var(--text-primary);
}

.logout-link {
  color: #b91c1c;
}

.user-name {
  max-width: 180px;
  overflow: hidden;
  color: var(--text-muted);
  font-size: var(--text-sm);
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Page scaffolding */
.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.page-header-main {
  min-width: 0;
}

.page-title {
  margin: 0;
  color: var(--text-primary);
  font-size: var(--text-xl);
  font-weight: 700;
  line-height: 1.25;
}

.page-subtitle {
  margin: var(--space-1) 0 0;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.page-header-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.section-card,
.glass-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.section-card + .section-card,
.glass-card + .glass-card {
  margin-top: var(--space-4);
}

.section-card.compact {
  padding: var(--space-4);
}

.section-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}

.section-title {
  margin: 0;
  color: var(--text-primary);
  font-size: var(--text-lg);
  font-weight: 650;
}

.section-subtitle {
  margin: var(--space-1) 0 0;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

/* Metrics */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: var(--space-3);
  margin-bottom: var(--space-5);
}

.metric-card {
  min-width: 0;
  padding: var(--space-4);
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.78);
}

.metric-label {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.metric-value {
  margin-top: var(--space-2);
  color: var(--text-primary);
  font-size: var(--text-2xl);
  font-weight: 750;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.metric-value.success {
  color: var(--success);
}

.metric-value.warning {
  color: var(--warning);
}

.metric-value.danger {
  color: var(--danger);
}

.metric-value.accent {
  color: var(--accent-hover);
}

/* Forms and controls */
.toolbar {
  display: grid;
  grid-template-columns: minmax(160px, 220px) minmax(240px, 1fr) auto;
  gap: var(--space-3);
  align-items: end;
  margin-bottom: var(--space-4);
  padding: var(--space-4);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.toolbar.toolbar-simple {
  grid-template-columns: minmax(160px, 220px) auto;
}

.toolbar-actions {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
}

.field label,
.form-group label {
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: 600;
}

.input {
  width: 100%;
  min-height: 42px;
  padding: 0 var(--space-3);
  color: var(--text-primary);
  background: #ffffff;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color var(--transition-normal), box-shadow var(--transition-normal), background var(--transition-normal);
}

textarea.input {
  min-height: 96px;
  padding-top: var(--space-3);
  padding-bottom: var(--space-3);
  resize: vertical;
}

.input::placeholder {
  color: var(--text-muted);
}

.input:focus {
  background: #ffffff;
  border-color: rgba(96, 165, 250, 0.82);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.22);
}

select.input {
  appearance: none;
  padding-right: 40px;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%), linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 18px, calc(100% - 13px) 18px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  padding: 0 var(--space-4);
  color: var(--text-primary);
  background: #ffffff;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-sm);
  font-weight: 650;
  line-height: 1;
  white-space: nowrap;
  transition: background var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast), transform var(--transition-fast);
}

.btn:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
  text-decoration: none;
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  cursor: wait;
  opacity: 0.62;
}

.btn-primary {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-danger {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.btn-danger:hover {
  background: #b91c1c;
  border-color: #b91c1c;
}

.btn-ghost {
  color: var(--text-secondary);
  background: transparent;
}

.btn-ghost:hover {
  color: var(--text-primary);
  background: var(--surface-hover);
}

.btn-sm {
  min-height: 34px;
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
}

.btn-block {
  width: 100%;
}

:focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 2px;
}

/* Badges */
.badge,
.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: #f8fafc;
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: 650;
  white-space: nowrap;
}

.badge-success,
.status-success {
  color: #166534;
  background: #dcfce7;
  border-color: #bbf7d0;
}

.badge-warning,
.status-warning {
  color: #92400e;
  background: #fef3c7;
  border-color: #fde68a;
}

.badge-danger,
.status-danger {
  color: #991b1b;
  background: #fee2e2;
  border-color: #fecaca;
}

.badge-accent,
.status-accent {
  color: #1d4ed8;
  background: #dbeafe;
  border-color: #bfdbfe;
}

.badge-purple,
.status-purple {
  color: #6d28d9;
  background: #ede9fe;
  border-color: #ddd6fe;
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: auto;
}

.data-table th,
.data-table td {
  padding: 13px var(--space-4);
  text-align: left;
  vertical-align: middle;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  color: var(--text-muted);
  background: var(--surface-soft);
  font-size: var(--text-xs);
  font-weight: 700;
}

.data-table td {
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

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

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

.device-cell {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 220px;
}

.device-title {
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-weight: 700;
  overflow-wrap: anywhere;
}

.device-id {
  color: var(--text-muted);
  font-size: var(--text-xs);
  overflow-wrap: anywhere;
}

.status-stack {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

/* Device mobile list */
.mobile-only {
  display: none !important;
}

.device-card-list {
  display: grid;
  gap: var(--space-3);
}

.device-mobile-card {
  display: grid;
  gap: var(--space-3);
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.device-mobile-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}

.device-mobile-meta {
  display: grid;
  gap: var(--space-2);
}

.meta-row,
.card-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.meta-row span:first-child,
.card-row .label {
  flex-shrink: 0;
  color: var(--text-muted);
}

.meta-row strong,
.card-row .value {
  min-width: 0;
  color: var(--text-primary);
  font-weight: 550;
  overflow-wrap: anywhere;
  text-align: right;
}

.device-mobile-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding-top: var(--space-2);
  border-top: 1px solid var(--border);
}

/* Detail page */
.detail-summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--space-4);
  align-items: center;
  margin-bottom: var(--space-5);
  background: var(--bg-elevated);
  box-shadow: 0 12px 34px rgba(15, 23, 42, 0.08);
}

.summary-title {
  margin: 0;
  color: var(--text-primary);
  font-size: var(--text-xl);
  font-weight: 750;
  overflow-wrap: anywhere;
}

.summary-id {
  margin-top: var(--space-1);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  overflow-wrap: anywhere;
}

.summary-badges {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

.summary-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr));
  gap: var(--space-3);
}

.summary-stat {
  padding: var(--space-3);
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.summary-stat-label {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.summary-stat-value {
  margin-top: var(--space-1);
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.workbench-grid,
.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: var(--space-4);
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
}

.info-item {
  min-width: 0;
  padding: var(--space-3);
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.info-label {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

.info-value {
  margin-top: 3px;
  color: var(--text-primary);
  font-size: var(--text-sm);
  overflow-wrap: anywhere;
}

.auth-code-display {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--space-3);
  align-items: center;
  margin: var(--space-4) 0;
  padding: var(--space-4);
  background: #f8fafc;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
}

.auth-code-display code {
  color: #166534;
  font-size: var(--text-base);
  font-weight: 700;
  overflow-wrap: anywhere;
}

.action-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

.action-group form {
  display: inline-flex;
}

.stacked-form {
  display: grid !important;
  gap: var(--space-2);
  min-width: min(100%, 260px);
  padding: var(--space-3);
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.remark-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: var(--space-3);
  align-items: end;
  margin-bottom: var(--space-4);
}

.danger-zone {
  margin-top: var(--space-4);
  background: #fff7f7;
  border-color: #fecaca;
}

.danger-zone .section-title {
  color: #991b1b;
}

.danger-zone .section-subtitle {
  color: #b91c1c;
}

.detail-full {
  margin-top: var(--space-4);
}

.timeline-list {
  display: grid;
  gap: var(--space-3);
  list-style: none;
  margin: 0;
  padding: 0;
}

.timeline-item {
  display: grid;
  gap: var(--space-2);
  padding: var(--space-4);
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.timeline-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.timeline-meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  color: var(--text-muted);
  font-size: var(--text-xs);
}

/* Active devices */
.rank-list {
  display: grid;
  gap: var(--space-3);
}

.rank-item {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto auto;
  gap: var(--space-4);
  align-items: center;
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.rank-item.rank-top {
  background-color: var(--surface);
  background-image: linear-gradient(90deg, #eff6ff, var(--surface) 54%);
  border-color: #bfdbfe;
}

.rank-number {
  color: var(--accent-hover);
  font-family: var(--font-mono);
  font-size: var(--text-lg);
  font-weight: 800;
  text-align: center;
}

.rank-device {
  min-width: 0;
}

.rank-device-title {
  color: var(--text-primary);
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-device-id {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: var(--text-xs);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-count {
  min-width: 92px;
  text-align: right;
}

.rank-count .metric-value {
  margin: 0;
}

.rank-count-label {
  color: var(--text-muted);
  font-size: var(--text-xs);
}

/* Empty states */
.empty-state {
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  padding: var(--space-6);
  color: var(--text-muted);
  text-align: center;
}

.empty-title {
  color: var(--text-primary);
  font-weight: 700;
}

.empty-text {
  max-width: 420px;
  color: var(--text-muted);
  font-size: var(--text-sm);
}

/* Login */
.login-scene {
  min-height: calc(100vh - 58px);
  display: grid;
  place-items: center;
  padding: var(--space-5);
}

.login-card {
  width: min(100%, 392px);
}

.login-card .section-card {
  padding: var(--space-6);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-popover);
}

.login-title {
  margin: 0;
  color: var(--text-primary);
  font-size: var(--text-xl);
  font-weight: 750;
  text-align: center;
}

.login-subtitle {
  margin: var(--space-2) 0 var(--space-5);
  color: var(--text-muted);
  font-size: var(--text-sm);
  text-align: center;
}

.form-group {
  display: grid;
  gap: var(--space-1);
  margin-bottom: var(--space-4);
}

.form-error {
  margin-bottom: var(--space-4);
  padding: var(--space-3);
  color: #991b1b;
  background: #fee2e2;
  border: 1px solid #fecaca;
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
}

/* Dialog */
dialog.confirm-dialog,
dialog.glass-dialog {
  width: min(420px, calc(100% - 32px));
  padding: 0;
  color: var(--text-primary);
  background: transparent;
  border: 0;
}

dialog.confirm-dialog::backdrop,
dialog.glass-dialog::backdrop {
  background: rgba(15, 23, 42, 0.36);
}

.dialog-card {
  padding: var(--space-5);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-popover);
}

.dialog-title {
  color: var(--text-primary);
  font-size: var(--text-lg);
  font-weight: 700;
}

.dialog-body {
  margin-top: var(--space-2);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-5);
}

/* Responsive */
@media (max-width: 1100px) {
  .metric-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .workbench-grid,
  .detail-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 860px) {
  .page-container {
    width: min(100% - 32px, 1240px);
  }

  .toolbar,
  .toolbar.toolbar-simple {
    grid-template-columns: 1fr;
  }

  .toolbar-actions {
    width: 100%;
  }

  .toolbar-actions .btn {
    flex: 1 1 auto;
  }

  .desktop-only {
    display: none !important;
  }

  .mobile-only {
    display: block !important;
  }

  .device-card-list.mobile-only,
  .timeline-list.mobile-only {
    display: grid !important;
  }

  .detail-summary {
    grid-template-columns: 1fr;
  }

  .summary-stats {
    grid-template-columns: 1fr 1fr;
  }

  .rank-item {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .rank-count,
  .rank-item .btn {
    grid-column: 2;
    justify-self: start;
    text-align: left;
  }

  .rank-item .btn {
    width: 100%;
  }
}

@media (max-width: 560px) {
  .nav-bar {
    padding: 0 var(--space-4);
  }

  .nav-right {
    gap: var(--space-2);
  }

  .user-name {
    display: none;
  }

  .page-container {
    width: min(100% - 24px, 1240px);
    padding-top: var(--space-4);
  }

  .page-header {
    align-items: stretch;
    flex-direction: column;
  }

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

  .section-card,
  .glass-card {
    padding: var(--space-4);
  }

  .toolbar-actions,
  .action-group,
  .remark-form,
  .auth-code-display {
    grid-template-columns: 1fr;
    flex-direction: column;
    align-items: stretch;
  }

  .action-group form,
  .action-group .btn,
  .toolbar-actions .btn,
  .remark-form .btn {
    width: 100%;
  }

  .info-grid,
  .summary-stats {
    grid-template-columns: 1fr;
  }

  .device-mobile-head,
  .device-mobile-actions,
  .timeline-head,
  .dialog-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .dialog-actions .btn {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
