:root {
  --color-a: #2563eb;       /* blue - line A */
  --color-b: #c026d3;       /* magenta/purple - line B */
  --bg: #f4f5f7;
  --panel-bg: #ffffff;
  --border: #dfe2e7;
  --text: #1f2430;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --danger: #dc2626;
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 4px;
}

.btn {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s, color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.btn:hover {
  background: #eef1f5;
}

.btn:active {
  transform: translateY(1px);
}

.file-btn {
  display: inline-flex;
  align-items: center;
  background: #eef2ff;
  border-color: #c7d2fe;
  color: #3730a3;
  font-weight: 600;
}

.file-btn:hover {
  background: #e0e7ff;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn.primary:hover {
  background: #1d4ed8;
}

.btn.danger {
  color: var(--danger);
  border-color: #fca5a5;
}

.btn.danger:hover {
  background: #fee2e2;
}

.mode-btn.line-a {
  border-color: var(--color-a);
  color: var(--color-a);
}

.mode-btn.line-b {
  border-color: var(--color-b);
  color: var(--color-b);
}

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

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

/* ---------- Layout ---------- */

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
}

.sidebar {
  width: 190px;
  flex-shrink: 0;
  background: var(--panel-bg);
  border-right: 1px solid var(--border);
  padding: 14px;
  overflow-y: auto;
}

.sidebar h3,
.result-panel h3 {
  margin: 0 0 12px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.empty-hint {
  font-size: 12px;
  color: var(--text-muted);
}

.thumb-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.thumb-item {
  position: relative;
  border: 2px solid transparent;
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  background: #fff;
}

.thumb-item.active {
  border-color: var(--accent);
}

.thumb-item img {
  width: 100%;
  height: 90px;
  object-fit: cover;
  display: block;
}

.thumb-item .thumb-name {
  font-size: 11px;
  padding: 4px 6px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thumb-item .thumb-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border: none;
  font-size: 12px;
  line-height: 18px;
  text-align: center;
  cursor: pointer;
  padding: 0;
}

.thumb-item .thumb-remove:hover {
  background: rgba(220,38,38,0.85);
}

.canvas-area {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  padding: 14px;
}

.drop-zone {
  position: relative;
  flex: 1;
  background: #fff;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.drop-zone.drag-over {
  border-color: var(--accent);
  background: #eef2ff;
}

.drop-message {
  position: absolute;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  pointer-events: none;
}

.drop-zone.has-image .drop-message {
  display: none;
}

#mainCanvas {
  display: none;
  max-width: 100%;
  max-height: 100%;
  touch-action: none;
}

.drop-zone.has-image #mainCanvas {
  display: block;
}

.zoom-controls {
  position: absolute;
  bottom: 14px;
  right: 14px;
  display: flex;
  gap: 6px;
  background: rgba(255,255,255,0.9);
  padding: 6px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}

.zoom-btn {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  font-size: 16px;
  cursor: pointer;
}

.zoom-btn.wide {
  width: auto;
  padding: 0 10px;
  font-size: 12px;
}

.zoom-btn:hover {
  background: #eef1f5;
}

.mode-indicator {
  position: absolute;
  top: 14px;
  left: 14px;
  background: rgba(255,255,255,0.9);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: var(--radius);
  font-size: 12px;
  color: var(--text-muted);
}

/* ---------- Result panel ---------- */

.result-panel {
  width: 260px;
  flex-shrink: 0;
  background: var(--panel-bg);
  border-left: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
}

.ratio-display {
  background: #f8f9fb;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  margin-bottom: 12px;
}

.ratio-display .ratio-msg {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.ratio-display .ratio-value {
  margin: 0;
  font-size: 26px;
  font-weight: 800;
  color: var(--text);
}

.ratio-display .ratio-value .label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  display: block;
  margin-bottom: 4px;
}

.ratio-display.warn .ratio-msg {
  color: var(--danger);
  font-weight: 600;
}

.length-details {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.6;
  margin-bottom: 16px;
}

.length-details span {
  color: var(--text);
  font-weight: 600;
}

.legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  margin-bottom: 20px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  display: inline-block;
}

.swatch-a {
  background: var(--color-a);
}

.swatch-b {
  background: var(--color-b);
}

.instructions {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.instructions summary {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
}

.instructions[open] summary {
  margin-bottom: 8px;
}

.instructions ol {
  margin: 0;
  padding-left: 18px;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.6;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .workspace {
    flex-direction: column;
  }

  .canvas-area {
    order: 1;
    min-height: 55vh;
    padding: 8px;
  }

  .sidebar {
    order: 2;
    width: auto;
    border: none;
    border-top: 1px solid var(--border);
    padding: 10px 14px;
  }

  .result-panel {
    order: 3;
    width: auto;
    border: none;
    border-top: 1px solid var(--border);
  }

  .thumb-list {
    flex-direction: row;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  .thumb-item {
    flex: 0 0 120px;
  }

  .thumb-item img {
    height: 70px;
  }
}

@media (max-width: 600px) {
  .brand {
    font-size: 13px;
  }

  .topbar {
    gap: 10px;
    padding: 8px 10px;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .toolbar {
    flex-wrap: nowrap;
  }

  .btn {
    padding: 10px 12px;
    font-size: 12px;
    flex: 0 0 auto;
  }

  .divider {
    flex: 0 0 auto;
  }

  .canvas-area {
    min-height: 48vh;
    padding: 6px;
  }

  .zoom-btn {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .zoom-btn.wide {
    padding: 0 12px;
    font-size: 13px;
  }

  .mode-indicator {
    font-size: 11px;
    padding: 5px 9px;
    max-width: calc(100% - 28px);
  }

  .result-panel,
  .sidebar {
    padding: 12px;
  }

  .ratio-display .ratio-value {
    font-size: 22px;
  }
}
