:root {
  --bg: #17142b;
  --bg-glow: #3a2d6b;
  --text: #f4ecdc;
  --muted: #b5adcf;
  --line: #4a4170;
  --button: #2a2549;
  --button-hover: #383162;
  --accent: #f1b92b;
  --accent-text: #241b05;
  --body: #100e20;
}

* { box-sizing: border-box; }

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

body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100dvh;
  overflow: hidden;
  background: radial-gradient(circle at 50% 42%, var(--bg-glow), var(--bg) 62%);
  background-color: var(--bg);
  color: var(--text);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px 0;
}

h1 { margin: 0; font-size: 1.5rem; letter-spacing: .02em; }

.stats { display: flex; gap: 24px; margin: 0; }
.stats div { text-align: right; }
.stats dt { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; }
.stats dd { margin: 0; font-size: 1.25rem; font-variant-numeric: tabular-nums; }

/* 3D scene */
#stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#stage.dragging { cursor: grabbing; }

#fit, #puzzle, .cubie { transform-style: preserve-3d; }

#fit { position: absolute; left: 50%; top: 50%; width: 0; height: 0; }
#puzzle, .cubie { position: absolute; left: 0; top: 0; width: 0; height: 0; }

.face {
  position: absolute;
  background: var(--body);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  pointer-events: none;
}

.face.tile { pointer-events: auto; cursor: pointer; }

.face.tile::before {
  content: "";
  position: absolute;
  inset: 5%;
  border-radius: 14%;
  background-color: var(--tile);
  background-image: var(--glyph), linear-gradient(145deg, rgba(255,255,255,.2), rgba(0,0,0,.12));
  background-position: center;
  background-repeat: no-repeat;
  background-size: 58%, 100%;
}

/* Six original face styles: colour plus a symbol, so they differ without relying on colour alone */
.c0 { --tile: #ef5b4c; --glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Crect x='5' y='5' width='14' height='14' rx='2' fill='%23300c08' fill-opacity='.45'/%3E%3C/svg%3E"); }
.c1 { --tile: #6a3fc9; --glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2 22 12 12 22 2 12Z' fill='%23fff' fill-opacity='.4'/%3E%3C/svg%3E"); }
.c2 { --tile: #17b2a6; --glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 3 22 21H2Z' fill='%23042b28' fill-opacity='.45'/%3E%3C/svg%3E"); }
.c3 { --tile: #f4ecdc; --glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='9' fill='%23362c18' fill-opacity='.4'/%3E%3C/svg%3E"); }
.c4 { --tile: #f1b92b; --glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2 15 9 22 9.5 16.6 14 18.5 21.500 12 17.500 5.500 21.500 7.400 14 2 9.500 9 9Z' fill='%233d2c02' fill-opacity='.45'/%3E%3C/svg%3E"); }
.c5 { --tile: #dd3f9c; --glyph: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M9 3h6v6h6v6h-6v6H9v-6H3V9h6Z' fill='%23fff' fill-opacity='.42'/%3E%3C/svg%3E"); }

/* Win message */
#toast {
  position: absolute;
  left: 50%;
  bottom: 14px;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 10px);
  transition: opacity .25s, transform .25s;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* The glow sits behind the puzzle: a filter on the 3D scene itself would flatten it */
#stage::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 50%, rgba(241, 185, 43, .4), transparent 60%);
  opacity: 0;
  pointer-events: none;
}

#stage.won::before { animation: won .9s ease-in-out 2; }

@keyframes won {
  50% { opacity: 1; }
}

/* Controls */
.panel {
  display: grid;
  gap: 10px;
  justify-items: center;
  padding: 0 16px 14px;
}

.hint { margin: 0; color: var(--muted); font-size: .9rem; text-align: center; }

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.sizes { display: flex; gap: 4px; margin-left: 8px; }

button {
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--button);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--button-hover); }
button:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .45; cursor: not-allowed; }

button.primary, .sizes button[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}
button.primary:hover:not(:disabled) { background: #f7cb57; }
.sizes button { padding: 0 10px; }
.sizes button[aria-pressed="true"]:hover { background: var(--accent); }

.keys { color: var(--muted); font-size: .85rem; max-width: 560px; }
.keys summary { cursor: pointer; text-align: center; }
.keys ul { margin: 8px 0 0; padding-left: 20px; line-height: 1.7; }

kbd {
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--button);
  color: var(--text);
  font: inherit;
  font-size: .8rem;
}

@media (max-width: 560px) {
  .topbar { padding: 10px 16px 0; }
  .sizes { margin-left: 0; }
  .hint { font-size: .82rem; }
  .keys { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  #stage.won::before { animation: none; opacity: 1; }
  #toast { transition: none; }
}
