:root {
  --bg: #fff4d6;
  --bg-2: #ffe7a8;
  --ink: #3b2a1a;
  --card: #ffffff;
  --accent: #ff7a59;
  --accent-2: #4cb5f5;
  --shadow: 0 4px 0 rgba(59, 42, 26, 0.18);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --swatch: min(62px, 8.6vh);
  --tool: min(80px, 10vh);
  --stk: min(76px, calc((100vh - 120px) / 8));
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}
body { position: fixed; inset: 0; }

button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; touch-action: manipulation; }

/* ------------------------------------------------------------ layout */

#app {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "palette stage tools";
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 12px);
  gap: 12px;
  background:
    radial-gradient(circle at 12% 88%, var(--bg-2) 0 90px, transparent 91px),
    radial-gradient(circle at 92% 6%, var(--bg-2) 0 70px, transparent 71px),
    var(--bg);
}

#palette {
  grid-area: palette;
  display: grid;
  grid-template-columns: repeat(2, var(--swatch));
  grid-auto-rows: var(--swatch);
  gap: 10px;
  align-content: center;
  padding-top: 84px; /* keep the parents hotspot corner clear */
}

#stage-wrap {
  grid-area: stage;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
}

#stage {
  position: relative;
  background: #fff;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 6px 0 rgba(59, 42, 26, 0.15), 0 0 0 4px #fff;
  touch-action: none;
}
#stage canvas, #paper, #paper-photo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#paper { background: #fff; }
#paper-photo { object-fit: fill; }
#line-layer, #sticker-layer { pointer-events: none; }
#stage.photo-paper #color-layer { mix-blend-mode: multiply; }

#tools {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 6px 0;
}
#tools .spacer { flex: 1; }

.tool {
  width: var(--tool);
  height: var(--tool);
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  font-size: calc(var(--tool) * 0.52);
  line-height: 1;
  transition: transform 0.15s ease, background 0.15s ease;
}
.tool span { pointer-events: none; }
.tool.on {
  background: var(--accent-2);
  transform: scale(1.08);
  box-shadow: 0 4px 0 #2f86bd;
}
.tool:active, .swatch:active, .size:active { transform: scale(0.92); }
.tool.hidden, .size.hidden, #sizes.hidden { display: none; }

#sizes {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 20px;
  padding: 6px;
}
.size {
  width: 62px;
  height: 42px;
  border-radius: 14px;
  display: grid;
  place-items: center;
}
.size i { display: block; border-radius: 50%; background: var(--ink); }
.size[data-size="s"] i { width: 12px; height: 12px; }
.size[data-size="m"] i { width: 22px; height: 22px; }
.size[data-size="l"] i { width: 36px; height: 36px; }
.size.on { background: #fff; box-shadow: var(--shadow); }

.swatch {
  width: var(--swatch);
  height: var(--swatch);
  border-radius: 50%;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.18), 0 3px 0 rgba(59, 42, 26, 0.18);
  border: 4px solid #fff;
  transition: transform 0.15s ease;
}
.swatch.on {
  transform: scale(1.18);
  border-color: var(--ink);
}
.swatch.rainbow {
  background: conic-gradient(#ff3b30, #ff9500, #ffd60a, #34c759, #32ade6, #5856d6, #af52de, #ff3b30);
}

/* Portrait (iPad mini held upright) */
@media (orientation: portrait) {
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto auto;
    grid-template-areas: "stage" "palette" "tools";
  }
  :root { --swatch: min(62px, 8.6vw); --tool: min(80px, 10.5vw); --stk: min(80px, calc((100vw - 140px) / 8)); }
  #stage-wrap { padding-top: 72px; }
  #palette {
    grid-template-columns: repeat(7, var(--swatch));
    padding-top: 0;
    justify-content: center;
  }
  #tools { flex-direction: row; justify-content: center; padding: 0; }
  #tools .spacer { flex: 0 0 24px; }
  #sizes { flex-direction: row; }
}

/* Small screens (phones) */
@media (max-height: 560px), (max-width: 560px) {
  .tool { border-radius: 18px; }
  .size { width: 46px; height: 40px; }
}

/* ------------------------------------------------- parents hotspot */

#parent-hotspot {
  position: fixed;
  top: 0;
  left: 0;
  width: calc(var(--safe-l) + 90px);
  height: calc(var(--safe-t) + 90px);
  z-index: 50;
  touch-action: none;
}
#parent-hotspot svg {
  position: absolute;
  left: calc(var(--safe-l) + 15px);
  top: calc(var(--safe-t) + 15px);
  width: 60px;
  height: 60px;
  transform: rotate(-90deg);
  opacity: 0;
  transition: opacity 0.3s;
}
#parent-hotspot.holding svg { opacity: 1; }
#hold-ring {
  fill: rgba(255, 255, 255, 0.7);
  stroke: var(--accent);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-dasharray: 251.3;
  stroke-dashoffset: 251.3;
}

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

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-t) + 16px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 16px) calc(var(--safe-l) + 16px);
  animation: pop-in 0.2s ease;
}
.overlay[hidden] { display: none; }
@keyframes pop-in { from { transform: scale(0.97); } }

.close-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 34px;
  font-weight: 700;
  flex: none;
}
.close-btn.small { width: 48px; height: 48px; font-size: 22px; }

.picker-head {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-left: 84px; /* hotspot corner */
}
#picker-tabs { flex: 1; display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.tab {
  width: 84px;
  height: 72px;
  border-radius: 22px;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 40px;
}
.tab.on { background: var(--accent); box-shadow: 0 4px 0 #c9553a; }
#picker-grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 18px;
  padding: 22px 6px 40px;
  align-content: start;
}
.thumb {
  display: block;
  background: #fff;
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 10px;
}
/* The square image sets the card's height (in normal flow, so the grid rows
   size to fit it). */
.thumb img { display: block; width: 100%; height: auto; aspect-ratio: 1 / 1; object-fit: contain; border-radius: 12px; pointer-events: none; }
.thumb.current { outline: 5px solid var(--accent-2); }
.empty-photos {
  grid-column: 1 / -1;
  text-align: center;
  font-size: 80px;
  opacity: 0.35;
  padding: 60px 0;
}

/* Grown-up menu: normal text UI */
.grown-up {
  align-items: center;
  justify-content: center;
  background: rgba(40, 30, 20, 0.45);
  user-select: text;
  -webkit-user-select: text;
}
.sheet {
  width: min(720px, 100%);
  max-height: 100%;
  overflow-y: auto;
  touch-action: pan-y;
  background: #fffdf7;
  border-radius: 24px;
  padding: 20px 24px 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  font-size: 17px;
}
.sheet header { display: flex; align-items: center; justify-content: space-between; }
.sheet h1 { margin: 0; font-size: 26px; }
.sheet h2 { margin: 22px 0 8px; font-size: 18px; }
.sheet section + section { border-top: 1px solid #eee2c8; }
.hint, .tips p { margin: 4px 0 10px; color: #6b5a48; font-size: 15px; line-height: 1.4; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 12px 18px;
  border-radius: 14px;
  background: #f3ead7;
  font-weight: 600;
  min-height: 48px;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: #ffe1db; color: #b2361b; }
.btn:active { transform: scale(0.97); }
.file-btn { position: relative; overflow: hidden; cursor: pointer; }
.file-btn input { position: absolute; inset: 0; opacity: 0; font-size: 100px; cursor: pointer; }

#my-photos { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.my-photo { position: relative; width: 104px; height: 104px; border-radius: 14px; background: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.12); overflow: hidden; display: grid; place-items: center; }
.my-photo img { width: 100%; height: 100%; object-fit: contain; }
.my-photo .del {
  position: absolute; top: 4px; right: 4px;
  min-width: 32px; height: 32px; padding: 0 8px;
  border-radius: 16px; background: rgba(0,0,0,.6); color: #fff; font-size: 14px; font-weight: 700;
}
.my-photo .del.confirm { background: #d63a1c; }

.toggle { display: flex; align-items: center; gap: 12px; padding: 8px 0; font-size: 17px; }
.toggle input { width: 26px; height: 26px; accent-color: var(--accent); }

#photo-editor { margin-top: 12px; }
.preview-wrap {
  position: relative;
  background: repeating-conic-gradient(#f2ead8 0% 25%, #fff 0% 50%) 0 0 / 24px 24px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  height: min(52vh, 460px);
  overflow: hidden;
}
#photo-preview { max-width: 100%; max-height: min(52vh, 460px); width: auto; height: auto; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,.15); }
#photo-busy {
  position: absolute; inset: 0; display: none; place-items: center;
  background: rgba(255,255,255,.6); font-weight: 700; font-size: 20px;
}
#photo-busy.on { display: grid; }
.editor-controls { display: flex; flex-direction: column; gap: 14px; margin-top: 16px; }
.seg { display: flex; background: #f3ead7; border-radius: 14px; padding: 4px; }
.seg button { flex: 1; padding: 12px 6px; border-radius: 11px; font-weight: 600; }
.seg button.on { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.15); }
.slider { display: flex; align-items: center; gap: 14px; font-weight: 600; }
.slider input { flex: 1; accent-color: var(--accent); height: 30px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + 30px);
  transform: translateX(-50%);
  background: rgba(40, 30, 20, 0.9);
  color: #fff;
  padding: 12px 20px;
  border-radius: 14px;
  z-index: 60;
  font-size: 16px;
}

/* ------------------------------------------------------------ stickers */

#sticker-drawer {
  position: fixed;
  z-index: 30;
  top: calc(var(--safe-t) + 12px);
  bottom: calc(var(--safe-b) + 12px);
  right: calc(var(--safe-r) + 26px + var(--tool));
  display: none;
  grid-template-columns: repeat(3, var(--stk));
  grid-auto-rows: var(--stk);
  gap: 6px;
  align-content: center;
  padding: 12px;
  background: rgba(255, 255, 255, 0.94);
  border-radius: 26px;
  box-shadow: 0 6px 0 rgba(59, 42, 26, 0.15), 0 10px 30px rgba(59, 42, 26, 0.15);
  touch-action: none;
  animation: drawer-in 0.22s ease-out;
}
#sticker-drawer.open { display: grid; }
@keyframes drawer-in { from { transform: translateX(30px); opacity: 0; } }
@keyframes drawer-up { from { transform: translateY(30px); opacity: 0; } }
.sticker {
  width: var(--stk);
  height: var(--stk);
  border-radius: 18px;
  display: grid;
  place-items: center;
  touch-action: none;
}
.sticker img { width: 86%; height: 86%; pointer-events: none; -webkit-user-drag: none; }
.sticker:active { background: var(--bg); transform: scale(0.94); }

#drag-sticker {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 70;
  pointer-events: none;
  filter: drop-shadow(0 10px 8px rgba(0, 0, 0, 0.25));
  will-change: transform;
}

/* --------------------------------------------------------- crop editor */

.crop-box { position: relative; display: inline-block; line-height: 0; max-width: 100%; max-height: 100%; }
#crop-frame {
  position: absolute;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
  outline: 3px solid #fff;
  touch-action: none;
  cursor: move;
}
#crop-frame[hidden] { display: none; }
#crop-frame .h {
  position: absolute;
  width: 34px;
  height: 34px;
  border: 6px solid #fff;
  touch-action: none;
}
#crop-frame .nw { left: -8px; top: -8px; border-right: 0; border-bottom: 0; cursor: nwse-resize; }
#crop-frame .ne { right: -8px; top: -8px; border-left: 0; border-bottom: 0; cursor: nesw-resize; }
#crop-frame .sw { left: -8px; bottom: -8px; border-right: 0; border-top: 0; cursor: nesw-resize; }
#crop-frame .se { right: -8px; bottom: -8px; border-left: 0; border-top: 0; cursor: nwse-resize; }
.preview-wrap { overflow: hidden; }
.version { font-size: 13px !important; opacity: 0.7; }

/* Portrait: the drawer sits as a wide strip just above the palette.
   (Must come after the base #sticker-drawer rule.) */
@media (orientation: portrait) {
  #sticker-drawer {
    top: auto;
    left: calc(var(--safe-l) + 12px);
    right: calc(var(--safe-r) + 12px);
    bottom: calc(var(--safe-b) + 12px + var(--tool) + 2 * var(--swatch) + 46px);
    grid-template-columns: repeat(8, var(--stk));
    justify-content: center;
    animation-name: drawer-up;
  }
}
