* { box-sizing: border-box; }
:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --accent: #0f766e;
  --accent-fg: #ffffff;
  --accent-soft: #ccfbf1;
  --warn: #b45309;
  --shadow: 0 1px 2px rgba(0,0,0,0.04);
}
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

/* ===== header ===== */
header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 18px; background: var(--panel);
  border-bottom: 1px solid var(--border); box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo {
  width: 32px; height: 32px; border-radius: 6px;
  background: var(--accent); color: var(--accent-fg);
  font-weight: 700; display: grid; place-items: center;
}
.brand h1 { margin: 0; font-size: 15px; font-weight: 600; }
.brand p { margin: 2px 0 0; font-size: 11px; color: var(--muted); }

#steps { display: flex; gap: 8px; }
#steps .step {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  background: #f1f5f9; color: var(--muted); font-size: 12px;
  border: 1px solid var(--border);
}
#steps .step span {
  width: 18px; height: 18px; border-radius: 999px;
  background: #cbd5e1; color: white; font-weight: 700;
  display: grid; place-items: center; font-size: 11px;
}
#steps .step.active {
  background: var(--accent-soft); color: var(--accent);
  border-color: var(--accent);
}
#steps .step.active span { background: var(--accent); }
#steps .step.done span { background: #94a3b8; }
#steps .step.done { color: var(--muted); }

/* ===== toolbar ===== */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px;
  padding: 10px 18px; background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.tool-group { display: flex; align-items: center; gap: 6px; }
.tool-group.right { margin-left: auto; }
.toolbar input[type="text"] {
  padding: 6px 10px; border: 1px solid var(--border);
  border-radius: 6px; font: inherit; min-width: 220px;
}
.toolbar select {
  padding: 6px 8px; border: 1px solid var(--border);
  border-radius: 6px; font: inherit; background: white;
}
button {
  padding: 6px 12px; border: 1px solid var(--border);
  border-radius: 6px; background: white; cursor: pointer;
  font: inherit; color: var(--text); transition: background .12s;
}
button:hover { background: #f1f5f9; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.primary:hover { background: #115e59; }
button.primary:disabled, button:disabled {
  opacity: 0.45; cursor: not-allowed;
}
button.big { padding: 8px 16px; font-weight: 600; }
button.muted-btn {
  background: transparent; color: var(--muted);
  border: 1px dashed var(--border); padding: 4px 10px;
  font-size: 12px;
}

.muted { color: var(--muted); font-size: 12px; }
.small { font-size: 11px; }
.status {
  width: 100%; padding: 6px 10px; margin-top: 4px;
  font-size: 12px; color: var(--muted);
  background: #f1f5f9; border-radius: 6px; min-height: 28px;
}
.status.error { color: #b91c1c; background: #fee2e2; }
.status.ok { color: #166534; background: #dcfce7; }

/* ===== identification ===== */
#ident-section {
  background: var(--panel); border-bottom: 1px solid var(--border);
}
#ident-section summary {
  cursor: pointer; padding: 10px 18px; font-weight: 600;
  font-size: 13px; color: var(--text);
}
#ident-section[open] summary { border-bottom: 1px solid var(--border); }
#ident-grid {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px; padding: 12px 18px;
}
#ident-grid fieldset {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 12px 12px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px;
}
#ident-grid fieldset legend {
  padding: 0 6px; font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600;
}
#ident-grid label {
  display: flex; flex-direction: column; gap: 2px;
  font-size: 11px; color: var(--muted);
}
#ident-grid input, #ident-grid select {
  padding: 5px 8px; border: 1px solid var(--border);
  border-radius: 4px; font: inherit; color: var(--text);
}
#ident-grid label:has(input[id$="-code"]),
#ident-grid label:has(input[maxlength="2"]),
#ident-grid label:has(input[maxlength="3"]),
#ident-grid label:has(input[maxlength="4"]) {
  max-width: 80px;
}
#ident-grid fieldset:nth-child(3) {
  grid-template-columns: 1fr;
}
#ident-grid fieldset:nth-child(3) label { width: 100%; }
#ident-grid fieldset:nth-child(3) button { margin-top: 4px; }

/* ===== main two-pane ===== */
main {
  display: grid; grid-template-columns: 1fr 1.2fr; gap: 12px;
  padding: 12px 18px; min-height: 480px;
  height: calc(100vh - 360px);
}
section {
  display: flex; flex-direction: column; min-width: 0;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; overflow: hidden;
}
.panel-title {
  padding: 8px 12px; font-weight: 600; font-size: 12px;
  background: #f8fafc; border-bottom: 1px solid var(--border);
  display: flex; gap: 12px; align-items: center;
}

/* ===== reference image viewer ===== */
.panel-title .spacer { flex: 1; }
.iconbtn {
  width: 26px; height: 26px; padding: 0; line-height: 1;
  border-radius: 4px; font-size: 13px;
}
#ref-wrap {
  flex: 1; position: relative; overflow: hidden;
  background:
    linear-gradient(45deg, #f1f5f9 25%, transparent 25%) -10px 0 / 20px 20px,
    linear-gradient(-45deg, #f1f5f9 25%, transparent 25%) -10px 0 / 20px 20px,
    linear-gradient(45deg, transparent 75%, #f1f5f9 75%) 0 10px / 20px 20px,
    linear-gradient(-45deg, transparent 75%, #f1f5f9 75%) 0 10px / 20px 20px,
    #fff;
  cursor: grab; touch-action: none; user-select: none;
}
#ref-wrap.grabbing { cursor: grabbing; }
#ref-wrap.dropzone.dragover {
  outline: 3px dashed var(--accent); outline-offset: -8px; background: var(--accent-soft);
}
#ref-canvas {
  position: absolute; left: 0; top: 0; width: 0; height: 0;
  transform-origin: 0 0; will-change: transform;
}
#ref-img {
  display: block; max-width: none; max-height: none;
  pointer-events: none; user-select: none;
}
.dropzone-hint {
  position: absolute; inset: 0; display: grid; place-items: center;
  text-align: center; color: var(--muted); pointer-events: none;
  font-size: 13px;
}
#ref-wrap.has-image .dropzone-hint { display: none; }
#ref-wrap:not(.has-image) #ref-canvas { visibility: hidden; }
#ref-zoom-badge {
  position: absolute; left: 8px; bottom: 8px;
  padding: 2px 8px; background: rgba(255,255,255,0.85);
  border: 1px solid var(--border); border-radius: 4px;
  font-size: 11px; pointer-events: none;
}

/* ===== map + leaflet polygon vertex polish ===== */
#map .leaflet-interactive { transition: filter .12s, opacity .12s; }
#map.snap-active { cursor: crosshair; }

/* loading overlay */
#busy {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.42);
  display: none; align-items: center; justify-content: center;
  z-index: 9999; backdrop-filter: blur(2px);
}
#busy.show { display: flex; }
#busy .card {
  background: white; padding: 18px 24px; border-radius: 10px;
  display: flex; gap: 12px; align-items: center;
  box-shadow: 0 10px 30px rgba(0,0,0,0.18);
  font-size: 13px; min-width: 280px;
}
#busy .spinner {
  width: 22px; height: 22px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* keyboard hints */
.kbd {
  display: inline-block; padding: 1px 6px; font-size: 10px;
  background: #f1f5f9; border: 1px solid var(--border);
  border-radius: 4px; font-family: ui-monospace, monospace;
  color: var(--muted);
}

/* ===== map ===== */
#map { flex: 1; }

/* ===== footer ===== */
footer {
  text-align: center;
  padding: 10px 16px;
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

@media (max-width: 1024px) {
  #ident-grid { grid-template-columns: 1fr; }
  main { grid-template-columns: 1fr; height: auto; min-height: 600px; }
  #steps { display: none; }
}
