* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  color: #1a1a1a; background: #f5f5f5;
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ---------- toolbar ---------- */

.toolbar {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: #1f1f1f;
  color: #eee;
  border-bottom: 1px solid #444;
  font-size: 13px;
}
.toolbar h1 {
  font-size: 14px;
  margin: 0 12px 0 0;
  color: #eee;
  font-weight: 600;
}
.toolbar button, .toolbar label.file-btn {
  background: #333;
  color: #eee;
  border: 1px solid #555;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
}
.toolbar button:hover, .toolbar label.file-btn:hover { background: #444; }
.toolbar button:disabled { opacity: 0.4; cursor: not-allowed; }
.toolbar .primary { background: #2a6fd4; border-color: #2a6fd4; }
.toolbar .primary:hover { background: #3580e0; }
.toolbar .sep {
  width: 1px; height: 22px; background: #555;
  margin: 0 4px;
}
.toolbar .spacer { flex: 1; }
.toolbar input[type="file"] { display: none; }
.toolbar input.image-url {
  background: #2a2a2a;
  color: #ddd;
  border: 1px solid #555;
  padding: 4px 8px;
  border-radius: 3px;
  font-size: 12px;
  font-family: monospace;
  width: 320px;
}
.toolbar .zoom-val {
  display: inline-block;
  min-width: 48px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: #ccc;
}
.toolbar .pgn-mode {
  font-size: 12px;
  color: #ccc;
}
.toolbar .pgn-mode strong { color: #fbc02d; }
.toolbar .pgn-mode .hint { color: #888; font-size: 11px; }

.toolbar .server-status {
  font-size: 11px;
  font-family: monospace;
  padding: 2px 6px;
  border-radius: 3px;
  background: #555;
  color: #ddd;
}
.toolbar .server-status.online {
  background: #1f6e2a;
  color: #c8efce;
}
.toolbar .server-status.busy {
  background: #b07a16;
  color: #fff8e0;
}
.toolbar .server-status.error {
  background: #8a2424;
  color: #fdd;
}

.tools { display: flex; gap: 4px; }
.tools .tool {
  background: #333;
  color: #eee;
  border: 1px solid #555;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
  font-family: inherit;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.tools .tool .hk {
  background: #555;
  color: #eee;
  padding: 1px 4px;
  border-radius: 2px;
  font-family: monospace;
  font-size: 10px;
  line-height: 1.2;
}
.tools .tool:hover { background: #444; }
.tools .tool.active {
  background: #2a6fd4;
  border-color: #2a6fd4;
  color: #fff;
}
.tools .tool.active .hk { background: #1a55b0; }

/* ---------- main split ---------- */

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

.workspace {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #2a2a2a;
  cursor: crosshair;
}
.workspace.tool-select { cursor: default; }
.workspace.panning { cursor: grab; }
.workspace.panning-active { cursor: grabbing; }
.workspace.space-down { cursor: grab; }

.scene {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  /* transform проставляется через JS */
}
.scene img {
  display: block;
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
  user-select: none;
}
.scene canvas {
  display: block;
  position: absolute;
  top: 0; left: 0;
  pointer-events: none; /* события ловим на workspace */
}

.placeholder {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  color: #aaa;
  font-size: 14px;
  text-align: center;
  line-height: 1.5;
  pointer-events: none;
  z-index: 5;
}
.placeholder.error { color: #e57373; }

.help {
  position: absolute;
  bottom: 8px; left: 8px;
  background: rgba(0,0,0,0.7);
  color: #ddd;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 11px;
  line-height: 1.6;
  max-width: 320px;
  pointer-events: none;
  z-index: 100;
}
.help kbd {
  background: #444;
  color: #eee;
  padding: 1px 5px;
  border-radius: 2px;
  font-family: monospace;
  font-size: 10px;
}

/* ---------- layers panel ---------- */

.layers {
  flex: 0 0 280px;
  background: #f5f5f5;
  border-left: 1px solid #ccc;
  padding: 12px 14px;
  overflow-y: auto;
  font-size: 13px;
  color: #1a1a1a;
}
.layers h2 {
  margin: 0 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #666;
  font-weight: 600;
}
.layer-list {
  list-style: none;
  padding: 0; margin: 0 0 14px;
}
.layer-list li {
  margin: 0;
  padding: 4px 0;
}
.layer-list label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}
.layer-list .swatch {
  display: inline-block;
  width: 14px; height: 14px;
  border-radius: 2px;
  border: 1px solid rgba(0,0,0,0.2);
  flex: 0 0 14px;
}
.layer-list .count {
  margin-left: auto;
  background: #ddd;
  color: #333;
  padding: 1px 6px;
  border-radius: 8px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  min-width: 22px;
  text-align: center;
}

.bg-opacity {
  padding: 8px 0;
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  margin-bottom: 12px;
}
.bg-opacity .row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
  font-size: 12px;
  color: #555;
}
.bg-opacity input[type="range"] {
  width: 100%;
}

.selected h3 {
  margin: 0 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #666;
  font-weight: 600;
}
.selected #selected-info {
  font-size: 12px;
  color: #333;
  line-height: 1.5;
  font-family: monospace;
  background: #fff;
  border: 1px solid #ddd;
  padding: 8px;
  border-radius: 3px;
  max-height: 200px;
  overflow-y: auto;
}
.selected #selected-info.empty { color: #999; font-family: inherit; }
.selected button {
  margin-top: 6px;
  font-size: 12px;
  padding: 4px 8px;
  background: #f5f5f5;
  border: 1px solid #bbb;
  border-radius: 3px;
  cursor: pointer;
}
.selected button:hover { background: #eee; }
.selected button.danger { color: #b03030; border-color: #b03030; }
.selected button.danger:hover { background: #fbeaea; }

/* ---------- calibration panel ---------- */

.calibration {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid #ddd;
}
.calibration h3 {
  margin: 0 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #666;
  font-weight: 600;
}
.cal-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}
.cal-tab {
  flex: 1;
  background: #eee;
  border: 1px solid #bbb;
  padding: 4px 6px;
  border-radius: 3px;
  font-size: 11px;
  cursor: pointer;
  font-family: inherit;
  color: #333;
}
.cal-tab:hover { background: #e0e0e0; }
.cal-tab.active {
  background: #2a6fd4;
  border-color: #2a6fd4;
  color: #fff;
}
.cal-body {
  font-size: 12px;
  color: #333;
}
.cal-hint {
  margin: 0 0 6px;
  color: #666;
  line-height: 1.4;
}
.cal-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  font-size: 12px;
}
.cal-row > span {
  flex: 0 0 80px;
  color: #555;
}
.cal-row select,
.cal-row input[type="number"] {
  flex: 1;
  padding: 3px 6px;
  border: 1px solid #bbb;
  border-radius: 3px;
  font-size: 12px;
  font-family: inherit;
}
.cal-body button {
  font-size: 12px;
  padding: 4px 10px;
  background: #f5f5f5;
  border: 1px solid #bbb;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  margin-bottom: 6px;
}
.cal-body button:hover { background: #eee; }
.cal-status {
  margin-top: 6px;
  padding: 6px 8px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 3px;
  font-family: monospace;
  font-size: 11px;
  color: #444;
  line-height: 1.4;
  min-height: 28px;
}
.cal-status.ready { color: #1a5e1a; border-color: #66bb66; background: #f1f8e9; }
.cal-status.error { color: #b03030; border-color: #e57373; background: #fbeaea; }
.cal-status.picking { color: #1565c0; border-color: #64b5f6; background: #e3f2fd; }
.cal-actions {
  display: flex;
  gap: 6px;
  margin-top: 10px;
}
.cal-actions button {
  flex: 1;
  font-size: 12px;
  padding: 5px 10px;
  background: #f5f5f5;
  border: 1px solid #bbb;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
}
.cal-actions button:disabled { opacity: 0.4; cursor: not-allowed; }
.cal-actions .primary {
  background: #2a6fd4;
  color: #fff;
  border-color: #2a6fd4;
}
.cal-actions .primary:hover:not(:disabled) { background: #3580e0; }
.cal-sep {
  border: none;
  border-top: 1px solid #ddd;
  margin: 12px 0 8px;
}
#cal-save-png {
  width: 100%;
  font-size: 12px;
  padding: 5px 10px;
  background: #f5f5f5;
  border: 1px solid #bbb;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
}
#cal-save-png:hover { background: #eee; }

.workspace.picking { cursor: crosshair; }
.workspace.rectify-dragging { cursor: grabbing; }

.cal-rectify-actions {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
}
.cal-rectify-actions button {
  flex: 1;
  font-size: 12px;
  padding: 4px 8px;
  background: #f5f5f5;
  border: 1px solid #bbb;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  margin-bottom: 0;
}
.cal-rectify-actions button:hover:not(:disabled) { background: #eee; }
.cal-rectify-actions button:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- status bar ---------- */

.status {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 6px 12px;
  background: #1f1f1f;
  color: #bbb;
  border-top: 1px solid #444;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.status .spacer { flex: 1; }
.status .item strong { color: #eee; font-weight: 600; }

/* ---------- toast ---------- */

.toast-container {
  position: fixed;
  right: 16px;
  bottom: 48px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 2000;
  pointer-events: none;
}
.toast {
  background: #2a2a2a;
  color: #f0f0f0;
  border-left: 3px solid #2a6fd4;
  padding: 10px 14px;
  border-radius: 4px;
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
  min-width: 240px;
  max-width: 480px;
  pointer-events: auto;
  animation: toast-in 0.18s ease-out;
}
.toast.success { border-left-color: #43a047; }
.toast.error { border-left-color: #e53935; background: #3a1f1f; }
.toast.info { border-left-color: #1e88e5; }
.toast pre {
  margin: 4px 0 0;
  font-size: 11px;
  font-family: monospace;
  color: #ccc;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 120px;
  overflow-y: auto;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- modal (Run CV extract) ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1500;
}
.modal {
  background: #f5f5f5;
  border-radius: 6px;
  width: min(560px, 92vw);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  overflow: hidden;
  color: #1a1a1a;
}
.modal-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: #1f1f1f;
  color: #eee;
  border-bottom: 1px solid #444;
}
.modal-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  flex: 1;
}
.modal-close {
  background: transparent;
  color: #ccc;
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.modal-close:hover { color: #fff; }
.modal-body {
  padding: 14px 18px;
  overflow-y: auto;
  font-size: 13px;
}
.modal-section {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid #ddd;
}
.modal-section:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}
.modal-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.modal-row > span {
  flex: 0 0 130px;
  color: #555;
  font-size: 12px;
}
.modal-row input[type="text"],
.modal-row input[type="number"] {
  flex: 1;
  padding: 5px 8px;
  border: 1px solid #bbb;
  border-radius: 3px;
  font-size: 12px;
  font-family: monospace;
}
.modal-row.two-col {
  align-items: stretch;
}
.modal-row.two-col label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  font-size: 12px;
  color: #555;
}
.modal-row.two-col label input {
  padding: 5px 8px;
  border: 1px solid #bbb;
  border-radius: 3px;
  font-size: 12px;
  font-family: monospace;
}
.modal-check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
.modal-sub {
  margin-top: 8px;
  padding-left: 22px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.modal-sub.disabled { opacity: 0.4; pointer-events: none; }
.modal-radio {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  cursor: pointer;
  color: #333;
}
.modal-sub input[type="text"] {
  padding: 4px 8px;
  border: 1px solid #bbb;
  border-radius: 3px;
  font-size: 12px;
  font-family: monospace;
}
.modal-sub input[type="text"]:disabled {
  background: #ececec;
  color: #888;
}
.modal-slider {
  margin-bottom: 10px;
}
.modal-slider-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12px;
  color: #555;
  margin-bottom: 2px;
}
.modal-slider-head strong {
  color: #1a55b0;
  font-variant-numeric: tabular-nums;
  font-family: monospace;
}
.modal-slider input[type="range"] {
  width: 100%;
}
.modal-hint {
  margin: 6px 0 0;
  font-size: 11px;
  color: #777;
  line-height: 1.4;
}
.modal-hint em {
  font-style: normal;
  font-family: monospace;
  color: #444;
}
.modal-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid #ddd;
  background: #ececec;
}
.modal-footer .spacer { flex: 1; }
.modal-footer button {
  font-size: 12px;
  padding: 6px 14px;
  background: #fff;
  border: 1px solid #bbb;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
}
.modal-footer button:hover:not(:disabled) { background: #f0f0f0; }
.modal-footer button:disabled { opacity: 0.4; cursor: not-allowed; }
.modal-footer button.primary {
  background: #2a6fd4;
  color: #fff;
  border-color: #2a6fd4;
}
.modal-footer button.primary:hover:not(:disabled) { background: #3580e0; }
.modal-status {
  font-size: 11px;
  font-family: monospace;
  color: #666;
}
.modal-status.busy { color: #b07a16; }
.modal-status.error { color: #b03030; }

.modal-overlay[hidden] { display: none !important; }
