:root {
  color-scheme: light;
  --bg: #fff7cf;
  --panel: #fffdf3;
  --ink: #151515;
  --muted: #6d6454;
  --line: rgba(21, 21, 21, 0.14);
  --shadow: 0 16px 44px rgba(45, 35, 5, 0.16);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  background:
    linear-gradient(135deg, rgba(255, 216, 61, 0.48), transparent 38%),
    linear-gradient(220deg, rgba(53, 182, 255, 0.18), transparent 48%),
    var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  color: inherit;
  touch-action: manipulation;
}

.app {
  width: min(100vw, 980px);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 10px max(14px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
}

.topbar,
.stage,
.controls {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  box-shadow: var(--shadow);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: clamp(24px, 6.8vw, 42px);
  line-height: 1;
}

.current-color {
  min-width: 74px;
  display: grid;
  justify-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 900;
}

#currentColorDot {
  width: 44px;
  height: 44px;
  border: 3px solid var(--ink);
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.55);
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 6px;
  background: #fff;
}

.stage-actions {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 2;
  display: flex;
  gap: 8px;
}

.icon-button,
.command-row button,
.tool-button {
  min-height: 44px;
  border-radius: 8px;
  background: #fff;
  border: 1px solid var(--line);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.12);
  display: inline-grid;
  place-items: center;
  font-weight: 900;
}

.icon-button {
  min-width: 46px;
}

.icon-button:active,
.command-row button:active,
.tool-button:active,
.swatch:active {
  transform: translateY(1px) scale(0.98);
}

#coloringCanvas {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: calc(100svh - 292px);
  background: #fff;
  touch-action: none;
  user-select: none;
}

#coloringCanvas.tool-brush {
  cursor: crosshair;
}

#coloringCanvas.tool-eraser {
  cursor: grab;
}

.controls {
  display: grid;
  gap: 9px;
  padding: 9px;
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.tool-button[aria-pressed="true"] {
  background: var(--ink);
  color: #fff;
}

.size-control {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 34px;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
}

.size-control input {
  width: 100%;
  accent-color: #ffd83d;
}

.size-control strong {
  color: var(--ink);
  text-align: right;
}

.palette {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
  padding-bottom: 2px;
}

.palette::-webkit-scrollbar {
  display: none;
}

.swatch {
  flex: 0 0 58px;
  aspect-ratio: 1;
  border-radius: 8px;
  border: 3px solid #fff;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18), 0 5px 14px rgba(0, 0, 0, 0.12);
}

.swatch[aria-pressed="true"] {
  border-color: var(--ink);
}

.command-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.command-row button {
  width: 100%;
  background: var(--ink);
  color: #fff;
}

dialog {
  width: min(92vw, 760px);
  border: 0;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 22px 70px rgba(0, 0, 0, 0.34);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.48);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.sample-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.sample-grid img {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 20;
  translate: -50% 18px;
  opacity: 0;
  pointer-events: none;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  transition: opacity 180ms ease, translate 180ms ease;
}

.toast.show {
  opacity: 1;
  translate: -50% 0;
}

@media (max-width: 560px) {
  .app {
    padding-left: 8px;
    padding-right: 8px;
    gap: 8px;
  }

  .topbar {
    padding: 9px 10px;
  }

  .current-color {
    min-width: 58px;
  }

  #currentColorDot {
    width: 36px;
    height: 36px;
  }

  .stage {
    padding: 4px;
  }

  #coloringCanvas {
    max-height: calc(100svh - 272px);
  }

  .swatch {
    flex-basis: 54px;
  }

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

@media (orientation: landscape) and (max-height: 560px) {
  .app {
    grid-template-columns: minmax(0, 1fr) 285px;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .topbar {
    grid-column: 1 / -1;
  }

  #coloringCanvas {
    max-height: calc(100svh - 100px);
  }
}
