:root {
  --ink: #1d2935;
  --muted: #5f6c7a;
  --paper: #fffdf7;
  --paper-edge: #f1d58b;
  --panel: rgba(255, 255, 255, 0.88);
  --panel-strong: rgba(255, 255, 255, 0.96);
  --line: rgba(29, 41, 53, 0.16);
  --shadow: 0 22px 60px rgba(45, 57, 75, 0.2);
  --accent: #f05d5e;
  --blue: #2176ff;
  --green: #2db67d;
  --yellow: #ffc857;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  color: var(--ink);
  font-family:
    "Segoe UI",
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    sans-serif;
  background:
    linear-gradient(120deg, rgba(255, 253, 247, 0.52), rgba(225, 244, 255, 0.34)),
    url("assets/art-desk-bg.png") center / cover fixed no-repeat;
}

button {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  padding: 16px;
  gap: 12px;
}

.topbar {
  width: min(1320px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(240px, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 12px 38px rgba(45, 57, 75, 0.14);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  border: 3px solid var(--ink);
  border-radius: 8px;
  background:
    linear-gradient(135deg, #f05d5e 0 30%, transparent 30%),
    linear-gradient(45deg, #2176ff 0 45%, #ffc857 45% 72%, #2db67d 72%);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.7);
}

.brand h1 {
  margin: 0;
  font-size: 1.2rem;
  line-height: 1.1;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  min-height: 1.25em;
}

.top-actions {
  display: flex;
  gap: 8px;
}

.shape-picker {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  overflow-y: auto;
  padding: 2px 4px 2px 2px;
  max-height: calc(100vh - 152px);
  scrollbar-width: thin;
}

.shape-button,
.region-button,
.pencil-button,
.more-colors-button,
.install-button,
.icon-button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--ink);
  cursor: pointer;
  transition:
    transform 140ms ease,
    border-color 140ms ease,
    box-shadow 140ms ease,
    background 140ms ease;
}

.shape-button {
  min-width: 0;
  min-height: 62px;
  display: grid;
  grid-template-columns: 44px 1fr;
  align-items: center;
  gap: 7px;
  padding: 7px 9px;
  font-weight: 700;
  white-space: nowrap;
  text-align: left;
}

.install-button {
  min-width: 104px;
  height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 12px;
  font-weight: 850;
}

.install-button svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button svg {
  width: 100%;
  height: 100%;
}

.shape-preview {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: #fffdf7;
}

.shape-button.is-active,
.region-button.is-active,
.pencil-button.is-active {
  border-color: rgba(33, 118, 255, 0.55);
  box-shadow: 0 0 0 3px rgba(33, 118, 255, 0.16);
  background: #f8fbff;
}

.shape-button:hover,
.region-button:hover,
.pencil-button:hover,
.more-colors-button:hover,
.install-button:hover,
.icon-button:hover {
  transform: translateY(-1px);
}

.workbench {
  width: min(1320px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 318px;
  gap: 12px;
  align-items: stretch;
}

.library-panel,
.canvas-zone,
.tool-panel {
  min-width: 0;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  background: rgba(255, 255, 255, 0.74);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.library-panel,
.tool-panel {
  align-self: stretch;
  max-height: calc(100vh - 104px);
  overflow: hidden;
}

.library-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
}

.panel-heading {
  min-height: 34px;
  display: flex;
  align-items: center;
  padding: 0 4px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 850;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.canvas-zone {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  align-self: stretch;
}

#gameCanvas {
  display: block;
  width: 100%;
  height: min(calc(100vh - 160px), 690px);
  min-height: 430px;
  border-radius: 8px;
  border: 2px solid var(--paper-edge);
  background: var(--paper);
  touch-action: none;
  user-select: none;
  box-shadow: inset 0 0 0 7px rgba(255, 243, 201, 0.62);
}

.progress-shell {
  height: 9px;
  margin-top: 10px;
  border-radius: 999px;
  background: rgba(29, 41, 53, 0.12);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #f05d5e, #ffc857, #2db67d, #2176ff);
  transition: width 180ms ease;
}

.panel-section {
  display: grid;
  gap: 12px;
}

.tool-panel {
  display: grid;
  align-content: start;
  gap: 12px;
  overflow-y: auto;
  box-shadow: 0 16px 44px rgba(45, 57, 75, 0.16);
  scrollbar-width: thin;
}

.mode-chip {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #fff7ea;
  font-weight: 800;
}

.mode-chip.color-mode {
  background: #eefaf6;
}

.chip-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 18px 0 0 var(--yellow), 36px 0 0 var(--green);
}

.part-stack,
.region-picker {
  display: grid;
  gap: 8px;
}

.part-item {
  display: grid;
  grid-template-columns: 24px 1fr;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.7);
  color: var(--muted);
  font-weight: 700;
}

.part-bullet {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px dashed rgba(29, 41, 53, 0.34);
}

.part-item.is-active {
  color: var(--ink);
  border-color: rgba(240, 93, 94, 0.42);
  background: #fff9f5;
}

.part-item.is-done .part-bullet {
  border-style: solid;
  border-color: var(--green);
  background: var(--green);
  box-shadow: inset 0 0 0 4px #fff;
}

.palette {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 7px;
}

.pencil-button {
  height: 52px;
  padding: 0;
  position: relative;
  overflow: hidden;
  background: #fffdf7;
}

.pencil-button::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 8px;
  width: 12px;
  height: 36px;
  transform: translateX(-50%);
  border-radius: 5px 5px 2px 2px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.42), transparent 28% 72%, rgba(0, 0, 0, 0.12)),
    var(--pencil-color);
  box-shadow: inset 0 0 0 1px rgba(29, 41, 53, 0.12);
}

.pencil-button::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 5px;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 13px solid #d7a66d;
}

.pencil-tip {
  position: absolute;
  left: 50%;
  bottom: 5px;
  z-index: 2;
  width: 0;
  height: 0;
  transform: translateX(-50%) translateY(7px);
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 7px solid var(--pencil-color);
}

.pencil-body {
  display: none;
}

.guide-card {
  min-height: 138px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(255, 248, 223, 0.65), rgba(240, 249, 255, 0.74)),
    #fffdf7;
}

#guideCanvas {
  display: block;
  width: 100%;
  max-width: 230px;
  height: auto;
}

.more-colors-button {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 10px;
  font-weight: 800;
}

.more-colors-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
}

.more-colors-button.is-active {
  border-color: rgba(45, 182, 125, 0.5);
  box-shadow: 0 0 0 3px rgba(45, 182, 125, 0.14);
}

.region-button {
  min-height: 70px;
  padding: 6px;
  display: grid;
  place-items: center;
}

.region-picker {
  grid-template-columns: repeat(3, 1fr);
}

.region-preview {
  display: block;
  width: 100%;
  max-width: 76px;
  height: 58px;
}

.icon-button {
  width: 46px;
  height: 46px;
  padding: 12px;
}

.icon-button svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.mobile-toolbar,
.mobile-panel-backdrop {
  display: none;
}

.is-hidden {
  display: none;
}

@media (max-width: 860px) {
  html,
  body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
  }

  body {
    position: fixed;
    inset: 0;
    touch-action: none;
  }

  .app-shell {
    width: 100vw;
    height: 100dvh;
    min-height: 0;
    display: block;
    padding: 0;
    overflow: hidden;
  }

  .topbar {
    display: none;
  }

  .workbench {
    width: 100vw;
    height: 100dvh;
    display: block;
    margin: 0;
  }

  .library-panel,
  .tool-panel {
    position: fixed;
    z-index: 30;
    left: 10px;
    right: 10px;
    bottom: calc(74px + env(safe-area-inset-bottom));
    max-height: min(72dvh, 520px);
    display: grid;
    overflow: hidden;
    padding: 10px;
    border-radius: 8px;
    transform: translateY(calc(100% + 96px));
    opacity: 0;
    pointer-events: none;
    transition:
      transform 180ms ease,
      opacity 180ms ease;
  }

  .mobile-library-open .library-panel,
  .mobile-tools-open .tool-panel {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  .shape-picker {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-flow: row;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: min(58dvh, 430px);
    padding: 2px;
  }

  .shape-button {
    min-width: 0;
    min-height: 58px;
    grid-template-columns: 38px minmax(0, 1fr);
    white-space: normal;
    font-size: 0.84rem;
  }

  .shape-preview {
    width: 38px;
    height: 38px;
  }

  .canvas-zone {
    width: 100vw;
    height: 100dvh;
    min-height: 0;
    display: grid;
    grid-template-rows: minmax(0, 1fr) 7px;
    padding: 6px 6px calc(72px + env(safe-area-inset-bottom));
    border: 0;
    border-radius: 0;
    box-shadow: none;
    background: rgba(255, 255, 255, 0.48);
  }

  #gameCanvas {
    width: 100%;
    height: 100%;
    min-height: 0;
    aspect-ratio: auto;
    border-radius: 8px;
    touch-action: none;
  }

  .progress-shell {
    height: 7px;
    margin-top: 5px;
  }

  .tool-panel {
    align-content: start;
    overflow-y: auto;
  }

  .trace-panel {
    gap: 8px;
  }

  .part-stack {
    grid-template-columns: 1fr;
  }

  .part-item {
    min-height: 32px;
    padding: 6px;
    font-size: 0.86rem;
  }

  .guide-card {
    min-height: 96px;
  }

  #guideCanvas {
    max-width: 170px;
  }

  .palette {
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
  }

  .pencil-button {
    height: 44px;
  }

  .region-picker {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .region-button {
    min-height: 58px;
  }

  .region-preview {
    height: 48px;
  }

  .mobile-panel-backdrop {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: block;
    background: rgba(29, 41, 53, 0.18);
    opacity: 0;
    pointer-events: none;
    transition: opacity 160ms ease;
  }

  .mobile-library-open .mobile-panel-backdrop,
  .mobile-tools-open .mobile-panel-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .mobile-toolbar {
    position: fixed;
    z-index: 40;
    left: 10px;
    right: 10px;
    bottom: max(10px, env(safe-area-inset-bottom));
    min-height: 54px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    padding: 8px;
    border: 1px solid rgba(255, 255, 255, 0.75);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 12px 36px rgba(29, 41, 53, 0.18);
    backdrop-filter: blur(14px);
  }

  .mobile-tool-button {
    min-width: 0;
    height: 42px;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fffdf7;
    color: var(--ink);
  }

  .mobile-tool-button svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.3;
    stroke-linecap: round;
    stroke-linejoin: round;
  }

  .mobile-library-open #mobileObjectButton,
  .mobile-tools-open #mobileToolButton,
  .mobile-tool-button.is-color-ready {
    border-color: rgba(33, 118, 255, 0.55);
    box-shadow: 0 0 0 3px rgba(33, 118, 255, 0.15);
    background: #f8fbff;
  }

  .is-hidden {
    display: none !important;
  }
}

@media (max-width: 520px) {
  .shape-button {
    grid-template-columns: 36px 1fr;
    min-height: 56px;
    padding: 7px 8px;
    font-size: 0.82rem;
  }

  .shape-preview {
    width: 36px;
    height: 36px;
  }

  .palette {
    grid-template-columns: repeat(6, 1fr);
  }

  .region-picker {
    grid-template-columns: repeat(2, 1fr);
  }
}
