:root {
  color-scheme: light;
  --bg: #ffffff;
  --ink: #111111;
  --muted: #686868;
  --line: #d8d8d8;
  --soft: #f7f7f4;
  --soft-2: #efeee9;
  --focus: #1e88e5;
  --danger: #d93025;
  --ok: #2f8f4e;
  --radius: 8px;
  --bottom-bar-height: 68px;
  --top-space: 104px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, ui-sans-serif, system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  letter-spacing: 0;
  overflow: hidden;
}

button,
input {
  font: inherit;
}

button {
  min-height: 44px;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.app-shell {
  width: min(100%, 480px);
  margin: 0 auto;
  height: 100dvh;
  max-height: 100dvh;
  padding: 12px 18px calc(var(--bottom-bar-height) + env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 10px;
}

.brand {
  font-size: 28px;
  line-height: 1;
  font-weight: 800;
  letter-spacing: 0;
}

.help-button,
.icon-button {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #ffffff;
  color: var(--ink);
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
}

.help-button {
  display: inline-grid;
}

.stepper {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0;
  margin-bottom: 14px;
  position: relative;
}

.stepper::before {
  content: "";
  position: absolute;
  left: 8%;
  right: 8%;
  top: 17px;
  height: 1px;
  background: var(--line);
}

.step {
  border: 0;
  background: transparent;
  color: var(--muted);
  display: grid;
  gap: 5px;
  justify-items: center;
  min-width: 0;
  position: relative;
  z-index: 1;
  padding: 0;
}

.step span {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #ffffff;
  display: grid;
  place-items: center;
  font-size: 16px;
  font-weight: 700;
}

.step small {
  font-size: 10px;
  line-height: 1.15;
  white-space: nowrap;
}

.step.is-active span,
.step.is-done span {
  background: #111111;
  color: #ffffff;
  border-color: #111111;
}

.step.is-active small,
.step.is-done small {
  color: #111111;
  font-weight: 700;
}

.screen {
  display: grid;
  gap: 10px;
  align-content: start;
  min-height: 0;
  overflow: hidden;
  overflow-x: hidden;
  padding-bottom: 0;
}

.section {
  display: grid;
  gap: 10px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 26px;
  line-height: 1.08;
  font-weight: 800;
  letter-spacing: 0;
}

h2 {
  font-size: 18px;
  line-height: 1.2;
  font-weight: 740;
}

h3 {
  font-size: 18px;
  line-height: 1.2;
}

.lede {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.28;
}

.fine-print {
  color: var(--muted);
  font-size: 13px;
}

.palette-workspace {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  min-height: 0;
}

.palette-side {
  order: 2;
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 7px;
  background: #ffffff;
}

.palette-side h2 {
  font-size: 15px;
}

.photo-panel {
  order: 1;
  min-height: 0;
  height: clamp(142px, 25dvh, 240px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--soft);
  position: relative;
  overflow: hidden;
  display: grid;
  place-items: center;
}

.photo-panel img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

.empty-photo {
  display: grid;
  gap: 8px;
  text-align: center;
  color: var(--muted);
  padding: 28px;
}

.empty-photo strong {
  color: var(--ink);
  font-size: 20px;
}

.photo-action {
  position: absolute;
  top: 10px;
  width: 46px;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  display: grid;
  place-items: center;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.12);
  font-size: 12px;
  font-weight: 760;
}

.photo-action input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.photo-camera {
  left: 10px;
}

.photo-upload {
  right: 10px;
}

.section-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.text-button,
.outline-button,
.primary-button {
  border-radius: var(--radius);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-decoration: none;
}

.text-button {
  border: 0;
  background: transparent;
  color: var(--ink);
  font-size: 15px;
  padding: 0;
  white-space: nowrap;
  font-weight: 580;
}

.palette-list,
.option-list,
.legend-list {
  display: grid;
  gap: 0;
}

.palette-list {
  max-height: clamp(216px, 40dvh, 386px);
  overflow-y: auto;
  border-top: 1px solid var(--line);
}

.palette-side .palette-list {
  max-height: clamp(156px, 29dvh, 252px);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
  border-top: 0;
}

.empty-list {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  padding: 20px;
  text-align: center;
}

.palette-row {
  display: grid;
  grid-template-columns: 40px 24px minmax(82px, 1fr) 96px 50px;
  gap: 8px;
  align-items: center;
  min-height: 48px;
  border-bottom: 1px solid var(--line);
}

.palette-side .palette-row {
  grid-template-columns: 28px 1fr 28px;
  gap: 4px;
  min-height: 33px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 3px;
}

.palette-side .color-picker {
  width: 27px;
  height: 27px;
}

.palette-side .color-number {
  font-size: 13px;
  text-align: left;
}

.palette-side .name-input,
.palette-side .hex-input {
  display: none;
}

.palette-side .icon-button {
  width: 27px;
  min-width: 27px;
  height: 27px;
  min-height: 27px;
  padding: 0;
}

.palette-list-wide {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  max-height: clamp(126px, 24dvh, 220px);
  border-top: 0;
}

.palette-list-wide .palette-row {
  grid-template-columns: 34px 24px 1fr 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px;
}

.palette-list-wide .hex-input {
  display: none;
}

.color-picker {
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  background: transparent;
}

.color-number {
  font-size: 16px;
  text-align: center;
  font-weight: 520;
}

.name-input,
.hex-input,
.field input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-height: 44px;
  padding: 0 12px;
  background: #ffffff;
}

.name-input {
  border-color: transparent;
  padding-left: 0;
  font-size: 15px;
  font-weight: 520;
}

.hex-input {
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  font-weight: 520;
  font-size: 14px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

.segmented-2 {
  grid-template-columns: repeat(2, 1fr);
}

.segmented button {
  border: 0;
  border-right: 1px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  font-size: 16px;
  font-weight: 700;
}

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

.segmented .is-selected {
  background: #111111;
  color: #ffffff;
}

.a4-hint,
.summary-strip,
.stats-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 14px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 12px;
}

.a4-hint span,
.summary-strip span,
.stats-row span {
  color: var(--muted);
}

.a4-icon {
  width: 46px;
  aspect-ratio: 210 / 297;
  border: 2px solid #111111;
  border-radius: 3px;
  display: grid;
  place-items: center;
  font-weight: 780;
  font-size: 20px;
}

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

.chip {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
  min-height: 42px;
  padding: 0 12px;
  font-weight: 620;
}

.chip.is-selected {
  border-color: #111111;
  background: #111111;
  color: #ffffff;
}

.field {
  display: grid;
  gap: 8px;
  color: var(--muted);
}

.summary-strip,
.stats-row {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  text-align: center;
  font-weight: 620;
  line-height: 1.25;
  font-size: 14px;
}

.option-list {
  max-height: calc(100dvh - var(--top-space) - var(--bottom-bar-height) - 150px);
  overflow-y: auto;
}

.option-card {
  display: grid;
  grid-template-columns: 45% 1fr;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

.option-card img,
.selected-image {
  width: 100%;
  aspect-ratio: 210 / 297;
  max-height: 26dvh;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #ffffff;
}

.option-meta {
  display: grid;
  align-content: center;
  gap: 12px;
}

.option-meta p {
  color: var(--muted);
  line-height: 1.4;
}

.outline-button {
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  padding: 0 14px;
  min-height: 48px;
  font-weight: 680;
}

.option-card.is-selected .outline-button {
  border-color: var(--ok);
  color: var(--ok);
}

.message {
  border: 1px solid #b7d6c1;
  background: #f1faf4;
  color: #1d6634;
  border-radius: var(--radius);
  padding: 12px 14px;
  line-height: 1.35;
  font-weight: 520;
}

.message.is-error {
  border-color: #f1b7b2;
  background: #fff5f4;
  color: var(--danger);
}

.result-shell {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  background: #ffffff;
  min-height: 0;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid var(--line);
}

.tabs button {
  border: 0;
  border-right: 1px solid var(--line);
  background: #ffffff;
  color: var(--muted);
  font-weight: 660;
  line-height: 1.15;
  padding: 0 6px;
}

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

.tabs .is-selected {
  color: var(--ink);
  box-shadow: inset 0 -3px 0 #111111;
}

.result-mount {
  padding: 8px;
  max-height: calc(100dvh - var(--top-space) - var(--bottom-bar-height) - 118px);
  overflow: auto;
}

/* Step 5: header, color-bar, canvas */
.screen[data-step="5"] {
  grid-template-rows: auto auto 1fr;
}

/* Step 6: header, canvas, action-stack */
.screen[data-step="6"] {
  grid-template-rows: auto 1fr auto;
}

.screen[data-step="5"] .result-shell,
.screen[data-step="6"] .result-shell {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.screen[data-step="5"] .result-mount,
.screen[data-step="6"] .result-mount {
  flex: 1;
  min-height: 0;
  max-height: none;
}

.numbers-header-section {
  gap: 6px;
}

.numbers-title-row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}

.numbers-title-row h1 {
  font-size: 22px;
}

.stats-badge {
  color: var(--muted);
  font-size: 13px;
  font-weight: 520;
  white-space: nowrap;
}

.color-bar-section {
  display: grid;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.color-count-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.color-count-label {
  font-size: 14px;
  font-weight: 680;
  color: var(--muted);
  white-space: nowrap;
}

.color-count-seg {
  flex: 1;
  max-width: 220px;
}

.add-color-btn {
  margin-left: auto;
  font-size: 13px;
}

.color-swatch-strip {
  display: flex;
  flex-flow: row wrap;
  gap: 8px;
  align-items: flex-start;
}

.color-swatch-item {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: default;
  transition: opacity 0.15s;
}

.color-swatch-item.is-dim {
  opacity: 0.28;
}

.color-swatch-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.28);
  display: block;
}

.color-swatch-num {
  font-size: 11px;
  font-weight: 700;
  color: var(--ink);
  line-height: 1;
}

.canvas-frame {
  background: var(--soft-2);
  border-radius: var(--radius);
  padding: 8px;
}

.canvas-frame canvas {
  width: 100%;
  height: auto;
  display: block;
  background: #ffffff;
  border: 1px solid var(--line);
}

.legend-list {
  gap: 0;
}

.legend-row {
  min-height: 44px;
  display: grid;
  grid-template-columns: 34px 28px 1fr auto;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--line);
  font-weight: 520;
}

.legend-row:last-child {
  border-bottom: 0;
}

.legend-swatch {
  width: 26px;
  height: 26px;
  border-radius: 5px;
  border: 1px solid rgba(0, 0, 0, 0.18);
}

.legend-row code {
  color: var(--muted);
  font-size: 13px;
}

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

.action-stack .primary-button,
.action-stack .outline-button {
  min-height: 46px;
  padding: 0 8px;
  font-size: 14px;
  line-height: 1.15;
  gap: 6px;
}

.bottom-bar {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(100%, 480px);
  min-height: var(--bottom-bar-height);
  padding: 8px 18px calc(8px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.94);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(10px);
  z-index: 20;
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 8px;
}

.restart-button,
.primary-button {
  width: 100%;
  border-radius: var(--radius);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 0 12px;
  letter-spacing: 0;
}

.restart-button {
  border: 1px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  font-size: 13px;
  font-weight: 760;
  line-height: 1.05;
}

.restart-button .icon {
  width: 18px;
  height: 18px;
}

.primary-button {
  border: 0;
  background: #111111;
  color: #ffffff;
  font-size: 18px;
  font-weight: 800;
}

.primary-button[data-busy="true"]::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #ffffff;
  animation: spin 0.8s linear infinite;
}

.icon {
  width: 22px;
  height: 22px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  font-weight: 800;
}

.icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

.icon-button .icon {
  width: 20px;
  height: 20px;
}

.icon-button {
  width: 46px;
  min-width: 46px;
  padding: 0 8px;
  border-radius: var(--radius);
}

.photo-action .icon {
  width: 26px;
  height: 26px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#print-page {
  display: none;
}

#print-page h1 {
  font-size: 18pt;
  margin: 0 0 5mm;
}

#print-page img {
  width: 100%;
  display: block;
}

.print-legend {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 2mm 6mm;
  margin-top: 4mm;
  font-size: 9pt;
}

.print-legend-row {
  display: grid;
  grid-template-columns: 8mm 1fr auto;
  gap: 3mm;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 390px) {
  .app-shell {
    padding: 10px 14px calc(var(--bottom-bar-height) + env(safe-area-inset-bottom));
  }

  .brand {
    font-size: 25px;
  }

  h1 {
    font-size: 24px;
  }

  .palette-row {
    grid-template-columns: 42px 24px minmax(70px, 1fr) 92px 42px;
    gap: 7px;
  }

  .step small {
    font-size: 9px;
  }
}

@media (max-height: 760px) {
  :root {
    --bottom-bar-height: 62px;
    --top-space: 92px;
  }

  .app-shell {
    padding-top: 8px;
  }

  .topbar {
    margin-bottom: 7px;
  }

  .help-button {
    width: 36px;
    height: 36px;
  }

  .brand {
    font-size: 24px;
  }

  .stepper {
    margin-bottom: 10px;
  }

  .step span {
    width: 30px;
    height: 30px;
    font-size: 14px;
  }

  .stepper::before {
    top: 15px;
  }

  .step small {
    font-size: 8.5px;
  }

  .section {
    gap: 7px;
  }

  h1 {
    font-size: 22px;
  }

  .lede,
  .fine-print {
    font-size: 12px;
  }

  .photo-panel {
    height: clamp(220px, 42dvh, 340px);
  }

  .palette-list {
    max-height: 96px;
  }

  .palette-side .palette-list {
    max-height: none;
  }

  .chip {
    min-height: 38px;
  }

  .selected-image {
    max-height: 20dvh;
  }

  .result-intro .selected-image {
    display: none;
  }

  .result-mount {
    max-height: calc(100dvh - var(--top-space) - var(--bottom-bar-height) - 92px);
  }

  .screen[data-step="5"] .result-mount,
  .screen[data-step="6"] .result-mount {
    max-height: none;
  }

  .primary-button {
    min-height: 46px;
    font-size: 17px;
  }

  .restart-button {
    min-height: 46px;
    font-size: 12px;
    padding: 0 6px;
    gap: 5px;
  }
}

@media print {
  body * {
    visibility: hidden;
  }

  #print-page,
  #print-page * {
    visibility: visible;
  }

  #print-page {
    display: block;
    position: absolute;
    inset: 0;
    width: 210mm;
    min-height: 297mm;
    padding: 0;
  }

  @page {
    size: A4 portrait;
    margin: 8mm;
  }
}
