/* Spinner: plain, flat styling modelled on GitHub's light and dark themes.
   One font everywhere: the operating system's own sans-serif UI font. */

/* ── Themes ─────────────────────────────────────────────────── */

:root,
[data-theme='light'] {
  color-scheme: light;
  --bg: #ffffff;
  --subtle: #f6f8fa;
  --fg: #1f2328;
  --muted: #59636e;
  --border: #d1d9e0;
  --accent: #0969da;
  --btn: #f6f8fa;
  --btn-hover: #eff2f5;
}

[data-theme='dark'] {
  color-scheme: dark;
  --bg: #0d1117;
  --subtle: #151b23;
  --fg: #f0f6fc;
  --muted: #9198a1;
  --border: #3d444d;
  --accent: #4493f8;
  --btn: #212830;
  --btn-hover: #262c36;
}

/* ── Base ───────────────────────────────────────────────────── */

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

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, sans-serif;
  line-height: 1.5;
}

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

button {
  cursor: pointer;
}

h1,
h2,
h3,
p {
  margin: 0;
  font-size: inherit;
}

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

[hidden] {
  display: none !important;
}

.label {
  font-weight: 600;
  color: var(--muted);
}

kbd {
  font: inherit;
  padding: 0 0.35em;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--subtle);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--btn);
  font-weight: 500;
  white-space: nowrap;

  &:hover:not(:disabled) {
    background: var(--btn-hover);
  }
  &[aria-pressed='true'] {
    border-color: var(--accent);
    background: color-mix(in srgb, var(--accent) 12%, var(--bg));
    color: var(--accent);
  }
  &:disabled {
    color: var(--muted);
    opacity: 0.5;
    cursor: default;
  }
  svg {
    flex: none;
    inline-size: 16px;
    block-size: 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.4;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
}

.btn-icon {
  padding: 0.4rem;
}

[data-theme='light'] .icon-sun,
[data-theme='dark'] .icon-moon {
  display: none;
}

.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);

  &:hover:not(:disabled) {
    text-decoration: underline;
  }
  &:disabled {
    color: var(--muted);
    opacity: 0.6;
    cursor: default;
  }
}

/* ── Layout ─────────────────────────────────────────────────── */

.topbar {
  position: fixed;
  z-index: 5;
  top: 1rem;
  right: 1.5rem;
}

.app {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 0 1.5rem;
}

/* ── Wheel ──────────────────────────────────────────────────── */

/* The stage is exactly the wheel; the result and hint hang above and below it
   out of flow, so nothing they do can move the wheel off the screen's centre. */
.stage {
  position: relative;
  inline-size: min(100%, 620px, calc(100dvh - 13rem));
}

.wheel {
  position: relative;
  inline-size: 100%;
  /* The spinning square layer's corners must not widen the page. */
  overflow: clip;
  overflow-clip-margin: 1rem;
  aspect-ratio: 1;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  cursor: grab;

  &[data-state='dragging'] {
    cursor: grabbing;
  }

  &::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    box-shadow: inset 0 0 0 1px var(--border);
    pointer-events: none;
  }
}

.rotor {
  position: absolute;
  inset: 0;
  will-change: transform;

  svg {
    display: block;
    inline-size: 100%;
    block-size: 100%;
  }
}

.slice {
  transition: opacity 0.3s;

  path,
  circle {
    fill: var(--seg);
    stroke: var(--bg);
    stroke-width: 0.006;
    stroke-linejoin: round;
  }

  text {
    fill: var(--fg);
    /* Dark text on light slices, light text on dark ones. */
    fill: oklch(from var(--seg) clamp(0.2, (0.66 - l) * 999, 1) 0 0);
    pointer-events: none;
  }
}

.has-winner .slice:not(.win) {
  opacity: calc(1 - var(--fade-others, 0.7));
}

.empty {
  fill: var(--subtle);
  stroke: var(--border);
  stroke-width: 0.006;
}

.empty-text {
  fill: var(--muted);
}

.pointer {
  position: absolute;
  z-index: 2;
  top: -1.5%;
  left: 50%;
  inline-size: 4.5%;
  aspect-ratio: 1;
  translate: -50% 0;
  transform-origin: 50% 0;
  pointer-events: none;
  background: var(--fg);
  clip-path: polygon(0 0, 100% 0, 50% 100%);
}

.hub {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  translate: -50% -50%;
  inline-size: 20%;
  aspect-ratio: 1;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--bg);
  font-weight: 600;

  &:hover:not(:disabled) {
    background: var(--subtle);
  }
  &:disabled {
    color: var(--muted);
    cursor: default;
  }
}

.hub:active:not(:disabled) {
  background: var(--btn-hover);
}

.hint {
  position: absolute;
  top: calc(100% + 1rem);
  left: 50%;
  translate: -50% 0;
  inline-size: max-content;
  max-inline-size: calc(100vw - 2rem);
  text-align: center;
  color: var(--muted);
  transition: opacity 0.2s;
}

.wheel[data-state='spinning'] + .hint {
  opacity: 0;
}

@media (pointer: coarse) {
  .key-hint {
    display: none;
  }
}

/* ── Result ─────────────────────────────────────────────────── */

.result {
  position: absolute;
  bottom: calc(100% + 1.5rem);
  left: 50%;
  translate: -50% 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  inline-size: min(calc(100vw - 2rem), 48rem);
}

/* Eye toggle beside the result: hide the preview while spinning. */
.eye {
  flex: none;
  display: grid;
  place-items: center;
  inline-size: 1.7rem;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  opacity: 0.7;

  &:hover {
    opacity: 1;
    color: var(--fg);
    background: var(--subtle);
  }
  svg {
    inline-size: 16px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.4;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
  .icon-eye-off,
  &[aria-pressed='true'] .icon-eye {
    display: none;
  }
  &[aria-pressed='true'] .icon-eye-off {
    display: block;
  }
}

.winner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  font-size: 2rem;
  font-weight: 600;
  line-height: 1.25;
  min-inline-size: 0;
  max-inline-size: 100%;
}

/* One line only: long names are cut off with an ellipsis instead of wrapping. */
.winner-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.winner-dot {
  flex: none;
  inline-size: 0.6em;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--seg, var(--border));
}

.winner[data-state='idle'] {
  color: var(--muted);
  font-weight: 400;

  .winner-dot {
    background: none;
    box-shadow: inset 0 0 0 1px var(--border);
  }
}

/* No eye toggle mid-spin. */
.stage:has(.wheel[data-state='spinning']) .eye {
  visibility: hidden;
}

.winner[data-state='live'] {
  color: var(--muted);
}

/* Preview hidden (eye toggle): an empty dot and "…" until the wheel stops. */
.winner.pending .winner-dot {
  background: none;
  box-shadow: inset 0 0 0 1px var(--border);
}

/* ── Entries panel ──────────────────────────────────────────── */

.panel {
  position: fixed;
  z-index: 10;
  top: 4rem;
  right: 1.5rem;
  container: panel / inline-size;
  inline-size: min(24rem, calc(100vw - 2rem));
  display: flex;
  flex-direction: column;
  max-block-size: calc(100dvh - 5.5rem);
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.12);

  &.is-placed {
    right: auto;
  }
  /* During a spin: locked and dimmed, except the header (hide + drag still work). */
  &.is-locked {
    > :not(.panel-head),
    #mode-toggle {
      opacity: 0.55;
    }
  }
}

/* Split toggle on the left, shuffle + reset-order icons on the right. */
.layout-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-block-start: 1px solid var(--border);
}

.layout-order {
  display: flex;
  gap: 0.4rem;

  #shuffle-btn.is-on {
    color: var(--accent);
    border-color: var(--accent);
  }
}

.panel-tab {
  position: fixed;
  z-index: 10;
  top: 50%;
  right: 0;
  translate: 0 -50%;
  display: grid;
  place-items: center;
  inline-size: 1.4rem;
  block-size: 3rem;
  padding: 0;
  border: 1px solid var(--border);
  border-right: 0;
  border-radius: 6px 0 0 6px;
  background: var(--btn);

  &:hover {
    background: var(--btn-hover);
  }
  svg {
    inline-size: 14px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
  }
}

.panel-actions {
  display: flex;
  gap: 0.5rem;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-block-end: 1px solid var(--border);
  border-radius: 6px 6px 0 0;
  background: var(--subtle);
  cursor: grab;
  touch-action: none;
  user-select: none;

  .is-dragging & {
    cursor: grabbing;
  }
  button {
    cursor: pointer;
  }
}

.panel-title {
  font-weight: 600;
}

.count {
  color: var(--muted);
}

.panel-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1 1 auto;
  min-block-size: 0;
  padding: 0.5rem 0;
}

.entry-list {
  flex: 1 1 auto;
  min-block-size: 0;
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.entry {
  display: grid;
  grid-template-columns: 10px minmax(0, 1fr) auto 4.5ch 24px;
  align-items: center;
  gap: 0.5rem;
  padding: 0.2rem 1rem;

  &:hover,
  &:focus-within {
    background: var(--subtle);
  }

  &[data-excluded] :is(.swatch, .name, .pct) {
    opacity: 0.45;
  }

  &[data-winner] {
    background: color-mix(in srgb, var(--seg) 20%, transparent);
  }
}

.swatch {
  inline-size: 10px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--seg);
}

.name {
  min-inline-size: 0;
  padding: 0.25rem 0.4rem;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  text-overflow: ellipsis;

  &::placeholder {
    color: var(--muted);
  }
  &:focus {
    outline: none;
    border-color: var(--accent);
    background: var(--bg);
  }
}

.weight {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);

  &:focus-within {
    border-color: var(--accent);
  }

  .w {
    inline-size: 3.5ch;
    padding: 0.15rem 0;
    border: 0;
    background: transparent;
    text-align: center;
    font-variant-numeric: tabular-nums;
    appearance: textfield;
    -moz-appearance: textfield;

    &::-webkit-inner-spin-button,
    &::-webkit-outer-spin-button {
      appearance: none;
      margin: 0;
    }
    &:focus {
      outline: none;
    }
  }

  .step {
    inline-size: 1.5rem;
    block-size: 1.6rem;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--muted);

    &:hover {
      color: var(--fg);
    }
  }
}

.pct {
  text-align: end;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.remove {
  display: grid;
  place-items: center;
  inline-size: 24px;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);

  svg {
    inline-size: 12px;
    stroke: currentColor;
    stroke-width: 1.5;
    stroke-linecap: round;
  }
  &:hover {
    color: var(--fg);
    background: var(--btn-hover);
  }
}

@container panel (width < 280px) {
  .entry {
    grid-template-columns: 10px minmax(0, 1fr) auto 24px;
  }
  .pct {
    display: none;
  }
}

.add-entry {
  margin-inline: 1rem;
  padding: 0.4rem;
  border: 1px dashed var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--muted);

  &:hover {
    color: var(--fg);
    background: var(--subtle);
  }
}

.bulk-wrap {
  display: none;
  padding-inline: 1rem;
  flex: 1 1 auto;
  min-block-size: 0;
}

.bulk {
  inline-size: 100%;
  min-block-size: 14rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  line-height: 1.6;
  resize: vertical;

  &:focus {
    outline: none;
    border-color: var(--accent);
  }
  &::placeholder {
    color: var(--muted);
  }
}

.panel[data-mode='text'] {
  .entry-list,
  .add-entry {
    display: none;
  }
  .bulk-wrap {
    display: flex;
  }
}

/* ── Advanced: collapsible settings at the bottom of the panel ── */

.advanced {
  flex: none;
  border-block-start: 1px solid var(--border);

  summary {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.55rem 1rem;
    cursor: pointer;
    list-style: none;
    user-select: none;

    &::-webkit-details-marker {
      display: none;
    }
    &::before {
      content: '';
      inline-size: 0.45em;
      aspect-ratio: 1;
      border-inline-end: 1.5px solid currentColor;
      border-block-end: 1.5px solid currentColor;
      rotate: -45deg;
      transition: rotate 0.15s;
    }
    &:hover {
      background: var(--subtle);
    }
    .link {
      margin-inline-start: auto;
    }
  }
  &[open] summary::before {
    rotate: 45deg;
  }
}

.advanced-title {
  font-weight: 600;
}

.advanced-body {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  column-gap: 0.75rem;
  max-block-size: 45dvh;
  overflow: hidden auto;
  padding: 0 1rem 0.6rem;
}

.group-label {
  grid-column: 1 / -1;
  padding-block: 0.5rem 0.1rem;
  color: var(--muted);
  font-weight: 600;

  &:not(:first-child) {
    margin-block-start: 0.35rem;
    border-block-start: 1px solid var(--border);
  }
}

/* One line per setting: label | control | value (rows share the body's columns). */
.tune {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: subgrid;
  align-items: center;
  min-block-size: 1.9rem;

  .tune-label {
    cursor: help;
    white-space: nowrap;
    text-decoration: underline dotted var(--border);
    text-underline-offset: 3px;
  }
  input[type='range'] {
    inline-size: 100%;
    margin: 0;
    accent-color: var(--accent);
  }
  output {
    min-inline-size: 4.5em;
    text-align: end;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }
}

/* Hover/focus descriptions: one shared popover in the top layer (see tooltip.js). */
.tooltip {
  position: fixed;
  inset: auto;
  margin: 0;
  max-inline-size: 17rem;
  padding: 0.4rem 0.6rem;
  border: 0;
  border-radius: 6px;
  background: var(--fg);
  color: var(--bg);
  line-height: 1.4;
  pointer-events: none;
}

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