/* =========================================================
   Pip Colors — warm paper-craft tabletop
   ========================================================= */

*, *::before, *::after { box-sizing: border-box; }

:root {
  --paper:    #FBF6EE;
  --ink:      #3E3A36;
  --muted:    #6E655C;
  --edge:     #E6DDCF;
  --card:     #FFFFFF;
  --sage:     #8FB79A;
  --sage-d:   #6F9B7C;
  --cream:    #F3E7D3;
  --star:     #F2C438;
  --shadow:   0 6px 12px rgba(62, 58, 54, 0.10);
  --shadow-up:0 10px 18px rgba(62, 58, 54, 0.14);
  --radius:   32px;
  --pip:      clamp(92px, 17vh, 132px);
}

html, body { height: 100%; margin: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-weight: 700;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; color: inherit; }

/* ---------- app frame ---------- */

.app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: transparent;
  z-index: 2;
}

.app::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  opacity: 0.055;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---------- prompt bar ---------- */

.prompt {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 64px 10px 14px;
}

.swatch {
  flex: 0 0 auto;
  width: clamp(60px, 11.5vh, 88px);
  aspect-ratio: 1 / 1;
  border-radius: 22px;
  background: #FFFFFF;
  border: 2px solid var(--edge);
  box-shadow: var(--shadow);
}

.prompt-text {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0;
}

.kicker {
  font-size: 19px;
  font-weight: 700;
  color: var(--muted);
  line-height: 1.1;
}

.color-word {
  font-size: clamp(34px, 11.5vw, 54px);
  font-weight: 800;
  line-height: 1.08;
  color: var(--ink);
  white-space: nowrap;
  display: block;
}

.round-btn {
  flex: 0 0 auto;
  width: clamp(52px, 7.4vh, 64px);
  height: clamp(52px, 7.4vh, 64px);
  border-radius: 50%;
  background: var(--card);
  border: 2px solid var(--edge);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
  transition: transform 0.15s ease-out, box-shadow 0.15s ease-out;
}
.round-btn:active { transform: scale(0.94); box-shadow: 0 3px 7px rgba(62,58,54,0.12); }
.round-btn:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 3px; }
.round-btn.small { width: 48px; height: 48px; }

/* ---------- tiles ---------- */

.tiles {
  display: grid;
  gap: 14px;
  padding: 10px 14px;
  align-content: center;
  min-height: 0;
}

.tiles[data-count="2"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: minmax(140px, 1fr);
}

.tiles[data-count="3"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: minmax(140px, 1fr);
}
.tiles[data-count="3"] > .tile:last-child { grid-column: 1 / -1; }

.tile {
  position: relative;
  min-height: 140px;
  max-height: min(460px, 62vh);
  align-self: center;
  background: var(--card);
  border: 2px solid var(--edge);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  cursor: pointer;
  touch-action: manipulation;
  animation: tileIn 0.26s ease-out both;
  transition: transform 0.18s ease-out, box-shadow 0.18s ease-out, opacity 0.3s ease;
}

@keyframes tileIn {
  from { opacity: 0; transform: scale(0.96); }
  to   { opacity: 1; transform: none; }
}

.tile:active { transform: scale(0.985); }
.tile:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 3px; }

.tile.is-chosen {
  transform: scale(1.02);
  box-shadow: var(--shadow-up);
}

.tile.is-hint { animation: hintPulse 1.25s ease-in-out 1; }

@keyframes hintPulse {
  0%, 100% { opacity: 1; }
  45%      { opacity: 0.42; }
}

.shape {
  display: block;
  width: 66%;
  aspect-ratio: 1 / 1;
  max-height: 66%;
}

/* flying tile clone */
.flyer {
  position: fixed;
  z-index: 40;
  margin: 0;
  pointer-events: none;
  will-change: transform;
}

/* ---------- bottom: Pip + basket ---------- */

.bottom {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 6px 14px 12px;
  z-index: 3;
}

.dressing {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  opacity: 0.2;
  filter: blur(2px);
  pointer-events: none;
}

.pip {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  width: var(--pip);
}
.pip .pip-svg { width: 100%; height: auto; display: block; }

/* Pip colouring */
.pip-shell   { fill: var(--sage); }
.pip-plates polygon { fill: var(--sage-d); }
.pip-head, .pip-neck { fill: var(--cream); }
.pip-shadow  { fill: rgba(62, 58, 54, 0.10); }
.eyes circle { fill: var(--ink); }

.head-look {
  transform-box: fill-box;
  transform-origin: 8% 68%;
  transform: translate(var(--hx, 0px), var(--hy, 0px)) rotate(var(--hrot, 0deg));
  transition: transform 0.7s cubic-bezier(0.34, 0.72, 0.3, 1);
}

.head-nod {
  transform-box: fill-box;
  transform-origin: 6% 70%;
}
.head-nod.nod { animation: nod 820ms ease-in-out 1; }

@keyframes nod {
  0%   { transform: rotate(0deg); }
  42%  { transform: rotate(9deg); }
  100% { transform: rotate(0deg); }
}

.eyes { transform-box: fill-box; transform-origin: center; }
.eyes.blink { animation: blink 260ms ease-in-out 1; }

@keyframes blink {
  0%, 100% { transform: scaleY(1); }
  50%      { transform: scaleY(0.12); }
}

.basket-wrap {
  position: relative;
  z-index: 1;
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sets {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
  min-height: 22px;
  font-size: 17px;
  font-weight: 800;
  color: var(--muted);
}
.sets[hidden] { display: none; }

.basket {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  align-content: flex-end;
  gap: 8px;
  padding: 8px;
  min-height: 60px;
}

.tray {
  position: absolute;
  inset: 0;
  border: 2px dashed #EFE6D8;
  border-radius: 22px;
  pointer-events: none;
}

.chip {
  position: relative;
  z-index: 1;
  width: clamp(26px, 3.5vh, 38px);
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  border: 2px solid var(--edge);
  box-shadow: 0 3px 6px rgba(62, 58, 54, 0.08);
  transition: opacity 0.3s ease, transform 0.24s ease-out;
}

/* ---------- helper dot ---------- */

.helper-dot {
  position: fixed;
  top: 8px;
  right: 8px;
  width: 56px;
  height: 56px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  display: grid;
  place-items: center;
  z-index: 30;
  cursor: pointer;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.helper-dot .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #DED3C2;
  transition: transform 0.2s ease-out, background 0.2s ease-out;
}
.helper-dot:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 2px; }
.helper-dot.holding .dot { transform: scale(1.25); background: var(--sage-d); }

.helper-dot .ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
  opacity: 0;
  transition: opacity 0.15s ease-out;
}
.helper-dot.holding .ring { opacity: 1; }
.helper-dot.holding .ring circle { animation: ringFill 3s linear forwards; }

@keyframes ringFill {
  from { stroke-dashoffset: 125.7; }
  to   { stroke-dashoffset: 0; }
}

/* ---------- overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(251, 246, 238, 0.86);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  overflow: auto;
}
.overlay[hidden] { display: none; }

.card {
  background: var(--card);
  border: 2px solid var(--edge);
  border-radius: 28px;
  box-shadow: var(--shadow-up);
  padding: 26px;
  width: 100%;
}

.start-card {
  max-width: 400px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.start-pip { width: 150px; max-width: 46%; }
.start-pip .pip-svg { width: 100%; height: auto; display: block; }

.start-card h1 {
  font-size: 34px;
  font-weight: 800;
  margin: 0;
  letter-spacing: 0.4px;
}

.start-sub {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.55;
  color: var(--muted);
  max-width: 32ch;
}

.mini {
  margin: 2px 0 0;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.5;
  color: #857B71;
}

.primary {
  border: 0;
  border-radius: 22px;
  background: var(--sage);
  color: #2B2825;
  font-size: 20px;
  font-weight: 800;
  min-height: 56px;
  padding: 0 26px;
  cursor: pointer;
  box-shadow: 0 5px 10px rgba(62, 58, 54, 0.12);
  transition: transform 0.15s ease-out, background 0.2s ease-out;
}
.primary:active { transform: scale(0.975); background: var(--sage-d); }
.primary:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 3px; }
.primary.wide { width: 100%; }
.primary.big { min-height: 64px; font-size: 22px; max-width: 280px; }

/* ---------- settings sheet ---------- */

.sheet {
  max-width: 460px;
  max-height: 88vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
  text-align: left;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.sheet-head h2 { margin: 0; font-size: 22px; font-weight: 800; }

.group { display: flex; flex-direction: column; gap: 10px; }
.group h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 6px;
  background: #F4EDE2;
  padding: 6px;
  border-radius: 20px;
}
.seg button {
  min-height: 48px;
  border: 0;
  border-radius: 15px;
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  font-weight: 800;
  cursor: pointer;
  transition: background 0.18s ease-out, color 0.18s ease-out;
}
.seg button[aria-pressed="true"] {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(62, 58, 54, 0.10);
}
.seg button:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 2px; }

.color-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.color-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 52px;
  padding: 0 12px;
  border-radius: 16px;
  border: 2px solid var(--edge);
  background: var(--card);
  cursor: pointer;
  font-size: 16px;
  font-weight: 800;
  text-align: left;
  opacity: 0.42;
  transition: opacity 0.2s ease-out, border-color 0.2s ease-out;
}
.color-item[aria-pressed="true"] { opacity: 1; border-color: var(--sage-d); }
.color-item:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 2px; }
.color-item .dot,
.per .dot {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(62, 58, 54, 0.05);
}

.hint { margin: 0; font-size: 14px; font-weight: 600; color: var(--muted); }

.stats { display: flex; flex-direction: column; gap: 8px; }
.stat {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 14px;
  background: #F8F2E8;
  font-size: 16px;
  font-weight: 700;
}
.stat .k { color: var(--muted); font-weight: 700; }
.stat .v { font-weight: 800; }

.per-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 12px;
  margin-top: 6px;
}
.per {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 700;
}
.per .nm { flex: 1 1 auto; }
.per .v { color: var(--muted); font-weight: 800; }

.ghost {
  min-height: 48px;
  border-radius: 16px;
  border: 2px solid var(--edge);
  background: var(--card);
  color: var(--muted);
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  transition: background 0.18s ease-out, color 0.18s ease-out, border-color 0.18s ease-out;
}
.ghost:active { transform: scale(0.985); }
.ghost.armed { border-color: #E4574C; color: #C4453B; background: #FDF1EF; }
.ghost:focus-visible { outline: 3px solid var(--sage-d); outline-offset: 2px; }

/* ---------- responsive ---------- */

/* wide / landscape: three tiles side by side */
@media (min-width: 620px) {
  .tiles[data-count="2"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tiles[data-count="3"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tiles[data-count="3"] > .tile:last-child { grid-column: auto; }
  .color-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .per-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* short screens (landscape phone, small windows) */
@media (max-height: 540px) {
  :root { --pip: clamp(64px, 20vh, 92px); }
  .prompt { padding: 8px 60px 6px 12px; gap: 10px; }
  .swatch { width: clamp(48px, 14vh, 66px); border-radius: 18px; }
  .kicker { font-size: 15px; }
  .color-word { font-size: clamp(28px, 6.6vh, 42px); }
  .tiles { gap: 12px; padding: 8px 12px; }
  .tiles[data-count="2"] { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-auto-rows: minmax(104px, 1fr); }
  .tiles[data-count="3"] { grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-rows: minmax(104px, 1fr); }
  .tiles[data-count="3"] > .tile:last-child { grid-column: auto; }
  .tile { min-height: 104px; border-radius: 24px; }
  .basket { min-height: 46px; padding: 6px; }
  .bottom { padding: 4px 12px 8px; gap: 10px; }
  .sets { min-height: 18px; font-size: 15px; }
}

/* ---------- reduced motion: everything becomes a soft crossfade ---------- */

@media (prefers-reduced-motion: reduce) {
  @keyframes tileIn { from { opacity: 0; } to { opacity: 1; } }
  .tile { animation-duration: 0.3s; }
  .tile.is-hint { animation: none; }
  .head-look { transition-duration: 0.3s; }
  .head-nod.nod { animation: none; }
  .chip { transition-duration: 0.3s; }
  .primary:active, .round-btn:active, .tile:active { transform: none; }
  .helper-dot.holding .ring circle { animation-duration: 3s; }
}