:root {
  color-scheme: dark;
  --bg: #0b1020;
  --panel: rgba(18, 24, 39, 0.92);
  --panel-2: #161f33;
  --text: #f8fafc;
  --muted: #93a4bf;
  --line: rgba(255,255,255,0.11);
  --accent: #8b5cf6;
  --accent-2: #22d3ee;
  --success: #22c55e;
  --danger: #fb7185;
  --rod: #b6c2d1;
  --rod-dark: #64748b;
  --shadow: 0 24px 70px rgba(0,0,0,.35);
}

:root.light {
  color-scheme: light;
  --bg: #eef2ff;
  --panel: rgba(255,255,255,.94);
  --panel-2: #f8fafc;
  --text: #172033;
  --muted: #64748b;
  --line: rgba(23,32,51,.12);
  --rod: #94a3b8;
  --rod-dark: #475569;
  --shadow: 0 24px 70px rgba(71,85,105,.18);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 18% 12%, rgba(139,92,246,.18), transparent 24rem),
    radial-gradient(circle at 82% 4%, rgba(34,211,238,.12), transparent 22rem),
    var(--bg);
  color: var(--text);
}

button { font: inherit; }

.app-shell {
  width: min(100% - 24px, 980px);
  margin: 0 auto;
  padding: 28px 0 44px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--accent-2);
  font-size: .74rem;
  font-weight: 800;
  letter-spacing: .17em;
}

h1, h2, p { margin-top: 0; }

h1 {
  margin-bottom: 5px;
  font-size: clamp(2rem, 5vw, 3.4rem);
  line-height: .95;
  letter-spacing: -.045em;
}

.subtitle {
  margin-bottom: 0;
  color: var(--muted);
}

.top-actions { display: flex; gap: 8px; }

.icon-btn,
.controls button,
.modal-actions button,
.mode-tab {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  border-radius: 14px;
  transition: transform .16s ease, border-color .16s ease, background .16s ease, opacity .16s ease;
}

.icon-btn {
  width: 46px;
  height: 46px;
  font-size: 1.1rem;
}

button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 65%, transparent);
  outline-offset: 2px;
}

@media (hover:hover) {
  button:hover:not(:disabled) { transform: translateY(-1px); border-color: rgba(139,92,246,.6); }
}

button:disabled { opacity: .38; cursor: not-allowed; }

.mode-tabs {
  display: inline-flex;
  gap: 6px;
  padding: 5px;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: color-mix(in srgb, var(--panel) 86%, transparent);
}

.mode-tab {
  min-height: 40px;
  padding: 0 17px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
}

.mode-tab.active {
  background: var(--accent);
  color: white;
}

.game-card {
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
  border-radius: 26px;
  overflow: hidden;
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border-bottom: 1px solid var(--line);
}

.hud > div {
  padding: 14px 16px;
  min-width: 0;
}

.hud > div + div { border-left: 1px solid var(--line); }

.label {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: .7rem;
  font-weight: 800;
  letter-spacing: .1em;
}

.hud strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mechanic-banner {
  margin: 16px 16px 0;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(139,92,246,.09);
  color: var(--muted);
  font-size: .92rem;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), minmax(66px, 1fr));
  gap: clamp(10px, 2.4vw, 24px);
  align-items: end;
  min-height: 420px;
  padding: 34px 18px 22px;
  overflow-x: auto;
  scrollbar-width: thin;
}

.rod-slot {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  min-width: 66px;
  min-height: 330px;
  padding-bottom: 20px;
  border: 0;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
}

.rod-slot::before {
  content: "";
  position: absolute;
  bottom: 25px;
  width: 12px;
  height: 278px;
  border-radius: 12px 12px 4px 4px;
  background: linear-gradient(90deg, var(--rod-dark), var(--rod) 45%, #e2e8f0 52%, var(--rod-dark));
  box-shadow: 0 4px 10px rgba(0,0,0,.28);
  z-index: 0;
}

.rod-slot::after {
  content: "";
  position: absolute;
  bottom: 12px;
  width: 78px;
  height: 25px;
  border-radius: 50%;
  background: linear-gradient(#475569, #1e293b);
  border: 2px solid rgba(255,255,255,.12);
  box-shadow: 0 9px 16px rgba(0,0,0,.3);
  z-index: 1;
}

.rod-slot.selected .ring:last-child {
  transform: translateY(-27px) scale(1.03);
  filter: brightness(1.08);
}

.rod-slot.valid-target::after { box-shadow: 0 0 0 4px rgba(34,211,238,.22), 0 9px 16px rgba(0,0,0,.3); }
.rod-slot.invalid-target { animation: shake .2s linear 2; }

.stack {
  position: relative;
  z-index: 2;
  width: 100%;
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 4px;
  padding-bottom: 13px;
  pointer-events: none;
}

.ring {
  position: relative;
  width: min(94%, 96px);
  height: 41px;
  border-radius: 50% / 36%;
  background: var(--ring-color);
  border: 3px solid color-mix(in srgb, var(--ring-color) 76%, black);
  box-shadow:
    inset 0 8px 8px rgba(255,255,255,.28),
    inset 0 -8px 10px rgba(0,0,0,.18),
    0 5px 8px rgba(0,0,0,.23);
  transition: transform .2s ease, filter .2s ease;
}

.ring::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 31%;
  height: 42%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: color-mix(in srgb, var(--panel-2) 70%, #020617);
  border: 2px solid rgba(255,255,255,.14);
  box-shadow: inset 0 3px 4px rgba(0,0,0,.5);
}

.ring.wild {
  background: conic-gradient(#ef4444, #f59e0b, #22c55e, #06b6d4, #8b5cf6, #ef4444);
  border-color: rgba(255,255,255,.7);
}

.ring.drop { animation: drop .22s ease-out; }
.ring.portal-flash { animation: portalFlash .45s ease; }

.rod-badge {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
  padding: 5px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--muted);
  font-size: .68rem;
  font-weight: 800;
  white-space: nowrap;
}

.rod-slot.portal::before { box-shadow: 0 0 0 4px rgba(139,92,246,.13), 0 0 22px rgba(139,92,246,.28); }
.rod-slot.one-way::before { background: linear-gradient(90deg, #713f12, #facc15 48%, #fef08a 55%, #713f12); }

.controls {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 9px;
  padding: 15px 16px 8px;
  border-top: 1px solid var(--line);
}

.controls button,
.modal-actions button {
  min-height: 46px;
  padding: 0 14px;
  font-weight: 800;
}

.primary {
  background: linear-gradient(135deg, var(--accent), #6d28d9) !important;
  border-color: transparent !important;
  color: white !important;
}

.status {
  min-height: 24px;
  margin: 0;
  padding: 6px 16px 18px;
  text-align: center;
  color: var(--muted);
  font-size: .9rem;
}

.legend {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.legend > div {
  display: grid;
  grid-template-columns: auto auto 1fr;
  gap: 7px;
  align-items: center;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--muted);
  font-size: .78rem;
}

.legend b { color: var(--text); }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; }
.legend-dot.wild { background: conic-gradient(#ef4444,#f59e0b,#22c55e,#06b6d4,#8b5cf6,#ef4444); }
.legend-dot.portal { background: #8b5cf6; box-shadow: 0 0 0 3px rgba(139,92,246,.2); }
.legend-dot.one-way { background: #facc15; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(2,6,23,.68);
  backdrop-filter: blur(8px);
}

.modal[hidden] { display: none; }

.modal-card {
  width: min(100%, 410px);
  padding: 30px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--panel);
  box-shadow: var(--shadow);
  text-align: center;
}

.win-orb {
  display: grid;
  place-items: center;
  width: 66px;
  height: 66px;
  margin: 0 auto 16px;
  border-radius: 50%;
  background: rgba(34,197,94,.14);
  color: var(--success);
  font-size: 2rem;
  font-weight: 900;
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-top: 18px;
}

@keyframes drop {
  from { transform: translateY(-42px) scale(.98); opacity: .6; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes portalFlash {
  0%,100% { filter: brightness(1); }
  45% { filter: brightness(1.7) saturate(1.4); transform: scale(1.05); }
}

@keyframes shake {
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

@media (max-width: 720px) {
  .app-shell { width: min(100% - 14px, 980px); padding-top: 16px; }
  .topbar { align-items: center; }
  .subtitle { font-size: .88rem; }
  .game-card { border-radius: 20px; }
  .board { min-height: 380px; padding-inline: 10px; gap: 7px; }
  .rod-slot { min-width: 62px; min-height: 305px; }
  .rod-slot::before { height: 252px; width: 10px; }
  .rod-slot::after { width: 64px; }
  .ring { height: 37px; border-width: 2px; }
  .controls { grid-template-columns: repeat(2, 1fr); }
  .legend { grid-template-columns: 1fr; }
}

@media (max-width: 460px) {
  .hud { grid-template-columns: repeat(2, 1fr); }
  .hud > div:nth-child(3) { border-left: 0; border-top: 1px solid var(--line); }
  .hud > div:nth-child(4) { border-top: 1px solid var(--line); }
  .board { grid-template-columns: repeat(var(--cols, 5), 64px); justify-content: start; }
  .rod-slot { min-width: 64px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
