:root {
  --paper: #f7f7f2;
  --field: #d8d8d4;
  --field-dark: #9a9b99;
  --ink: #111111;
  --muted: #5f5f5b;
  --line: #111111;
  --red: #f21823;
  --white: #ffffff;
  --panel-title-size: 0.78rem;
  --panel-status-size: 0.68rem;
  --control-text-size: 0.78rem;
  --control-helper-size: 0.66rem;
  --control-button-size: 0.84rem;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--field);
  color: var(--ink);
  font-family: "Helvetica Neue", Arial, sans-serif;
  letter-spacing: 0;
  overscroll-behavior-x: none;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  padding: 18px;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(300px, 388px) minmax(0, 1fr);
  gap: 18px;
  max-width: 1480px;
  min-height: calc(100vh - 36px);
  margin: 0 auto;
}

.controls,
.preview-panel {
  border: 4px solid var(--line);
  border-radius: 0;
  background: var(--paper);
  box-shadow: 12px 12px 0 var(--ink);
}

.controls {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
}

.control-section {
  display: grid;
  gap: 12px;
}

.control-section summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  margin: 0;
  min-height: 28px;
  padding: 5px 8px;
  border: 3px solid var(--ink);
  background: var(--ink);
  color: var(--white);
  cursor: default;
  font-size: var(--panel-title-size);
  line-height: 1;
  font-weight: 950;
  text-transform: uppercase;
  list-style: none;
}

.control-section summary::-webkit-details-marker {
  display: none;
}

.control-section summary::after {
  content: none;
}

.control-section[open] summary {
  margin-bottom: 12px;
}

.control-section > .control-group + .control-group {
  margin-top: 12px;
}

.section-status {
  max-width: 170px;
  overflow: hidden;
  color: var(--field);
  font-size: var(--panel-status-size);
  line-height: 1;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.control-group {
  display: grid;
  gap: 8px;
}

.control-group label,
.swatch-label {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: stretch;
  gap: 0;
  font-weight: 950;
  text-transform: uppercase;
}

.control-group label span,
.control-group output,
.swatch-label span {
  display: flex;
  align-items: center;
  min-height: 34px;
  padding: 7px 9px;
  border: 3px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  font-size: var(--control-text-size);
  line-height: 1.05;
}

.control-group output {
  justify-content: flex-end;
  min-width: 86px;
  border-left: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.roundness-group output,
.hole-edge-group output {
  min-width: 102px;
}

.hole-size-group output,
.nfc-group output {
  min-width: 96px;
}

input[type="range"] {
  appearance: none;
  width: 100%;
  height: 18px;
  border: 3px solid var(--ink);
  border-radius: 0;
  background: var(--white);
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 10px;
  border-radius: 0;
  background: var(--white);
}

input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -9px;
  border: 3px solid var(--ink);
  border-radius: 0;
  background: var(--ink);
}

input[type="range"]::-moz-range-track {
  height: 10px;
  border: 0;
  border-radius: 0;
  background: var(--white);
}

input[type="range"]::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border: 3px solid var(--ink);
  border-radius: 0;
  background: var(--ink);
}

input[type="range"]:active,
input[type="range"]:focus-visible {
  border-color: var(--red);
}

input[type="range"]:active::-webkit-slider-thumb,
input[type="range"]:focus-visible::-webkit-slider-thumb {
  background: var(--red);
}

input[type="range"]:active::-moz-range-thumb,
input[type="range"]:focus-visible::-moz-range-thumb {
  background: var(--red);
}

input[type="range"]:disabled {
  border-color: var(--field-dark);
  cursor: not-allowed;
}

input[type="range"]:disabled::-webkit-slider-thumb {
  border-color: var(--field-dark);
  background: var(--field-dark);
}

input[type="range"]:disabled::-moz-range-thumb {
  border-color: var(--field-dark);
  background: var(--field-dark);
}

.control-group.is-disabled {
  opacity: 0.5;
}

.control-group.is-disabled label span,
.control-group.is-disabled output {
  border-color: var(--field-dark);
  color: var(--muted);
}

.range-with-ends {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: var(--control-helper-size);
  font-weight: 950;
  line-height: 1;
  text-transform: uppercase;
}

.range-with-ends span:first-child,
.range-with-ends span:last-child {
  color: var(--ink);
}

.toggle-row {
  cursor: pointer;
}

.toggle-group .toggle-row {
  grid-template-columns: minmax(0, 1fr) 64px;
}

.checkbox-toggle {
  appearance: none;
  position: relative;
  width: 64px;
  height: 36px;
  margin: 0;
  border: 3px solid var(--ink);
  border-radius: 0;
  background: var(--white);
  cursor: pointer;
}

.toggle-group .checkbox-toggle {
  border-left: 0;
}

.checkbox-toggle::after {
  content: "";
  position: absolute;
  top: 5px;
  left: 4px;
  width: 20px;
  height: 20px;
  background: var(--ink);
}

.checkbox-toggle:checked {
  background: var(--red);
}

.checkbox-toggle:checked::after {
  transform: translateX(30px);
  background: var(--white);
}

.checkbox-toggle:focus-visible {
  outline: 3px solid var(--red);
  outline-offset: 3px;
}

.color-picker {
  display: grid;
  gap: 0;
  border: 4px solid var(--ink);
  background: var(--ink);
}

.color-picker {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.color-swatch,
.upload-button,
.mode-button,
.primary-action,
.ghost-action,
.zoom-button {
  border-radius: 0;
  cursor: pointer;
  font-weight: 950;
  text-transform: uppercase;
}

.shape-select,
.template-select {
  appearance: none;
  width: 100%;
  min-height: 42px;
  padding: 8px 38px 8px 10px;
  border: 4px solid var(--ink);
  border-radius: 0;
  background:
    linear-gradient(45deg, transparent 50%, var(--ink) 50%) right 17px center / 7px 7px no-repeat,
    linear-gradient(135deg, var(--ink) 50%, transparent 50%) right 10px center / 7px 7px no-repeat,
    var(--white);
  color: var(--ink);
  font-size: var(--control-button-size);
  font-weight: 950;
  text-transform: uppercase;
  cursor: pointer;
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.upload-button {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  border: 4px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  font-size: var(--control-button-size);
}

.upload-button:hover,
.shape-select:hover,
.template-select:hover,
.ghost-action:hover,
.primary-action:hover {
  filter: grayscale(0.18) contrast(1.12);
}

.color-swatch {
  min-width: 0;
  min-height: 38px;
  border: 0;
  border-right: 3px solid var(--ink);
  background: var(--swatch);
}

.color-swatch:last-child {
  border-right: 0;
}

.color-swatch.is-active {
  box-shadow: inset 0 0 0 5px var(--white), inset 0 0 0 8px var(--ink);
}

.action-row {
  display: grid;
  gap: 0;
  margin-top: auto;
  border: 4px solid var(--ink);
}

.credit-status {
  min-height: 34px;
  padding: 8px 10px;
  border-top: 4px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  font-size: 0.78rem;
  font-weight: 800;
  line-height: 1.2;
  text-align: center;
}

.secondary-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.primary-action,
.ghost-action,
.mode-button,
.zoom-button {
  min-height: 46px;
  border: 0;
  font-size: var(--control-button-size);
}

.primary-action {
  min-height: 54px;
  padding: 9px;
  background: var(--ink);
  color: var(--white);
  font-size: 0.92rem;
}

.primary-action.is-login-locked,
.upload-button.is-login-locked {
  background: repeating-linear-gradient(
    -45deg,
    var(--white) 0,
    var(--white) 8px,
    #eeeeea 8px,
    #eeeeea 16px
  );
  color: var(--ink);
}

.primary-action.is-login-locked {
  border-bottom: 4px solid var(--ink);
}

.upload-button.is-login-locked {
  opacity: 0.92;
}

.ghost-action {
  border: 4px solid var(--ink);
  background: var(--white);
  color: var(--ink);
}

.preview-panel {
  position: relative;
  min-height: 600px;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(17, 17, 17, 0.08) 1px, transparent 1px),
    linear-gradient(0deg, rgba(17, 17, 17, 0.06) 1px, transparent 1px),
    var(--field);
  background-size: 48px 48px;
}

@media (min-width: 861px) {
  .controls {
    align-self: start;
  }

  .preview-panel {
    position: sticky;
    top: 18px;
    align-self: start;
    height: calc(100vh - 36px);
  }
}

.preview-heading {
  position: absolute;
  top: 18px;
  right: 18px;
  z-index: 3;
  display: grid;
  justify-items: end;
  gap: 6px;
  width: max-content;
  max-width: calc(100% - 36px);
}

.preview-title {
  margin: 0;
  width: max-content;
  max-width: 100%;
  padding: 6px 13px 13px;
  border: 4px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  font-size: clamp(1.45rem, 3vw, 2.75rem);
  line-height: 1;
  font-weight: 950;
  text-align: right;
  text-transform: lowercase;
  white-space: nowrap;
  pointer-events: none;
}

.mesh-stats {
  max-width: 100%;
  padding: 5px 8px;
  border: 3px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  font-size: 0.62rem;
  font-weight: 950;
  line-height: 1;
  text-align: right;
  text-transform: uppercase;
}

.view-toggle {
  position: absolute;
  left: 18px;
  bottom: 18px;
  z-index: 3;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  width: min(272px, calc(100% - 96px));
  border: 4px solid var(--ink);
  background: var(--white);
}

.mode-button {
  border-right: 3px solid var(--ink);
  background: var(--white);
  color: var(--ink);
  text-transform: none;
}

.mode-button:last-child {
  border-right: 0;
}

.mode-button.is-active {
  background: var(--red);
  color: var(--white);
}

.zoom-controls {
  position: absolute;
  right: 18px;
  bottom: 18px;
  z-index: 3;
  display: grid;
  width: 48px;
  border: 4px solid var(--ink);
  background: var(--white);
}

.zoom-button {
  width: 100%;
  min-height: 44px;
  background: var(--white);
  color: var(--ink);
  font-size: 1.8rem;
  line-height: 1;
}

.zoom-button:first-child {
  border-bottom: 3px solid var(--ink);
}

.zoom-button:hover {
  background: var(--ink);
  color: var(--white);
}

#meshCanvas {
  position: relative;
  z-index: 0;
  display: block;
  width: 100%;
  height: 100%;
  min-height: 600px;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

#meshCanvas.is-dragging {
  cursor: grabbing;
}

.nfc-label {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 2;
  display: none;
  align-items: center;
  justify-content: center;
  width: auto;
  height: auto;
  padding: 0;
  transform: translate(-50%, -50%);
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.95);
  font-size: clamp(0.86rem, 1.65vw, 1.18rem);
  font-weight: 950;
  line-height: 1;
  pointer-events: none;
  text-align: center;
  text-shadow: 0 2px 4px rgba(17, 17, 17, 0.42);
  text-transform: uppercase;
}

.preview-panel.show-nfc-label .nfc-label {
  display: flex;
}

@media (max-width: 860px) {
  body {
    background: var(--paper);
  }

  .app-shell {
    min-height: 100svh;
    padding: 0;
    overflow-x: hidden;
  }

  .workbench {
    grid-template-columns: 1fr;
    gap: 0;
    width: 100%;
    min-height: 100svh;
  }

  .controls,
  .preview-panel {
    border-width: 3px;
    box-shadow: none;
  }

  .controls {
    order: 2;
    gap: 12px;
    padding: 12px;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
  }

  .preview-panel {
    order: 1;
    height: clamp(430px, 58svh, 620px);
    min-height: 430px;
    border-top: 0;
    border-right: 0;
    border-left: 0;
  }

  #meshCanvas {
    height: 100%;
    min-height: 0;
  }

  .control-section[open] summary {
    margin-bottom: 10px;
  }

  .control-section > .control-group + .control-group {
    margin-top: 10px;
  }

  .control-group label span,
  .control-group output,
  .swatch-label span {
    min-height: 32px;
    padding: 7px 8px;
    border-width: 3px;
  }

  .color-picker,
  .shape-select,
  .template-select,
  .upload-button,
  .action-row,
  .ghost-action {
    border-width: 3px;
  }

  .preview-heading {
    top: 12px;
    right: 12px;
    left: 12px;
    justify-items: center;
    width: auto;
    max-width: calc(100% - 24px);
  }

  .preview-title {
    width: auto;
    font-size: clamp(1.22rem, 5.7vw, 1.72rem);
    text-align: center;
  }

  .mesh-stats {
    font-size: 0.56rem;
    text-align: center;
  }

  .view-toggle {
    left: 12px;
    bottom: 12px;
    width: min(250px, calc(100% - 78px));
    border-width: 3px;
  }

  .view-toggle .mode-button {
    min-height: 40px;
    padding: 5px 6px;
    font-size: 0.78rem;
  }

  .zoom-controls {
    right: 12px;
    bottom: 12px;
    width: 42px;
    border-width: 3px;
  }

  .zoom-button {
    min-height: 40px;
    font-size: 1.45rem;
  }

  .color-swatch {
    min-height: 42px;
  }

  .primary-action,
  .ghost-action {
    min-height: 50px;
  }
}

@media (max-width: 420px) {
  .preview-panel {
    height: 56svh;
    min-height: 390px;
  }

  .preview-title {
    font-size: clamp(1.08rem, 5.4vw, 1.34rem);
  }

  .controls {
    gap: 10px;
    padding: 10px;
  }

  .control-group {
    gap: 6px;
  }

  .control-group label span,
  .control-group output,
  .swatch-label span {
    min-height: 31px;
    padding: 6px 7px;
  }

  .view-toggle {
    width: min(236px, calc(100% - 72px));
  }
}
