:root {
  --paper: #fbf7e9;
  --ink: #27313b;
  --blue: #2864e8;
  --panel: rgba(255, 255, 255, 0.96);
  --shadow: 0 16px 44px rgba(46, 55, 73, 0.20), 0 3px 8px rgba(46, 55, 73, 0.10);
  --control: clamp(46px, 4vw, 64px);
  --gap: 5px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: ui-rounded, "Arial Rounded MT Bold", "Trebuchet MS", system-ui, sans-serif;
  background: var(--paper);
  color: var(--ink);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
button, img { user-select: none; -webkit-user-drag: none; }
button { font: inherit; }

.app { position: fixed; inset: 0; overflow: hidden; touch-action: none; }
#drawingCanvas, #effectsCanvas {
  position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none;
}
#drawingCanvas {
  background:
    radial-gradient(circle at 18% 9%, rgba(126, 208, 244, .16), transparent 36%),
    radial-gradient(circle at 82% 100%, rgba(142, 205, 103, .13), transparent 38%),
    repeating-linear-gradient(7deg, rgba(90, 72, 42, .018) 0 1px, transparent 1px 5px),
    #fbf7e9;
}
#effectsCanvas { pointer-events: none; }

.topbar { position: absolute; inset: 0 0 auto 0; min-height: 80px; pointer-events: none; display: flex; align-items: center; justify-content: space-between; padding: 14px clamp(14px, 2.2vw, 30px); }
.topbar h1 {
  margin: 0; font-size: clamp(24px, 2vw, 40px); letter-spacing: -.035em; color: #334052;
  text-shadow: 0 2px 0 #fff; opacity: .92;
}
.topbar h1 span { color: #8d56d9; }
.topbar .top-button { pointer-events: auto; width: var(--control); height: var(--control); }

.tracking-status {
  position: absolute;
  z-index: 11;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 18px;
  background: rgba(255, 255, 255, .78);
  box-shadow: 0 4px 14px rgba(46, 55, 73, .10);
  color: #485463;
  font-family: system-ui, sans-serif;
  font-size: 13px;
  font-weight: 650;
  pointer-events: none;
  backdrop-filter: blur(7px);
}
.status-item { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.status-item i { width: 9px; height: 9px; border-radius: 50%; background: #a7afb7; box-shadow: 0 0 0 3px rgba(167,175,183,.18); }
.status-item.is-online i, .status-item.is-detected i { background: #43b943; box-shadow: 0 0 0 3px rgba(67,185,67,.18); }
.status-item.is-offline i { background: #ec625a; box-shadow: 0 0 0 3px rgba(236,98,90,.16); }
.simulation-badge { padding: 4px 8px; border-radius: 10px; color: #fff; background: #8650d2; }

.toolbar, .stamp-panel {
  position: absolute; left: 50%; transform: translateX(-50%); background: var(--panel);
  border: 1px solid rgba(255,255,255,.9); box-shadow: var(--shadow); backdrop-filter: blur(8px);
}
.toolbar {
  bottom: max(12px, env(safe-area-inset-bottom)); z-index: 10; display: flex; align-items: center; gap: var(--gap);
  width: min(calc(100vw - 24px), max-content); max-width: calc(100vw - 24px); padding: clamp(7px, .6vw, 12px); border-radius: clamp(18px, 2vw, 34px);
  overflow-x: auto; justify-content: flex-start; scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.tool-group { display: flex; align-items: center; gap: var(--gap); }
.tool-group + .tool-group { border-left: 2px dashed rgba(61,73,87,.14); padding-left: var(--gap); }
.icon-button, .color-button, .size-button, .stamp-button {
  position: relative; display: grid; place-items: center; flex: 0 0 auto; border: 0; cursor: pointer;
  touch-action: manipulation; transition: transform .14s ease, box-shadow .18s ease, background .18s ease;
}
.icon-button {
  width: var(--control); height: var(--control); border-radius: 26%; background: #fff;
  color: var(--ink); box-shadow: inset 0 0 0 2px rgba(52,65,80,.08), 0 5px 10px rgba(40,47,56,.10);
}
.icon-button img { width: 58%; height: 58%; object-fit: contain; pointer-events: none; }
.icon-button:active, .color-button:active, .size-button:active, .stamp-button:active { transform: scale(.9); }
.icon-button.is-selected {
  background: #eaf1ff; box-shadow: inset 0 0 0 5px var(--blue), 0 8px 18px rgba(40,100,232,.25);
}
.icon-button:disabled { opacity: .35; cursor: default; }

.color-choices { gap: clamp(5px, .45vw, 9px); }
.color-button {
  width: var(--control); height: var(--control); border-radius: 50%;
  background: var(--color); box-shadow: inset 0 -7px 0 rgba(0,0,0,.10), inset 0 4px 0 rgba(255,255,255,.32);
}
.color-button::after { content: ""; position: absolute; inset: -6px; border: 4px solid transparent; border-radius: inherit; }
.color-button.is-hand-hover::before {
  content: ""; position: absolute; inset: -10px; border-radius: inherit; pointer-events: none;
  background: conic-gradient(#27313b var(--hand-dwell, 0deg), rgba(39,49,59,.12) 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 6px), #000 0);
  mask: radial-gradient(farthest-side, transparent calc(100% - 6px), #000 0);
}
.color-button.is-selected { transform: translateY(-5px) scale(1.08); box-shadow: inset 0 -5px 0 rgba(0,0,0,.10), 0 8px 15px rgba(44,51,60,.22); }
.color-button.is-selected::after { border-color: #fff; box-shadow: 0 0 0 4px var(--ink); animation: color-pop .32s ease; }
@keyframes color-pop { 50% { transform: scale(1.18); } }

.size-button { width: var(--control); height: var(--control); background: transparent; border-radius: 24px; }
.size-dot { border-radius: 50%; background: #354150; box-shadow: inset 0 -3px 0 rgba(0,0,0,.2); }
.size-button.is-selected { background: #edf3ff; box-shadow: inset 0 0 0 4px var(--blue); }

.stamp-panel { bottom: calc(clamp(18px, 2.3vh, 36px) + var(--control) + clamp(44px, 4.2vh, 68px)); z-index: 9; padding: 13px; border-radius: 30px; }
.stamp-panel[hidden] { display: none; }
.stamp-choices { display: flex; gap: 9px; }
.stamp-button { width: var(--control); height: var(--control); border-radius: 22px; background: #fff; box-shadow: inset 0 0 0 2px rgba(52,65,80,.08); }
.stamp-button img { width: 65%; height: 65%; pointer-events: none; }
.stamp-button.is-selected { background: #fff4cf; box-shadow: inset 0 0 0 4px #f0a900; transform: translateY(-3px); }

.clear-button { overflow: visible; }
.clear-button.is-holding { animation: clear-wiggle .22s ease-in-out infinite alternate; background: #fff0ef; }
.hold-ring { position: absolute; inset: -6px; width: calc(100% + 12px); height: calc(100% + 12px); transform: rotate(-90deg); pointer-events: none; }
.hold-ring circle { fill: none; stroke: #ee493d; stroke-width: 7; stroke-linecap: round; stroke-dasharray: 283; stroke-dashoffset: 283; }
.clear-button.is-holding .hold-ring circle { animation: hold-progress 1.5s linear forwards; }
@keyframes hold-progress { to { stroke-dashoffset: 0; } }
@keyframes clear-wiggle { from { transform: rotate(-2deg) scale(.98); } to { transform: rotate(2deg) scale(1.03); } }

.eraser-cursor { position: fixed; display: none; border: 3px dashed #485463; background: rgba(255,255,255,.42); border-radius: 50%; transform: translate(-50%,-50%); pointer-events: none; z-index: 20; }
.eraser-cursor.is-visible { display: block; }
.hand-cursor {
  position: fixed;
  z-index: 7;
  left: 0;
  top: 0;
  width: 24px;
  height: 24px;
  border: 4px solid #fff;
  border-radius: 50%;
  opacity: 0;
  transform: translate(-50%, -50%) scale(.88);
  background: var(--cursor-color, #2864e8);
  box-shadow: 0 4px 13px rgba(39,49,59,.28), 0 0 0 4px var(--cursor-color, #2864e8);
  transition: opacity .15s ease, transform .12s ease, box-shadow .12s ease;
  pointer-events: none;
}
.hand-cursor.is-visible { opacity: .88; transform: translate(-50%, -50%) scale(1); }
.hand-cursor.is-pinching {
  opacity: 1;
  transform: translate(-50%, -50%) scale(.78);
  box-shadow: 0 3px 10px rgba(39,49,59,.34), 0 0 0 7px color-mix(in srgb, var(--cursor-color, #2864e8) 45%, transparent);
}
.hand-cursor.is-fist-holding::after {
  content: ""; position: absolute; inset: -12px; border-radius: 50%;
  background: conic-gradient(#ff7a2f var(--gesture-progress, 0deg), rgba(255,122,47,.14) 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 6px), #000 0);
  mask: radial-gradient(farthest-side, transparent calc(100% - 6px), #000 0);
}
.hand-cursor.is-erasing {
  background: rgba(255,255,255,.72);
  border-color: #485463;
  box-shadow: 0 3px 10px rgba(39,49,59,.28), 0 0 0 5px #485463;
}
.hand-cursor.is-clicking { animation: hand-click-pop .22s ease; }
@keyframes hand-click-pop { 50% { transform: translate(-50%, -50%) scale(.58); } }
.stamp-bounce { position: fixed; z-index: 30; pointer-events: none; width: 64px; height: 64px; transform: translate(-50%,-50%) scale(0); background: center/contain no-repeat; }
.stamp-bounce.play { animation: stamp-pop .44s cubic-bezier(.18,.89,.32,1.5); }
@keyframes stamp-pop { 0% { transform: translate(-50%,-50%) scale(.2) rotate(-12deg); opacity: .3; } 65% { transform: translate(-50%,-50%) scale(1.25) rotate(5deg); opacity: .9; } 100% { transform: translate(-50%,-50%) scale(1); opacity: 0; } }

.idle-overlay { position: absolute; inset: 0; z-index: 8; pointer-events: none; opacity: 0; visibility: hidden; transition: opacity .4s ease, visibility .4s; }
.idle-overlay.is-visible { opacity: 1; visibility: visible; }
.idle-card { position: absolute; left: 50%; top: 42%; transform: translate(-50%,-50%); display: flex; align-items: center; gap: 22px; padding: 22px 36px; border-radius: 34px; background: rgba(255,255,255,.88); box-shadow: var(--shadow); }
.idle-card p { margin: 0; font-size: clamp(26px, 2.3vw, 46px); color: #384557; white-space: nowrap; }
.touch-hand { position: relative; font-size: clamp(48px, 5vw, 92px); animation: hand-tap 1.8s ease-in-out infinite; }
.touch-ripple { position: absolute; left: 62%; top: 9%; width: 24px; height: 24px; border: 4px solid #3c78ed; border-radius: 50%; animation: ripple 1.8s ease-out infinite; }
@keyframes hand-tap { 50% { transform: translateY(8px) scale(.94); } }
@keyframes ripple { 20% { opacity: 0; transform: scale(.2); } 60% { opacity: .65; } 100% { opacity: 0; transform: scale(2.5); } }
.idle-decoration { position: absolute; animation: floaty 5s ease-in-out infinite alternate; color: #f4b917; font-size: 54px; }
.idle-decoration.one { left: 16%; top: 22%; }
.idle-decoration.two { right: 19%; top: 25%; width: 42px; height: 42px; border-radius: 50%; background: #ee4b78; animation-delay: -1s; }
.idle-decoration.three { left: 23%; bottom: 23%; animation-delay: -2.2s; }
.idle-decoration.four { right: 24%; bottom: 25%; color: #8650d2; animation-delay: -.6s; }
@keyframes floaty { to { transform: translate(10px,-22px) rotate(10deg); } }

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

@media (max-aspect-ratio: 3/2), (max-width: 1300px) {
  :root { --control: clamp(44px, 7vw, 58px); }
  .toolbar { overflow-x: auto; justify-content: flex-start; left: 16px; right: 16px; transform: none; scrollbar-width: none; }
  .toolbar::-webkit-scrollbar { display: none; }
  .color-button { width: var(--control); height: var(--control); }
  .size-button { width: var(--control); }
  .stamp-panel { max-width: calc(100vw - 32px); overflow-x: auto; }
  .tracking-status { top: 112px; font-size: 12px; }
}

@media (min-width: 2500px) {
  :root { --control: 136px; --gap: 18px; }
  .toolbar { padding: 20px; bottom: 42px; border-radius: 52px; }
  .color-button { width: 112px; height: 112px; }
  .size-button { width: 112px; }
  .topbar .top-button { width: 112px; height: 112px; }
  .tracking-status { top: 30px; gap: 16px; padding: 12px 18px; border-radius: 24px; font-size: 20px; }
  .status-item i { width: 14px; height: 14px; }
  .stamp-button { width: 112px; height: 112px; }
  .stamp-panel { bottom: 232px; }
}

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms!important; animation-iteration-count: 1!important; scroll-behavior: auto!important; } }
