:root {
  color-scheme: dark;
  --canvas: #050506;
  --panel: rgba(31, 31, 34, 0.72);
  --panel-strong: rgba(35, 35, 38, 0.88);
  --text: #f5f5f7;
  --muted: #9a9aa0;
  --line-new: #78787f;
  --line-measured: #f5f5f7;
  --line-aux: #78957f;
  --accent: #ffe66f;
  --dimension: #ffac52;
  --danger: #ff5d5d;
  --radius-lg: 20px;
  --radius-md: 14px;
  font-family: "SF Pro SC", "SF Pro Display", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--canvas);
}

body {
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  user-select: none;
}

button,
input {
  font: inherit;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  color: inherit;
}

[hidden] {
  display: none !important;
}

.cad-app,
.canvas-shell,
.cad-canvas {
  width: 100%;
  height: 100%;
}

.cad-app {
  position: relative;
  min-height: 100svh;
  background: var(--canvas);
  isolation: isolate;
}

.glass-surface {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.13);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.105), rgba(255, 255, 255, 0.025) 46%, rgba(255, 255, 255, 0.065)),
    var(--panel);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.17),
    inset 1px 0 0 rgba(116, 205, 255, 0.045),
    inset -1px 0 0 rgba(255, 111, 195, 0.035),
    0 18px 55px rgba(0, 0, 0, 0.34);
  backdrop-filter: blur(22px) saturate(145%);
  -webkit-backdrop-filter: blur(22px) saturate(145%);
}

.glass-surface::before {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(105deg, rgba(255, 255, 255, 0.11), transparent 24%, transparent 74%, rgba(255, 255, 255, 0.045));
  content: "";
  pointer-events: none;
}

.topbar {
  position: fixed;
  z-index: 30;
  top: max(12px, env(safe-area-inset-top));
  right: 14px;
  left: 14px;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 62px;
  padding: 8px 9px;
  border-radius: var(--radius-lg);
}

.icon-button,
.text-button,
.tool-dock button,
.dimension-actions button {
  position: relative;
  z-index: 1;
  min-width: 44px;
  min-height: 44px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease, opacity 140ms ease, transform 140ms ease;
}

.icon-button:active,
.text-button:active,
.tool-dock button:active,
.dimension-actions button:active {
  transform: scale(0.96);
}

.icon-button:hover,
.text-button:hover,
.tool-dock button:hover,
.dimension-actions button:hover {
  background: rgba(255, 255, 255, 0.08);
}

.icon-button:focus-visible,
.text-button:focus-visible,
.tool-dock button:focus-visible,
.dimension-actions button:focus-visible,
.dimension-field:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  color: var(--text);
  text-decoration: none;
}

.icon-button svg,
.tool-dock svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.back-button {
  background: rgba(255, 255, 255, 0.06);
}

.brand-lockup {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border: 1px solid rgba(255, 230, 111, 0.46);
  border-radius: 11px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: -0.04em;
}

.brand-lockup h1,
.brand-lockup p {
  overflow: hidden;
  margin: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand-lockup h1 {
  font-size: 16px;
  font-weight: 720;
  letter-spacing: -0.02em;
}

.brand-lockup p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.export-actions {
  position: relative;
  z-index: 1;
  display: flex;
  gap: 5px;
}

.text-button {
  padding: 0 14px;
  color: #d6d6da;
  font-size: 13px;
  font-weight: 650;
}

.text-button.is-primary,
.dimension-actions .is-primary {
  color: #18180f;
  background: var(--accent);
}

.text-button.is-primary:hover,
.dimension-actions .is-primary:hover {
  background: #fff08e;
}

.canvas-shell {
  position: fixed;
  inset: 0;
  background: var(--canvas);
}

.cad-canvas {
  display: block;
  overflow: visible;
  cursor: crosshair;
  touch-action: none;
}

.handwriting-canvas {
  position: fixed;
  z-index: 8;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.grid-fill {
  pointer-events: none;
}

.segment {
  fill: none;
  stroke: var(--line-new);
  stroke-width: 2.2;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.segment.is-measured {
  stroke: var(--line-measured);
}

.segment.is-selected {
  stroke: var(--dimension);
  stroke-width: 2.8;
  filter: drop-shadow(0 0 5px rgba(255, 172, 82, 0.42));
}

.segment.is-auto-dimension {
  stroke: var(--line-new);
}

.segment.is-auxiliary {
  stroke: var(--line-aux);
  stroke-dasharray: 9 6;
}

.segment.is-conflict {
  stroke: var(--danger) !important;
  stroke-width: 3;
  filter: drop-shadow(0 0 5px rgba(255, 93, 93, 0.48));
}

.segment-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 24;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  pointer-events: stroke;
}

.endpoint {
  fill: var(--canvas);
  stroke: rgba(245, 245, 247, 0.55);
  stroke-width: 1.5;
  pointer-events: none;
  vector-effect: non-scaling-stroke;
}

.active-endpoint {
  fill: var(--accent);
  stroke: rgba(255, 230, 111, 0.28);
  stroke-width: 8;
  paint-order: stroke;
  pointer-events: none;
  vector-effect: non-scaling-stroke;
}

.preview-segment {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.2;
  stroke-dasharray: 7 5;
  stroke-linecap: round;
  pointer-events: none;
  vector-effect: non-scaling-stroke;
}

.preview-segment.is-auxiliary {
  stroke: var(--line-aux);
}

.dimension-label {
  fill: var(--dimension);
  stroke: var(--canvas);
  stroke-width: 5;
  paint-order: stroke;
  font-size: 13px;
  font-weight: 720;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.01em;
  pointer-events: none;
  text-anchor: middle;
}

.dimension-label.is-auto {
  fill: #9a9aa0;
}

.dimension-label.is-estimate {
  fill: #8f8f96;
  font-size: 11px;
  font-weight: 620;
}

.dimension-label.is-estimate.is-auxiliary {
  fill: var(--line-aux);
}

.dimension-label.is-conflict {
  fill: var(--danger);
}

.auto-label {
  fill: #8f8f96;
  stroke: var(--canvas);
  stroke-width: 4;
  paint-order: stroke;
  font-size: 10px;
  font-weight: 620;
  pointer-events: none;
  text-anchor: middle;
}

.snap-ring {
  fill: rgba(255, 230, 111, 0.16);
  stroke: var(--accent);
  stroke-width: 1.5;
  pointer-events: none;
  vector-effect: non-scaling-stroke;
}

.empty-hint {
  position: fixed;
  z-index: 3;
  top: 50%;
  left: 50%;
  width: min(320px, calc(100vw - 48px));
  color: var(--muted);
  text-align: center;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.empty-hint[hidden] {
  display: none;
}

.hint-line {
  display: block;
  width: 104px;
  height: 1px;
  margin: 0 auto 22px;
  background: linear-gradient(90deg, transparent, var(--line-new) 18%, var(--line-new) 82%, transparent);
  transform: rotate(-17deg);
}

.empty-hint strong {
  display: block;
  color: #d7d7da;
  font-size: 16px;
  font-weight: 650;
}

.empty-hint p {
  margin: 8px 0 0;
  font-size: 12px;
  line-height: 1.65;
}

.status-pill {
  position: fixed;
  z-index: 12;
  bottom: calc(96px + env(safe-area-inset-bottom));
  left: 50%;
  min-height: 32px;
  padding: 7px 13px;
  border-radius: 999px;
  color: #d6d6da;
  font-size: 11px;
  font-weight: 620;
  opacity: 0.92;
  transform: translateX(-50%);
  white-space: nowrap;
  pointer-events: none;
}

.dimension-panel {
  position: fixed;
  z-index: 25;
  right: 18px;
  top: 50%;
  width: min(310px, calc(100vw - 28px));
  padding: 15px;
  border-radius: var(--radius-lg);
  transform: translateY(-50%);
}

.dimension-heading {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.dimension-heading span,
.dimension-heading small {
  display: block;
}

.handwriting-summary {
  position: relative;
  z-index: 1;
  display: flex;
  min-height: 58px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid rgba(255, 172, 82, 0.28);
  border-radius: var(--radius-md);
  background: rgba(38, 25, 13, 0.42);
}

.handwriting-summary div {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 4px;
}

.handwriting-summary strong {
  color: #f5f5f7;
  font-size: 13px;
  font-weight: 700;
}

.handwriting-summary small {
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.handwriting-summary output {
  flex: 0 0 auto;
  color: var(--dimension);
  font-size: 21px;
  font-variant-numeric: tabular-nums;
  font-weight: 760;
  white-space: nowrap;
}

.dimension-panel.is-handwriting .dimension-field,
.dimension-panel.is-handwriting .segment-metadata {
  display: none;
}

.dimension-panel.is-handwriting .dimension-actions {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.dimension-heading span {
  font-size: 14px;
  font-weight: 700;
}

.dimension-heading small {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
}

.panel-close {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
}

.dimension-field {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  min-height: 56px;
  padding: 0 14px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: var(--radius-md);
  background: rgba(0, 0, 0, 0.28);
}

.dimension-field input {
  min-width: 0;
  height: 54px;
  border: 0;
  outline: 0;
  color: var(--dimension);
  background: transparent;
  font-size: 24px;
  font-weight: 680;
  font-variant-numeric: tabular-nums;
  user-select: text;
}

.dimension-field input::placeholder {
  color: #68686e;
}

.dimension-field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.segment-metadata {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 0.72fr) minmax(0, 1.28fr);
  gap: 8px;
  margin-top: 8px;
}

.metadata-field {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 11px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 11px;
  background: rgba(0, 0, 0, 0.22);
}

.metadata-field-wide {
  grid-column: 1 / -1;
}

.metadata-field span,
.metadata-field small {
  color: var(--muted);
  font-size: 10px;
  font-weight: 650;
  white-space: nowrap;
}

.metadata-field input {
  min-width: 0;
  height: 42px;
  border: 0;
  outline: 0;
  color: #e4e4e7;
  background: transparent;
  font-size: 12px;
  user-select: text;
}

.metadata-field input::placeholder {
  color: #66666d;
}

.metadata-field:focus-within {
  border-color: rgba(255, 230, 111, 0.7);
  outline: 1px solid rgba(255, 230, 111, 0.34);
}

.dimension-actions {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 9px;
}

.dimension-conflict {
  position: relative;
  z-index: 1;
  margin: 10px 2px 0;
  padding: 10px 11px;
  border: 1px solid rgba(255, 93, 93, 0.3);
  border-radius: 11px;
  color: #ffb0b0;
  background: rgba(255, 93, 93, 0.09);
  font-size: 11px;
  line-height: 1.55;
}

.dimension-actions button {
  background: rgba(255, 255, 255, 0.07);
  font-size: 13px;
  font-weight: 680;
}

.tool-dock {
  position: fixed;
  z-index: 20;
  right: 14px;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 14px;
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 4px;
  width: fit-content;
  max-width: calc(100vw - 28px);
  min-height: 68px;
  margin: 0 auto;
  padding: 7px;
  border-radius: var(--radius-lg);
}

.tool-dock button {
  display: grid;
  grid-template-rows: 23px auto;
  place-items: center;
  gap: 2px;
  min-width: 64px;
  padding: 6px 9px 5px;
  color: #c7c7cb;
}

.tool-dock button span {
  font-size: 10px;
  white-space: nowrap;
}

.tool-dock button[aria-pressed="true"] {
  color: var(--accent);
  background: rgba(255, 230, 111, 0.095);
}

.tool-dock button:disabled {
  cursor: default;
  opacity: 0.28;
}

.tool-dock .finish-button {
  color: #17170d;
  background: var(--accent);
}

.tool-dock .finish-button:hover {
  background: #fff08e;
}

.tool-dock .danger-button.is-confirming {
  color: #fff;
  background: rgba(255, 93, 93, 0.78);
}

.dock-divider {
  width: 1px;
  margin: 9px 4px;
  flex: 0 0 auto;
  background: rgba(255, 255, 255, 0.1);
}

@media (max-width: 760px) {
  .topbar {
    top: max(8px, env(safe-area-inset-top));
    right: 8px;
    left: 8px;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    min-height: 58px;
    border-radius: 18px;
  }

  .brand-mark {
    display: none;
  }

  .brand-lockup {
    gap: 0;
  }

  .export-actions {
    gap: 2px;
  }

  .text-button {
    padding: 0 10px;
    font-size: 12px;
  }

  .tool-dock {
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    width: auto;
    max-width: none;
    min-height: 0;
    margin: 0;
  }

  .tool-dock button {
    min-width: 0;
    width: 100%;
    padding-right: 4px;
    padding-left: 4px;
  }

  .dock-divider {
    display: none;
  }

  .status-pill {
    bottom: calc(126px + env(safe-area-inset-bottom));
  }

  .dimension-panel {
    top: auto;
    right: 8px;
    bottom: calc(132px + env(safe-area-inset-bottom));
    left: 8px;
    width: auto;
    max-height: calc(100dvh - 148px - env(safe-area-inset-bottom));
    overflow-y: auto;
    transform: none;
  }
}

@media (max-width: 390px) {
  .brand-lockup p {
    max-width: 90px;
  }

  .text-button {
    min-width: 42px;
    padding: 0 8px;
  }

  #dxf-button {
    width: 48px;
    overflow: hidden;
    color: transparent;
  }

  #dxf-button::after {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: #18180f;
    content: "DXF";
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}

@media print {
  @page {
    size: auto;
    margin: 10mm;
  }

  html,
  body,
  .cad-app,
  .canvas-shell {
    position: static;
    overflow: visible;
    width: 100%;
    height: 100%;
    background: #fff;
  }

  .topbar,
  .tool-dock,
  .status-pill,
  .dimension-panel,
  .empty-hint {
    display: none !important;
  }

  .cad-canvas {
    width: 100%;
    height: 100%;
    background: #fff;
  }

  .grid-fill,
  .endpoint,
  .active-endpoint,
  .preview-segment,
  .snap-ring {
    display: none;
  }

  .segment,
  .segment.is-measured,
  .segment.is-selected {
    stroke: #111;
    filter: none;
  }

  .segment.is-auxiliary {
    stroke: #607966;
  }

  .dimension-label {
    fill: #111;
    stroke: #fff;
  }

  .auto-label {
    fill: #666;
    stroke: #fff;
  }
}
