/* ---------- tokens ---------- */

:root {
  --bg: #f3f5fb;
  --surface: #ffffff;
  --surface-2: #e9ecf6;
  --ink: #1b1f3b;
  --muted: #5d6385;
  --line: #d5d9ea;
  --teal: #1fa99f;
  --sun: #ffd23f;
  --sun-deep: #e8b400;
  --focus: #2f6bff;
  --danger: #d6455d;

  /* wheel */
  --seg-1: #ffd23f;
  --seg-2: #4fd1c5;
  --seg-3: #ff8a7a;
  --seg-4: #a596ff;
  --seg-5: #74c7ff;
  --seg-6: #ffab5c;
  --seg-7: #8ddc7d;
  --seg-8: #ff9ccb;
  --seg-text: #1b1f3b;
  --seg-edge: #ffffff;
  --wheel-empty: #e9ecf6;
  --wheel-shadow: 0 24px 60px -24px rgba(27, 31, 59, 0.35);

  --font-display: 'Bricolage Grotesque', 'Trebuchet MS', sans-serif;
  --font-ui: 'Figtree', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --radius: 14px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #14162b;
    --surface: #1d2040;
    --surface-2: #272b52;
    --ink: #eef0ff;
    --muted: #a3a8cc;
    --line: #363b66;
    --teal: #3fd0c4;
    --focus: #7aa2ff;
    --danger: #e0566e;
    --seg-edge: #14162b;
    --wheel-empty: #1d2040;
    --wheel-shadow: 0 24px 70px -20px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme='dark'] {
  --bg: #14162b;
  --surface: #1d2040;
  --surface-2: #272b52;
  --ink: #eef0ff;
  --muted: #a3a8cc;
  --line: #363b66;
  --teal: #3fd0c4;
  --focus: #7aa2ff;
  --danger: #e0566e;
  --seg-edge: #14162b;
  --wheel-empty: #1d2040;
  --wheel-shadow: 0 24px 70px -20px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

/* ---------- base ---------- */

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

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

body {
  overflow-x: clip;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

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

button {
  cursor: pointer;
}

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

[hidden] {
  display: none !important;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem clamp(1rem, 3vw, 2rem);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: inherit;
  text-decoration: none;
  font: 800 1.7rem/1 var(--font-display);
  letter-spacing: -0.02em;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  transition: background-color 0.15s;
}

.icon-btn:hover {
  background: var(--surface-2);
}

.icon-btn svg {
  width: 1.2rem;
  height: 1.2rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#themeBtn svg {
  display: none;
}
#themeBtn[data-mode='auto'] .i-auto,
#themeBtn[data-mode='light'] .i-light,
#themeBtn[data-mode='dark'] .i-dark {
  display: block;
}

.seg-toggle {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
}

.seg-toggle::before {
  content: '';
  position: absolute;
  inset: 3px auto 3px 3px;
  width: calc(50% - 3px);
  border-radius: 999px;
  background: var(--ink);
  transition: transform 0.35s var(--ease-spring);
}

.seg-toggle[data-active='en']::before {
  transform: translateX(100%);
}

.seg-toggle button {
  position: relative;
  border: 0;
  border-radius: 999px;
  padding: 0.3rem 0.65rem;
  background: none;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--muted);
}

.seg-toggle button {
  transition: color 0.25s;
}

.seg-toggle button[aria-pressed='true'] {
  color: var(--bg);
}

/* ---------- layout ---------- */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  gap: clamp(1rem, 3vw, 2.5rem);
  padding: 0 clamp(1rem, 3vw, 2rem) 2rem;
  align-items: start;
}

/* ---------- panel ---------- */

.panel {
  position: relative;
  z-index: 5; /* keeps the preset menu above the wheel on small screens */
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.tabs {
  position: relative;
  display: flex;
  border-bottom: 1px solid var(--line);
}

.tab-ink {
  position: absolute;
  left: 0;
  bottom: -1px;
  height: 3px;
  width: 0;
  border-radius: 3px 3px 0 0;
  background: var(--sun-deep);
  transition: transform 0.4s var(--ease-out), width 0.4s var(--ease-out);
}

.tabs [role='tab'] {
  flex: 1;
  border: 0;
  background: none;
  padding: 0.85rem 0.5rem;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--muted);
  transition: color 0.2s;
}

.tabs [role='tab']:hover,
.tabs [role='tab'][aria-selected='true'] {
  color: var(--ink);
}

.count {
  display: inline-block;
  min-width: 1.4em;
  padding: 0 0.35em;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.75rem;
  line-height: 1.4em;
}

.pane {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.1rem;
}

textarea {
  width: 100%;
  min-height: 15rem;
  resize: vertical;
  padding: 0.8rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  line-height: 1.6;
}

.hint {
  margin: -0.4rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  display: inline-flex;
  align-items: center;
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.9rem;
  font-weight: 500;
  list-style: none;
  cursor: pointer;
  user-select: none;
}

.chip::-webkit-details-marker {
  display: none;
}

.chip:hover {
  background: var(--surface-2);
}

.chip-accent {
  border-color: var(--ink);
  font-weight: 600;
}

.menu {
  position: relative;
}

.menu summary::after {
  content: '';
  width: 0.4em;
  height: 0.4em;
  margin-left: 0.5em;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: translateY(-2px) rotate(45deg);
}

.menu-list {
  position: absolute;
  z-index: 20;
  top: calc(100% + 6px);
  left: 0;
  min-width: 13rem;
  max-height: min(22rem, 60vh);
  overflow-y: auto;
  padding: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 16px 40px -16px rgba(0, 0, 0, 0.35);
}

.menu-list button {
  display: block;
  width: 100%;
  padding: 0.5rem 0.7rem;
  border: 0;
  border-radius: 8px;
  background: none;
  text-align: left;
}

.menu-list button:hover {
  background: var(--surface-2);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-weight: 500;
}

.field-row {
  display: flex;
  justify-content: space-between;
}

.field output {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

select {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}

input[type='range'] {
  width: 100%;
  accent-color: var(--ink);
}

.switch {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  cursor: pointer;
}

.switch input {
  appearance: none;
  flex: none;
  width: 2.6rem;
  height: 1.5rem;
  margin: 0;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background-color 0.2s;
}

.switch input::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc(1.5rem - 6px);
  height: calc(1.5rem - 6px);
  border-radius: 50%;
  background: var(--surface);
  transition: transform 0.3s var(--ease-spring);
}

.switch input:checked {
  background: var(--ink);
}

.switch input:checked::after {
  transform: translateX(1.1rem);
}

.history {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 22rem;
  overflow-y: auto;
}

.history li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.55rem 0.2rem;
  border-bottom: 1px solid var(--line);
}

.history li:first-child span {
  font-weight: 600;
}

.history time {
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

/* ---------- stage & wheel ---------- */

.stage {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
}

.wheel-wrap {
  position: relative;
  width: min(calc(100% - 2.5rem), calc(100dvh - 11rem), 760px);
  aspect-ratio: 1;
  min-width: 260px;
  border-radius: 50%;
  box-shadow: var(--wheel-shadow);
}

#wheel {
  display: block;
  width: 100%;
  height: 100%;
}

.pointer {
  position: absolute;
  z-index: 2;
  top: 50%;
  right: -20px;
  width: 44px;
  height: 36px;
  margin-top: -18px;
  background: var(--ink);
  clip-path: polygon(0 50%, 100% 0, 100% 100%);
  transform-origin: 88% 50%;
}

.pointer::after {
  content: '';
  position: absolute;
  top: 50%;
  right: 7px;
  width: 12px;
  height: 12px;
  margin-top: -6px;
  border-radius: 50%;
  background: var(--sun);
}

.pointer.flick {
  animation: flick 0.12s ease-out;
}

/* The wheel turns clockwise, so passing edges push the tip downward. */
@keyframes flick {
  40% {
    transform: rotate(-14deg);
  }
}

.hub {
  position: absolute;
  z-index: 2;
  left: 50%;
  top: 50%;
  width: 24%;
  aspect-ratio: 1;
  translate: -50% -50%;
  display: grid;
  place-items: center;
  padding: 0;
  border: 5px solid var(--seg-edge);
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  font: 800 clamp(0.9rem, 2.4vw, 1.5rem) / 1 var(--font-display);
  letter-spacing: -0.01em;
  box-shadow: 0 0 0 5px var(--sun), 0 10px 26px -6px rgba(0, 0, 0, 0.45);
  transition: scale 0.15s, box-shadow 0.15s;
}

.hub:hover:not(:disabled) {
  scale: 1.04;
}

.hub:active:not(:disabled) {
  scale: 0.96;
}

.hub:disabled {
  cursor: default;
  opacity: 0.6;
}

.is-spinning .hub {
  box-shadow: 0 0 0 5px var(--sun), 0 0 0 10px color-mix(in srgb, var(--sun) 35%, transparent),
    0 10px 26px -6px rgba(0, 0, 0, 0.45);
}

.empty-note {
  position: absolute;
  inset: auto 0 18%;
  margin: 0;
  text-align: center;
  color: var(--muted);
  font-weight: 500;
}

.summary {
  margin: 0.6rem 0 0;
  font-weight: 600;
  min-height: 1.5em;
}

.key-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.stage-exit {
  display: none;
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 3;
}

/* Fullscreen: the wheel takes over */
.stage:fullscreen,
.fake-fullscreen .stage {
  justify-content: center;
  background: var(--bg);
  padding: 2rem;
}

.fake-fullscreen .stage {
  position: fixed;
  inset: 0;
  z-index: 50;
}

.stage:fullscreen .stage-exit,
.fake-fullscreen .stage-exit {
  display: grid;
}

.stage:fullscreen .wheel-wrap,
.fake-fullscreen .wheel-wrap {
  width: min(92vw, calc(100dvh - 7rem));
}

.stage:fullscreen .key-hint,
.fake-fullscreen .key-hint {
  display: none;
}

/* ---------- result dialog ---------- */

dialog {
  width: min(92vw, 30rem);
  padding: 2rem 1.75rem 1.5rem;
  border: 0;
  border-radius: 22px;
  background: var(--surface);
  color: var(--ink);
  text-align: center;
  box-shadow: 0 30px 80px -20px rgba(0, 0, 0, 0.5);
}

dialog::backdrop {
  background: rgba(10, 12, 30, 0.55);
  backdrop-filter: blur(3px);
}

dialog[open] {
  animation: pop 0.28s cubic-bezier(0.2, 1.4, 0.4, 1);
}

@keyframes pop {
  from {
    transform: scale(0.85);
    opacity: 0;
  }
}

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

.result-name {
  margin: 0.4rem 0 1.6rem;
  font: 800 clamp(2rem, 9vw, 3.6rem) / 1.05 var(--font-display);
  letter-spacing: -0.03em;
  overflow-wrap: anywhere;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
}

.btn {
  padding: 0.65rem 1.2rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
}

.btn:hover {
  background: var(--surface-2);
}

.btn-primary {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--bg);
}

.btn-primary:hover {
  background: var(--ink);
  opacity: 0.88;
}

.btn-quiet {
  border-color: transparent;
  background: none;
  color: var(--muted);
}

/* ---------- panel spin button & cancel ---------- */

.panel-foot {
  padding: 0 1.1rem 1.1rem;
}

.btn-block {
  width: 100%;
  padding-block: 0.8rem;
  font: 800 1.1rem/1 var(--font-display);
}

.btn-primary.is-cancel {
  border-color: var(--line);
  background: var(--surface-2);
  color: var(--ink);
}

.btn-danger {
  border-color: var(--danger);
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover {
  background: var(--danger);
  opacity: 0.9;
}

.cancel-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.6rem 1rem;
  margin-top: 0.6rem;
  padding: 0.7rem 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.cancel-bar p {
  margin: 0;
  font-weight: 600;
}

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

/* Confetti layer: a popover, so it can sit above the modal result */
#confettiLayer {
  position: fixed;
  inset: 0;
  z-index: 2000;
  width: 100vw;
  height: 100vh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  pointer-events: none;
  overflow: visible;
}

#confettiLayer::backdrop {
  display: none;
}

/* ---------- motion ---------- */

/* Pressed feel on everything clickable */
.btn,
.chip,
.icon-btn,
.menu-list button {
  transition: background-color 0.15s, transform 0.15s var(--ease-out), opacity 0.15s;
}

.btn:active,
.chip:active,
.icon-btn:active,
.menu-list button:active {
  transform: scale(0.95);
}

/* Switching tabs */
.pane:not([hidden]) {
  animation: pane-in 0.32s var(--ease-out);
}

@keyframes pane-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

/* Preset menu */
.menu[open] .menu-list {
  animation: menu-in 0.22s var(--ease-out);
  transform-origin: top left;
}

.menu[open] .menu-list button {
  animation: pane-in 0.3s var(--ease-out) backwards;
}

.menu[open] .menu-list button:nth-child(2) { animation-delay: 0.02s; }
.menu[open] .menu-list button:nth-child(3) { animation-delay: 0.04s; }
.menu[open] .menu-list button:nth-child(4) { animation-delay: 0.06s; }
.menu[open] .menu-list button:nth-child(5) { animation-delay: 0.08s; }
.menu[open] .menu-list button:nth-child(6) { animation-delay: 0.1s; }
.menu[open] .menu-list button:nth-child(7) { animation-delay: 0.12s; }

.menu summary::after {
  transition: transform 0.25s var(--ease-out);
}

.menu[open] summary::after {
  transform: translateY(1px) rotate(225deg);
}

@keyframes menu-in {
  from {
    opacity: 0;
    transform: translateY(-6px) scale(0.96);
  }
}

/* Things that appear */
.pop {
  animation: pop-in 0.4s var(--ease-spring);
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: scale(0.8);
  }
}

.slide-in {
  animation: slide-in 0.45s var(--ease-out);
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateX(-16px);
    background: color-mix(in srgb, var(--sun) 40%, transparent);
  }
}

.bump {
  animation: bump 0.3s var(--ease-spring);
}

@keyframes bump {
  40% {
    transform: scale(1.25);
  }
}

.cancel-bar:not([hidden]) {
  animation: rise 0.3s var(--ease-out);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

.empty-note:not([hidden]) {
  animation: pane-in 0.4s var(--ease-out);
}

/* Wheel feedback */
.wheel-wrap.pulse {
  animation: pulse 0.45s var(--ease-spring);
}

@keyframes pulse {
  30% {
    transform: scale(0.965);
  }
}

.hub.press {
  animation: press 0.35s var(--ease-spring);
}

@keyframes press {
  35% {
    scale: 0.88;
  }
}

.icon-btn.twist svg {
  animation: twist 0.5s var(--ease-spring);
}

@keyframes twist {
  from {
    transform: rotate(-120deg) scale(0.6);
  }
}

/* Page load: one entrance */
.wheel-wrap {
  animation: wheel-in 0.9s var(--ease-out) backwards;
}

.panel {
  animation: pane-in 0.6s var(--ease-out) 0.15s backwards;
}

.pointer {
  animation: pointer-in 0.6s var(--ease-spring) 0.5s backwards;
}

@keyframes wheel-in {
  from {
    opacity: 0;
    transform: scale(0.88);
  }
}

@keyframes pointer-in {
  from {
    opacity: 0;
    translate: 24px 0;
  }
}

/* Result dialog leaving */
dialog.closing {
  animation: pop-out 0.2s ease-in forwards;
}

dialog.closing::backdrop {
  animation: fade-out 0.2s ease-in forwards;
}

dialog[open]::backdrop {
  animation: fade-in 0.25s ease-out;
}

.result-name {
  animation: name-in 0.5s var(--ease-spring) 0.08s backwards;
}

@keyframes pop-out {
  to {
    opacity: 0;
    transform: scale(0.92);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

@keyframes fade-out {
  to {
    opacity: 0;
  }
}

@keyframes name-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.9);
  }
}

/* Theme and language switch: soft crossfade of the whole page */
::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 0.35s;
  animation-timing-function: ease;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top, 0px) + 1rem);
  z-index: 3000;
  padding: 0.6rem 1.1rem;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 500;
  opacity: 0;
  transform: translate(-50%, -10px);
  pointer-events: none;
  transition: opacity 0.25s, transform 0.35s var(--ease-spring);
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- small screens ---------- */

@media (max-width: 820px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .stage {
    order: -1;
  }
  .wheel-wrap {
    width: min(calc(100% - 1.5rem), 520px);
    min-width: 0;
    margin-top: 0.6rem;
  }
  .key-hint {
    display: none;
  }
  .brand {
    font-size: 1.4rem;
  }
}

@media (max-width: 380px) {
  .icon-btn {
    width: 2.2rem;
    height: 2.2rem;
  }
  .top-actions {
    gap: 0.25rem;
  }
}

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