:root {
  --cyan: #22bfd8;
  --sea: #0a86a8;
  --deep: #05506e;
  --butter: #fff0aa;
  --foam: #d2f5fb;

  --bg: var(--butter);
  --card: #fffdf3;
  --ink: var(--deep);
  --muted: #3f7186;
  --accent: var(--sea);
  --accent-ink: #ffffff;
  --stitch: var(--cyan);
  --soft: var(--foam);
  --shadow: 0 3px 0 rgba(5, 80, 110, 0.18);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #04263a;
    --card: #073650;
    --ink: var(--foam);
    --muted: #93c9d8;
    --accent: var(--cyan);
    --accent-ink: #04263a;
    --stitch: var(--butter);
    --soft: #0b4766;
    --shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, sans-serif;
}

.top, main, footer {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 16px;
}

.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-top: 28px;
  padding-bottom: 8px;
}

.spool { width: 52px; height: 52px; flex: none; transform: rotate(-8deg); }
.spool-end { fill: var(--deep); }
.spool-thread { fill: var(--cyan); }
.spool-wind { stroke: var(--sea); stroke-width: 1.5; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .spool-end { fill: var(--butter); }
}

h1 {
  margin: 0;
  font-size: 2rem;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

h2 { margin: 0 0 6px; font-size: 1.05rem; }

.tag { margin: 2px 0 0; color: var(--muted); }

.card {
  background: var(--card);
  border-radius: 18px;
  padding: 16px;
  margin: 16px 0;
  box-shadow: var(--shadow);
  outline: 2px dashed var(--stitch);
  outline-offset: -8px;
}

.drop {
  display: grid;
  place-items: center;
  gap: 4px;
  min-height: 170px;
  padding: 24px;
  border-radius: 12px;
  background: var(--soft);
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s;
}
.drop:hover, .drop.over { transform: scale(1.01); }
.drop:focus-within { outline: 3px solid var(--accent); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop-big { font-size: 1.2rem; font-weight: 700; }
.drop-small { color: var(--muted); font-size: 0.95rem; }

.canvas-wrap { position: relative; line-height: 0; width: fit-content; max-width: 100%; margin: 0 auto; }

#photo {
  display: block;
  max-width: 100%;
  max-height: 55vh;
  width: auto;
  height: auto;
  border-radius: 12px;
  cursor: crosshair;
  touch-action: manipulation;
}
#photo:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.loupe {
  position: absolute;
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 2px var(--deep), 0 2px 8px rgba(0, 0, 0, 0.35);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}

.size {
  display: flex;
  gap: 4px;
  border: 0;
  padding: 0;
  margin: 0 auto 0 0;
}
.size legend { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.size label {
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--soft);
  cursor: pointer;
  font-size: 0.9rem;
}
.size input { position: absolute; opacity: 0; }
.size label:has(input:checked) { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.size label:has(input:focus-visible) { outline: 3px solid var(--accent); outline-offset: 2px; }

.btn {
  font: inherit;
  font-weight: 700;
  padding: 8px 16px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  box-shadow: var(--shadow);
}
.btn:active { transform: translateY(2px); box-shadow: none; }
.btn.ghost { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 2px var(--accent); }
.btn:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

.quick {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 12px 0 0;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--soft);
}
.quick i { width: 28px; height: 28px; border-radius: 50%; flex: none; box-shadow: 0 0 0 2px var(--deep); }
.quick a { margin-left: auto; color: var(--accent); font-weight: 700; white-space: nowrap; }
@media (min-width: 960px) { .quick a { display: none; } }

.or-pick {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 0.95rem;
}
#color-pick {
  width: 44px;
  height: 32px;
  padding: 0;
  border: 2px solid var(--accent);
  border-radius: 8px;
  background: none;
  cursor: pointer;
}

.hint { margin: 0 0 10px; color: var(--muted); font-size: 0.9rem; }

.palette {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 10px;
  padding: 0;
  margin: 0;
  list-style: none;
}
.palette button {
  width: 100%;
  font: inherit;
  color: inherit;
  text-align: left;
  padding: 0;
  border: 0;
  background: var(--soft);
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
}
.palette button[aria-pressed="true"] { outline: 3px solid var(--accent); }
.palette button:focus-visible { outline: 3px solid var(--ink); }
.pal-swatches { display: grid; grid-template-columns: 1fr 1fr; height: 54px; }
.pal-text { display: block; padding: 6px 8px; font-size: 0.82rem; line-height: 1.25; }
.pal-text b { display: block; font-size: 0.95rem; }

.sample { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }
.sample h2 { margin: 0; }
.sample-swatch {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  flex: none;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.6), 0 0 0 2px var(--deep);
}
code { font-size: 0.95rem; color: var(--muted); }

.matches { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.match {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--soft);
}
.pair {
  display: flex;
  width: 72px;
  height: 40px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 0 2px var(--deep);
}
.pair span { flex: 1; }
.match-name b { font-size: 1.05rem; }
.match-name small { display: block; color: var(--muted); }
.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  vertical-align: 2px;
  background: var(--card);
}
.badge.twin { background: var(--cyan); color: #04263a; }
.badge.close { background: var(--butter); color: var(--deep); }
.add {
  font: inherit;
  font-weight: 700;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}
.add[aria-pressed="true"] { background: var(--card); color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); font-size: 1rem; }
.add:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

.list { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 4px;
  border-radius: 999px;
  background: var(--soft);
  font-size: 0.9rem;
}
.chip i { width: 22px; height: 22px; border-radius: 50%; box-shadow: 0 0 0 1.5px var(--deep); }
.chip button {
  font: inherit;
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  width: 24px;
  height: 24px;
  border-radius: 50%;
}
.chip button:hover { background: var(--card); }
.list-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.copied { color: var(--muted); font-size: 0.9rem; }

.tips { padding: 4px 4px 0; }
.tips ul { margin: 6px 0 0; padding-left: 20px; color: var(--muted); }
.tips li { margin-bottom: 4px; }

footer { padding-top: 12px; padding-bottom: 32px; font-size: 0.82rem; color: var(--muted); }
footer p { margin: 4px 0; }
footer a { color: inherit; }

@media (min-width: 960px) {
  .top, main, footer { max-width: 1120px; }
  main {
    display: grid;
    grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
    column-gap: 20px;
    align-items: start;
  }
  main > * { grid-column: 2; }
  #photo-card {
    grid-column: 1;
    grid-row: 1 / span 6;
    position: sticky;
    top: 16px;
  }
  #photo { max-height: 70vh; }
}

@media (max-width: 480px) {
  h1 { font-size: 1.6rem; }
  .match { grid-template-columns: auto 1fr auto; gap: 8px; }
  .pair { width: 56px; }
}
