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

:root {
  --bg-base: #0a0a0f;
  --bg-panel: #0f1018;
  --bg-card: #141420;
  --border: #1a1a2e;
  --text-primary: #e0e0ec;
  --text-secondary: #7a7a92;
  --text-dim: #4a4a60;
  --green-safe: #22c55e;
  --green-glow: #22c55e66;
  --red-alarm: #ef4444;
  --red-glow: #ef444466;
  --amber: #f59e0b;
  --teal: #14b8a6;
  --blue: #3b82f6;
  --purple: #a855f7;
  --orange: #f97316;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

html, body {
  height: 100%;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font);
  overflow: hidden;
}

/* ── Top Bar ───────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}
.topbar-logo {
  height: 32px;
  width: auto;
}
.topbar-sip {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-secondary);
}
.sip-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
  transition: background 0.3s, box-shadow 0.3s;
}
.sip-dot.connected {
  background: var(--green-safe);
  box-shadow: 0 0 6px var(--green-glow);
}
.sip-dot.error {
  background: var(--red-alarm);
  box-shadow: 0 0 6px var(--red-glow);
}
.sip-duration { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.topbar-lag {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: 4px;
  background: rgba(255,255,255,0.04);
}
.topbar-lag.warn { color: var(--amber); }
.topbar-lag:empty { display: none; }
.topbar-clock {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}

/* ── Main Layout — vertical stack ─────────────── */
.main {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 48px);
}

/* Row 1: Alarm sidebar + Timeline */
.row-timeline {
  display: flex;
  flex: 0 0 42%;
  min-height: 200px;
  border-bottom: 1px solid var(--border);
}

/* ── Alarm Indicator (left sidebar) ───────────── */
.alarm-sidebar {
  flex: 0 0 140px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--border);
  background: var(--bg-panel);
  padding: 12px;
}
.alarm-badge {
  position: relative;
  width: 110px;
  height: 110px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.alarm-circle {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 2px;
  transition: background 0.5s, box-shadow 0.5s;
  z-index: 2;
  position: relative;
}
.alarm-circle.safe {
  background: radial-gradient(circle at 40% 35%, #2dd66a, #188a3e);
  box-shadow: 0 0 30px var(--green-glow), 0 0 60px rgba(34,197,94,0.15);
}
.alarm-circle.alarm {
  background: radial-gradient(circle at 40% 35%, #f87171, #b91c1c);
  box-shadow: 0 0 30px var(--red-glow), 0 0 60px rgba(239,68,68,0.2);
}
.alarm-label {
  font-size: 10px;
  color: var(--text-secondary);
  margin-top: 10px;
  height: 32px;
  transition: color 0.3s;
  text-align: center;
  line-height: 1.3;
}
.alarm-label.active { color: var(--red-alarm); font-weight: 600; }
.alarm-subtitle {
  font-size: 9px;
  color: var(--text-secondary);
  opacity: 0.5;
  text-align: center;
  margin-top: 2px;
}

/* Pulse rings */
.pulse-ring {
  position: absolute;
  top: 50%; left: 50%;
  width: 100px; height: 100px;
  border-radius: 50%;
  transform: translate(-50%, -50%) scale(1);
  border: 2px solid var(--red-alarm);
  opacity: 0;
  pointer-events: none;
}
.alarm-badge.alarming .pulse-ring {
  animation: pulseExpand 2s ease-out infinite;
}
.pulse-ring:nth-child(2) { animation-delay: 0.5s; }
.pulse-ring:nth-child(3) { animation-delay: 1.0s; }
.pulse-ring:nth-child(4) { animation-delay: 1.5s; }

@keyframes pulseExpand {
  0%   { transform: translate(-50%, -50%) scale(1);   opacity: 0.6; }
  100% { transform: translate(-50%, -50%) scale(2.0); opacity: 0; }
}

/* ── Timeline ──────────────────────────────────── */
.timeline-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.timeline-wrap {
  flex: 1;
  position: relative;
  padding: 12px 12px 0 12px;
  min-height: 0;
}
.timeline-canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 6px;
}
/* Scanline overlay */
.timeline-wrap::after {
  content: '';
  position: absolute;
  inset: 12px 12px 0 12px;
  border-radius: 6px;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg, transparent, transparent 2px,
    rgba(0,0,0,0.04) 2px, rgba(0,0,0,0.04) 4px
  );
}
.timeline-legend {
  display: flex;
  gap: 14px;
  padding: 4px 16px 6px;
  flex-wrap: wrap;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 10px;
  color: var(--text-dim);
}
.legend-dot {
  width: 8px; height: 8px;
  border-radius: 2px;
}

/* ── Row 2: Histogram ─────────────────────────── */
.row-histogram {
  flex: 1;
  display: flex;
  min-height: 120px;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  position: relative;
}
.histo-column {
  flex: 1;
  padding: 10px 20px 8px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.histo-column + .histo-column {
  border-left: 1px solid var(--border);
}
.histo-column-header {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  margin-bottom: 8px;
}
.histo-column-header.alarm-group { color: #f87171; }
.histo-column-header.other-group { color: var(--teal); }
.histo-rows {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 4px;
}
.histo-row {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 22px;
}
.histo-label {
  flex: 0 0 110px;
  font-size: 11px;
  color: var(--text-secondary);
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.histo-bar-wrap {
  flex: 1;
  height: 16px;
  background: rgba(255,255,255,0.03);
  border-radius: 3px;
  overflow: hidden;
}
.histo-bar {
  height: 100%;
  border-radius: 3px;
  transition: width 0.6s cubic-bezier(0.22, 1, 0.36, 1);
  min-width: 0;
}
.histo-pct {
  flex: 0 0 40px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
  text-align: right;
}

/* Column hidden/visible transitions */
.histo-column.hidden-alarm,
.histo-column.hidden-other {
  flex: 0 0 0;
  padding: 0;
  overflow: hidden;
  opacity: 0;
  border-left: none;
  border-right: none;
  transition: flex 0.6s, opacity 0.6s, padding 0.6s;
}
.histo-column.visible-alarm,
.histo-column.visible-other {
  flex: 1;
  opacity: 1;
  padding: 10px 20px 8px;
  transition: flex 0.6s, opacity 0.6s, padding 0.6s;
}
.histo-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--text-dim);
  font-style: italic;
}

/* Histogram waiting state */
.histo-waiting {
  display: none;
  position: absolute;
  inset: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg-base);
  z-index: 2;
}
.histo-waiting.visible { display: flex; }
.histo-waiting-icon {
  font-size: 28px;
  opacity: 0.4;
  animation: waitPulse 2s ease-in-out infinite;
}
@keyframes waitPulse {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 0.5; }
}

/* ── Row 3: Event Feed ────────────────────────── */
.row-feed {
  flex: 0 0 auto;
  max-height: 25%;
  min-height: 80px;
  padding: 0 20px 10px;
  display: flex;
  flex-direction: column;
}
.feed-header {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 4px;
  padding-top: 8px;
}
.feed-list {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.feed-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
  font-size: 11px;
  transition: opacity 0.3s;
}
.feed-item.old { opacity: 0.4; }
.feed-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}
.feed-time {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  flex: 0 0 52px;
}
.feed-label {
  flex: 1;
  color: var(--text-secondary);
}
.feed-conf {
  flex: 0 0 42px;
  text-align: right;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 3px;
  background: rgba(255,255,255,0.05);
  color: var(--text-dim);
}

/* ── Threshold control ────────────────────────── */
.threshold-control {
  margin-top: 16px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.threshold-label {
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-dim);
}
.threshold-slider {
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  outline: none;
  cursor: pointer;
}
.threshold-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--red-alarm);
  border: 2px solid var(--bg-panel);
  cursor: pointer;
}
.threshold-slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--red-alarm);
  border: 2px solid var(--bg-panel);
  cursor: pointer;
}
.threshold-value {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
}

/* ── Status bar ────────────────────────────────── */
.status-bar {
  flex: 0 0 auto;
  padding: 2px 20px;
  font-size: 10px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
}

/* ── Dial Panel ───────────────────────────────── */
.dial-panel {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 15, 0.88);
  backdrop-filter: blur(6px);
  z-index: 100;
}
.dial-panel.hidden { display: none; }
.dial-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 36px 40px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  min-width: 340px;
  box-shadow: 0 24px 64px rgba(0,0,0,0.6);
}
.dial-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  text-transform: uppercase;
}
.dial-row {
  display: flex;
  gap: 8px;
  width: 100%;
}
.dial-input {
  flex: 1;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 2px;
  padding: 10px 14px;
  outline: none;
  transition: border-color 0.2s;
}
.dial-input:focus { border-color: var(--blue); }
.dial-btn {
  background: var(--green-safe);
  border: none;
  border-radius: 6px;
  color: #000;
  font-size: 14px;
  font-weight: 700;
  padding: 10px 18px;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s;
}
.dial-btn:hover { opacity: 0.85; }
.dial-btn:active { opacity: 0.7; }
.dial-domain {
  font-size: 11px;
  color: var(--text-dim);
}
.dial-error {
  font-size: 11px;
  color: var(--red-alarm);
  min-height: 14px;
}

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