/* ===== Reset & base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f0f2f5;
  color: #1a1a2e;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Header ===== */
header {
  background: linear-gradient(135deg, #1a3a5c 0%, #16213e 100%);
  color: #fff;
  padding: 1rem 2rem;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
}
.header-inner { display: flex; align-items: center; gap: 0.75rem; }
.header-icon  { font-size: 1.6rem; color: #e74c3c; }
.header-title { font-size: 1.3rem; font-weight: 700; letter-spacing: 0.02em; }
.header-sub   { font-size: 0.8rem; color: #aac4e8; margin-left: auto; }

/* ===== Footer ===== */
footer {
  text-align: center;
  padding: 1rem;
  font-size: 0.75rem;
  color: #888;
  margin-top: auto;
}

/* ===== Main layout ===== */
main {
  flex: 1;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  padding: 2rem 1.5rem;
}

section { margin-bottom: 2.5rem; }
h2 {
  font-size: 1.1rem;
  font-weight: 600;
  color: #1a3a5c;
  margin-bottom: 0.4rem;
  border-left: 4px solid #e74c3c;
  padding-left: 0.7rem;
}
.hint {
  font-size: 0.85rem;
  color: #555;
  margin-bottom: 1rem;
}

/* ===== Loading overlay ===== */
#loading-overlay {
  position: fixed; inset: 0; z-index: 999;
  background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
}
#loading-overlay[hidden] { display: none !important; }
.loading-box {
  background: #fff;
  border-radius: 12px;
  padding: 2rem 2.5rem;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,.3);
}
.spinner {
  width: 48px; height: 48px;
  border: 5px solid #e0e0e0;
  border-top-color: #e74c3c;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading-text { font-size: 1rem; color: #333; }

/* ===== Error banner ===== */
#error-banner {
  position: sticky; top: 0; z-index: 500;
  background: #ffe0e0;
  border-bottom: 2px solid #e74c3c;
}
.error-inner {
  max-width: 1400px; margin: 0 auto;
  padding: 0.6rem 1.5rem;
  display: flex; align-items: center; gap: 1rem;
}
#error-msg { flex: 1; color: #c0392b; font-size: 0.9rem; }
#error-banner button {
  background: none; border: none;
  font-size: 1.3rem; color: #c0392b; cursor: pointer;
}

/* ===== Drop zone ===== */
#dropzone {
  border: 2.5px dashed #b0bec5;
  border-radius: 12px;
  padding: 4rem 2rem;
  text-align: center;
  background: #fff;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  user-select: none;
}
#dropzone:hover, #dropzone.drag-over {
  border-color: #e74c3c;
  background: #fff5f5;
}
.dropzone-icon { font-size: 3rem; color: #ccc; margin-bottom: 0.5rem; }
.dropzone-text { font-size: 1.1rem; color: #444; font-weight: 500; }
.dropzone-sub  { font-size: 0.85rem; color: #999; margin-top: 0.3rem; }

/* ===== Caliper toolbar ===== */
.caliper-toolbar {
  display: flex; align-items: center; gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}
.caliper-status {
  font-size: 0.85rem;
  color: #555;
  background: #eef2ff;
  border-radius: 6px;
  padding: 0.3rem 0.7rem;
}
.caliper-status.active { background: #fff3e0; color: #e67e22; font-weight: 600; }
.caliper-status.done   { background: #e8f5e9; color: #27ae60; font-weight: 600; }
.caliper-hint {
  font-size: 0.82rem;
  background: #fffde7;
  border-left: 3px solid #f9a825;
  padding: 0.5rem 0.9rem;
  border-radius: 4px;
  margin-top: 0.5rem;
  color: #555;
}

/* ===== Canvas wrapper ===== */
#canvas-wrapper {
  width: 100%;
  overflow-x: auto;
  background: #ddd;
  border-radius: 8px;
  border: 1px solid #ccc;
}
#ecg-canvas {
  display: block;
  max-width: 100%;
  cursor: default;
}
#ecg-canvas.caliper-active { cursor: crosshair; }

/* ===== Buttons ===== */
.btn-primary {
  background: #e74c3c; color: #fff;
  border: none; border-radius: 6px;
  padding: 0.55rem 1.3rem;
  font-size: 0.9rem; font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-primary:hover:not(:disabled) { background: #c0392b; }
.btn-primary:disabled { background: #ccc; cursor: default; }

.btn-secondary {
  background: #fff; color: #444;
  border: 1.5px solid #ccc; border-radius: 6px;
  padding: 0.5rem 1.2rem;
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s;
}
.btn-secondary:hover { border-color: #e74c3c; color: #e74c3c; }

/* ===== Results ===== */
.results-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}
@media (max-width: 900px) {
  .results-grid { grid-template-columns: 1fr; }
}

.card {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
  padding: 1.25rem;
}
.card-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: #1a3a5c;
  margin-bottom: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Signal plot fill */
#signal-plot { min-height: 300px; }

/* ===== Measurements table ===== */
.measurements {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.measurements th {
  background: #1a3a5c;
  color: #fff;
  padding: 0.5rem 0.75rem;
  text-align: left;
  font-weight: 600;
}
.measurements td { padding: 0.5rem 0.75rem; border-bottom: 1px solid #eee; }
.measurements .value-cell {
  font-family: 'Courier New', monospace;
  font-size: 1.05rem;
  font-weight: 700;
}
.measurements tr.normal  { background: #f0fff4; }
.measurements tr.abnormal { background: #fff5f5; }
.measurements tr.missing  { color: #aaa; }
.badge-ok  { color: #27ae60; font-weight: 700; }
.badge-bad { color: #e74c3c; font-weight: 700; }
.beats-note { font-size: 0.8rem; color: #888; margin-bottom: 0.6rem; }

/* ===== Peak marking toolbar ===== */
#marking-toolbar {
  background: #f7f8ff;
  border: 1.5px solid #d4d8f0;
  border-radius: 8px;
  padding: 0.85rem 1rem 0.75rem;
  margin-top: 0.75rem;
}
.mtb-title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #666;
  margin-bottom: 0.65rem;
}
.mtb-grid {
  display: grid;
  grid-template-columns: 5.5rem 1fr;
  gap: 0.45rem 0.75rem;
  align-items: center;
}
.mtb-group-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: #444;
  text-align: right;
  padding-right: 0.5rem;
  border-right: 2px solid #ddd;
}
.mtb-group-btns {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.3rem;
}
.mtb-actions { gap: 0.5rem; }

/* Mark buttons — color driven by CSS var --mc set inline */
.mbtn {
  background: var(--mc);
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 0.28rem 0.65rem;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  opacity: 0.85;
  transition: opacity 0.12s, box-shadow 0.12s;
}
.mbtn:hover { opacity: 1; }
.mbtn.btn-marking-active {
  opacity: 1;
  box-shadow: 0 0 0 3px rgba(0,0,0,0.22), 0 0 0 5px rgba(255,255,255,0.6);
}

/* Undo button (tiny) */
.mbtn-undo {
  background: none;
  border: 1px solid #ccc;
  border-radius: 3px;
  padding: 0.2rem 0.35rem;
  font-size: 0.75rem;
  cursor: pointer;
  color: #777;
  line-height: 1;
}
.mbtn-undo:hover { border-color: #888; color: #333; }

/* Count badge */
.mbadge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.4rem;
  height: 1.4rem;
  border-radius: 3px;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0 0.3rem;
  background: color-mix(in srgb, var(--mc) 18%, white);
  color: color-mix(in srgb, var(--mc) 85%, black 30%);
  margin-right: 0.5rem;
}

/* Source badges in measurements table */
.source-badge {
  display: inline-block;
  font-size: 0.78rem; font-weight: 600;
  border-radius: 4px; padding: 0.15rem 0.5rem;
  margin-bottom: 0.4rem;
}
.source-manual { background: #e8f8e8; color: #1a7f37; }
.source-auto   { background: #eef2ff; color: #3a5bc7; }

/* ===== Warnings ===== */
.warnings-box {
  background: #fff8e1;
  border-left: 4px solid #ffc107;
  border-radius: 6px;
  padding: 0.75rem 1rem;
  margin-top: 1rem;
  font-size: 0.85rem;
  color: #555;
}
.warnings-box ul { margin: 0.3rem 0 0 1.2rem; }
