:root {
  color-scheme: light;
  --sky: #d8f4ff;
  --sea: #2f6f73;
  --leaf: #3e8c61;
  --sand: #fff0c7;
  --gold: #ffc857;
  --berry: #ff7aa8;
  --lilac: #c6b7ff;
  --ink: #17313a;
  --card: rgba(255, 255, 255, 0.88);
  --shadow: 0 24px 80px rgba(20, 62, 72, 0.22);
}

* { box-sizing: border-box; }

html {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  height: 100dvh;
  overflow: hidden;
  font-family: ui-rounded, "Avenir Next", "SF Pro Rounded", system-ui, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 15%, rgba(255, 200, 87, 0.45), transparent 30%),
    radial-gradient(circle at 90% 10%, rgba(105, 200, 185, 0.55), transparent 30%),
    radial-gradient(circle at 70% 80%, rgba(198, 183, 255, 0.45), transparent 28%),
    linear-gradient(145deg, var(--sky), #fef6db 58%, #ccefcf);
}

button, select, input { font: inherit; touch-action: manipulation; }

.shell {
  position: relative;
  width: min(1180px, 100%);
  min-height: 100vh;
  height: 100dvh;
  overflow: hidden;
  margin: 0 auto;
  padding: clamp(12px, 2.4vw, 28px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  align-content: stretch;
  gap: clamp(10px, 2vw, 18px);
}

.sparkle-field {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  color: rgba(47, 111, 115, 0.28);
  font-size: clamp(2rem, 5vw, 4rem);
}

.sparkle-field span {
  position: absolute;
  animation: float 7s ease-in-out infinite;
}

.sparkle-field span:nth-child(1) { left: 8%; top: 18%; }
.sparkle-field span:nth-child(2) { right: 10%; top: 28%; animation-delay: -2s; }
.sparkle-field span:nth-child(3) { left: 74%; bottom: 10%; animation-delay: -4s; }

@keyframes float {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  50% { transform: translateY(-18px) rotate(7deg); }
}

.hero {
  text-align: center;
  z-index: 1;
}

.eyebrow {
  margin: 0 0 4px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--sea);
}

h1 {
  margin: 0;
  font-size: clamp(2.15rem, 6vw, 4.2rem);
  line-height: 0.95;
  text-shadow: 0 5px 0 rgba(255, 255, 255, 0.8), 0 14px 34px rgba(47, 111, 115, 0.2);
}

.tagline {
  margin: 8px auto 0;
  max-width: 720px;
  font-size: clamp(1rem, 2vw, 1.25rem);
}

.tablet-reader {
  z-index: 1;
  display: grid;
  gap: clamp(18px, 3vw, 28px);
  align-items: start;
  min-height: 0;
  overflow: hidden;
}

.controls, .story-card, .login-card, .settings-panel {
  background: var(--card);
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 34px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.login-modal[hidden] { display: none; }

.login-modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(23, 49, 58, 0.42);
}

.login-card {
  width: min(440px, 100%);
  padding: clamp(22px, 5vw, 34px);
  display: grid;
  gap: 16px;
}

.login-card h2 {
  margin: 0;
  font-size: clamp(2rem, 8vw, 3.2rem);
}

.settings-panel {
  padding: clamp(16px, 3vw, 24px);
}

.reading-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.reading-badges span {
  padding: 8px 12px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(255, 200, 87, 0.72), rgba(255, 122, 168, 0.25));
  font-weight: 900;
  box-shadow: 0 8px 18px rgba(158, 105, 18, 0.14);
}

.controls {
  display: grid;
  gap: 18px;
  padding: 0;
  background: transparent;
  border: 0;
  box-shadow: none;
  backdrop-filter: none;
}

label {
  display: grid;
  gap: 10px;
  font-size: 1.05rem;
  font-weight: 800;
}

select, input[type="range"], input[type="text"], input[type="password"], input:not([type]) {
  width: 100%;
  min-height: 54px;
  border: 3px solid rgba(47, 111, 115, 0.22);
  border-radius: 18px;
  background: #fffdf5;
  color: var(--ink);
  padding: 0 16px;
  font-size: 16px;
}

button {
  min-height: 58px;
  border: 0;
  border-radius: 22px;
  padding: 16px 22px;
  font-size: clamp(1rem, 2vw, 1.28rem);
  font-weight: 900;
  cursor: pointer;
  transition: transform 160ms ease, box-shadow 160ms ease, filter 160ms ease;
}

button:active { transform: scale(0.98); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 4px solid rgba(255, 200, 87, 0.9); outline-offset: 3px; }

.primary {
  color: #17313a;
  background: linear-gradient(135deg, var(--gold), #ffe58d);
  box-shadow: 0 12px 24px rgba(158, 105, 18, 0.22);
}

.secondary, .choice-button {
  color: white;
  background: linear-gradient(135deg, var(--sea), var(--leaf));
  box-shadow: 0 12px 24px rgba(47, 111, 115, 0.22);
}

.story-stage {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
}

.story-card {
  position: relative;
  padding: clamp(24px, 4vw, 52px);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  max-height: 100%;
  min-height: 0;
  border-color: rgba(255, 255, 255, 0.96);
  -webkit-overflow-scrolling: touch;
}

.story-card::before {
  content: "";
  position: absolute;
  inset: auto -12% -30% 36%;
  height: 220px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 200, 87, 0.34), transparent 68%);
}

.map-dot {
  position: absolute;
  top: 18px;
  right: 24px;
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--sand);
  font-size: 2rem;
  box-shadow: inset 0 -5px 0 rgba(158, 105, 18, 0.12);
}

.progress-label {
  margin: 0 72px 14px 0;
  color: var(--sea);
  font-weight: 900;
}

.story-text {
  position: relative;
  min-height: 180px;
  font-size: clamp(1.55rem, 3.7vw, 2.35rem);
  line-height: 1.45;
  font-weight: 750;
}

.choices {
  display: grid;
  gap: 14px;
  margin-top: 24px;
}

.choice-button {
  width: 100%;
  text-align: left;
  font-size: clamp(1.2rem, 2.7vw, 1.6rem);
}

.story-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 22px;
}

.ghost {
  background: rgba(47, 111, 115, 0.12);
  color: var(--sea);
  border: 2px solid rgba(47, 111, 115, 0.22);
  box-shadow: none;
}

.story-loader {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-top: 18px;
  color: var(--sea);
  font-weight: 900;
}

.story-loader[hidden] { display: none; }

.story-loader span {
  width: 22px;
  height: 22px;
  border: 4px solid rgba(47, 111, 115, 0.2);
  border-top-color: var(--sea);
  border-radius: 50%;
  animation: spin 850ms linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.message {
  min-height: 1.4em;
  margin: 16px 0 0;
  font-weight: 800;
  color: #9b4d0f;
}

/* Loading state hook: aria-busy="true" */
[aria-busy="true"] {
  filter: saturate(0.8);
}

[aria-busy="true"] button {
  pointer-events: none;
  filter: grayscale(0.25);
}

@media (min-width: 780px) {
  .tablet-reader {
    grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  }

  .settings-panel {
    position: sticky;
    top: clamp(18px, 4vw, 42px);
  }

  .story-card {
    min-height: 0;
    display: grid;
    align-content: start;
  }
}

@media (max-width: 760px) {
  .shell { align-content: stretch; }
  .story-actions { justify-content: stretch; }
  .story-actions button { width: 100%; }
}

@supports (-webkit-touch-callout: none) {
  html, body {
    height: auto;
    overflow: auto;
  }

  .shell {
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
    overflow: visible;
  }

  .tablet-reader,
  .story-stage {
    overflow: visible;
  }

  .story-card {
    max-height: none;
    overflow-y: visible;
  }
}

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