:root {
  color-scheme: dark;
  --bg: #1a1c22;
  --panel: #0c0e13;
  --panel-high: #12151c;
  --line: #262a33;
  --line-strong: #3a404c;
  --text: #f6f7f9;
  --muted: #9299a8;
  --red: #ff3b0a;
  --blue: #7892f3;
  --cyan: #25b6ff;
  --yellow: #f6cb42;
  --edge: 20px;
  --fab-height: 44px;
  --fab-gap: 12px;
  --card-width: 360px;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
  min-width: 320px;
  overflow: hidden;
  background: var(--bg);
  /* Disable double-tap zoom so rapid taps on the menu button do not zoom; pinch zoom still works. */
  touch-action: manipulation;
}

body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 0%, rgb(37 182 255 / 8%), transparent 29rem),
    radial-gradient(circle at 88% 5%, rgb(255 59 10 / 7%), transparent 25rem),
    var(--bg);
}

button,
input,
select {
  font: inherit;
}

button,
select,
input[type="range"],
input[type="checkbox"],
input[type="color"] {
  cursor: pointer;
}

.workspace {
  --stage-size: min(
    620px,
    calc(100dvh - 2 * (var(--edge) + var(--fab-height) + var(--fab-gap))),
    max(440px, calc(100vw - 2 * (var(--card-width) + 2 * var(--edge)))),
    calc(100vw - 2 * var(--edge))
  );
  position: relative;
  height: 100dvh;
  overflow: hidden;
}

.preview-panel {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  transition: bottom 240ms ease;
}

.controls-heading,
.export-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
}

.eyebrow {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font: 650 0.72rem/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.14em;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.65rem, 3vw, 2.55rem);
  line-height: 1.08;
  letter-spacing: -0.045em;
}

h2 {
  margin-bottom: 0;
  font-size: 1.25rem;
  letter-spacing: -0.025em;
}

h3 {
  margin-bottom: 13px;
  font-size: 0.92rem;
  letter-spacing: -0.01em;
}

input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--blue);
}

.stage-shell {
  position: relative;
  width: var(--stage-size);
  aspect-ratio: 1;
  transition: width 240ms ease;
}

/* Checkerboard shows where the export will be transparent when the background layer is off. */
.stage-shell.is-transparent {
  background: repeating-conic-gradient(rgb(255 255 255 / 7%) 0% 25%, transparent 0% 50%) 0 0 / 24px 24px;
}

#maru-icon {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: 100%;
}

.controls-panel,
.theme-panel {
  position: fixed;
  right: var(--edge);
  bottom: calc(var(--edge) + var(--fab-height) + var(--fab-gap));
  z-index: 20;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  width: var(--card-width);
  height: min(720px, calc(100dvh - 2 * var(--edge) - var(--fab-height) - var(--fab-gap)));
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 16px;
  background: rgb(12 14 19 / 94%);
  box-shadow: 0 24px 64px rgb(0 0 0 / 55%);
  backdrop-filter: blur(16px);
  opacity: 0;
  transform: translateY(8px) scale(0.98);
  transform-origin: bottom right;
  visibility: hidden;
  transition:
    opacity 180ms ease,
    transform 180ms ease,
    visibility 0s linear 180ms;
}

/* Themes share the Edit card's corner: the stage keeps that width clear, so the icon stays visible for the hover preview. */
.theme-panel {
  display: block;
  height: auto;
  max-height: min(560px, calc(100dvh - 2 * var(--edge) - var(--fab-height) - var(--fab-gap)));
}

.theme-scroll {
  max-height: inherit;
  padding: 22px 24px 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

/* Theme page content, in the corner opposite the buttons. It has no toggle, so it never leaves the page empty. */
.content-panel {
  position: fixed;
  left: var(--edge);
  bottom: calc(var(--edge) + var(--fab-height) + var(--fab-gap));
  z-index: 15;
  width: var(--card-width);
  max-height: min(560px, calc(100dvh - 2 * var(--edge) - var(--fab-height) - var(--fab-gap)));
  border: 1px solid var(--line-strong);
  border-radius: 16px;
  background: rgb(12 14 19 / 94%);
  box-shadow: 0 24px 64px rgb(0 0 0 / 55%);
  backdrop-filter: blur(16px);
}

.content-scroll {
  max-height: inherit;
  padding: 22px 24px 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

/* The card can be minimized to its title; on phones the icon then takes the space back. */
.content-details > summary {
  cursor: pointer;
}

.content-details h2 {
  margin: 0;
}

.content-details[open] > summary {
  margin-bottom: 10px;
}

.content-panel .geometry-note {
  margin-bottom: 12px;
}

.theme-options,
.work-list {
  display: grid;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Faces with a short label underneath, stacked from the bottom-right corner (nearest the button) leftwards
   and upwards, so a new theme appears next to the button and older ones move up. */
.theme-options {
  --theme-columns: 3;
  --theme-gap: 6px;
  display: flex;
  flex-direction: row-reverse;
  flex-wrap: wrap-reverse;
  gap: var(--theme-gap);
}

.theme-options li {
  flex: 0 0 calc((100% - (var(--theme-columns) - 1) * var(--theme-gap)) / var(--theme-columns));
}

.theme-option {
  display: grid;
  justify-items: center;
  gap: 7px;
  padding: 10px 6px 9px;
  border-radius: 10px;
  color: var(--text);
  text-decoration: none;
}

.theme-option:hover,
.theme-option:focus-visible {
  background: var(--panel-high);
}

.theme-option[aria-current="page"] {
  background: var(--panel-high);
  box-shadow: inset 0 0 0 1px var(--line-strong);
}

.theme-option img {
  display: block;
  width: 56px;
  height: 56px;
}

.theme-option strong {
  display: block;
  font-size: 0.76rem;
  font-weight: 600;
  line-height: 1.3;
  text-align: center;
}

.work-list {
  font-size: 0.86rem;
}

.work-row,
.work-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 36px;
  padding: 0 8px;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
}

.work-link:hover,
.work-link:focus-visible {
  background: var(--panel-high);
}

.work-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Works without a public URL are listed but not clickable, so only links carry the arrow. */
.work-arrow {
  flex-shrink: 0;
  width: 15px;
  height: 15px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.work-link:hover .work-arrow,
.work-link:focus-visible .work-arrow {
  stroke: var(--text);
}

.is-themes-open .theme-panel,
.is-controls-open .controls-panel {
  opacity: 1;
  transform: none;
  visibility: visible;
  transition-delay: 0s;
}

.panel-toggle {
  position: fixed;
  right: var(--edge);
  bottom: var(--edge);
  z-index: 30;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  width: var(--fab-height);
  height: var(--fab-height);
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  color: var(--text);
  background: rgb(18 21 28 / 92%);
  box-shadow: 0 12px 32px rgb(0 0 0 / 45%);
  backdrop-filter: blur(12px);
  font-size: 0.88rem;
  font-weight: 650;
  transition: opacity 180ms ease;
}

/* Themes sits next to Edit, and both open the same corner. */
.theme-toggle {
  right: calc(var(--edge) + var(--fab-height) + 8px);
}

/* Small links to X, GitHub, and Zenn, mirroring the buttons on the opposite corner. */
.profile-links {
  position: fixed;
  left: var(--edge);
  bottom: var(--edge);
  z-index: 10;
  display: flex;
  gap: 8px;
}

.profile-links a {
  display: grid;
  place-items: center;
  width: var(--fab-height);
  height: var(--fab-height);
  border-radius: 999px;
  color: var(--muted);
  transition: color 150ms ease, background-color 150ms ease;
}

.profile-links a:hover {
  color: var(--text);
  background: rgb(255 255 255 / 6%);
}

.profile-links svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.panel-toggle:hover {
  background: rgb(26 30 39 / 96%);
}

.panel-toggle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}

.panel-toggle .icon-close,
.panel-toggle[aria-expanded="true"] .icon-open,
.panel-toggle .label-close,
.panel-toggle[aria-expanded="true"] .label-open {
  display: none;
}

.panel-toggle[aria-expanded="true"] .icon-close,
.panel-toggle[aria-expanded="true"] .label-close {
  display: inline;
}

.controls-scroll {
  min-height: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
  padding: 22px 24px 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.controls-heading {
  align-items: center;
  min-height: 32px;
  margin-bottom: 10px;
}

.text-button {
  padding: 0;
  border: 0;
  color: var(--muted);
  background: transparent;
  font-size: 0.78rem;
  line-height: 1.2;
}

.text-button:hover,
.text-button:focus-visible {
  color: var(--text);
}

/* Browsers treat a clicked select as focus-visible, so use a quieter border instead of the ring. */
select:focus-visible {
  outline: none;
  border-color: #91a4ff;
}

.control-section {
  display: block;
  padding: 13px 0;
  border-top: 1px solid var(--line);
}

select {
  min-height: 40px;
  padding: 0 34px 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  color: var(--text);
  /* Custom chevron so the native arrow does not eat into the label width. */
  background: var(--panel-high) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5 6 7.5 9 4.5' fill='none' stroke='%239299a8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 10px center / 12px 12px;
  appearance: none;
}

.layer-list {
  display: grid;
  gap: 4px;
}

.layer-row {
  display: grid;
  grid-template-columns: 18px 12px 1fr auto;
  align-items: center;
  min-height: 32px;
  gap: 10px;
  padding: 0 9px;
  border-radius: 6px;
  font-size: 0.88rem;
}

.layer-row:hover {
  background: var(--panel-high);
}

.layer-row small {
  color: #68707f;
  font: 500 0.67rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}

.layer-swatch,
.dot {
  display: inline-block;
  border-radius: 50%;
}

.layer-swatch {
  width: 10px;
  height: 10px;
  box-shadow: 0 0 7px currentColor;
}

.swatch-black { color: #565b64; background: #050505; border: 1px solid #565b64; }
.swatch-red { color: var(--red); background: var(--red); }
.swatch-blue { color: var(--cyan); background: var(--blue); }
.swatch-white { color: #fff; background: #fff; }
.swatch-guide {
  border-radius: 0;
  box-shadow: none;
  background:
    linear-gradient(var(--yellow), var(--yellow)) center / 100% 1.5px no-repeat,
    linear-gradient(var(--yellow), var(--yellow)) center / 1.5px 100% no-repeat;
}

details summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 650;
  list-style: none;
}

details summary::-webkit-details-marker {
  display: none;
}

details summary::after {
  content: "+";
  color: var(--muted);
  font: 400 1.1rem/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}

details[open] summary {
  margin-bottom: 13px;
}

details[open] summary::after {
  content: "−";
}

.field-grid {
  display: grid;
  gap: 8px;
}

.color-grid {
  grid-template-columns: repeat(5, 1fr);
  margin-bottom: 18px;
}

.color-grid label {
  display: grid;
  grid-template-rows: 2.5em 33px;
  align-items: end;
  justify-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.25;
  text-align: center;
}

input[type="color"] {
  width: 100%;
  height: 33px;
  padding: 3px;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: var(--panel-high);
}

.range-field {
  display: grid;
  gap: 8px;
  margin-top: 13px;
}

.range-field > span {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 0.77rem;
}

.range-field output {
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

input[type="range"] {
  width: 100%;
  height: 4px;
  accent-color: var(--blue);
}

.range-group + .range-group {
  margin-top: 22px;
  padding-top: 17px;
  border-top: 1px dashed var(--line);
}

.geometry-note {
  margin: 0 0 15px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.55;
}

.range-group p {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 0.78rem;
  font-weight: 650;
}

.dot {
  width: 8px;
  height: 8px;
}

.dot-blue { background: var(--blue); box-shadow: 0 0 7px var(--cyan); }
.dot-red { background: var(--red); box-shadow: 0 0 7px var(--red); }

.export-section {
  margin: 0;
  padding: 14px 24px 16px;
  border-top: 1px solid var(--line-strong);
  background: rgb(14 16 22 / 78%);
}

.export-heading {
  align-items: center;
}

.export-heading h2 {
  font-size: 1rem;
}

.export-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) repeat(3, auto);
  gap: 8px;
  margin-top: 10px;
}

.export-select {
  min-width: 0;
}

.export-select select {
  width: 100%;
  height: 40px;
  padding: 0 26px 0 10px;
}

.button {
  min-height: 40px;
  border-radius: 7px;
  font-weight: 720;
  white-space: nowrap;
}

.button[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.4;
}

.button:disabled {
  cursor: wait;
  opacity: 0.5;
}

.button-primary {
  border: 1px solid #91a4ff;
  color: #081022;
  background: #91a4ff;
  box-shadow: 0 0 24px rgb(120 146 243 / 18%);
}

.button-secondary {
  border: 1px solid var(--line-strong);
  color: var(--text);
  background: var(--panel-high);
}

.button:hover:not(:disabled, [aria-disabled="true"]) {
  filter: brightness(1.08);
}

.export-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  padding: 0;
}

.export-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.export-button .icon-done,
.export-button.is-done .icon-idle {
  display: none;
}

.export-button.is-done .icon-done {
  display: inline;
}

.status {
  margin: 8px 0 0;
  color: #ff8c72;
  font-size: 0.72rem;
}

.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;
}

:focus-visible {
  outline: 2px solid var(--yellow);
  outline-offset: 3px;
}

.export-section {
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

@media (max-width: 700px) {
  :root {
    --edge: 16px;
    --fab-bottom: max(var(--edge), env(safe-area-inset-bottom));
  }

  .workspace {
    --sheet-top: min(46dvh, calc(100vw + 8px));
  }

  .workspace.has-content:not(.is-content-collapsed),
  .workspace.is-themes-open,
  .workspace.is-controls-open {
    --stage-size: min(calc(100vw - 2 * var(--edge)), calc(var(--sheet-top) - 24px));
  }

  .workspace.has-content:not(.is-content-collapsed) .preview-panel,
  .workspace.is-themes-open .preview-panel,
  .workspace.is-controls-open .preview-panel {
    bottom: calc(100dvh - var(--sheet-top));
  }

  /* The content sheet sits under the icon; opening a panel covers it. */
  .content-panel {
    top: var(--sheet-top);
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: none;
    border-width: 1px 0 0;
    border-radius: 18px 18px 0 0;
  }

  .content-scroll {
    height: 100%;
    padding: 18px 16px calc(var(--fab-bottom) + var(--fab-height) + 16px);
  }

  .workspace.is-content-collapsed .content-panel {
    top: auto;
  }

  .workspace.is-content-collapsed .content-scroll {
    height: auto;
  }

  .controls-panel,
  .theme-panel {
    top: var(--sheet-top);
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    height: auto;
    border-width: 1px 0 0;
    border-radius: 18px 18px 0 0;
    transform: translateY(32px);
    transform-origin: bottom center;
  }

  .panel-toggle,
  .profile-links {
    bottom: var(--fab-bottom);
  }

  .theme-panel {
    max-height: none;
  }

  /* Keep the last rows clear of the fixed buttons along the bottom edge. */
  .theme-scroll {
    height: 100%;
    padding: 18px 16px calc(var(--fab-bottom) + var(--fab-height) + 16px);
  }

  .controls-scroll {
    padding: 18px 16px 12px;
  }

  .export-section {
    padding-right: 16px;
    padding-bottom: var(--fab-bottom);
    padding-left: 16px;
  }

  /* Leave the bottom-right corner to the fixed Edit/Close button so it never moves. */
  .export-row {
    margin-right: calc(var(--fab-height) + 8px);
  }

  .theme-options {
    --theme-columns: 4;
  }

  /* The sheet is taller than the faces need, so they sit at its bottom edge, next to the button. */
  .theme-scroll {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
  }

  /* The Themes button would sit on top of the export row, so the open editor takes the corner alone. */
  .workspace.is-controls-open ~ .theme-toggle {
    opacity: 0;
    pointer-events: none;
  }

  .export-select select,
  .export-button {
    height: var(--fab-height);
  }

}

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

  .preview-panel,
  .stage-shell,
  .controls-panel,
  .theme-panel,
  .panel-toggle {
    transition: none;
  }
}
