:root {
  --bg: #f5f5f7;
  --card-bg: #ffffff;
  --card-border: transparent;
  --card-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --text-primary: #1d1d1f;
  --text-secondary: #86868b;
  --text-tertiary: #aeaeb2;
  --accent: #007aff;
  --success: #34c759;
  --warning: #ff9f0a;
  --error: #ff3b30;
  --overlay: rgba(0, 0, 0, 0.4);
  --panel-bg: #ffffff;
  --skeleton: #e8e8ed;
  --header-bg: rgba(245, 245, 247, 0.72);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card-bg: #1c1c1e;
    --card-border: rgba(255, 255, 255, 0.08);
    --card-shadow: none;
    --text-primary: #f5f5f7;
    --text-secondary: #98989d;
    --text-tertiary: #636366;
    --accent: #0a84ff;
    --success: #30d158;
    --warning: #ffd60a;
    --error: #ff453a;
    --overlay: rgba(0, 0, 0, 0.6);
    --panel-bg: #1c1c1e;
    --skeleton: #2c2c2e;
    --header-bg: rgba(0, 0, 0, 0.72);
  }
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

.dashboard {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 24px 32px;
  gap: 24px;
  overflow: hidden;
}

/* Header */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--card-border);
}

.title {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.5px;
}

.subtitle {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 24px;
}

.global-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 500;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
}

.status-dot.warning { background: var(--warning); box-shadow: 0 0 8px var(--warning); }
.status-dot.error { background: var(--error); box-shadow: 0 0 8px var(--error); }

.clock {
  font-size: 32px;
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  letter-spacing: -1px;
  color: var(--text-primary);
}

/* KPI Row */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  flex-shrink: 0;
}

.kpi-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 20px 24px;
  box-shadow: var(--card-shadow);
  transition: transform 0.2s ease;
}

.kpi-label {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.kpi-value {
  font-size: 52px;
  font-weight: 600;
  letter-spacing: -2px;
  line-height: 1.1;
  margin-top: 8px;
}

.kpi-value.success { color: var(--success); }
.kpi-value.warning { color: var(--warning); }
.kpi-value.error { color: var(--error); }

.kpi-detail {
  font-size: 13px;
  color: var(--text-tertiary);
  margin-top: 6px;
}

/* Section */
.section-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.integrations-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.integration-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  overflow-y: auto;
  flex: 1;
  padding-bottom: 4px;
}

.integration-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 20px 24px;
  box-shadow: var(--card-shadow);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.integration-card:hover {
  transform: translateY(-2px);
}

.integration-card:active {
  transform: translateY(0);
}

.integration-name {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 12px;
}

.integration-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 16px;
}

.integration-status .status-dot {
  width: 8px;
  height: 8px;
}

.integration-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-item {
  display: flex;
  flex-direction: column;
}

.stat-label {
  font-size: 12px;
  color: var(--text-tertiary);
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -1px;
}

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

.integration-meta {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--card-border);
  font-size: 12px;
  color: var(--text-tertiary);
  display: flex;
  justify-content: space-between;
}

/* Errors */
.errors-section {
  flex-shrink: 0;
  max-height: 160px;
}

.errors-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  max-height: 120px;
}

.error-item {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
}

.error-item .error-icon {
  color: var(--error);
  font-size: 16px;
  flex-shrink: 0;
}

.error-item .error-msg {
  flex: 1;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.error-item .error-time {
  color: var(--text-tertiary);
  font-size: 12px;
  flex-shrink: 0;
}

.error-empty {
  color: var(--text-tertiary);
  font-size: 14px;
  padding: 12px 0;
}

/* Skeleton */
.skeleton {
  background: linear-gradient(90deg, var(--skeleton) 25%, var(--card-bg) 50%, var(--skeleton) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  min-height: 80px;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.fade-in {
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Detail Panel */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  backdrop-filter: blur(4px);
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.overlay.visible { opacity: 1; }

.detail-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 480px;
  max-width: 90vw;
  height: 100vh;
  background: var(--panel-bg);
  border-left: 1px solid var(--card-border);
  z-index: 101;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.detail-panel.visible {
  transform: translateX(0);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 24px;
  border-bottom: 1px solid var(--card-border);
  flex-shrink: 0;
}

.panel-title {
  font-size: 20px;
  font-weight: 600;
}

.panel-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.panel-close {
  background: none;
  border: none;
  font-size: 28px;
  color: var(--text-secondary);
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
}

.panel-close:hover { color: var(--text-primary); }

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.panel-loading {
  color: var(--text-secondary);
  text-align: center;
  padding: 40px 0;
}

.panel-section {
  margin-bottom: 28px;
}

.panel-section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

.panel-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 8px;
}

.panel-stat {
  background: var(--bg);
  border-radius: 12px;
  padding: 16px;
  text-align: center;
}

.panel-stat-value {
  font-size: 32px;
  font-weight: 600;
}

.panel-stat-label {
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 4px;
}

.device-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.device-table th {
  text-align: left;
  padding: 8px 12px;
  color: var(--text-tertiary);
  font-weight: 500;
  border-bottom: 1px solid var(--card-border);
}

.device-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--card-border);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-table tr:last-child td { border-bottom: none; }

.error-timeline {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.timeline-item {
  padding-left: 16px;
  border-left: 2px solid var(--error);
}

.timeline-device {
  font-weight: 500;
  font-size: 14px;
}

.timeline-error {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.timeline-time {
  font-size: 12px;
  color: var(--text-tertiary);
  margin-top: 4px;
}

.panel-empty {
  color: var(--text-tertiary);
  font-size: 14px;
  padding: 16px 0;
}

/* Mobile */
@media (max-width: 768px) {
  html, body {
    overflow: auto;
  }

  .dashboard {
    height: auto;
    min-height: 100vh;
    overflow: visible;
    padding: 16px;
    gap: 20px;
  }

  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .header-right {
    width: 100%;
    justify-content: space-between;
  }

  .title {
    font-size: 22px;
  }

  .clock {
    font-size: 24px;
  }

  .kpi-row {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .kpi-card {
    padding: 16px;
  }

  .kpi-value {
    font-size: 36px;
  }

  .integration-grid {
    grid-template-columns: 1fr;
    overflow: visible;
  }

  .integration-card {
    padding: 16px 20px;
  }

  .stat-value {
    font-size: 24px;
  }

  .errors-section {
    max-height: none;
  }

  .errors-list {
    max-height: none;
  }

  .detail-panel {
    width: 100vw;
    max-width: 100vw;
  }

  .panel-body {
    padding: 16px;
  }

  .device-table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}
