:root {
  --slate-50: #f8fafc;
  --slate-100: #f1f5f9;
  --slate-200: #e2e8f0;
  --slate-300: #cbd5e1;
  --slate-400: #94a3b8;
  --slate-500: #64748b;
  --slate-600: #475569;
  --slate-700: #334155;
  --slate-800: #1e293b;
  --slate-900: #0f172a;
  --blue: #2563eb;
  --blue-2: #3b82f6;
  --sky: #0ea5e9;
  --teal: #0d9488;
  --teal-2: #14b8a6;
  --purple: #9333ea;
  --rose: #e11d48;
  --rose-2: #f43f5e;
  --orange: #ea580c;
  --amber: #f59e0b;
  --amber-2: #fbbf24;
  --emerald: #059669;
  --indigo: #4f46e5;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  background: var(--slate-100);
  color: var(--slate-800);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.35;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

button,
select {
  cursor: pointer;
}

button:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

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

.svg-sprite {
  position: absolute;
}

svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.app {
  display: flex;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  flex-direction: column;
  background: var(--slate-100);
  user-select: none;
}

.toolbar {
  z-index: 2;
  display: flex;
  height: 56px;
  flex: 0 0 56px;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--slate-300);
  background: #ffffff;
  padding: 0 16px;
}

.toolbar-left,
.toolbar-right,
.brand,
.connect-btn,
.tool-btn,
.live-badge,
.card-title,
.api-row,
.mode-row,
.bottom-bar,
.status-metrics div {
  display: flex;
  align-items: center;
}

.toolbar-left {
  min-width: 0;
  gap: 14px;
}

.toolbar-right {
  gap: 8px;
}

.brand {
  gap: 8px;
  min-width: 238px;
}

.brand h1 {
  margin: 0;
  color: var(--slate-800);
  font-size: 14px;
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
}

.brand-icon {
  color: var(--blue);
  width: 20px;
  height: 20px;
}

.divider {
  width: 1px;
  height: 18px;
  background: var(--slate-300);
}

.connect-btn {
  gap: 7px;
  min-height: 32px;
  border: 0;
  border-radius: 6px;
  background: var(--blue);
  color: #ffffff;
  padding: 0 12px;
  font-size: 12px;
  font-weight: 700;
  transition: background 160ms ease;
}

.connect-btn:hover {
  background: #1d4ed8;
}

.connect-btn.connected {
  background: #d1fae5;
  color: #047857;
}

.connect-btn.connected:hover {
  background: #a7f3d0;
}

select {
  height: 32px;
  border: 1px solid var(--slate-200);
  border-radius: 6px;
  background: var(--slate-50);
  color: var(--slate-600);
  padding: 0 8px;
  font-size: 12px;
}

.live-badge {
  gap: 6px;
  border-radius: 999px;
  background: var(--slate-100);
  color: var(--slate-500);
  padding: 4px 8px;
  font-size: 10px;
  font-weight: 700;
}

.live-badge i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--slate-400);
}

.live-badge.live {
  background: #ecfdf5;
  color: var(--emerald);
}

.live-badge.live i {
  background: #10b981;
  animation: pulse 1.1s infinite;
}

.tool-btn {
  gap: 5px;
  min-height: 32px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--slate-600);
  padding: 0 8px;
  font-size: 12px;
}

.tool-btn:hover {
  background: var(--slate-100);
}

.workspace {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  gap: 12px;
  overflow: hidden;
  padding: 12px;
}

.sensor-column {
  display: grid;
  width: 25%;
  min-width: 220px;
  max-width: 330px;
  height: 100%;
  min-height: 0;
  grid-template-rows: repeat(6, minmax(0, 1fr));
  gap: 8px;
}

.chart-column {
  display: grid;
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  min-height: 0;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.ai-panel {
  display: flex;
  width: 25%;
  min-width: 260px;
  max-width: 390px;
  min-height: 0;
  flex-direction: column;
  border: 1px solid var(--slate-200);
  background: #ffffff;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.sensor-card,
.chart-box {
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--slate-200);
  background: #ffffff;
}

.sensor-card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 8px;
}

.card-title {
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
  color: var(--slate-500);
}

.card-title span {
  overflow: hidden;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}

.card-title svg {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
}

.card-body {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  justify-content: center;
}

.card-body.two-col {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 8px;
}

.value-row {
  display: grid;
  min-width: 0;
  grid-template-columns: minmax(46px, 1fr) auto auto;
  align-items: baseline;
  gap: 5px;
  border-bottom: 1px solid var(--slate-50);
  padding: 2px 0;
}

.two-col .value-row {
  grid-template-columns: minmax(24px, 1fr) auto auto;
}

.value-row:last-child {
  border-bottom: 0;
}

.value-row span {
  overflow: hidden;
  color: var(--slate-500);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.value-row b {
  color: var(--slate-700);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  font-weight: 800;
  text-align: right;
  white-space: nowrap;
}

.value-row b.large {
  font-size: 18px;
}

.value-row small {
  color: var(--slate-400);
  font-size: 10px;
}

.blue { color: var(--blue) !important; }
.blue-2 { color: var(--blue-2) !important; }
.sky { color: var(--sky) !important; }
.teal { color: var(--teal) !important; }
.teal-2 { color: var(--teal-2) !important; }
.purple { color: var(--purple) !important; }
.rose { color: var(--rose) !important; }
.rose-2 { color: var(--rose-2) !important; }
.orange { color: var(--orange) !important; }
.amber { color: var(--amber) !important; }
.amber-2 { color: var(--amber-2) !important; }

.chart-box {
  display: flex;
  flex-direction: column;
}

.chart-box header {
  flex: 0 0 24px;
  border-bottom: 1px solid var(--slate-100);
  background: var(--slate-50);
  color: var(--slate-500);
  padding: 5px 8px;
  font-size: 10px;
  font-weight: 800;
  line-height: 1;
}

.chart-host {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  padding: 4px;
}

.empty-chart {
  display: flex;
  height: 100%;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 6px;
  border: 1px dashed var(--slate-200);
  border-radius: 3px;
  background: rgba(248, 250, 252, 0.7);
  color: var(--slate-400);
  font-size: 12px;
  font-weight: 700;
}

.empty-chart svg {
  width: 22px;
  height: 22px;
  opacity: 0.55;
}

.mini-chart {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.legend {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 7px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.82);
  padding: 1px 4px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 10px;
  font-weight: 800;
}

.ai-header {
  flex: 0 0 34px;
  border-bottom: 1px solid var(--slate-200);
  background: var(--slate-50);
  padding: 0 12px;
}

.ai-header h2 {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 100%;
  margin: 0;
  color: #4338ca;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.ai-settings {
  flex: 0 0 auto;
  border-bottom: 1px solid var(--slate-100);
  padding: 10px 12px;
}

.api-row {
  gap: 8px;
}

.api-row svg {
  color: var(--slate-400);
  flex: 0 0 auto;
}

.api-row input {
  width: 100%;
  min-width: 0;
  height: 30px;
  border: 1px solid var(--slate-200);
  border-radius: 5px;
  background: var(--slate-50);
  color: var(--slate-700);
  padding: 0 8px;
  font-size: 11px;
  outline: none;
}

.api-row input:focus {
  border-color: #a5b4fc;
  background: #ffffff;
}

.mode-row {
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.mode-row > span {
  color: var(--slate-500);
  font-size: 10px;
  font-weight: 700;
}

.mode-switch {
  display: flex;
  border-radius: 6px;
  background: var(--slate-100);
  padding: 2px;
}

.mode-switch button {
  min-height: 22px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--slate-500);
  padding: 0 8px;
  font-size: 10px;
}

.mode-switch button.active {
  background: #ffffff;
  color: var(--slate-800);
  font-weight: 800;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

.analyze-btn {
  display: flex;
  width: 100%;
  min-height: 32px;
  align-items: center;
  justify-content: center;
  gap: 5px;
  margin-top: 10px;
  border: 1px solid #c7d2fe;
  border-radius: 6px;
  background: #eef2ff;
  color: #4338ca;
  font-size: 12px;
  font-weight: 800;
}

.analyze-btn:not(:disabled):hover {
  background: #e0e7ff;
}

.spin {
  display: inline-block;
  animation: spin 850ms linear infinite;
}

.ai-log {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  background: rgba(248, 250, 252, 0.65);
  padding: 12px;
  font-size: 12px;
}

.ai-message {
  border: 1px solid var(--slate-200);
  border-radius: 6px;
  background: #ffffff;
  padding: 8px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.ai-message.system {
  background: var(--slate-100);
  color: var(--slate-500);
  box-shadow: none;
}

.ai-message.auto {
  border-color: #bfdbfe;
  background: #eff6ff;
}

.ai-message.warning {
  border-color: #fecdd3;
  background: #fff1f2;
}

.ai-message.manual {
  border-color: #e0e7ff;
}

.message-head {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 4px;
  color: var(--slate-500);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.message-head time {
  margin-left: auto;
  font-weight: 600;
  letter-spacing: 0;
}

.ai-message p {
  margin: 0;
  color: var(--slate-700);
  line-height: 1.45;
}

.ai-message.warning p {
  color: #9f1239;
  font-weight: 650;
}

.bottom-bar {
  height: 80px;
  flex: 0 0 80px;
  border-top: 1px solid var(--slate-800);
  background: var(--slate-900);
}

.serial-log {
  flex: 1 1 auto;
  height: 100%;
  overflow-y: auto;
  padding: 8px;
  color: var(--slate-400);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 10px;
}

.serial-line {
  display: flex;
  gap: 8px;
  min-height: 14px;
}

.serial-line time {
  color: var(--slate-600);
}

.status-metrics {
  display: flex;
  width: 256px;
  flex: 0 0 256px;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
  border-left: 1px solid var(--slate-800);
  padding: 8px;
  color: var(--slate-400);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 10px;
}

.status-metrics div {
  justify-content: space-between;
}

.status-metrics b {
  color: var(--slate-300);
}

#statusText.connected {
  color: #34d399;
}

#statusText.disconnected {
  color: #f59e0b;
}

#packetText {
  color: #60a5fa;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

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

@media (max-width: 1100px) {
  .brand {
    min-width: 184px;
  }

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

  .sensor-column {
    min-width: 204px;
  }

  .ai-panel {
    min-width: 248px;
  }
}

@media (max-height: 720px) {
  .toolbar {
    height: 52px;
    flex-basis: 52px;
  }

  .workspace {
    gap: 8px;
    padding: 8px;
  }

  .sensor-column,
  .chart-column {
    gap: 6px;
  }

  .sensor-card {
    padding: 6px;
  }

  .bottom-bar {
    height: 72px;
    flex-basis: 72px;
  }
}
