:root {
  --bg: #eef2f5;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #607080;
  --line: #cfd8df;
  --accent: #1769aa;
  --accent-2: #0f8a6a;
  --danger: #b44242;
  --soft: #e8f3fb;
  --input: #ddf0d8;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

button,
input,
select {
  font: inherit;
}

button,
.file-button {
  border: 1px solid #9fb4c5;
  background: #f8fbfd;
  color: #142433;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.toolbar-select {
  width: auto;
  min-width: 130px;
}

button:hover,
.file-button:hover {
  border-color: var(--accent);
  background: #edf7ff;
}

button.active,
.report-tabs button.active,
#pickPointBtn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.file-button input {
  display: none;
}

.app-header {
  min-height: 58px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #1f3c5b;
  color: #fff;
  border-bottom: 1px solid #18324d;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.brand strong {
  font-size: 18px;
}

.brand span {
  color: #d3e2ee;
  font-size: 13px;
}

.header-actions,
.toolbar,
.button-row,
.report-header,
.report-tabs,
.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.header-actions button {
  background: #294d71;
  color: #fff;
  border-color: #5f7f9d;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 390px;
  gap: 12px;
  padding: 12px;
}

.workspace,
.side-panel,
.reports {
  min-width: 0;
}

.toolbar {
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px 8px 0 0;
}

.canvas-wrap {
  position: relative;
  height: 68vh;
  min-height: 520px;
  background: #fff;
  border: 1px solid var(--line);
  border-top: 0;
  overflow: hidden;
}

#ecgCanvas {
  width: 100%;
  height: 100%;
  display: block;
  background: #fff;
  cursor: crosshair;
}

.magnifier-popup {
  position: absolute;
  z-index: 4;
  width: 190px;
  height: 214px;
  padding: 4px;
  border: 2px solid #1769aa;
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 12px 30px rgba(16, 40, 63, 0.24);
  pointer-events: none;
}

.magnifier-popup.hidden {
  display: none;
}

#magnifierCanvas {
  width: 180px;
  height: 180px;
  display: block;
  border: 1px solid #c2d3df;
  border-radius: 5px;
  background: #fff;
  image-rendering: pixelated;
}

#magnifierLabel {
  display: block;
  margin-top: 3px;
  color: #17405f;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 14px;
  color: var(--muted);
  pointer-events: none;
  background: #fff;
}

.empty-state.hidden {
  display: none;
}

.empty-state img {
  max-width: min(560px, 75%);
  max-height: 330px;
  object-fit: contain;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-section,
.reports,
dialog {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.panel-section {
  padding: 12px;
}

h2 {
  margin: 0 0 10px;
  font-size: 16px;
}

.form-grid {
  display: grid;
  gap: 8px;
}

.form-grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

label {
  display: grid;
  gap: 4px;
  color: #394b5a;
  font-size: 13px;
}

input,
select {
  width: 100%;
  min-height: 34px;
  border: 1px solid #b8c8d3;
  border-radius: 6px;
  padding: 5px 8px;
  background: #fff;
  color: var(--ink);
}

.coordinate-readout,
.coordinate-readout,
.summary-grid {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.calibration-readout {
  margin-top: 10px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.coordinate-readout span,
.calibration-readout span,
.summary-grid div {
  background: var(--soft);
  border: 1px solid #c6dcea;
  border-radius: 6px;
  padding: 8px;
}

.calibration-readout span,
.summary-grid span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.calibration-readout strong {
  display: block;
  margin-top: 3px;
  color: #10283f;
}

.table-scroll {
  overflow: auto;
  border: 1px solid var(--line);
  background: #fff;
}

.table-scroll.compact {
  max-height: 340px;
}

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

th,
td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: center;
  white-space: nowrap;
}

th {
  background: #e7ecf1;
  font-weight: 600;
}

td.input-cell {
  background: var(--input);
}

tr.selected-row td {
  background: #d9ebff;
}

.reports {
  margin: 0 12px 12px;
  padding: 12px;
}

.report-tabs {
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
  margin-bottom: 12px;
}

.report-panel {
  display: none;
}

.report-panel.active {
  display: block;
}

.report-header {
  justify-content: space-between;
  margin-bottom: 10px;
}

.section-header {
  justify-content: space-between;
  margin-bottom: 10px;
}

.section-header h2 {
  margin: 0;
}

.report-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.report-grid.two-cols {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

dialog {
  width: min(1120px, calc(100vw - 32px));
  padding: 0;
}

dialog::backdrop {
  background: rgba(20, 32, 44, 0.55);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.help-grid {
  display: grid;
  gap: 12px;
  padding: 12px;
  max-height: 78vh;
  overflow: auto;
}

.help-grid img {
  max-width: 100%;
  height: auto;
  border: 1px solid var(--line);
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .side-panel {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .report-grid,
  .report-grid.two-cols {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 700px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }

  .side-panel,
  .form-grid.two {
    grid-template-columns: 1fr;
  }

  .canvas-wrap {
    min-height: 420px;
  }
}
