:root {
  --ink: #121212;
  --desk: #050505;
  --desk-light: #151515;
  --paper: #fffdf4;
  --paper-shadow: #d4d0c1;
  --panel: #fff7ce;
  --panel-dark: #302717;
  --accent: #ffcf47;
  --pink: #ff83ad;
  --green: #6bd36f;
  --blue: #5fa9ff;
  --pixel-border: 4px solid var(--ink);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: "Courier New", "Microsoft YaHei", monospace;
  background:
    linear-gradient(45deg, #181818 25%, transparent 25%) 0 0 / 18px 18px,
    linear-gradient(45deg, transparent 75%, #181818 75%) 0 0 / 18px 18px,
    #0c0c0c;
  overflow: hidden;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
  user-select: none;
}

.game-shell {
  width: 100%;
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 18px;
}

.pixel-table {
  position: relative;
  width: min(1120px, 100%);
  height: min(720px, calc(100vh - 36px));
  min-height: 560px;
  display: grid;
  grid-template-columns: minmax(148px, 190px) 1fr;
  gap: 24px;
  padding: 24px;
  border: 6px solid #262626;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.05) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(0deg, rgba(255, 255, 255, 0.04) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(135deg, var(--desk), var(--desk-light));
  box-shadow:
    0 0 0 6px #000,
    0 18px 0 #000;
  image-rendering: pixelated;
}

.tool-dock {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 18px 12px;
  border: var(--pixel-border);
  background: #1f1f1f;
  box-shadow: inset -6px -6px 0 #080808, inset 6px 6px 0 #363636;
  min-width: 0;
}

.game-title {
  position: absolute;
  left: 28px;
  top: -18px;
  z-index: 2;
  padding: 7px 12px;
  border: var(--pixel-border);
  background: #ffd96a;
  font-size: clamp(15px, 2vw, 20px);
  font-weight: 900;
  box-shadow: 5px 5px 0 #000;
  white-space: nowrap;
}

.tool-button,
.brush-choice,
.page-button,
.big-leaf-choice,
.target-color-button,
#playAgainButton {
  border: var(--pixel-border);
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 5px 0 #000;
}

.tool-button:active,
.brush-choice:active,
.page-button:active,
.big-leaf-choice:active,
.target-color-button:active,
#playAgainButton:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #000;
}

.tool-button.active,
.brush-choice.active {
  background: #fff;
  outline: 4px solid var(--accent);
}

.tool-button {
  width: 74px;
  height: 74px;
  display: grid;
  place-items: center;
}

.color-orb {
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 27%, #fff 0 6%, transparent 7%),
    conic-gradient(#ff4d4d, #ffd84a, #5ee06b, #4f9dff, #a66cff, #ff4d4d);
}

.color-orb.active {
  background:
    radial-gradient(circle at 32% 27%, #fff 0 6%, transparent 7%),
    conic-gradient(#ff4d4d, #ffd84a, #5ee06b, #4f9dff, #a66cff, #ff4d4d);
}

.eraser-icon {
  width: 44px;
  height: 30px;
  display: block;
  background: linear-gradient(135deg, #fff 0 55%, #ffb4c9 56%);
  border: 4px solid var(--ink);
  transform: rotate(-18deg);
  box-shadow: 4px 4px 0 #8a8a8a;
}

.leaf-icon {
  width: 42px;
  height: 22px;
  display: block;
  position: relative;
  background: var(--green);
  border: 4px solid var(--ink);
  border-radius: 100% 0 100% 0;
  transform: rotate(-35deg);
}

.leaf-icon::after {
  content: "";
  position: absolute;
  left: 7px;
  top: 8px;
  width: 26px;
  height: 4px;
  background: var(--ink);
  transform: rotate(16deg);
}

.leaf-icon.large {
  width: 90px;
  height: 48px;
}

.leaf-icon.large::after {
  left: 18px;
  top: 21px;
  width: 56px;
}

.brush-list {
  width: 100%;
  display: grid;
  gap: 10px;
  margin-top: 2px;
}

.brush-choice {
  width: 100%;
  min-height: 42px;
  padding: 6px 4px;
  font-size: clamp(13px, 1.9vw, 16px);
  font-weight: 700;
  line-height: 1.1;
}

.board-wrap {
  min-width: 0;
  display: grid;
  align-items: center;
  justify-items: end;
}

.board-frame {
  position: relative;
  width: min(100%, 820px);
  aspect-ratio: 4 / 3;
  padding: 18px;
  border: 6px solid #111;
  background: var(--paper-shadow);
  box-shadow:
    inset -10px -10px 0 #a9a494,
    inset 10px 10px 0 #fff7da,
    12px 12px 0 #000;
}

#paintCanvas {
  display: block;
  width: 100%;
  height: 100%;
  background: var(--paper);
  border: var(--pixel-border);
  touch-action: none;
  image-rendering: pixelated;
}

.brush-cursor {
  position: fixed;
  left: 0;
  top: 0;
  width: 16px;
  height: 16px;
  border: 3px solid #111;
  background: rgba(255, 255, 255, 0.45);
  pointer-events: none;
  transform: translate(-999px, -999px);
  z-index: 20;
  mix-blend-mode: multiply;
}

.page-button {
  position: absolute;
  right: 26px;
  bottom: 26px;
  width: 52px;
  height: 48px;
  font-size: 24px;
  font-weight: 900;
  background: var(--accent);
}

.popover {
  position: absolute;
  z-index: 30;
  display: none;
  padding: 14px;
  border: var(--pixel-border);
  background: var(--panel);
  box-shadow: 8px 8px 0 #000;
}

.popover.open {
  display: block;
}

.color-popover {
  left: 126px;
  top: 24px;
  width: min(330px, calc(100vw - 36px));
}

.eraser-popover {
  left: 126px;
  top: 116px;
  width: min(310px, calc(100vw - 36px));
}

.leaf-popover {
  left: 126px;
  top: 206px;
  width: 180px;
  display: none;
  place-items: center;
}

.leaf-popover.open {
  display: grid;
}

.hue-field {
  position: relative;
  height: 170px;
  border: var(--pixel-border);
  background:
    linear-gradient(180deg, #ff3030 0%, #ff9b2f 18%, #fff04d 34%, #8ee94f 52%, #26c96f 66%, #34b4ff 83%, #324dff 100%);
}

.hue-picker {
  position: absolute;
  left: 50%;
  top: 0;
  width: 22px;
  height: 22px;
  border: 4px solid #111;
  background: #fff;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.55);
}

.preview-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0;
}

.paint-preview {
  flex: 1;
  height: 34px;
  border: var(--pixel-border);
  background: #000;
}

.target-color-button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #2f80ff;
}

.hidden-color-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.slider-row {
  display: grid;
  grid-template-columns: 48px 1fr 38px;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-weight: 700;
}

.gradient-row {
  grid-template-columns: 48px 1fr 36px 38px;
}

.slider-row input[type="range"] {
  width: 100%;
  accent-color: #ffcf47;
}

.slider-row output {
  min-width: 34px;
  text-align: right;
}

.big-leaf-choice {
  width: 130px;
  height: 94px;
  display: grid;
  place-items: center;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  z-index: 50;
  min-width: 180px;
  padding: 12px 16px;
  border: var(--pixel-border);
  background: #fff;
  text-align: center;
  font-weight: 900;
  box-shadow: 6px 6px 0 #000;
  transform: translate(-50%, 120px);
  transition: transform 160ms steps(2);
}

.toast.show {
  transform: translate(-50%, 0);
}

.win-screen {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: none;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.78);
}

.win-screen.show {
  display: grid;
}

.win-panel {
  width: min(520px, 100%);
  padding: 30px 22px;
  border: 6px solid #111;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.18) 1px, transparent 1px) 0 0 / 16px 16px,
    #ffe680;
  text-align: center;
  box-shadow: 10px 10px 0 #000;
}

.win-panel h1 {
  margin: 0 0 22px;
  font-size: clamp(24px, 5vw, 44px);
  line-height: 1.18;
}

#playAgainButton {
  padding: 10px 18px;
  font-size: 18px;
  font-weight: 900;
  background: #fff;
}

@media (max-width: 760px) {
  body {
    overflow: auto;
  }

  .game-shell {
    align-items: start;
    padding: 12px;
  }

  .pixel-table {
    height: auto;
    min-height: 0;
    grid-template-columns: 1fr;
    gap: 16px;
    padding: 14px;
  }

  .tool-dock {
    display: grid;
    grid-template-columns: repeat(3, 62px) 1fr;
    align-items: start;
    gap: 10px;
    padding: 12px;
  }

  .tool-button {
    width: 62px;
    height: 62px;
  }

  .brush-list {
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin-top: 0;
  }

  .board-wrap {
    justify-items: stretch;
  }

  .board-frame {
    padding: 10px;
  }

  .page-button {
    right: 16px;
    bottom: 16px;
    width: 46px;
    height: 42px;
  }

  .color-popover,
  .eraser-popover,
  .leaf-popover {
    left: 14px;
    top: 98px;
  }
}
