:root {
  --color-bg: #f5f7fb;
  --color-surface: #ffffff;
  --color-surface-soft: #f8fafc;
  --color-border: #d9e1ec;
  --color-border-strong: #b7c4d6;
  --color-text: #152033;
  --color-muted: #65758b;
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-primary-soft: #dbeafe;
  --color-danger: #dc2626;
  --color-danger-soft: #fee2e2;
  --shadow-soft: 0 18px 55px rgba(15, 23, 42, 0.12);
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --toolbar-height: auto;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  min-width: 320px;
  height: 100vh;
  margin: 0;
  overflow: hidden;
  color: var(--color-text);
  background:
    radial-gradient(circle at 10% 10%, rgba(37, 99, 235, 0.1), transparent 28rem),
    linear-gradient(135deg, #f8fbff 0%, var(--color-bg) 100%);
}

button,
input {
  font: inherit;
}

.app {
  display: grid;
  height: 100vh;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.toolbar {
  display: grid;
  flex: 0 0 auto;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--color-border);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 8px 30px rgba(15, 23, 42, 0.06);
  backdrop-filter: blur(18px);
  z-index: 2;
}

.toolbar__form,
.toolbar__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 10px;
}

.workspace__title,
.empty-state__title,
.empty-state__text {
  margin: 0;
}

.toolbar__form {
  flex-wrap: nowrap;
}

.toolbar__form .field {
  min-width: 190px;
}

.field {
  display: grid;
  min-width: min(100%, 150px);
  gap: 6px;
}

.field--modal {
  min-width: 100%;
}

.field__label {
  color: var(--color-muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.field__input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0 12px;
  color: var(--color-text);
  background: var(--color-surface);
  outline: none;
  transition:
    border-color 160ms ease,
    box-shadow 160ms ease;
}

.field__input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}

.button {
  display: inline-flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0 14px;
  color: var(--color-text);
  background: var(--color-surface);
  cursor: pointer;
  font-weight: 700;
  transition:
    transform 160ms ease,
    border-color 160ms ease,
    background-color 160ms ease,
    color 160ms ease,
    box-shadow 160ms ease;
}

.button:hover:not(:disabled) {
  border-color: var(--color-border-strong);
  transform: translateY(-1px);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.08);
}

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

.button--primary {
  border-color: var(--color-primary);
  color: #ffffff;
  background: var(--color-primary);
}

.button--primary:hover:not(:disabled) {
  border-color: var(--color-primary-dark);
  background: var(--color-primary-dark);
}

.button--danger {
  border-color: transparent;
  color: var(--color-danger);
  background: var(--color-danger-soft);
}

.button--tool[aria-pressed="true"] {
  border-color: var(--color-primary);
  color: var(--color-primary-dark);
  background: var(--color-primary-soft);
}

.shortcut {
  color: var(--color-muted);
  font-size: 11px;
  font-weight: 600;
}

.workspace {
  --image-pane-size: 50%;
  display: grid;
  min-height: 0;
  grid-template-columns: 1fr;
  align-items: stretch;
  gap: 12px;
  overflow: hidden;
  padding: 12px;
}

.workspace__side {
  display: grid;
  min-width: 0;
  min-height: 0;
  gap: 16px;
  align-content: stretch;
}

.workspace__panel {
  display: none;
}

.workspace__divider {
  display: none;
}

.image-panel {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 16px;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.05);
}

.image-panel {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  gap: 14px;
  grid-template-rows: auto minmax(0, 1fr);
}

.workspace__title {
  font-size: 15px;
}

.image-panel__dropzone {
  position: relative;
  display: grid;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  place-items: center;
  border: 2px dashed var(--color-border-strong);
  border-radius: var(--radius-md);
  color: var(--color-muted);
  background:
    linear-gradient(45deg, rgba(37, 99, 235, 0.05) 25%, transparent 25% 75%, rgba(37, 99, 235, 0.05) 75%),
    var(--color-surface-soft);
  background-size: 24px 24px;
  cursor: pointer;
  outline: none;
  transition:
    border-color 160ms ease,
    box-shadow 160ms ease,
    color 160ms ease;
}

.image-panel__dropzone:focus,
.image-panel__dropzone.is-dragover {
  border-color: var(--color-primary);
  color: var(--color-primary-dark);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}

.image-panel__input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.image-panel__placeholder,
.image-panel__preview,
.image-panel__emf {
  display: grid;
  place-items: center;
  text-align: center;
}

.image-panel__placeholder {
  gap: 8px;
  padding: 20px;
  line-height: 1.45;
}

.image-panel__preview {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: auto;
  padding: 12px;
  --image-scale-size: 100%;
}

.image-panel__frame {
  position: relative;
  display: inline-block;
  width: var(--image-scale-size);
  height: var(--image-scale-size);
  min-width: 25%;
  min-height: 25%;
  margin: 0 auto;
}

.image-panel__image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  pointer-events: none;
}

.image-panel__emf {
  max-width: 220px;
  gap: 8px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 18px;
  color: var(--color-text);
  background: rgba(255, 255, 255, 0.9);
}

.image-panel__emf span {
  max-width: 100%;
  overflow-wrap: anywhere;
  color: var(--color-primary-dark);
  font-weight: 800;
}

.image-panel__emf small {
  color: var(--color-muted);
  line-height: 1.35;
}

.image-panel__controls {
  position: absolute;
  top: 0;
  right: 16px;
  z-index: 2;
  display: flex;
  gap: 6px;
  border: 1px solid rgba(217, 225, 236, 0.9);
  border-radius: 999px;
  padding: 5px;
  background: rgba(255, 255, 255, 0.82);
  box-shadow: 0 12px 26px rgba(15, 23, 42, 0.1);
  backdrop-filter: blur(12px);
}

.image-panel__control-button {
  display: inline-grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  color: var(--color-text);
  background: var(--color-surface);
  cursor: pointer;
  font-size: 20px;
  font-weight: 800;
  line-height: 1;
  transition:
    border-color 160ms ease,
    background-color 160ms ease,
    color 160ms ease,
    transform 160ms ease;
}

.image-panel__control-button:hover {
  border-color: var(--color-primary);
  color: var(--color-primary-dark);
  background: var(--color-primary-soft);
  transform: translateY(-1px);
}

.image-panel__control-button--danger {
  color: var(--color-danger);
  background: var(--color-danger-soft);
}

.image-panel__control-button--danger:hover {
  border-color: var(--color-danger);
  color: var(--color-danger);
  background: var(--color-danger-soft);
}

.workspace__hint {
  margin: 14px 0 0;
  color: var(--color-muted);
  font-size: 13px;
  line-height: 1.5;
}

.stats {
  display: grid;
  gap: 9px;
  margin: 14px 0 0;
}

.stats__row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px dashed var(--color-border);
  padding-bottom: 8px;
  font-size: 13px;
}

.stats__row dt {
  color: var(--color-muted);
}

.stats__row dd {
  margin: 0;
  font-weight: 800;
  text-align: right;
}

.canvas-shell {
  position: relative;
  min-width: 0;
  min-height: 480px;
  overflow: hidden;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  box-shadow: var(--shadow-soft);
}

.canvas-shell__surface {
  position: relative;
  display: grid;
  width: 100%;
  height: 100%;
  min-height: inherit;
  place-items: center;
  padding: 28px;
}

.canvas-shell__export-controls {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 2;
  display: flex;
  gap: 6px;
  border: 1px solid rgba(217, 225, 236, 0.9);
  border-radius: 999px;
  padding: 5px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 12px 26px rgba(15, 23, 42, 0.1);
  backdrop-filter: blur(12px);
}

.canvas-shell__export-button {
  display: inline-grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  color: var(--color-text);
  background: var(--color-surface);
  cursor: pointer;
  transition:
    border-color 160ms ease,
    background-color 160ms ease,
    color 160ms ease,
    transform 160ms ease;
}

.canvas-shell__export-button:hover {
  border-color: var(--color-primary);
  color: var(--color-primary-dark);
  background: var(--color-primary-soft);
  transform: translateY(-1px);
}

.canvas-shell__export-button:focus-visible {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}

.canvas-shell__export-icon {
  width: 20px;
  height: 20px;
}

.canvas-shell__status {
  position: absolute;
  top: 16px;
  left: 50%;
  max-width: min(520px, calc(100% - 32px));
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 9px 14px;
  color: var(--color-muted);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 10px 26px rgba(15, 23, 42, 0.08);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition:
    opacity 180ms ease,
    transform 180ms ease;
  z-index: 1;
}

.canvas-shell__status.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  width: 100%;
  height: 100%;
  place-content: center;
  justify-items: center;
  padding: 28px;
  text-align: center;
}

.empty-state > * {
  max-width: 360px;
}

.empty-state__icon {
  width: 88px;
  height: 64px;
  margin: 0 auto 18px;
  border: 2px solid var(--color-primary);
  border-radius: 16px;
  background:
    linear-gradient(90deg, transparent 49%, var(--color-primary-soft) 49% 51%, transparent 51%),
    linear-gradient(0deg, transparent 49%, var(--color-primary-soft) 49% 51%, transparent 51%);
}

.empty-state__title {
  font-size: 20px;
}

.empty-state__text {
  margin-top: 8px;
  color: var(--color-muted);
  line-height: 1.55;
}

.drawing {
  width: 100%;
  height: 100%;
  max-height: calc(100vh - 210px);
  overflow: visible;
}

.drawing__rect {
  fill: transparent;
  stroke: #334155;
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  transition:
    fill 140ms ease,
    stroke 140ms ease;
}

.drawing__rect:hover,
.drawing__rect.is-hovered {
  stroke: var(--color-primary);
}

.drawing__rect.is-selected {
  stroke: var(--color-primary-dark);
  stroke-width: 2.4;
}

.drawing__rect.is-split-tool-active {
  cursor: crosshair;
}

.drawing__resize-line {
  stroke: rgba(37, 99, 235, 0.34);
  stroke-dasharray: 8 6;
  stroke-linecap: square;
  stroke-width: 1.8;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.drawing__resize-hitbox {
  fill: none;
  stroke: transparent;
  stroke-linecap: square;
  stroke-width: 16;
  vector-effect: non-scaling-stroke;
  pointer-events: stroke;
  outline: none;
}

.drawing__resize-hitbox--vertical {
  cursor: ew-resize;
}

.drawing__resize-hitbox--horizontal {
  cursor: ns-resize;
}

.drawing__resize-edge:hover .drawing__resize-line,
.drawing__resize-edge:focus-within .drawing__resize-line {
  stroke: var(--color-primary);
  stroke-width: 2.6;
}

.drawing__label {
  fill: #0f172a;
  font-weight: 800;
  pointer-events: none;
  text-anchor: middle;
  dominant-baseline: middle;
  user-select: none;
}

.drawing__scale {
  pointer-events: none;
}

.drawing__scale-axis {
  stroke: #4b5563;
  stroke-linecap: square;
  stroke-width: 2.8;
  vector-effect: non-scaling-stroke;
}

.drawing__scale-arrow {
  fill: none;
  stroke: #4b5563;
  stroke-linecap: square;
  stroke-linejoin: miter;
  stroke-width: 2.8;
  vector-effect: non-scaling-stroke;
}

.drawing__scale-tick,
.drawing__scale-marker {
  stroke: #9ca3af;
  stroke-linecap: square;
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}

.drawing__scale-marker {
  stroke-width: 2.4;
}

.drawing__scale-axis-label {
  fill: #4b5563;
  font-weight: 800;
  pointer-events: none;
  text-anchor: middle;
  dominant-baseline: middle;
  user-select: none;
}

.drawing__preview-line {
  stroke: var(--color-primary);
  stroke-dasharray: 10 7;
  stroke-linecap: round;
  stroke-width: 2.5;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.drawing__preview-label {
  fill: #64748b;
  font-weight: 800;
  pointer-events: none;
  text-anchor: middle;
  dominant-baseline: middle;
  paint-order: stroke;
  stroke: rgba(255, 255, 255, 0.92);
  stroke-linejoin: round;
  stroke-width: 4px;
  user-select: none;
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 10;
}

.modal[hidden] {
  display: none;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.38);
  backdrop-filter: blur(8px);
}

.modal__dialog {
  position: relative;
  width: min(100%, 440px);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  box-shadow: var(--shadow-soft);
  animation: modal-in 160ms ease-out;
}

.modal__form {
  display: grid;
  gap: 16px;
  padding: 20px;
}

.modal__fields {
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr;
}

.modal__header,
.modal__actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal__title,
.modal__text,
.modal__hint,
.modal__error {
  margin: 0;
}

.modal__title {
  font-size: 20px;
}

.modal__close {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 999px;
  color: var(--color-muted);
  background: var(--color-surface-soft);
  cursor: pointer;
  font-size: 24px;
  line-height: 1;
}

.modal__text,
.modal__hint {
  color: var(--color-muted);
  line-height: 1.45;
}

.modal__error {
  min-height: 20px;
  color: var(--color-danger);
  font-size: 13px;
  font-weight: 700;
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.98);
  }
}

@media (min-width: 900px) {
  .toolbar {
    grid-template-columns: minmax(0, auto) minmax(0, 1fr);
    align-items: end;
  }

  .toolbar__actions {
    justify-content: end;
  }

  .modal__fields {
    grid-template-columns: 1fr 1fr;
  }

  .workspace {
    grid-template-columns: minmax(260px, var(--image-pane-size)) 10px minmax(260px, 1fr);
    gap: 8px;
  }

  .workspace__divider {
    position: relative;
    display: block;
    align-self: stretch;
    border-radius: 999px;
    cursor: col-resize;
    touch-action: none;
  }

  .workspace__divider::before {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: 3px;
    border-radius: inherit;
    background: rgba(183, 196, 214, 0.75);
    content: "";
    transform: translateX(-50%);
    transition:
      background-color 160ms ease,
      width 160ms ease;
  }

  .workspace__divider:hover::before,
  .workspace__divider:focus::before,
  .workspace.is-resizing .workspace__divider::before {
    width: 5px;
    background: var(--color-primary);
  }

  .workspace__side,
  .image-panel,
  .canvas-shell {
    height: 100%;
    min-height: 0;
  }
}

@media print {
  body {
    height: auto;
    overflow: visible;
    background: #ffffff;
  }

  .app {
    display: block;
    height: auto;
    overflow: visible;
  }

  .toolbar,
  .workspace__side,
  .workspace__divider,
  .canvas-shell__export-controls,
  .canvas-shell__status,
  .empty-state {
    display: none !important;
  }

  .workspace {
    display: block;
    overflow: visible;
    padding: 0;
  }

  .canvas-shell {
    min-height: auto;
    overflow: visible;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .canvas-shell__surface {
    display: block;
    min-height: auto;
    padding: 0;
  }

  .drawing {
    width: 100%;
    height: auto;
    max-height: none;
  }
}
