:root {
  --ink: #141414;
  --paper: #ffffff;
  --bg: #f6f4ee;
  --muted: #5d5a52;
  --line: #e2ded3;
  --yellow: #ffde28;
  --red: #e41c34;
  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100vh; background: var(--bg); }

/* ------------------------------------------------------------ header */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  background: var(--yellow);
  border-bottom: 3px solid var(--ink);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: var(--ink);
  color: var(--yellow);
  font-size: 24px;
}
.top h1 { margin: 0; font-size: 24px; font-weight: 900; letter-spacing: -0.5px; }
.tagline { margin: 0; font-weight: 700; color: var(--red); font-size: 14px; }
.top-right { display: flex; align-items: center; gap: 10px; }
.privacy {
  margin: 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.privacy .narrow { display: none; }

/* ------------------------------------------------------------ layout */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  gap: 20px;
  padding: 20px 24px;
  max-width: 1500px;
  margin: 0 auto;
}
.stage-wrap { min-width: 0; display: flex; flex-direction: column; }

.drop {
  display: grid;
  place-items: center;
  height: calc(100vh - 150px);
  min-height: 420px;
  border: 3px dashed var(--ink);
  border-radius: var(--radius);
  background: var(--paper);
  text-align: center;
  padding: 24px;
}
.drop.over { background: #fff7c2; }
.drop-inner { max-width: 440px; }
.drop-big { font-size: 30px; font-weight: 900; margin: 0 0 8px; }
.drop-small { color: var(--muted); margin: 0; }

.canvas-box {
  position: relative;
  display: grid;
  place-items: center;
  height: calc(100vh - 150px);
  min-height: 420px;
  background: #2a2a2a;
  border-radius: var(--radius);
  padding: 12px;
  overflow: hidden;
}
.canvas-box.over { outline: 4px solid var(--yellow); }
#stage { display: block; border-radius: 6px; touch-action: none; cursor: crosshair; }
#stage.fill { cursor: grab; }
#stage.fill.dragging { cursor: grabbing; }

.busy {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  background: rgba(20, 20, 20, 0.6);
  color: #fff;
  font-weight: 700;
  text-align: center;
  padding: 16px;
}
.busy p { margin: 0; max-width: 340px; }
.spinner {
  width: 44px;
  height: 44px;
  border: 5px solid rgba(255, 255, 255, 0.25);
  border-top-color: var(--yellow);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------ sheet */
.sheet {
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  padding: 18px;
  align-self: start;
  position: sticky;
  top: 16px;
}
.grab { display: none; }
.sheet h2 { margin: 0 0 8px; font-size: 20px; }
.sheet section > * + * { margin-top: 10px; }
.muted { color: var(--muted); font-size: 15px; line-height: 1.45; margin: 0; }
.fine { color: var(--muted); font-size: 13px; margin: 0; }
.status { margin: 0 0 10px; padding: 8px 10px; border-radius: 10px; background: #fff7c2; font-size: 14px; font-weight: 600; }
.status.error { background: #fde4e7; color: #8a0f20; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 16px;
  border: 2px solid var(--ink);
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  text-align: center;
}
.btn:hover { background: #fff7c2; }
.btn.primary { background: var(--ink); color: var(--yellow); }
.btn.primary:hover { background: #333; }
.btn.big { width: 100%; min-height: 52px; font-size: 17px; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.ghost { border-color: transparent; background: transparent; text-decoration: underline; }
.btn[aria-pressed="true"] { background: var(--yellow); }
.wide-btn { width: 100%; }
.stack { display: grid; gap: 10px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row2 > [hidden] + *, .row2 > *:only-child { grid-column: 1 / -1; }

.bar { height: 10px; border-radius: 99px; background: var(--line); overflow: hidden; }
#loadBar { height: 100%; width: 0; background: var(--red); transition: width 0.3s; }
#loadBar.indeterminate { width: 35%; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(290%); } }

.preview {
  display: grid;
  place-items: center;
  /* a workspace, not a thumbnail: erasing happens in here too */
  height: clamp(260px, 46vh, 520px);
  border-radius: 12px;
  border: 1px solid var(--line);
  background: repeating-conic-gradient(#e9e6dd 0 25%, #fff 0 50%) 0 0 / 16px 16px;
}
#preview { max-width: 100%; max-height: 100%; touch-action: none; }
#preview.erasing { cursor: crosshair; }
.eraser { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; }
.eraser .btn { min-height: 38px; padding: 6px 14px; }
.eraser .brush { display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: center; font-size: 14px; font-weight: 700; }

.clean > * + * { margin-top: 6px; }
.seg { display: flex; width: 100%; border: 2px solid var(--ink); border-radius: 12px; overflow: hidden; background: var(--paper); }
.seg button { flex: 1; min-height: 40px; border: 0; background: transparent; font: inherit; font-weight: 800; color: var(--ink); cursor: pointer; }
.seg button + button { border-left: 2px solid var(--ink); }
.seg button.on { background: var(--ink); color: var(--yellow); }
.engine { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.engine-label { font-size: 13px; font-weight: 800; }
.seg.small { width: auto; border-radius: 10px; }
.seg.small button { min-height: 32px; padding: 0 14px; font-size: 13px; }
.engine-hint { font-size: 13px; color: var(--muted); }
details { border-top: 1px solid var(--line); padding-top: 10px; }
summary { font-weight: 800; cursor: pointer; min-height: 32px; display: flex; align-items: center; }
details > * + * { margin-top: 10px; }

.thumbs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  border: 3px solid transparent;
  cursor: pointer;
  padding: 0;
  background: #ddd;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb.on { border-color: var(--red); }
.thumb .x {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 0;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 13px;
  line-height: 24px;
  cursor: pointer;
  padding: 0;
}
.thumb.add {
  display: grid;
  place-items: center;
  border: 2px dashed var(--ink);
  background: var(--paper);
  font-size: 28px;
  font-weight: 700;
}

.controls label {
  display: grid;
  grid-template-columns: 100px 1fr;
  align-items: center;
  font-size: 14px;
  font-weight: 700;
  min-height: 36px;
}
input[type="range"] { accent-color: var(--red); width: 100%; }
.check { display: flex; gap: 10px; align-items: center; font-weight: 700; font-size: 15px; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--red); margin: 0; }

.foot { max-width: 1500px; margin: 0 auto; padding: 0 24px 24px; font-size: 14px; }
.foot p { margin: 0; }
.foot a { color: var(--ink); font-weight: 700; }

/* ------------------------------------------------------------ phone */
@media (max-width: 899px) {
  /* one screen, no page scroll: the photo takes what the sheet leaves */
  body { display: flex; flex-direction: column; height: 100vh; height: 100dvh; min-height: 0; overflow: hidden; }
  .top { padding: 8px 12px; border-bottom-width: 2px; }
  .logo { width: 34px; height: 34px; font-size: 20px; }
  .top h1 { font-size: 19px; white-space: nowrap; }
  .tagline { font-size: 12px; white-space: nowrap; }
  /* the promise is repeated in the first step; the header needs the room for "New photo" */
  .privacy { display: none; }
  .app { flex: 1; min-height: 0; min-width: 0; display: flex; flex-direction: column; padding: 0; gap: 0; }
  .stage-wrap, .sheet { min-width: 0; width: 100%; }
  .stage-wrap { flex: 1; min-height: 0; padding: 8px; }
  .drop, .canvas-box { height: 100%; min-height: 0; padding: 6px; }
  .drop { border-width: 2px; }
  .drop-big { font-size: 22px; }
  .sheet {
    position: relative;
    top: auto;
    flex: none;
    max-height: 52dvh;
    overflow-y: auto;
    border: 0;
    border-top: 2px solid var(--ink);
    border-radius: 18px 18px 0 0;
    padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 18px rgba(0, 0, 0, 0.12);
  }
  .grab { display: block; width: 44px; height: 5px; border-radius: 3px; background: var(--line); margin: 2px auto 10px; }
  .sheet h2 { font-size: 18px; }
  .preview { height: 140px; }
  .foot { display: none; }
}

.touch-only { display: none; }
@media (hover: none) {
  .mouse-only { display: none; }
  .touch-only { display: inline; }
}
