:root {
  --ink: #30343a;
  --soft-ink: rgba(48, 52, 58, 0.48);
  --paper: #fbfaf4;
  --paper-warm: #f2efe6;
  --focus: #315eea;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: "Segoe Print", "Comic Sans MS", "Noto Sans KR", system-ui, sans-serif;
  background: var(--paper);
}

button {
  font: inherit;
}

#app {
  width: min(100%, 1000px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: 14px 18px 18px;
}

.sketch-header {
  width: min(100%, 760px);
  margin: 0 auto 10px;
  padding: 0 4px;
}

.sketch-header h1 {
  margin: 0;
  font-size: clamp(19px, 2.2vw, 25px);
  font-weight: 500;
  line-height: 1.05;
  letter-spacing: 0.03em;
}

.sketch-header p {
  margin: 4px 0 0;
  color: var(--soft-ink);
  font-size: clamp(10px, 1.4vw, 12px);
  letter-spacing: 0.24em;
  line-height: 1.2;
  text-transform: uppercase;
}

main {
  margin: 0;
}

.fitting-board {
  display: grid;
  gap: 9px;
  justify-items: center;
}

.preview-shell {
  width: min(100%, clamp(350px, calc(100dvh - 220px), 570px));
  padding: 0;
  background: var(--paper);
}

.sketch-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  background: var(--paper);
}

.sketch-frame::after {
  position: absolute;
  inset: 0;
  content: '';
  pointer-events: none;
  background: rgba(255, 255, 255, 0.78);
  opacity: 0;
}

.sketch-frame.save-flash {
  animation: save-frame-motion 420ms ease-out;
}

.sketch-frame.save-flash::after {
  animation: save-flash 420ms ease-out;
}

body.capture-flash::after {
  position: fixed;
  inset: 0;
  z-index: 9999;
  content: '';
  pointer-events: none;
  background: rgba(255, 255, 255, 0.62);
  animation: viewport-capture-flash 520ms ease-out;
}

@keyframes save-frame-motion {
  0% {
    transform: scale(1);
  }

  18% {
    transform: scale(0.985);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes viewport-capture-flash {
  0% {
    opacity: 0;
  }

  12% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

@keyframes save-flash {
  0% {
    opacity: 0;
  }

  18% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

#overlay {
  width: 100%;
  height: 100%;
  display: block;
}

.preview-controls {
  width: min(100%, 760px);
  display: grid;
  grid-template-columns: auto auto auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
}

#start-camera,
.zoom-button,
.export-controls button {
  appearance: none;
  min-height: 30px;
  padding: 3px 10px;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--ink);
  border-radius: 0;
  cursor: pointer;
}

#start-camera {
  min-width: 96px;
}

#start-camera:disabled,
.zoom-button:disabled {
  cursor: default;
  opacity: 0.38;
}

.zoom-controls {
  display: inline-grid;
  grid-template-columns: 30px 44px 30px;
  align-items: center;
  gap: 3px;
}

.zoom-button {
  width: 30px;
  height: 30px;
  padding: 0;
  line-height: 1;
}

.zoom-value {
  min-width: 44px;
  text-align: center;
  color: var(--soft-ink);
  font-size: 12px;
  line-height: 1;
}

.export-controls {
  display: inline-grid;
  grid-template-columns: repeat(2, auto);
  gap: 5px;
}

.export-controls button {
  min-width: 54px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-size: 10px;
}

.export-controls .icon-button {
  display: grid;
  place-items: center;
  width: 30px;
  min-width: 30px;
  padding: 0;
  letter-spacing: 0;
}

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

.muted {
  margin: 0;
  color: var(--soft-ink);
  font-size: 12px;
  line-height: 1.3;
}

.palette-board {
  width: min(100%, 960px);
  padding: 0 4px;
}

.palette-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 62px 62px;
  gap: 8px;
  align-items: stretch;
}

.preview-palette {
  display: grid;
  grid-template-columns: repeat(14, minmax(0, 1fr));
  gap: 5px;
}

.swatch {
  appearance: none;
  aspect-ratio: 1 / 1;
  min-width: 0;
  padding: 0;
  border: 1px solid var(--ink);
  border-radius: 0;
  box-shadow: none;
  cursor: pointer;
}

.swatch.selected {
  outline: 1.5px solid var(--focus);
  outline-offset: 3px;
}

.custom-color,
.image-upload {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 35px;
  padding: 3px;
  background: transparent;
  border: 1px solid var(--ink);
  cursor: pointer;
  overflow: hidden;
}

.custom-color.selected,
.image-upload.selected {
  outline: 1.5px solid var(--focus);
  outline-offset: 3px;
}

.custom-color span,
.image-upload span {
  position: relative;
  z-index: 1;
  color: var(--ink);
  font-size: 10px;
  line-height: 1;
  letter-spacing: 0.12em;
  pointer-events: none;
  text-transform: uppercase;
}

.custom-color span {
  padding: 2px 4px;
  background: rgba(251, 250, 244, 0.75);
}

.custom-color input,
.image-upload input {
  position: absolute;
  inset: -8px;
  width: calc(100% + 16px);
  height: calc(100% + 16px);
  padding: 0;
  border: 0;
  cursor: pointer;
}

.image-upload input {
  opacity: 0;
}

.permission-modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(251, 250, 244, 0.78);
}

.permission-modal[hidden] {
  display: none;
}

.permission-panel {
  width: min(100%, 330px);
  padding: 18px;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--ink);
  box-shadow: 8px 8px 0 rgba(48, 52, 58, 0.12);
}

.permission-panel h2 {
  margin: 0 0 12px;
  font-size: 17px;
  font-weight: 500;
  line-height: 1.25;
}

.permission-panel p {
  margin: 0 0 9px;
  color: var(--soft-ink);
  font-size: 13px;
  line-height: 1.55;
}

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

.permission-actions button {
  appearance: none;
  min-height: 34px;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--ink);
  border-radius: 0;
  font: inherit;
  cursor: pointer;
}

.permission-actions button:last-child {
  background: var(--ink);
  color: var(--paper);
}

.camera-source {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

#video {
  width: 1px;
  height: 1px;
}

@media (max-width: 760px) {
  .palette-board {
    display: grid;
    justify-items: center;
  }

  .palette-row {
    width: auto;
    grid-template-columns: repeat(2, 62px);
    justify-content: center;
  }

  .preview-palette {
    grid-column: 1 / -1;
    justify-self: center;
    grid-template-columns: repeat(7, 30px);
    gap: 4px;
  }

  .swatch {
    width: 30px;
    height: 30px;
    aspect-ratio: auto;
  }

  .custom-color,
  .image-upload {
    width: 62px;
    height: 34px;
    min-height: 34px;
  }
}

@media (max-width: 560px) {
  #app {
    padding: 10px 12px 20px;
  }

  .preview-controls {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }

  #start-camera {
    width: auto;
    justify-self: center;
  }

  .zoom-controls,
  .export-controls,
  .muted {
    justify-self: center;
  }
}

@media (max-height: 720px) and (min-width: 700px) {
  .sketch-header p {
    display: none;
  }

  .preview-shell {
    width: min(100%, clamp(320px, calc(100dvh - 175px), 510px));
  }
}
