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

:root {
  --bg: #0a0a0b;
  --surface: #121214;
  --surface-2: #1a1a1e;
  --border: #222226;
  --border-active: #2a2a30;
  --text: #e8e8ea;
  --text-2: #88889a;
  --text-3: #55556a;
  --green: #22c55e;
  --green-bg: rgba(34, 197, 94, 0.08);
  --green-border: rgba(34, 197, 94, 0.2);
  --red: #ef4444;
  --yellow: #eab308;
  --radius: 12px;
  --radius-sm: 8px;
}

html { font-size: 16px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro', 'Inter', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 880px;
  margin: 0 auto;
  padding: 16px;
}

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

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.3px;
  background: linear-gradient(135deg, var(--green), #4ade80);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.logo-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.conn-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-3);
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-3);
  transition: background 0.3s;
}

.conn-dot.online { background: var(--green); box-shadow: 0 0 6px rgba(34,197,94,0.4); }
.conn-dot.offline { background: var(--red); }
.conn-dot.qr { background: var(--yellow); }

/* Contact Cards */
.contacts-grid {
  margin-bottom: 24px;
}

.contact-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.contact-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  cursor: pointer;
  transition: all 0.25s;
  position: relative;
  overflow: hidden;
}

.contact-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--border);
  transition: background 0.4s;
}

.contact-card.online::before {
  background: var(--green);
  box-shadow: 0 0 12px rgba(34,197,94,0.3);
}

.contact-card.online {
  border-color: var(--green-border);
  background: linear-gradient(135deg, var(--green-bg), var(--surface));
}

.contact-card.selected {
  border-color: var(--border-active);
  box-shadow: 0 0 0 1px rgba(255,255,255,0.05);
}

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.card-name {
  font-size: 16px;
  font-weight: 600;
}

.card-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: all 0.3s;
}

.card-badge.online {
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid var(--green-border);
}

.card-badge.offline {
  background: rgba(136,136,154,0.1);
  color: var(--text-2);
}

.card-meta {
  font-size: 12px;
  color: var(--text-3);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.card-pulse {
  position: absolute;
  top: 18px;
  right: 60px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  opacity: 0;
}

.card-pulse.active {
  animation: pulse-ring 1.5s ease-out;
}

@keyframes pulse-ring {
  0% { transform: scale(1); opacity: 0.8; }
  50% { transform: scale(2.5); opacity: 0; }
  100% { transform: scale(1); opacity: 0; }
}

/* Section */
.section {
  margin-bottom: 24px;
}

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

.section-head h2 {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-2);
}

.section-sub {
  font-size: 12px;
  color: var(--text-3);
}

.ctrl-select {
  background: var(--surface);
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  outline: none;
}

.ctrl-select:focus {
  border-color: var(--text-3);
}

/* Today Stats */
.today-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

/* Side-by-side comparison table (Today) */
.compare-table {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.ct-row {
  display: grid;
  align-items: center;
  border-bottom: 1px solid var(--border);
}

.ct-row:last-child { border-bottom: none; }

.ct-head {
  background: var(--surface-2);
}

.ct-metric {
  padding: 12px 14px;
  font-size: 12px;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.ct-cell {
  padding: 12px 14px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  text-align: center;
  border-left: 1px solid var(--border);
}

.ct-name {
  font-size: 15px;
  font-weight: 700;
}

.ct-badge {
  font-size: 12px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.ct-badge.online { color: var(--green); background: var(--green-bg); border: 1px solid var(--green-border); }
.ct-badge.offline { color: var(--text-3); background: var(--surface-2); border: 1px solid var(--border); }

/* Side-by-side columns (Pattern, Activity) */
.compare-cols {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.compare-col {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  min-width: 0;
}

.compare-col-head {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

@media (max-width: 640px) {
  .compare-cols { grid-template-columns: 1fr; }
}

/* ===== Together: verdict banner ===== */
.verdict {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
}
.verdict strong { color: var(--text); }
.verdict.high { border-color: var(--green-border); background: var(--green-bg); }
.verdict.high strong { color: var(--green); }
.verdict.mid { border-color: rgba(234, 179, 8, 0.3); background: rgba(234, 179, 8, 0.08); }
.verdict.mid strong { color: #eab308; }
.verdict.low { opacity: 0.85; }

/* ===== Together: overlap metric cards ===== */
.overlap-metrics {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}
.ov-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 10px;
  text-align: center;
}
.ov-card.active { border-color: var(--green-border); }
.ov-val { font-size: 20px; font-weight: 700; color: var(--text); margin-bottom: 4px; }
.ov-label { font-size: 11px; color: var(--text-3); line-height: 1.3; }

/* ===== Together: co-online timeline ===== */
.timeline-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 14px 8px;
}
.lane-row {
  display: grid;
  grid-template-columns: 60px 1fr;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.lane-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lane-both-label { color: var(--green); }
.lane {
  position: relative;
  height: 22px;
  background: var(--surface-2);
  border-radius: 5px;
  overflow: hidden;
}
.lane .seg {
  position: absolute;
  top: 0;
  height: 100%;
  background: var(--text-2);
  border-radius: 2px;
}
#lane-a .seg { background: #3b82f6; }
#lane-b .seg { background: #a855f7; }
.lane-both-track { background: rgba(34, 197, 94, 0.06); }
#lane-both .seg { background: var(--green); box-shadow: 0 0 6px rgba(34, 197, 94, 0.5); }
.lane-axis {
  position: relative;
  grid-column: 2;
  height: 16px;
  margin-left: 70px;
}
.axis-tick {
  position: absolute;
  transform: translateX(-50%);
  font-size: 10px;
  color: var(--text-3);
  white-space: nowrap;
}
.axis-tick:first-child { transform: none; }
.axis-tick:last-child { transform: translateX(-100%); }

/* ===== Daily rhythm heatmap ===== */
.heatmap { display: flex; flex-direction: column; gap: 4px; overflow-x: auto; }
.hm-row { display: grid; grid-template-columns: 52px repeat(24, 1fr); gap: 2px; align-items: center; }
.hm-name { font-size: 12px; font-weight: 600; color: var(--text-2); text-align: right; padding-right: 4px; }
.hm-cell { height: 20px; border-radius: 3px; background: var(--surface-2); }
.hm-head .hm-cell { height: auto; background: none; }
.hm-hlabel { font-size: 9px; color: var(--text-3); text-align: center; }
.hm-a { background: rgba(59, 130, 246, var(--op, 0)); }
.hm-b { background: rgba(168, 85, 247, var(--op, 0)); }
.hm-both { background: rgba(34, 197, 94, var(--op, 0)); }

@media (max-width: 640px) {
  .overlap-metrics { grid-template-columns: repeat(2, 1fr); }
  .lane-row { grid-template-columns: 46px 1fr; }
  .lane-axis { margin-left: 56px; }
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
  transition: all 0.3s;
}

.stat-card.disabled {
  opacity: 0.4;
}

.stat-card.active {
  border-color: var(--green-border);
}

.stat-val {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 10px;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 500;
}

/* Pattern Chart */
.pattern-chart {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-height: 180px;
}

.pattern-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.pattern-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pattern-label {
  width: 36px;
  font-size: 10px;
  color: var(--text-3);
  text-align: right;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

.pattern-bar-bg {
  flex: 1;
  height: 14px;
  background: rgba(255,255,255,0.03);
  border-radius: 7px;
  overflow: hidden;
  position: relative;
}

.pattern-bar {
  height: 100%;
  border-radius: 7px;
  transition: width 0.6s cubic-bezier(0.22, 1, 0.36, 1);
  background: linear-gradient(90deg, rgba(34,197,94,0.6), var(--green));
  min-width: 2px;
}

.pattern-count {
  width: 28px;
  font-size: 10px;
  color: var(--text-3);
  text-align: right;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

.pattern-legend {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-3);
  margin-top: 12px;
  padding: 0 44px;
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.online-dot { background: var(--green); }

/* Activity Timeline */
.activity-timeline {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 360px;
  overflow-y: auto;
}

.timeline-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.03);
  font-size: 13px;
}

.timeline-item:last-child {
  border-bottom: none;
}

.timeline-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.timeline-dot.online { background: var(--green); }
.timeline-dot.offline { background: var(--text-3); }

.timeline-time {
  color: var(--text-3);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  min-width: 60px;
}

.timeline-label {
  font-weight: 500;
}

.timeline-label.online { color: var(--green); }
.timeline-label.offline { color: var(--text-2); }

.timeline-duration {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-3);
}

/* QR Overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  animation: overlay-in 0.3s ease;
}

@keyframes overlay-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.overlay.hidden { display: none; }

.qr-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  text-align: center;
  max-width: 380px;
  width: 100%;
  animation: card-in 0.3s ease;
}

@keyframes card-in {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.qr-header {
  margin-bottom: 4px;
}

.qr-icon {
  font-size: 32px;
  display: block;
  margin-bottom: 8px;
}

.qr-card h2 {
  font-size: 18px;
  font-weight: 600;
}

.qr-sub {
  font-size: 13px;
  color: var(--text-2);
  margin-bottom: 24px;
}

.qr-code-wrap {
  margin: 0 auto 24px;
  width: 220px;
  height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border-radius: 12px;
  padding: 12px;
}

.qr-code-wrap img {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.qr-steps {
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.qr-step {
  font-size: 13px;
  color: var(--text-2);
  display: flex;
  align-items: center;
  gap: 10px;
}

.step-num {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  flex-shrink: 0;
}

.qr-error {
  font-size: 13px;
  color: var(--red);
  margin-top: 8px;
}

.qr-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
  justify-content: center;
}

.qr-btn {
  flex: 1;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  background: var(--green);
  border: 1px solid var(--green-border);
  border-radius: 10px;
  cursor: pointer;
  transition: opacity 0.15s, background 0.15s;
}

.qr-btn:hover { opacity: 0.9; }
.qr-btn:disabled { opacity: 0.45; cursor: default; }

.qr-btn-ghost {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
}

.qr-action-msg {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 10px;
  text-align: center;
}

/* States */
.empty-state {
  color: var(--text-3);
  font-size: 13px;
  text-align: center;
  padding: 40px 0;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 2px solid rgba(255,255,255,0.05);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* Loading Skeleton */
.loading-skeleton {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.skeleton-card {
  height: 108px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  animation: shimmer 1.5s infinite;
  background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%);
  background-size: 200% 100%;
}

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

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface-2); border-radius: 3px; }

/* Responsive */
@media (max-width: 640px) {
  .app { padding: 12px; }
  .contact-cards { grid-template-columns: 1fr; }
  .today-grid { grid-template-columns: repeat(2, 1fr); }
  .qr-card { padding: 24px 20px; }
  .qr-code-wrap { width: 180px; height: 180px; }
}
