:root {
  --bg: #ececec;
  --panel: rgba(255, 255, 255, 0.88);
  --panel-border: rgba(0, 0, 0, 0.12);
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.13);
  --text: #111111;
  --muted: #666666;
  --danger: #d54d4d;
  --stage-grid: rgba(0, 0, 0, 0.08);
  --radius: 18px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

img,
svg {
  display: block;
  max-width: 100%;
}

button {
  border: 0;
  background: transparent;
  padding: 0;
}

#stage {
  position: fixed;
  inset: 0;
  background: var(--bg);
}

#stage.drag-over::after {
  content: 'Drop image to use it as a tracing layer';
  position: absolute;
  inset: 24px;
  border: 3px dashed rgba(0, 0, 0, 0.28);
  border-radius: 28px;
  display: grid;
  place-items: center;
  color: #111111;
  font-size: clamp(18px, 2vw, 28px);
  font-weight: 800;
  letter-spacing: 0.02em;
  background: rgba(255, 255, 255, 0.48);
  z-index: 22;
  pointer-events: none;
}

#stage.cursor-brush02 canvas {
  cursor: url('../img/cursor-pencil-02-1x.png') 2 28, crosshair;
}

#stage.cursor-brush03 canvas {
  cursor: url('../img/cursor-pencil-03-1x.png') 2 28, crosshair;
}

#stage.cursor-brush04 canvas {
  cursor: url('../img/cursor-pencil-04-1x.png') 2 28, crosshair;
}

#stage.cursor-brush05 canvas {
  cursor: url('../img/cursor-pencil-05-1x.png') 2 28, crosshair;
}

#stage.cursor-eraser canvas {
  cursor: url('../img/cursor-eraser-1x.png') 10 10, cell;
}

#stage.cursor-line canvas,
#stage.cursor-rectangle canvas,
#stage.cursor-circle canvas,
#stage.cursor-fill canvas {
  cursor: crosshair;
}

#stage.cursor-text canvas {
  cursor: text;
}

#bgImage,
#gridOverlay,
#artboard,
#preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#bgImage,
#gridOverlay {
  pointer-events: none;
}

#bgImage {
  object-fit: contain;
  opacity: 0.35;
}

#gridOverlay {
  display: none;
  background-image:
    linear-gradient(var(--stage-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--stage-grid) 1px, transparent 1px);
  background-size: 32px 32px;
}

#stage.grid-on #gridOverlay {
  display: block;
}

#artboard,
#preview {
  touch-action: none;
}

.floating-panel {
  position: fixed;
  z-index: 10;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

#topBar {
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  width: min(1220px, calc(100vw - 28px));
  padding: 12px 16px;
  display: grid;
  grid-template-columns: minmax(250px, 1.9fr) minmax(170px, 1.2fr) minmax(165px, 1.1fr) minmax(150px, 1fr) minmax(150px, 1fr);
  gap: 12px;
  align-items: center;
}

#sidePanel {
  top: max(98px, calc(env(safe-area-inset-top) + 82px));
  right: 16px;
  width: min(370px, calc(100vw - 28px));
  max-height: calc(100vh - 188px);
  overflow: auto;
  padding: 16px;
}

#sidePanel.collapsed {
  display: none;
}

#compactToggleWrap {
  position: fixed;
  top: max(98px, calc(env(safe-area-inset-top) + 82px));
  right: 16px;
  z-index: 10;
}

.panel-toggle,
.ghost-button,
.action-button,
.chip,
.tool-button {
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.panel-toggle,
.ghost-button,
.action-button,
.chip {
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  background: rgba(255, 255, 255, 0.7);
  color: var(--text);
  padding: 10px 12px;
}

.panel-toggle:hover,
.ghost-button:hover,
.action-button:hover,
.chip:hover,
.tool-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.08);
}

.panel-toggle.hidden {
  display: none;
}

.ghost-button {
  min-width: 74px;
}

.ghost-button[disabled],
.action-button[disabled],
.tool-button[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
}

.action-button {
  width: 100%;
  cursor: pointer;
  font-weight: 700;
}

.action-button.primary {
  background: #111111;
  color: #ffffff;
}

.action-button.danger {
  background: rgba(213, 77, 77, 0.12);
  color: #8f2121;
  border-color: rgba(213, 77, 77, 0.28);
}

.action-button.subtle {
  background: rgba(0, 0, 0, 0.05);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
}

.panel-header h2 {
  margin: 4px 0 0;
  font-size: 20px;
  line-height: 1.1;
}

.panel-section + .panel-section {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid rgba(0, 0, 0, 0.08);
}

.panel-section h3 {
  margin: 0 0 12px;
  font-size: 14px;
}

.stack {
  display: grid;
  gap: 10px;
}

.stack.two-col {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  cursor: pointer;
  font-weight: 700;
}

.chip.active {
  background: #111111;
  color: #ffffff;
  border-color: #111111;
}

.label,
.control span,
.toggle-row span,
.helper-text {
  font-size: 12px;
  color: var(--muted);
}

.helper-text {
  margin: 10px 0 0;
  line-height: 1.45;
}

.control {
  display: grid;
  gap: 6px;
}

.control.compact {
  min-width: 0;
}

.control input[type="text"],
.control select,
.control input[type="file"] {
  width: 100%;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  background: rgba(255, 255, 255, 0.78);
  min-height: 42px;
  padding: 10px 12px;
}

.control input[type="color"] {
  width: 100%;
  min-height: 42px;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  background: rgba(255, 255, 255, 0.78);
  padding: 4px;
}

.slider-control input[type="range"] {
  width: 100%;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
}

.toggle-row input {
  width: 18px;
  height: 18px;
}

.shortcuts p {
  margin: 0;
  color: var(--muted);
  line-height: 1.6;
}

kbd {
  display: inline-block;
  padding: 3px 7px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.14);
  background: rgba(255, 255, 255, 0.82);
  color: var(--text);
  font-size: 11px;
  font-weight: 700;
}

.swatch-wrap {
  min-width: 0;
}

#palette,
#recentColors {
  display: flex;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
  flex-wrap: wrap;
}

#palette li,
#recentColors li {
  list-style: none;
}

#palette button,
#recentColors button {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 3px solid transparent;
  box-shadow: inset 0 0 0 2px #111111;
  cursor: pointer;
}

#palette button.active,
#recentColors button.active {
  transform: scale(1.08);
  border-color: rgba(0, 0, 0, 0.22);
}

.color-tools {
  display: grid;
  grid-template-columns: minmax(90px, 1fr);
  gap: 8px;
}

#eyedropperButton {
  width: 100%;
  cursor: pointer;
  font-weight: 700;
}

#tools {
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(1240px, calc(100vw - 28px));
  padding: 10px 12px;
  display: flex;
  gap: 10px;
  list-style: none;
  align-items: stretch;
  overflow-x: auto;
  margin: 0;
}

#tools li {
  list-style: none;
  flex: 0 0 auto;
}

.tool-button {
  width: 88px;
  min-height: 90px;
  padding: 8px 6px 10px;
  border-radius: 16px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  font-family: "Fira Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.tool-button.active {
  background: rgba(0, 0, 0, 0.07);
  border-color: rgba(0, 0, 0, 0.12);
}

.tool-button .icon {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 999px;
}

.tool-button .icon img {
  width: 52px;
  height: 52px;
  object-fit: contain;
}

.text-icon {
  font-size: 28px;
  font-weight: 800;
  line-height: 1;
}

.compact-tool,
.tool-button.action {
  width: 74px;
}

#intro {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(236, 236, 236, 0.92);
  padding: 20px;
}

#intro.hidden {
  display: none;
}

.intro-card {
  width: min(620px, calc(100vw - 40px));
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid rgba(0, 0, 0, 0.1);
  box-shadow: var(--shadow);
  border-radius: 26px;
  padding: 28px;
  text-align: center;
}

.intro-card h1 {
  margin: 8px 0 12px;
  font-size: clamp(32px, 5vw, 54px);
  line-height: 0.95;
}

.intro-card p {
  margin: 0 auto 18px;
  max-width: 50ch;
  color: var(--muted);
  line-height: 1.5;
}

#scribble {
  margin: 0 auto 18px;
  width: min(320px, 100%);
}

#scribble path {
  stroke-dasharray: 600;
  stroke-dashoffset: 600;
  animation: draw 4.6s linear infinite;
}

#startButton {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 180px;
  min-height: 46px;
  padding: 0 18px;
  border-radius: 999px;
  background: #111111;
  color: #ffffff;
  cursor: pointer;
  font-weight: 800;
}

@keyframes draw {
  0% { stroke-dashoffset: 600; }
  50% { stroke-dashoffset: 0; }
  100% { stroke-dashoffset: -600; }
}

@media (max-width: 1180px) {
  #topBar {
    grid-template-columns: 1fr 1fr;
    justify-items: stretch;
  }

  .color-tools {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
  }
}

@media (max-width: 1100px) {
  #sidePanel {
    top: auto;
    bottom: 118px;
    max-height: min(52vh, 560px);
  }

  #compactToggleWrap {
    top: auto;
    bottom: 118px;
  }
}

@media (max-width: 760px) {
  #topBar {
    width: calc(100vw - 16px);
    top: max(8px, env(safe-area-inset-top));
    padding: 10px;
    grid-template-columns: 1fr;
  }

  #sidePanel,
  #compactToggleWrap {
    right: 8px;
    width: calc(100vw - 16px);
  }

  #tools {
    width: calc(100vw - 16px);
    bottom: max(8px, env(safe-area-inset-bottom));
    padding: 8px;
    gap: 8px;
  }

  .tool-button,
  .compact-tool,
  .tool-button.action {
    width: 70px;
    min-height: 84px;
    font-size: 11px;
  }

  .tool-button .icon,
  .tool-button .icon img {
    width: 46px;
    height: 46px;
  }

  .stack.two-col,
  .color-tools {
    grid-template-columns: 1fr;
  }
}
