/* =====================================================================
   Graph Detector – Main Stylesheet
   ===================================================================== */

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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #ed425e;   /* Red color from logo */
  --accent2: #533483;
  --text: #eaeaea;
  --text-muted: #8892a4;
  --border: #2d3561;
  --success: #2ecc71;
  --warning: #f39c12;
  --danger: #e74c3c;
  --radius: 8px;
  --shadow: 0 4px 20px rgba(0,0,0,0.4);
  --transition: 0.2s ease;
}

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ───────────────────────────────────────────────────────── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}

header h1 {
  font-size: 1.4rem;
  color: white;
  font-weight: 700;
  letter-spacing: 0.5px;
}

header h1 span {
  color: var(--accent);
  font-weight: 700;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* ── Buttons ──────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  outline: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary   { background: var(--accent);  color: #fff; }
.btn-primary:hover:not(:disabled) { background: #c73652; }

.btn-secondary { background: var(--surface2); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: #1a4a7a; }

.btn-success   { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { background: #27ae60; }

.btn-danger    { background: var(--danger);  color: #fff; }
.btn-danger:hover:not(:disabled) { background: #c0392b; }

.btn-icon      { background: transparent; color: var(--text-muted); padding: 6px; border-radius: 4px; }
.btn-icon:hover { color: var(--text); background: var(--border); }

.btn-sm { padding: 5px 10px; font-size: 0.8rem; }

/* ── Toolbar ─────────────────────────────────────────────────────── */
.toolbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.toolbar-sep {
  width: 1px;
  height: 28px;
  background: var(--border);
}

.mode-group {
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 3px;
  border-radius: var(--radius);
}

.mode-btn {
  padding: 5px 12px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  transition: var(--transition);
  font-weight: 500;
}

.mode-btn.active {
  background: var(--accent);
  color: #fff;
}

.mode-btn:hover:not(.active) {
  background: var(--border);
  color: var(--text);
}

/* ── Layout ──────────────────────────────────────────────────────── */
.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - 118px);
}

/* ── Canvas area ──────────────────────────────────────────────────── */
.canvas-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #111;
  position: relative;
}

.canvas-wrapper {
  flex: 1;
  position: relative;
  overflow: hidden;
  cursor: crosshair;
}

#mainCanvas {
  position: absolute;
  top: 0;
  left: 0;
  image-rendering: pixelated;
}

.canvas-overlay-info {
  position: absolute;
  bottom: 8px;
  left: 12px;
  background: rgba(0,0,0,0.7);
  color: var(--text-muted);
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 4px;
  pointer-events: none;
}

.coord-tooltip {
  position: absolute;
  background: rgba(0,0,0,0.85);
  color: #fff;
  font-size: 0.75rem;
  padding: 4px 8px;
  border-radius: 4px;
  pointer-events: none;
  display: none;
  white-space: nowrap;
  z-index: 10;
}

/* ── Upload zone ─────────────────────────────────────────────────── */
.upload-zone {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  border: 2px dashed var(--border);
  cursor: pointer;
  transition: var(--transition);
  background: var(--surface);
}

.upload-zone.drag-over {
  border-color: var(--accent);
  background: rgba(233,69,96,0.08);
}

.upload-zone .upload-icon {
  font-size: 3rem;
  opacity: 0.7;
}

.upload-zone .upload-text {
  color: var(--text-muted);
  font-size: 0.95rem;
  text-align: center;
}

.upload-zone .upload-text strong {
  color: var(--text);
}

.upload-zone .upload-hint {
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
  opacity: 0.7;
}

/* ── Sidebar ──────────────────────────────────────────────────────── */
.sidebar {
  width: 280px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 14px 16px;
  font-weight: 700;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
  color: var(--accent);
  letter-spacing: 0.5px;
  display: flex;
  align-items: center;
}

.sidebar-section {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-section-title {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 8px;
  font-weight: 600;
}

.curves-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.curve-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: var(--transition);
}

.curve-item:hover {
  border-color: var(--accent2);
}

.curve-item.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.curve-item-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.color-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.2);
}

.curve-label {
  flex: 1;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.curve-label input {
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  width: 100%;
  outline: none;
}

.curve-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.confidence-badge {
  margin-left: auto;
  background: var(--surface2);
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 700;
}

.confidence-badge.high   { color: var(--success); }
.confidence-badge.medium { color: var(--warning); }
.confidence-badge.low    { color: var(--danger);  }

.curve-checkbox {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
}

.curve-actions {
  display: flex;
  gap: 4px;
  margin-top: 6px;
  justify-content: flex-end;
}

/* ── Axis info panel ─────────────────────────────────────────────── */
.axis-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  font-size: 0.8rem;
}

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

.axis-field label {
  color: var(--text-muted);
  font-size: 0.72rem;
}

.axis-field input,
.axis-field select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.78rem;
  width: 100%;
}

.axis-field input:focus,
.axis-field select:focus {
  outline: none;
  border-color: var(--accent);
}

/* ── Points table ───────────────────────────────────────────────── */
.points-panel {
  background: var(--surface);
  border-top: 1px solid var(--border);
  height: 160px;
  display: flex;
  flex-direction: column;
}

.points-panel-header {
  padding: 6px 16px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
}

.points-table-wrapper {
  flex: 1;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

th {
  background: var(--bg);
  padding: 5px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  position: sticky;
  top: 0;
}

td {
  padding: 4px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  color: var(--text);
  font-family: 'Courier New', monospace;
}

tr:hover td {
  background: rgba(255,255,255,0.03);
}

/* ── Loading overlay ─────────────────────────────────────────────── */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  display: none;
}

.loading-overlay.active {
  display: flex;
}

.loading-box {
  background: var(--surface);
  padding: 32px 40px;
  border-radius: var(--radius);
  text-align: center;
  box-shadow: var(--shadow);
}

.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 14px;
}

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

/* ── Toast notifications ─────────────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 1000;
}

.toast {
  background: var(--surface2);
  color: var(--text);
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  animation: slide-in 0.25s ease;
  max-width: 300px;
  border-left: 4px solid var(--accent);
}

.toast.success { border-color: var(--success); }
.toast.error   { border-color: var(--danger); }
.toast.warning { border-color: var(--warning); }

@keyframes slide-in {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

/* ── Calibration modal ───────────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  display: none;
}

.modal-backdrop.active {
  display: flex;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 40px;
  width: 580px;
  max-width: 95vw;
  box-shadow: var(--shadow);
}

.modal h3 {
  margin-bottom: 16px;
  color: var(--accent);
  font-size: 1rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.form-group label {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.form-group input,
.form-group select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 7px 10px;
  border-radius: 4px;
  font-size: 0.85rem;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--accent);
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 16px;
}

/* ── Scrollbar ───────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar { width: 220px; }
  header h1 { font-size: 1.1rem; }
}

@media (max-width: 560px) {
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: 200px;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}