/*
  The shell around the Unity canvas: demos on the left, the canvas, one drawer, and the rail of drawer
  buttons on the right. It follows the portfolio's design rules: system fonts, radii of 8 px at most,
  hairline borders, and no gradients, shadows or blur.
*/

/* Tokens copied from ethanhammar.com (packages/ui/src/styles/tokens.css). Keep them in sync, and keep
   the colors in sync with Assets/Showcase/Runtime/ShowcaseTheme.cs too. */
:root {
  --background: #faf9f7;
  --surface: #f3f2ef;
  --surface-raised: #ffffff;
  --text: #1f2328;
  --text-muted: #59616b;
  --text-faint: #8a919b;
  --border: #e2e0db;
  --border-strong: #c8c5be;
  --accent: #3d6b8f;
  --accent-hover: #33587a;
  --accent-subtle: #eef3f7;
  --selection: #d6e4ef;
  --code-background: #f4f4f2;
  --danger: #a13d2d;
  --warning: #8a6d1d;
  --success: #3d7a4a;

  --radius-sm: 4px;
  --radius-md: 6px;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;

  --font-size-xs: 0.75rem;
  --font-size-sm: 0.875rem;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;

  --duration-fast: 120ms;
  --ease-standard: cubic-bezier(0.2, 0, 0.2, 1);
  --duration-theme: 500ms;
  --ease-theme: cubic-bezier(0.4, 0, 0.2, 1);

  /* The shell's geometry, shared with the Editor's copy (EditorShell.cs). */
  --sidebar: 240px;
  --rail: 48px;
  --drawer: 360px;
  --drawer-code: 560px;

  color-scheme: light;
}

.dark {
  --background: #16181c;
  --surface: #1c1f24;
  --surface-raised: #22262c;
  --text: #e8e6e1;
  --text-muted: #a3a8af;
  --text-faint: #6d737b;
  --border: #2c3037;
  --border-strong: #43484f;
  --accent: #6b94b5;
  --accent-hover: #86aac7;
  --accent-subtle: #1c2933;
  --selection: #2b3f50;
  --code-background: #1d2025;
  --danger: #c96b5a;
  --warning: #c2a04a;
  --success: #6aa877;

  color-scheme: dark;
}

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

[hidden] {
  display: none !important;
}

::selection {
  background: var(--selection);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

body {
  display: grid;
  grid-template: "sidebar stage drawer rail" minmax(0, 1fr) / var(--sidebar) minmax(0, 1fr) auto var(--rail);
  height: 100vh;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  background: var(--background);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--font-size-sm);
  line-height: 1.5;
  accent-color: var(--accent);
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

/* Buttons */

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: color var(--duration-fast) var(--ease-standard);
}

.icon-button:hover {
  color: var(--text);
}

.icon-button[aria-expanded="true"] {
  background: var(--accent-subtle);
  color: var(--accent);
}

.icon-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.button {
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease-standard);
}

.button:hover {
  border-color: var(--accent);
}

.hint {
  margin: 0 0 var(--space-3);
  color: var(--text-muted);
}

/* Sidebar: "← Category →" over that category's demos */

.sidebar {
  grid-area: sidebar;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.category {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-2);
  border-bottom: 1px solid var(--border);
}

.category h1 {
  margin: 0;
  font-size: inherit;
  font-weight: 600;
}

.demos {
  flex: 1;
  margin: 0;
  padding: var(--space-2);
  overflow-y: auto;
  list-style: none;
}

.demos .hint {
  margin: 0;
  padding: var(--space-2) var(--space-3);
}

.demos a {
  display: block;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text);
  text-decoration: none;
}

.demos a:hover {
  background: var(--surface-raised);
}

.demos a[aria-current="page"] {
  background: var(--accent-subtle);
  color: var(--accent);
}

/* Stage: the Unity canvas, with loading and error messages over it */

.stage {
  grid-area: stage;
  position: relative;
  min-width: 0;
}

#unity-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#unity-canvas:focus-visible {
  outline-offset: -2px;
  border-radius: 0;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-4);
  background: var(--background);
  color: var(--text-muted);
  text-align: center;
}

.overlay-message {
  max-width: 60ch;
  margin: 0;
  white-space: pre-wrap;
}

.overlay.failed .overlay-message {
  color: var(--danger);
}

.overlay.failed .progress {
  display: none;
}

.progress {
  width: 160px;
  height: 2px;
  overflow: hidden;
  background: var(--border);
}

.progress-bar {
  height: 100%;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform var(--duration-fast) var(--ease-standard);
}

.toast {
  position: absolute;
  left: var(--space-3);
  bottom: var(--space-3);
  max-width: calc(100% - 2 * var(--space-3));
  margin: 0;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface-raised);
  color: var(--danger);
  font-size: var(--font-size-xs);
}

.toast:empty {
  display: none;
}

/* Drawers: one at a time, between the canvas and the rail */

.drawer {
  grid-area: drawer;
  display: flex;
  flex-direction: column;
  width: var(--drawer);
  min-height: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
}

#code {
  width: min(var(--drawer-code), 45vw);
}

.drawer h2 {
  margin: 0;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  font-size: inherit;
  font-weight: 600;
}

.drawer-body {
  flex: 1;
  padding: var(--space-3) var(--space-4);
  overflow: auto;
}

.rail {
  grid-area: rail;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-2) 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
}

/* Lists of buttons: the scene tree and the code files */

.tree,
.files {
  margin: 0 0 var(--space-4);
  padding: 0;
  list-style: none;
}

.tree button,
.files button {
  display: block;
  width: 100%;
  padding: 1px var(--space-2);
  padding-left: calc(var(--space-2) + var(--depth, 0) * 14px);
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  text-align: start;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.tree button:hover,
.files button:hover {
  background: var(--surface-raised);
}

.tree button[aria-current="true"],
.files button[aria-current="true"] {
  background: var(--accent-subtle);
  color: var(--accent);
}

.tree button.inactive {
  color: var(--text-faint);
}

.files .folder {
  padding: var(--space-2) var(--space-2) 1px;
  color: var(--text-faint);
  font-size: var(--font-size-xs);
}

.files button {
  --depth: 1; /* one step in from its folder */
}

/* Scene drawer: the selected object's components */

.component {
  margin-bottom: var(--space-3);
}

.component h3 {
  margin: 0 0 var(--space-1);
  color: var(--text-muted);
  font-size: var(--font-size-xs);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.fields {
  display: grid;
  grid-template-columns: 7.5rem minmax(0, 1fr);
  gap: 1px var(--space-3);
  margin: 0;
}

.fields dt {
  color: var(--text-muted);
}

.fields dd {
  margin: 0;
  font-family: var(--font-mono);
  font-size: var(--font-size-xs);
  line-height: 1.75;
  overflow-wrap: anywhere;
}

/* Code drawer: the source, with line numbers that copy-paste leaves out */

.source {
  margin: 0;
  padding: var(--space-3) var(--space-3) var(--space-3) 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--code-background);
  font-family: var(--font-mono);
  font-size: var(--font-size-xs);
  line-height: 1.6;
  white-space: pre-wrap; /* long lines wrap to fit the drawer */
  overflow-wrap: anywhere;
  tab-size: 4;
  counter-reset: line;
}

.source:empty {
  display: none;
}

/* The number takes the first 6ch; a wrapped line carries on past it, under its own indent. */
.source .line {
  display: block;
  padding-left: calc(6ch + var(--indent, 0ch));
  text-indent: calc(-6ch - var(--indent, 0ch));
}

.source .line::before {
  content: counter(line);
  counter-increment: line;
  display: inline-block;
  width: 3ch;
  margin: 0 2ch 0 1ch;
  text-indent: 0;
  color: var(--text-faint);
  text-align: right;
  user-select: none;
}

.source .failed {
  padding-left: var(--space-3);
  color: var(--danger);
}

.token-comment {
  color: var(--text-faint);
}

.token-string {
  color: var(--success);
}

.token-number {
  color: var(--warning);
}

.token-keyword {
  color: var(--accent);
}

/* Live values: where the code writes a setting's value, a control for the running demo (live-value.js) */

.live {
  position: relative;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: none;
  text-indent: 0; /* the line's hanging indent would push the text out of an inline-block */
  text-decoration: underline dotted var(--text-faint);
  text-underline-offset: 0.25em;
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-standard);
}

.live:hover,
.live.changed {
  background: var(--accent-subtle);
  text-decoration-color: var(--accent);
}

.live.changed {
  text-decoration-style: solid;
}

.live:focus-visible,
.live:has(:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* A number with a range drags sideways. Vertical swipes still scroll the code. */
.live-drag {
  display: inline-block;
  cursor: ew-resize;
  touch-action: pan-y;
}

.live-number:not(.live-drag) {
  cursor: text;
}

.live-input {
  padding: 0 0.5ch;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--surface-raised);
  text-indent: 0;
}

/* Enums and colors: the browser's own list or picker, invisible over the text */
.live-pick select,
.live-pick input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  opacity: 0;
  font-size: 16px; /* at least this, or iOS zooms in on it */
  cursor: pointer;
}

.live-color::before,
.live-run::before {
  content: "";
  display: inline-block;
  width: 0.75em;
  height: 0.75em;
  margin-right: 0.5ch;
  vertical-align: -0.05em;
}

.live-color::before {
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  background: var(--swatch);
}

.live-run::before {
  background: var(--accent);
  clip-path: polygon(10% 0, 100% 50%, 10% 100%);
}

/* Settings drawer: one control per [Expose] member */

.setting {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 2px var(--space-2);
  margin-bottom: var(--space-3);
}

.setting label {
  color: var(--text-muted);
}

.setting output {
  font-variant-numeric: tabular-nums;
}

.setting > .button {
  justify-self: start;
}

.setting input[type="range"],
.setting input[type="number"],
.setting select {
  grid-column: 1 / -1;
  width: 100%;
  margin: 0;
}

.setting input[type="number"],
.setting select {
  padding: var(--space-1) var(--space-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-raised);
}

.setting input[type="color"] {
  width: 48px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: transparent;
}

.setting-check {
  grid-template-columns: auto minmax(0, 1fr);
}

.setting-check input {
  margin: 0;
}

.setting-check label {
  color: inherit;
}

/* Narrow screens: the demo list becomes a bar across the top, and a drawer covers the canvas */

@media (max-width: 719px) {
  body {
    grid-template: "sidebar sidebar" auto "stage rail" minmax(0, 1fr) / minmax(0, 1fr) var(--rail);
  }

  .sidebar {
    flex-direction: row;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .category {
    flex: none;
    border-bottom: 0;
  }

  .demos {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    overflow-x: auto;
    overflow-y: hidden;
  }

  .demos a,
  .demos .hint {
    padding: var(--space-1) var(--space-3);
    white-space: nowrap;
  }

  .drawer,
  #code {
    grid-area: stage;
    z-index: 1;
    width: auto;
    border-left: 0;
  }
}

@media (pointer: coarse) {
  .live-input {
    font-size: 16px; /* at least this, or iOS zooms in on it */
  }
}

/* A theme switch fades every color with the portfolio and the Unity canvas (theme.js adds the class). */
.theme-fading,
.theme-fading * {
  transition-property: background-color, border-color, color !important;
  transition-duration: var(--duration-theme) !important;
  transition-timing-function: var(--ease-theme) !important;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
