:root {
  --chrome: #c9cdd1;
  --chrome-2: #b1b5b6;
  --chrome-dark: #8d9296;
  --panel: #e9ebed;
  --field: #d6d9dc;
  --ink: #101418;
  --ink-2: #4a5157;
  --plot: #000;
  --accent: #1f5fbf;
  --danger: #c62828;
  --mono: "Consolas", "SFMono-Regular", "Menlo", monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--chrome); color: var(--ink);
  font: 13px/1.35 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }

.titlebar {
  display: flex; align-items: center; gap: 10px; padding: 6px 12px;
  background: #fff; border-bottom: 1px solid var(--chrome-dark);
}
.titlebar h1 { margin: 0; font-size: 14px; font-weight: 600; }
.titlebar .sub { color: var(--ink-2); font-size: 12px; }
.titlebar a { margin-left: auto; color: var(--accent); font-size: 12px; font-weight: 600; text-decoration: none; }

.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 6px 10px;
  background: var(--panel); border-bottom: 1px solid var(--chrome-dark);
}
.toolbar .sep { width: 1px; height: 22px; background: var(--chrome-dark); margin: 0 4px; }
.btn {
  padding: 4px 10px; border: 1px solid #9aa0a5; border-radius: 3px; cursor: pointer;
  background: linear-gradient(#fdfdfd, #dfe2e5); white-space: nowrap;
}
.btn:hover { background: linear-gradient(#fff, #e9ecef); }
.btn:active { background: #d3d7db; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.toolbar label { display: inline-flex; align-items: center; gap: 4px; }
.toolbar input[type=number] { width: 52px; }

.tabs { display: flex; gap: 2px; overflow-x: auto; max-width: 30vw; }
.tab {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px; cursor: pointer;
  border: 1px solid #9aa0a5; border-radius: 3px 3px 0 0; background: #dfe2e5; white-space: nowrap;
  max-width: 240px;
}
.tab span { overflow: hidden; text-overflow: ellipsis; }
.tab.active { background: #fff; font-weight: 600; }
.tab b { color: var(--ink-2); font-weight: 400; cursor: pointer; }
.tab b:hover { color: var(--danger); }

.main { flex: 1; display: flex; min-height: 0; gap: 6px; padding: 6px; }
.scope {
  flex: 1; min-width: 0; display: flex; flex-direction: column;
  background: var(--chrome-2); border: 1px solid var(--chrome-dark); padding: 6px;
}
.canvas-wrap { position: relative; flex: 1; min-height: 240px; }
#plot { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; color: #cfd3d6; font-size: 14px; pointer-events: none; text-align: center; padding: 16px;
}
.empty strong { color: #fff; font-size: 16px; }
.empty[hidden] { display: none; }
.canvas-wrap.drag::after {
  content: "Thả file .cwf hoặc .csv để mở"; position: absolute; inset: 8px; border: 2px dashed #7fb3ff;
  display: flex; align-items: center; justify-content: center; color: #cfe3ff; font-size: 16px;
  background: rgba(31, 95, 191, .18); pointer-events: none;
}

.statusbar {
  display: grid; grid-template-columns: auto auto 1fr auto auto; gap: 10px; align-items: end; padding-top: 6px;
}
.field { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--ink-2); }
.readout {
  font-family: var(--mono); font-size: 12px; color: var(--ink); background: var(--field);
  border: 1px solid #9aa0a5; padding: 2px 6px; min-width: 92px; white-space: pre; line-height: 1.3;
}
#startAbs { cursor: pointer; }
#btnStart.attn { background: #fff3cd; border-color: #d39e00; color: #6b4d00; font-weight: 600; }
#startAbs:hover { border-color: var(--ink); }
.inline { display: flex; align-items: center; gap: 4px; }
.inline input { width: 64px; color: var(--ink); font-family: var(--mono); background: var(--field); border: 1px solid #9aa0a5; padding: 2px 4px; }
.center { justify-self: center; display: flex; gap: 10px; align-items: end; }

.channels {
  width: 244px; flex-shrink: 0; display: flex; flex-direction: column; gap: 4px; overflow-y: auto;
}
.ch {
  --c: #fff; background: var(--c); border: 1px solid #555; padding: 4px 5px 5px; color: #000;
  cursor: default; position: relative;
}
.ch.sel { outline: 2px solid #1a1a1a; outline-offset: -1px; }
.ch.off { filter: saturate(.25) brightness(.85); }
.ch-top { display: flex; gap: 3px; align-items: center; }
.ch-top select {
  flex: 1; min-width: 0; font-weight: 600; text-align: center; background: linear-gradient(#f4f4f4, #d0d0d0);
  border: 1px solid #777; border-radius: 2px; padding: 1px 2px;
}
.icon {
  min-width: 22px; height: 20px; border: 1px solid #777; border-radius: 2px; background: linear-gradient(#f4f4f4, #d0d0d0);
  cursor: pointer; font-size: 10px; font-weight: 600; line-height: 18px; padding: 0 3px; text-align: center;
}
.ch-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 3px 6px; margin-top: 4px; }
.spin { display: flex; align-items: center; gap: 2px; }
.spin .arrows { display: flex; flex-direction: column; }
.spin .arrows button {
  width: 14px; height: 10px; padding: 0; border: 1px solid #777; background: #e8e8e8; font-size: 8px; line-height: 8px; cursor: pointer;
}
.spin input {
  width: 100%; min-width: 0; text-align: right; font-family: var(--mono); font-size: 12px;
  background: #d9d9d9; border: 1px solid #777; padding: 1px 3px;
}
.unit { font-size: 11px; white-space: nowrap; }
.ab { display: flex; align-items: center; gap: 3px; font-size: 11px; }
.ab .readout { min-width: 0; flex: 1; text-align: right; background: #d9d9d9; border-color: #777; font-size: 11.5px; }
.add-ch { border-style: dashed; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 10; padding: 16px; }
.modal[hidden] { display: none; }
.dialog { background: var(--panel); border: 1px solid #555; width: min(920px, 100%); max-height: 100%; display: flex; flex-direction: column; }
.dialog header { display: flex; align-items: center; gap: 10px; padding: 6px 10px; background: #fff; border-bottom: 1px solid var(--chrome-dark); }
.dialog header h2 { margin: 0; font-size: 14px; }
.dialog header .btn { margin-left: auto; }
.dialog .body { padding: 10px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }
#fftCanvas { width: 100%; height: 360px; background: #000; display: block; }
.fft-info { font-family: var(--mono); font-size: 12px; color: var(--ink-2); white-space: pre-wrap; }

.toast {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); background: #1d2227; color: #fff;
  padding: 8px 14px; border-radius: 4px; font-size: 13px; z-index: 20; max-width: 90vw;
}
.toast.err { background: var(--danger); }
.toast[hidden] { display: none; }
.help { color: var(--ink-2); font-size: 12px; margin-left: auto; cursor: help; white-space: nowrap; text-decoration: underline dotted; }

@media (max-width: 820px) {
  body { overflow: auto; height: auto; }
  .main { flex-direction: column; }
  .canvas-wrap { min-height: 360px; }
  .channels { width: 100%; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
  .statusbar { grid-template-columns: 1fr 1fr; }
  .center { justify-self: start; grid-column: 1 / -1; }
  .help { display: none; }
}

.seg { display: inline-flex; border: 1px solid #9aa0a5; border-radius: 3px; overflow: hidden; }
.seg button { border: 0; padding: 4px 12px; background: linear-gradient(#fdfdfd, #dfe2e5); cursor: pointer; }
.seg button + button { border-left: 1px solid #9aa0a5; }
.seg button.on { background: #1f5fbf; color: #fff; font-weight: 600; }
body.mode-report .scope, body.mode-report .channels, body.mode-report .scope-only { display: none; }

.report-view { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chrome-2); border: 1px solid var(--chrome-dark); }
.report-view[hidden], .rp-panels[hidden] { display: none; }
.rp-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; padding: 6px 8px; background: var(--panel); border-bottom: 1px solid var(--chrome-dark); }
.rp-bar label { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.rp-bar label.grow { flex: 1 1 260px; }
.rp-bar label.grow input { flex: 1; min-width: 0; }
.rp-bar input:not([type=checkbox]) { padding: 3px 5px; border: 1px solid #9aa0a5; border-radius: 2px; background: #fff; }
.rp-bar input[type=number] { width: 70px; }
.rp-spacer { flex: 1; }
.paper-wrap { flex: 1; overflow: auto; padding: 16px; background: #8f969c; }
#reportCanvas { display: block; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,.35); cursor: grab; touch-action: none; }
#reportCanvas[hidden] { display: none; }
.rp-empty { color: #eef0f2; text-align: center; padding: 60px 16px; font-size: 14px; }
.rp-empty[hidden] { display: none; }

.rp-panels { width: 266px; flex-shrink: 0; display: flex; flex-direction: column; gap: 6px; overflow-y: auto; }
.rp-hint { font-size: 11.5px; color: var(--ink-2); background: #eef0f2; border: 1px solid #c3c8cc; padding: 6px 8px; line-height: 1.45; }
.rp { background: #fff; border: 1px solid #aab0b5; padding: 6px; display: flex; flex-direction: column; gap: 5px; }
.rp-row { display: flex; gap: 3px; align-items: center; }
.rp-row select { flex: 1; min-width: 0; padding: 2px; border: 1px solid #9aa0a5; border-radius: 2px; }
.rp-row input[type=color] { width: 24px; height: 22px; padding: 0; border: 1px solid #9aa0a5; background: none; }
.rp-title { width: 100%; padding: 3px 5px; border: 1px solid #9aa0a5; border-radius: 2px; font-weight: 600; }
.rp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 6px; }
.rp-grid label { display: flex; flex-direction: column; font-size: 11px; color: var(--ink-2); gap: 1px; }
.rp-grid input { width: 100%; box-sizing: border-box; padding: 2px 4px; border: 1px solid #9aa0a5; border-radius: 2px; font-family: var(--mono); font-size: 12px; min-width: 0; }
.rp-actions { display: flex; gap: 6px; }
.rp-actions .btn { flex: 1; }
.icon:disabled { opacity: .35; cursor: default; }

@media (max-width: 820px) {
  .rp-panels { width: 100%; }
  .report-view { min-height: 70vh; }
}

.rp-readout { background: #fff; border-bottom: 1px solid var(--chrome-dark); padding: 6px 8px; }
.rp-readout[hidden] { display: none; }
.rp-readout-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; margin-bottom: 6px; }
.rp-readout-bar .muted { color: var(--ink-2); font-size: 11.5px; }
.rp-readout-bar label { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.rp-readout-bar input[type=number] { width: 48px; padding: 2px 4px; border: 1px solid #9aa0a5; border-radius: 2px; }
.rp-table-wrap { overflow-x: auto; }
#repTable { border-collapse: collapse; font-size: 12px; white-space: nowrap; }
#repTable th, #repTable td { border: 1px solid #cfd4d8; padding: 3px 8px; }
#repTable thead th { background: #eef0f2; font-weight: 600; text-align: center; }
#repTable td { font-family: var(--mono); text-align: right; }
#repTable tbody th { text-align: center; width: 44px; }
#repTable tr.ra th { color: #fff; background: #d93025; }
#repTable tr.rb th { color: #fff; background: #188038; }
#repTable tr.rd th { background: #eef0f2; }
#repTable tr.rd td { font-weight: 600; }
