:root {
  --anim-bg: #f7f3e8;
  --anim-ink: #293739;
  --anim-muted: #657476;
  --anim-border: rgba(41, 55, 57, 0.24);
  --anim-border-faint: rgba(41, 55, 57, 0.14);
  --anim-cell-bg: rgba(41, 55, 57, 0.022);
  --anim-cell-bg-focus: rgba(41, 55, 57, 0.042);
  --anim-dot: rgba(41, 55, 57, 0.14);
  --anim-coral: #ef7569;
}

html[data-theme="dark"] {
  --anim-bg: #121212;
  --anim-ink: #dddeda;
  --anim-muted: #9aa7aa;
  --anim-border: rgba(221, 222, 218, 0.24);
  --anim-border-faint: rgba(221, 222, 218, 0.15);
  --anim-cell-bg: rgba(221, 222, 218, 0.028);
  --anim-cell-bg-focus: rgba(221, 222, 218, 0.052);
  --anim-dot: rgba(221, 222, 218, 0.16);
  --anim-coral: #ef7569;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--anim-bg);
  color: var(--anim-ink);
  transition: background-color 260ms ease, color 260ms ease;
}

body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  user-select: none;
  -webkit-user-select: none;
}

/* =========================================================================
   MAIN WORKSPACE SHELL (HORIZONTAL vs VERTICAL)
   ========================================================================= */
.workspace {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  overflow: hidden;
}

/* Horizontal variant: 3D stage on top, 2D front-on grid at bottom (right-to-left) */
body[data-layout="horizontal"] .workspace {
  flex-direction: column;
  align-items: center;
  padding: 0 6px 2px;
  gap: 0;
}

body[data-layout="horizontal"] .stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
}

body[data-layout="horizontal"] .front-grid {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row-reverse; /* Right-to-left horizontal progression */
  align-items: center;
  justify-content: center;
  gap: 10px;
}

body[data-layout="horizontal"] .front-cell {
  width: clamp(112px, 19.5vh, 178px);
  height: clamp(112px, 19.5vh, 178px);
}

/* Vertical variant: 3D stage on left, 2D front-on grid column on right */
body[data-layout="vertical"] .workspace {
  flex-direction: row;
  align-items: center;
  padding: 12px clamp(14px, 3vw, 36px) 6px;
  gap: clamp(12px, 2.2vw, 28px);
}

body[data-layout="vertical"] .stage {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
}

body[data-layout="vertical"] .front-grid {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

body[data-layout="vertical"] .front-cell {
  width: clamp(104px, 21vh, 164px);
  height: clamp(104px, 21vh, 164px);
}

/* Shared 2D cell styling: softly rounded dotted editorial squares */
.front-cell {
  position: relative;
  border-radius: 10px;
  border: 1px dashed var(--anim-border);
  background-color: var(--anim-cell-bg);
  background-image: radial-gradient(var(--anim-dot) 0.75px, transparent 0.75px);
  background-size: 12px 12px;
  background-position: 6px 6px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color 260ms ease, background-color 260ms ease, transform 180ms ease;
}

.front-cell:hover {
  border-color: var(--anim-ink);
  transform: translateY(-1px);
}

.front-cell.is-focus {
  background-color: var(--anim-cell-bg-focus);
  border-color: var(--anim-ink);
}

.front-cell canvas {
  width: 100%;
  height: 100%;
  display: block;
  background: transparent;
}

canvas#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: transparent;
  touch-action: pan-y;
  cursor: grab;
}

canvas#scene.is-dragging {
  cursor: grabbing;
}

/* Responsive fallback: narrow screens always place compact 2D strip below 3D stage */
@media (max-width: 720px) {
  body[data-layout="vertical"] .workspace {
    flex-direction: column;
    padding: 8px 12px 4px;
    gap: 8px;
  }

  body[data-layout="vertical"] .stage {
    width: 100%;
    height: auto;
    flex: 1 1 auto;
    min-height: 0;
  }

  body[data-layout="vertical"] .front-grid {
    flex-direction: row-reverse;
    gap: 8px;
  }

  body[data-layout="vertical"] .front-cell,
  body[data-layout="horizontal"] .front-cell {
    width: clamp(78px, 26vw, 112px);
    height: clamp(78px, 26vw, 112px);
    border-radius: 8px;
  }

  body[data-layout="vertical"] .front-cell::after,
  body[data-layout="horizontal"] .front-cell::after {
    border-radius: 7px;
  }
}

/* Compact, aligned timeline slider with coral progress fill */
.ctl {
  padding: 2px 10px max(0.8vh, 6px);
  display: flex;
  justify-content: center;
}

.ctl.essay-timeline {
  --timeline-ink: var(--anim-ink);
  --timeline-dot: 11px;
  --tl-progress: 0%;
  width: min(550px, 86vw);
  margin: 0 auto;
}

body[data-layout="vertical"] .ctl.essay-timeline {
  width: min(420px, 72vw);
}

.ctl.essay-timeline input[type="range"] {
  display: block;
  width: 100%;
  height: 28px;
  margin: 0;
  background: transparent;
  cursor: ew-resize;
}

.ctl.essay-timeline input[type="range"]::-webkit-slider-runnable-track {
  height: 2px;
  border-radius: 999px;
  background: linear-gradient(
    to right,
    var(--anim-coral) 0%,
    var(--anim-coral) var(--tl-progress),
    var(--anim-border) var(--tl-progress),
    var(--anim-border) 100%
  );
}

.ctl.essay-timeline input[type="range"]::-webkit-slider-thumb {
  width: var(--timeline-dot);
  height: var(--timeline-dot);
  margin-top: calc((2px - var(--timeline-dot)) / 2);
  border-radius: 50%;
  background: var(--anim-coral);
  box-shadow: 0 0 0 2.5px var(--anim-bg);
  transition: transform 140ms ease;
}

.ctl.essay-timeline input[type="range"]:hover::-webkit-slider-thumb {
  transform: scale(1.16);
}

.ctl.essay-timeline input[type="range"]::-moz-range-track {
  height: 2px;
  border-radius: 999px;
  background: var(--anim-border);
}

.ctl.essay-timeline input[type="range"]::-moz-range-progress {
  height: 2px;
  border-radius: 999px;
  background: var(--anim-coral);
}

.ctl.essay-timeline input[type="range"]::-moz-range-thumb {
  width: var(--timeline-dot);
  height: var(--timeline-dot);
  border: 2.5px solid var(--anim-bg);
  border-radius: 50%;
  background: var(--anim-coral);
}

.ctl.essay-timeline input[type="range"]:focus-visible {
  outline: 2px solid var(--anim-coral);
  outline-offset: 4px;
  border-radius: 6px;
}

/* Discrete standalone-only theme toggle (hidden when embedded in essay iframe) */
.theme-toggle {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 10;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--anim-border);
  background: var(--anim-bg);
  color: var(--anim-ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  transition: border-color 200ms ease, background-color 260ms ease, color 260ms ease;
}

.theme-toggle:hover {
  border-color: var(--anim-ink);
}

.theme-toggle:focus-visible {
  outline: 2px solid var(--anim-coral);
  outline-offset: 3px;
}

html.is-embedded .theme-toggle {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  html, body, .theme-toggle, .front-cell, .front-cell::after {
    transition: none;
  }
}
