:root {
  --paper: #fffdf8;
  --bg: #f6dfc4;
  --bg-2: #ffd9e6;
  --ink: #2b2118;
  --muted: #7a6454;
  --accent: #ff5b7a;
  --accent-dark: #e13e62;
  --sky: #7ed0ff;
  --shadow: 0 12px 32px rgba(80, 40, 20, 0.14);
  --radius: 28px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --swatch: 62px;
  --tool: 52px;
  font-family: "Nunito", "Avenir Next", "Segoe UI", sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  background:
    radial-gradient(1200px 600px at 10% -10%, var(--bg-2), transparent 60%),
    radial-gradient(900px 500px at 110% 10%, var(--sky), transparent 55%),
    var(--bg);
  color: var(--ink);
  overflow: hidden;
}

body {
  height: 100dvh;
  height: 100svh;
}

button,
a {
  font: inherit;
}

button {
  appearance: none;
  border: 0;
  cursor: pointer;
}

img {
  max-width: 100%;
}

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

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-t)) max(16px, var(--safe-r)) 10px max(16px, var(--safe-l));
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.logo {
  width: 44px;
  height: 44px;
  flex: none;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

[hidden] {
  display: none !important;
}

.icon-btn,
.tool-btn {
  min-width: var(--tool);
  min-height: var(--tool);
  border-radius: 18px;
  background: #fff;
  color: var(--ink);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  padding: 8px 12px;
  font-weight: 800;
  touch-action: manipulation;
}

.icon-btn[disabled],
.tool-btn[disabled] {
  opacity: 0.4;
}

.icon-btn.primary,
.tool-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
}

.icon-btn svg,
.tool-btn svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.screen {
  flex: 1;
  min-height: 0;
  display: none;
}

.screen.active {
  display: flex;
  flex-direction: column;
}

.home-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 8px max(16px, var(--safe-r)) calc(24px + var(--safe-b)) max(16px, var(--safe-l));
}

.gallery {
  display: grid;
  gap: 18px;
  grid-template-columns: 1fr;
  max-width: 1180px;
  margin: 0 auto;
}

.card {
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  text-align: left;
  color: inherit;
  padding: 0;
  width: 100%;
}

.card-preview {
  background: #fff;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  padding: 12px;
}

.card-preview img,
.card-preview object,
.card-preview svg {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.card-copy {
  padding: 14px 16px 18px;
}

.card-copy h2 {
  margin: 0;
  font-size: 1.35rem;
}

.card-copy p {
  margin: 4px 0 12px;
  color: var(--muted);
}

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

.pill.ghost {
  background: #ffe3ea;
  color: var(--accent-dark);
}

.note {
  max-width: 980px;
  margin: 18px auto 0;
  color: var(--muted);
  text-align: center;
  font-weight: 700;
}

.studio {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr auto auto;
  gap: 8px;
  padding: 0 max(10px, var(--safe-r)) calc(10px + var(--safe-b)) max(10px, var(--safe-l));
}

.stage-wrap {
  min-height: 0;
  min-width: 0;
  display: grid;
  place-items: center;
}

.stage {
  position: relative;
  width: min(100%, 100%);
  background: #fff;
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}

.stage svg,
.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#fill-layer {
  background: #fff;
}

#crayon-layer {
  pointer-events: none;
}

.stage.crayon-on #crayon-layer {
  pointer-events: auto;
}

#line-layer {
  pointer-events: none;
}

.dock {
  display: flex;
  gap: 8px;
  align-items: center;
  overflow: auto hidden;
  padding: 6px;
  background: rgba(255, 253, 248, 0.9);
  border-radius: 22px;
  box-shadow: var(--shadow);
  -webkit-overflow-scrolling: touch;
}

.tools {
  justify-content: center;
  flex-wrap: wrap;
}

.palette {
  justify-content: flex-start;
  padding-left: 10px;
  padding-right: 10px;
}

.swatch {
  flex: none;
  width: var(--swatch);
  height: var(--swatch);
  border-radius: 50%;
  border: 5px solid #fff;
  box-shadow: 0 6px 0 rgba(80, 40, 20, 0.12);
  touch-action: manipulation;
}

.swatch[aria-checked="true"] {
  outline: 4px solid var(--ink);
  outline-offset: 2px;
  transform: scale(1.06);
}

.size-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: currentColor;
}

.size-dot.fat {
  width: 20px;
  height: 20px;
}

.size-dot.chunky {
  width: 26px;
  height: 26px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + var(--safe-b));
  transform: translateX(-50%) translateY(120%);
  background: var(--ink);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 800;
  z-index: 20;
  opacity: 0;
  transition: 0.25s ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.sheet {
  position: fixed;
  inset: 0;
  background: rgba(43, 33, 24, 0.45);
  display: none;
  place-items: center;
  padding: max(16px, var(--safe-t)) max(16px, var(--safe-r)) max(16px, var(--safe-b)) max(16px, var(--safe-l));
  z-index: 30;
}

.sheet.open {
  display: grid;
}

.sheet-card {
  width: min(420px, 100%);
  background: var(--paper);
  border-radius: 28px;
  padding: 20px;
  box-shadow: var(--shadow);
  text-align: center;
}

.sheet-card img {
  width: 100%;
  border-radius: 16px;
  background: #fff;
  margin: 12px 0;
}

.sheet-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

@media (min-width: 700px) {
  :root {
    --swatch: 70px;
    --tool: 56px;
  }

  .brand h1 {
    font-size: 1.4rem;
  }

  .gallery {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 1100px) {
  .gallery {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

@media (orientation: landscape) and (max-height: 820px) {
  .studio {
    grid-template-rows: 1fr;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: stretch;
  }

  .stage-wrap {
    grid-column: 2;
    grid-row: 1;
  }

  .palette {
    grid-column: 1;
    grid-row: 1;
    display: grid;
    grid-template-columns: repeat(2, var(--swatch));
    justify-content: center;
    align-content: start;
    overflow: hidden auto;
    max-height: 100%;
    width: calc(var(--swatch) * 2 + 28px);
  }

  .tools {
    grid-column: 3;
    grid-row: 1;
    flex-direction: column;
    width: min(132px, 28vw);
    overflow: hidden auto;
  }

  .home-scroll {
    padding-top: 0;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --swatch: 40px;
    --tool: 44px;
  }

  .topbar {
    padding-top: calc(4px + var(--safe-t));
    padding-bottom: 4px;
  }

  .brand p {
    display: none;
  }

  .palette {
    grid-template-columns: repeat(3, var(--swatch));
    width: calc(var(--swatch) * 3 + 24px);
  }
}

@media (pointer: coarse) {
  .icon-btn,
  .tool-btn,
  .swatch {
    touch-action: manipulation;
  }
}
