:root {
  --bg1: #fff3d6; --bg2: #ffd9ec; --bg3: #d6f0ff; --ink: #2b2b3a; --card: #ffffff; --accent: #ff7aa8; --ok: #4cc96b;
  --shadow: 0 6px 0 rgba(0, 0, 0, .12), 0 10px 24px rgba(0, 0, 0, .12);
  --tb: clamp(44px, 7.4dvh, 62px);
  --sw: clamp(26px, calc((100dvh - 28px) / 16 - 5px), 50px);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Baloo 2", "Comic Sans MS", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; color: var(--ink); user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
body { background: linear-gradient(135deg, var(--bg1), var(--bg2) 50%, var(--bg3)); }
button { font: inherit; color: inherit; border: 0; cursor: pointer; touch-action: manipulation; }
[hidden] { display: none !important; }
#app { height: 100dvh; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.screen { display: none; height: 100%; flex-direction: column; padding: 14px 18px; gap: 12px; }
.screen.active { display: flex; animation: pop .25s ease; }
@keyframes pop { from { opacity: 0; transform: scale(.98); } }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.top h2 { margin: 0; font-size: clamp(20px, 3.4dvh, 30px); }
.logo { margin: 0; font-size: clamp(26px, 5dvh, 44px); letter-spacing: .5px; color: #e2508a; text-shadow: 0 3px 0 #fff; }
.logo span { display: inline-block; animation: wob 3s ease-in-out infinite; }
@keyframes wob { 50% { transform: rotate(-12deg) scale(1.1); } }
.top-actions { display: flex; gap: 10px; align-items: center; }
.round { width: 54px; height: 54px; border-radius: 50%; background: #fff; font-size: 24px; box-shadow: var(--shadow); }
.pill { border-radius: 999px; background: #fff; padding: 12px 22px; font-size: 18px; font-weight: 700; box-shadow: var(--shadow); }
.pill.primary { background: var(--accent); color: #fff; }
.pill.install { background: #5b8cff; color: #fff; animation: pulse 1.8s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.06); } }
button:active { transform: translateY(3px) scale(.98); }

/* home */
.themes { flex: 1; display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 18px; min-height: 0; }
.big-card { background: var(--card); border-radius: 28px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 16px; padding: 16px 22px; text-align: left; overflow: hidden; }
.theme-card { flex-direction: column; align-items: stretch; padding: 0; border: 6px solid #fff; min-height: 0; }
.theme-card canvas { width: 100%; flex: 1; min-height: 0; object-fit: cover; background: #fff; }
.theme-card .label { padding: 10px 18px 14px; display: flex; align-items: center; gap: 12px; font-size: clamp(20px, 3.4dvh, 30px); font-weight: 800; }
.theme-card .label small { display: block; font-size: 14px; font-weight: 600; opacity: .65; }
.theme-card .label .emoji { font-size: 1.5em; }
.gallery-card { font-size: 22px; justify-content: center; background: #fffbe6; }
.gallery-card .emoji { font-size: 40px; }
.gallery-card small { display: block; font-size: 14px; opacity: .65; }

/* picker */
.levels { display: flex; gap: 8px; }
.levels button { background: #fff; border-radius: 18px; padding: 6px 12px; font-size: 16px; box-shadow: var(--shadow); opacity: .6; }
.levels button small { display: block; font-size: 12px; font-weight: 700; }
.levels button.on { opacity: 1; outline: 4px solid var(--accent); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 10px; scrollbar-width: none; flex: none; }
.chips button { flex: none; background: #fff; border-radius: 999px; padding: 8px 16px; font-weight: 700; font-size: 15px; box-shadow: 0 3px 0 rgba(0, 0, 0, .12); }
.chips button.on { background: var(--accent); color: #fff; }
.pages { flex: 1; min-height: 0; display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: minmax(0, 1fr); gap: 14px; }
.page-card { background: #fff; border-radius: 20px; box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column; min-height: 0; position: relative; overflow: hidden; }
.page-card canvas, .page-card img { width: 100%; height: 100%; object-fit: contain; min-height: 0; flex: 1; border-radius: 14px; }
.page-card .cap { font-size: 12px; font-weight: 700; text-align: center; padding: 3px 4px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; opacity: .75; }
.picker-foot { display: flex; justify-content: center; gap: 14px; flex: none; }
.gallery-grid { overflow-y: auto; grid-auto-rows: minmax(180px, 30dvh); padding-bottom: 20px; align-content: start; }
.page-card .mini { position: absolute; top: 10px; width: 40px; height: 40px; border-radius: 50%; background: rgba(255, 255, 255, .92); box-shadow: 0 2px 6px rgba(0, 0, 0, .25); font-size: 18px; }
.page-card .mini.del { right: 10px; } .page-card .mini.again { left: 10px; }
.page-card .badge { position: absolute; bottom: 30px; right: 10px; font-size: 22px; }
.empty { text-align: center; font-size: 22px; margin-top: 12dvh; }

/* paint */
#paint.screen { flex-direction: row; padding: 8px; gap: 8px; }
.toolbar { display: grid; grid-template-columns: repeat(2, var(--tb)); grid-auto-rows: var(--tb); gap: 6px; align-content: start; flex: none; overflow-y: auto; scrollbar-width: none; padding: 2px; }
.tb { width: var(--tb); height: var(--tb); border-radius: 16px; background: #fff; font-size: calc(var(--tb) * .46); box-shadow: 0 3px 0 rgba(0, 0, 0, .15); display: grid; place-items: center; color: var(--cur, #ff4d4d); }
.tb svg { width: 70%; height: 70%; }
.tb.tool.on, .tb.toggle.on, .tb[data-act=fit].on { outline: 4px solid var(--accent); background: #fff5fa; transform: scale(1.04); }
.tb.toggle:not(.on) { filter: grayscale(1); opacity: .55; }
.tb.ok { background: var(--ok); color: #fff; font-weight: 900; }
.tb.danger { background: #ffe1e1; }
.sizes { grid-column: 1 / -1; display: flex; justify-content: space-between; align-items: center; background: #fff; border-radius: 16px; padding: 0 4px; height: var(--tb); box-shadow: 0 3px 0 rgba(0, 0, 0, .15); }
.sizes button { flex: 1; height: 100%; background: none; display: grid; place-items: center; padding: 0; }
.sizes i { display: block; border-radius: 50%; background: #c9c9d4; }
.sizes button.on i { background: var(--cur, #ff4d4d); outline: 3px solid var(--ink); outline-offset: 1px; }
.stage-wrap { flex: 1; min-width: 0; min-height: 0; display: grid; place-items: center; container-type: size; }
.stage { width: min(100cqw, calc(100cqh * 4 / 3)); aspect-ratio: 4 / 3; background: #fff; border-radius: 14px; box-shadow: var(--shadow); position: relative; overflow: hidden; touch-action: none; cursor: crosshair; }
.stage .view { position: absolute; inset: 0; transform-origin: 0 0; will-change: transform; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.stage canvas.line { mix-blend-mode: multiply; pointer-events: none; }
.palette { display: grid; grid-template-columns: repeat(4, var(--sw)); grid-auto-rows: var(--sw); gap: 5px; align-content: center; flex: none; background: rgba(255, 255, 255, .7); border-radius: 18px; padding: 8px; }
.palette button { border-radius: 50%; border: 2px solid rgba(0, 0, 0, .18); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .12); padding: 0; }
.palette button.on { transform: scale(1.22); border: 3px solid var(--ink); box-shadow: 0 0 0 3px #fff, 0 4px 8px rgba(0, 0, 0, .3); z-index: 1; }

@media (orientation: portrait) {
  :root { --sw: clamp(22px, calc((100vw - 34px) / 16 - 5px), 46px); --tb: clamp(40px, 9vw, 58px); }
  #paint.screen { flex-direction: column; }
  .toolbar { grid-template-columns: none; grid-auto-flow: column; grid-auto-columns: var(--tb); grid-template-rows: var(--tb); overflow-x: auto; overflow-y: hidden; }
  .sizes { grid-column: span 3; grid-row: 1; }
  .palette { grid-template-columns: repeat(16, var(--sw)); justify-content: center; }
  .pages { grid-template-columns: repeat(2, 1fr); }
}
@media (max-height: 520px) and (orientation: landscape) { .toolbar { grid-template-columns: repeat(3, var(--tb)); } }

/* overlays */
.modal { position: fixed; inset: 0; background: rgba(40, 30, 60, .45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal-box { background: #fff; border-radius: 28px; padding: 26px; max-width: 520px; width: 100%; text-align: center; font-size: 20px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); animation: pop .2s ease; }
.modal-box .emoji { font-size: 64px; line-height: 1.1; }
.modal-box ol { text-align: left; line-height: 1.7; }
.modal-actions { display: flex; gap: 14px; justify-content: center; margin-top: 20px; flex-wrap: wrap; }
.modal-actions button { border-radius: 999px; padding: 14px 28px; font-size: 20px; font-weight: 800; background: #eee; box-shadow: 0 4px 0 rgba(0, 0, 0, .15); }
.modal-actions button.yes { background: var(--ok); color: #fff; } .modal-actions button.no { background: #ff6b6b; color: #fff; }
.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 60; }
.toast { position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 12px 22px; border-radius: 999px; font-weight: 700; z-index: 70; }
@media print { body * { visibility: hidden; } #printImg { visibility: visible; position: fixed; inset: 0; width: 100%; height: 100%; object-fit: contain; } }
