:root {
  color-scheme: light;
  --ink: #15365f;
  --ink-soft: #5b6f84;
  --paper: #fffefa;
  --grid: #7bd9f3;
  --yellow: #ffd84d;
  --button-shadow: 4px 4px 0 rgba(21, 54, 95, 0.94);
  --panel-shadow: 5px 5px 0 rgba(21, 54, 95, 0.9), 0 16px 36px rgba(21, 54, 95, 0.2);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-width: 320px;
  min-height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  background: #fff;
  color: var(--ink);
  font-family: "PingFang SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

.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-shell {
  position: relative;
  width: 100vw;
  min-height: 100dvh;
  overflow: hidden;
}

#gridCanvas {
  position: fixed;
  inset: 0;
  display: block;
  width: 100vw;
  height: 100dvh;
  cursor: crosshair;
  touch-action: none;
  user-select: none;
}

#gridCanvas:focus-visible,
button:focus-visible,
input:focus-visible {
  outline: 3px solid #217fd3;
  outline-offset: 3px;
}

.floating-tools {
  position: fixed;
  left: clamp(14px, 2.2vw, 30px);
  bottom: clamp(14px, 2.4vw, 26px);
  z-index: 20;
  display: flex;
  max-width: calc(100vw - 126px);
  align-items: center;
  gap: 10px;
  padding: 0;
  flex-wrap: wrap;
}

.saved-workspaces {
  position: fixed;
  right: 12px;
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 21;
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  width: 76px;
}

.saved-workspaces .tool-button {
  width: 76px;
  height: 48px;
  min-height: 48px;
  padding: 0;
  flex: 0 0 48px;
}

.saved-workspace-cards {
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  max-height: calc(100dvh - 130px);
  overflow-y: auto;
  padding: 3px;
  margin: -3px;
}

.saved-workspace-card {
  overflow: hidden;
  border-radius: 8px;
}

.saved-workspace-card img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.saved-workspace-card[aria-pressed="true"] {
  border-color: #f47c61;
  box-shadow: 0 0 0 2px #f47c61;
}

.tool-button,
.icon-button,
.pen-swatch,
.secondary-button,
.danger-button {
  border: 2px solid var(--ink);
  background: #fff;
  cursor: pointer;
  font-weight: 1000;
  transition: transform 150ms ease, box-shadow 150ms ease, background 150ms ease, opacity 150ms ease;
}

.tool-button,
.icon-button {
  min-height: 48px;
  box-shadow: var(--button-shadow);
}

.tool-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 18px;
  border-radius: 14px;
  font-size: 16px;
}

.icon-button {
  display: grid;
  width: 50px;
  height: 48px;
  place-items: center;
  padding: 0;
  border-radius: 14px;
}

.tool-button:hover:not(:disabled),
.icon-button:hover:not(:disabled),
.pen-swatch:hover:not(:disabled),
.secondary-button:hover,
.danger-button:hover {
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0 rgba(21, 54, 95, 0.94);
}

.tool-button[aria-pressed="true"],
.icon-button[aria-pressed="true"] {
  background: var(--yellow);
}

.icon-button svg {
  display: block;
  width: 25px;
  height: 25px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.25;
}

.help-button span {
  display: block;
  font-size: 25px;
  font-weight: 1000;
  line-height: 1;
}

.view-button span {
  display: block;
  font-size: 12px;
  font-weight: 1000;
  letter-spacing: 0;
  line-height: 1;
}

.clear-button {
  background: #ffe8e0;
}

.pen-cluster {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 48px;
  padding: 4px;
  border: 2px solid var(--ink);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--button-shadow);
}

.pen-cluster .icon-button,
.pen-cluster .pen-swatch {
  box-shadow: none;
}

.pen-cluster .icon-button:hover:not(:disabled),
.pen-cluster .pen-swatch:hover:not(:disabled) {
  box-shadow: none;
}

.pen-swatch {
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  padding: 0;
  border-radius: 50%;
}

.pen-swatch.red {
  background: #e84855;
}

.pen-swatch.green {
  background: #25a95f;
}

.pen-swatch.blue {
  background: #247ce8;
}

.pen-swatch.is-selected {
  outline: 3px solid var(--yellow);
  outline-offset: 3px;
}

.icon-button:disabled,
.tool-button:disabled,
.pen-swatch:disabled {
  cursor: not-allowed;
  opacity: 0.42;
  transform: none;
}

.fold-timeline {
  position: fixed;
  z-index: 18;
  display: flex;
  align-items: center;
  border: 2px solid var(--ink);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--button-shadow);
}

.fold-timeline {
  left: calc(50% - 44px);
  bottom: calc(clamp(14px, 2.4vw, 26px) + 68px);
  display: grid;
  grid-template-columns: 22px minmax(150px, 330px) 22px 42px;
  width: min(450px, calc(100vw - 116px));
  min-height: 48px;
  padding: 9px 12px;
  gap: 9px;
  border-radius: 16px;
  transform: translateX(-50%);
  transition: opacity 150ms ease;
}

.fold-timeline.is-disabled {
  opacity: 0.48;
}

.timeline-end {
  position: relative;
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
}

.timeline-end::before {
  display: block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--ink);
  background: #dff3f8;
  content: "";
}

.timeline-end.folded::before {
  width: 12px;
  background: #ffd84d;
  transform: skewY(-16deg);
}

.fold-timeline output {
  color: var(--ink-soft);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 900;
  text-align: right;
}

input[type="range"] {
  width: 100%;
  height: 7px;
  margin: 0;
  border-radius: 999px;
  appearance: none;
  background: linear-gradient(to right, #f47c61 var(--progress, 0%), #d6ecf5 var(--progress, 0%));
  cursor: pointer;
}

input[type="range"]:disabled {
  cursor: not-allowed;
}

input[type="range"]::-webkit-slider-thumb {
  width: 20px;
  height: 20px;
  border: 3px solid #fff;
  border-radius: 50%;
  appearance: none;
  background: #f47c61;
  box-shadow: 0 2px 6px rgba(21, 54, 95, 0.28);
}

input[type="range"]::-moz-range-thumb {
  width: 15px;
  height: 15px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: #f47c61;
  box-shadow: 0 2px 6px rgba(21, 54, 95, 0.28);
}

.bubble {
  position: fixed;
  left: var(--bubble-left, 50vw);
  top: var(--bubble-top, 50vh);
  z-index: 30;
  width: max-content;
  max-width: calc(100vw - 24px);
  padding: 12px;
  border: 2px solid var(--ink);
  border-radius: 17px;
  background: var(--paper);
  box-shadow: var(--panel-shadow);
}

.bubble::after {
  position: absolute;
  left: var(--tail-x, 50%);
  top: var(--tail-y, 100%);
  width: 16px;
  height: 16px;
  border: 2px solid var(--ink);
  border-top: 0;
  border-left: 0;
  background: var(--paper);
  content: "";
  transform: translate(-50%, -8px) rotate(45deg);
}

.bubble[data-placement="below"]::after {
  top: 0;
  border: 2px solid var(--ink);
  border-right: 0;
  border-bottom: 0;
  transform: translate(-50%, -8px) rotate(45deg);
}

.bubble-title,
.help-title {
  margin: 0 0 9px;
  font-size: 15px;
  font-weight: 1000;
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(3, 42px);
  gap: 10px;
}

.color-choice {
  width: 42px;
  height: 42px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 150ms ease, box-shadow 150ms ease;
}

.color-choice:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(21, 54, 95, 0.18);
}

.color-choice.green { background: #d7ec8b; }
.color-choice.purple { background: #c8c6df; }
.color-choice.yellow { background: #fff7bd; }
.color-choice.blue { background: #dff3f8; }
.color-choice.pink { background: #f5d1e2; }
.color-choice.blank {
  background:
    linear-gradient(45deg, rgba(130, 145, 163, 0.25) 25%, transparent 25% 75%, rgba(130, 145, 163, 0.25) 75%),
    linear-gradient(45deg, rgba(130, 145, 163, 0.25) 25%, transparent 25% 75%, rgba(130, 145, 163, 0.25) 75%);
  background-position: 0 0, 8px 8px;
  background-size: 16px 16px;
  background-color: #fff;
}

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

.secondary-button,
.danger-button {
  min-width: 66px;
  min-height: 38px;
  padding: 0 14px;
  border-radius: 12px;
  box-shadow: 3px 3px 0 rgba(21, 54, 95, 0.94);
}

.danger-button {
  background: #ffe0e0;
}

.help-bubble {
  width: min(360px, calc(100vw - 24px));
}

.help-content {
  display: grid;
  gap: 10px;
}

.help-content section {
  margin: 0;
}

.help-content h2 {
  margin: 0 0 4px;
  font-size: 14px;
}

.help-content p {
  margin: 0;
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.55;
}

@media (max-width: 700px) {
  .floating-tools {
    left: 12px;
    right: 100px;
    bottom: max(12px, env(safe-area-inset-bottom));
    max-width: none;
    gap: 7px;
  }

  .tool-button {
    min-height: 44px;
    padding: 0 13px;
    font-size: 14px;
  }

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

  .pen-cluster {
    min-height: 44px;
    gap: 5px;
  }

  .pen-swatch {
    width: 26px;
    height: 26px;
  }

  .fold-timeline {
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 112px);
    grid-template-columns: 20px minmax(0, 1fr) 20px 38px;
  }

}

@media (max-width: 430px) {
  .floating-tools {
    gap: 5px;
  }

  .tool-button {
    min-height: 40px;
    padding: 0 10px;
    font-size: 13px;
  }

  .icon-button {
    width: 40px;
    height: 40px;
  }

  .pen-cluster {
    min-height: 40px;
    gap: 4px;
    padding: 3px;
  }

  .pen-swatch {
    width: 23px;
    height: 23px;
  }

  .fold-timeline {
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 148px);
    padding: 8px;
    gap: 6px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    scroll-behavior: auto !important;
  }
}

.regular-polygon-bubble { width: min(320px, calc(100vw - 24px)); }
.polygon-choices { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.regular-polygon-bubble p { margin: 12px 0 0; font-size: 13px; color: #536780; }
