*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #1a1a1a;
  --surface: #242424;
  --border: #3a3a3a;
  --text: #f2f2f2;
  --muted: #9a9a9a;
  --accent: #3d8bfd;
  --accent-press: #2a6fd4;
  --danger: #e85d5d;
  --pad-bg: #ffffff;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --touch: 48px;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

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

.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  gap: 12px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}

.brand {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.size-label select {
  appearance: none;
  -webkit-appearance: none;
  min-height: var(--touch);
  min-width: 96px;
  padding: 0 36px 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background-color: var(--surface);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%239a9a9a' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
}

.size-label select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.canvas-wrap {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: min(100%, calc(100dvh - 220px));
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  touch-action: none;
}

#pad {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: var(--pad-bg);
  cursor: crosshair;
  touch-action: none;
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
}

.color-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.color-indicator {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 10px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--border);
}

.palette {
  display: flex;
  flex: 1;
  gap: 8px;
  overflow-x: auto;
  padding: 2px;
  -webkit-overflow-scrolling: touch;
}

.swatch {
  width: var(--touch);
  height: var(--touch);
  flex: 0 0 var(--touch);
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 0;
  cursor: pointer;
  background: var(--c);
}

.swatch[aria-pressed="true"] {
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--accent);
}

.swatch:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

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

.btn {
  min-height: var(--touch);
  border: none;
  border-radius: 12px;
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0 16px;
}

.btn:active {
  transform: scale(0.98);
}

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:active {
  background: #2e2e2e;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:active {
  background: var(--accent-press);
}

@media (min-width: 480px) {
  .brand {
    font-size: 1.5rem;
  }

  .app {
    padding-top: calc(20px + var(--safe-top));
    gap: 16px;
  }
}
