:root {
  --bg: #fef6e4;
  --panel: #ffffff;
  --ink: #2b2140;
  --accent: #ff8a5b;
  --accent-dark: #f76c3c;
  --blue: #4f9dde;
  --green: #4caf7d;
  --red: #e5534b;
  --radius: 20px;
  font-family: "Segoe UI", "Comic Sans MS", "Trebuchet MS", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: linear-gradient(180deg, #fef6e4 0%, #fde8d0 100%);
  color: var(--ink);
  min-height: 100vh;
}

#app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px 20px 40px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 4px 20px;
}

.topbar h1 {
  font-size: clamp(1rem, 2.2vw, 1.5rem);
  margin: 0;
  flex: 1;
  text-align: left;
  cursor: default;
  user-select: none;
}

.menu-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  flex: none;
}

.menu-btn svg {
  width: 22px;
  height: 22px;
}

.menu-btn[hidden] {
  display: none;
}

.spacer {
  min-width: 110px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: flex-end;
}

.signin-terms {
  max-width: 260px;
  margin: 6px 0 0;
  font-size: 0.75rem;
  line-height: 1.3;
  text-align: right;
  color: #8a7c9d;
}

.signin-terms a {
  color: inherit;
}

/* Only alongside the Google button, i.e. on the sign-in screen. */
#googleSignInBtn[hidden] + .signin-terms {
  display: none;
}

.account-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.account-bar[hidden] {
  display: none;
}

.credit-balance {
  font-weight: 700;
  white-space: nowrap;
}

.topup-buttons {
  display: flex;
  gap: 6px;
}

.topup-btn {
  border: none;
  background: var(--green);
  color: white;
  font-weight: 700;
  padding: 8px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.9rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.topup-btn:not(:disabled):hover {
  transform: translateY(-1px);
}

.topup-btn:disabled {
  background: #c9c2d6;
  cursor: not-allowed;
}

.icon-btn {
  border: none;
  background: var(--panel);
  color: var(--ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 1rem;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.icon-btn:hover {
  background: #fff2df;
}

/* ---------- Login view ---------- */

#loginView {
  padding: 0 20px 60px;
}

#loginView[hidden] {
  display: none;
}

.landing-grid {
  max-width: 1200px;
  margin: 16px auto 0;
  display: grid;
  grid-template-columns: 1fr 2fr;
  align-items: start;
  gap: 40px;
}

.login-card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 40px 44px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sign-in-error {
  margin: 0;
  padding: 12px 16px;
  border-radius: 12px;
  border: 2px solid var(--red);
  font-weight: 600;
}

.sign-in-error small {
  display: block;
  font-weight: 400;
  margin-top: 4px;
}

.landing-title {
  margin: 0;
  font-size: clamp(1.3rem, 3vw, 1.8rem);
}

.landing-lead {
  font-weight: 600;
  margin: 0;
}

.login-tagline {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0;
}

.landing-image {
  display: flex;
  justify-content: center;
}

.landing-image img {
  width: 100%;
  height: auto;
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

@media (max-width: 860px) {
  .landing-grid {
    grid-template-columns: 1fr;
  }

  .landing-image {
    order: -1;
  }
}

/* ---------- Consent view ---------- */

#consentView {
  display: flex;
  justify-content: center;
  padding-top: 24px;
}

#consentView[hidden] {
  display: none;
}

#verifyEmailView {
  display: flex;
  justify-content: center;
  padding-top: 24px;
}

#verifyEmailView[hidden] {
  display: none;
}

.verify-email-address {
  font-weight: 700;
  text-align: center;
  word-break: break-all;
}

.tutor-message.info {
  color: inherit;
}

.verify-email-switch {
  text-align: center;
  font-size: 0.9rem;
  margin-bottom: 0;
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
}

.consent-card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  padding: 32px 36px;
  max-width: 640px;
  width: 100%;
}

.consent-card h2 {
  margin-top: 0;
}

.consent-points {
  padding-left: 20px;
  line-height: 1.5;
}

.consent-points li {
  margin-bottom: 10px;
}

.account-type {
  border: none;
  padding: 0;
  margin: 20px 0 0;
}

.account-type legend {
  font-weight: 700;
  margin-bottom: 8px;
}

.account-type-option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  line-height: 1.4;
}

.consent-checkbox[hidden] {
  display: none;
}

.consent-checkbox {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 20px 0;
  cursor: pointer;
  line-height: 1.4;
}

.consent-checkbox input {
  flex: none;
  width: 20px;
  height: 20px;
  margin-top: 2px;
}

/* ---------- Side menu (Top Up / Sign out, behind the parent gate) ---------- */

.side-menu-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 20;
}

.side-menu-overlay[hidden] {
  display: none;
}

.side-menu {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 220px;
  max-width: 75vw;
  background: var(--panel);
  box-shadow: 4px 0 16px rgba(0, 0, 0, 0.15);
  z-index: 21;
  display: flex;
  flex-direction: column;
  padding: 24px 0;
}

.side-menu[hidden] {
  display: none;
}

.side-menu-item {
  border: none;
  background: none;
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 600;
  text-align: left;
  padding: 14px 24px;
  cursor: pointer;
}

.side-menu-item:hover {
  background: #fff2df;
}

/* ---------- Parents-only view ---------- */

#parentGateView,
#parentView {
  display: flex;
  justify-content: center;
  padding-top: 60px;
}

#parentGateView[hidden],
#parentView[hidden] {
  display: none;
}

.parent-gate-question {
  font-size: 1.5rem;
  font-weight: 700;
  text-align: center;
}

#parentGateAnswer {
  width: 100%;
  font-size: 1.2rem;
  padding: 10px 14px;
  border-radius: 12px;
  border: 2px solid #e0d5f0;
  box-sizing: border-box;
}

.parent-gate-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 16px;
}

.parent-email {
  font-size: 0.9rem;
  color: #8a7c9d;
  text-align: center;
  margin: 0;
}

.parent-credit-balance {
  font-size: 1.3rem;
  font-weight: 700;
  text-align: center;
  margin: 12px 0;
}

/* ---------- Library view ---------- */

.tabs {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-bottom: 24px;
}

.tab-btn {
  border: none;
  padding: 9.6px 17.6px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  transition: transform 0.1s ease, background 0.15s ease;
}

.tab-btn:hover {
  transform: translateY(-2px);
}

.tab-btn.active {
  background: var(--accent);
  color: white;
}

.deck-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 18px;
}

.deck-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
  border: 3px solid transparent;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.deck-card:hover {
  transform: translateY(-4px);
  border-color: var(--accent);
}

.deck-card img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 14px;
  background: #f2e9d8;
}

.deck-card .cover-placeholder {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  background: #f2e9d8;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
}

.deck-card .deck-name {
  margin-top: 10px;
  font-weight: 700;
  font-size: 1.525rem;
}

.deck-card .deck-count {
  font-size: 1.25rem;
  color: #766a8a;
}

.empty-note {
  text-align: center;
  color: #8a7c9d;
  padding: 40px 0;
}

/* ---------- Reader view ---------- */

.reader-meta {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 4px 8px 14px;
  font-weight: 600;
  color: #6a5c85;
}

.reader-title-group {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-self: start;
}

.reader-nav-group {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-self: center;
}

.reader-meta-right {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 14px;
}

.nav-btn-small {
  border: none;
  background: var(--blue);
  color: white;
  line-height: 1;
  width: 56px;
  height: 56px;
  flex: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
  transition: transform 0.1s ease;
}

.nav-btn-small svg {
  width: 28px;
  height: 28px;
}

.nav-btn-small:not(:disabled):hover {
  transform: translateY(-1px) scale(1.05);
}

.nav-btn-small:disabled {
  background: #c9c2d6;
  cursor: not-allowed;
  box-shadow: none;
}

.reader-meta .tutor-btn {
  padding: 8px 16px;
  font-size: 0.9rem;
}

.reader-panels {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  margin-top: 26px;
}

.text-panel,
.image-panel {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  min-height: 320px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  /* min-width: 0 lets the two panels shrink to share a narrow row instead of
     each demanding their full width and wrapping (which stacked them on
     phones). */
  min-width: 0;
}

.text-panel {
  flex: 1 1 0;
}

.image-panel {
  flex: 1 1 0;
  overflow: hidden;
  align-items: flex-start;
}

#pageText {
  font-size: clamp(1.8rem, 4vw, 3rem);
  line-height: 1.35;
  font-weight: 700;
  text-align: center;
  margin: 0;
  word-break: break-word;
}

#readerView[data-category="Books"] #pageText {
  font-size: clamp(0.9rem, 2vw, 1.5rem);
  font-weight: 400;
  text-align: left;
}

.word {
  cursor: pointer;
  border-radius: 6px;
}

.word.letter {
  font-size: 1.8em;
}

.word:hover {
  background: #fff2df;
}

.word.mispronounced,
.word.tapped {
  text-decoration: underline;
  text-decoration-color: var(--red);
  text-decoration-thickness: 3px;
  text-underline-offset: 4px;
}

.image-panel img {
  max-width: 100%;
  max-height: 420px;
  border-radius: 14px;
  object-fit: contain;
}

.image-placeholder {
  font-size: 4rem;
  color: #cbb;
  opacity: 0.6;
}

/* ---------- Tutor controls ---------- */

.tutor-message {
  margin: 0 8px 14px;
  color: var(--red);
  font-weight: 600;
}

.tutor-message[hidden] {
  display: none;
}

.tutor-btn {
  border: none;
  background: var(--accent);
  color: white;
  font-size: 1.05rem;
  font-weight: 700;
  padding: 12px 20px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
}

.tutor-btn.secondary {
  background: var(--green);
}

.tutor-btn:disabled {
  background: #c9c2d6;
  cursor: not-allowed;
  box-shadow: none;
}

.tutor-btn.active {
  background: var(--red);
}

/* Buddy face status: a big, literal indicator (face + word) so young kids
   can tell at a glance whether Buddy is listening, thinking, or talking -
   far clearer than a 16px colored dot. */
.buddy-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px;
  background: var(--panel);
  border-radius: 999px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.buddy-face {
  width: 42px;
  height: 42px;
  flex: none;
  border-radius: 50%;
  background: #f2e9d8;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.55rem;
}

/* Kept in the DOM (not display:none) so screen readers still announce the
   status text via the parent's aria-live region; sighted users see only
   the face icon. */
.buddy-caption {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.buddy-status.connecting .buddy-face {
  background: #fbe6bd;
  animation: buddy-pulse 1s infinite;
}

.buddy-status.listening .buddy-face {
  background: rgba(79, 157, 222, 0.28);
  animation: buddy-pulse 0.9s infinite;
}

.buddy-status.thinking .buddy-face {
  background: #fff2df;
}

.buddy-status.speaking .buddy-face {
  background: rgba(255, 138, 91, 0.32);
  animation: buddy-bounce 0.6s infinite;
}

.buddy-status.error .buddy-face {
  background: rgba(229, 83, 75, 0.25);
}

@keyframes buddy-pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes buddy-bounce {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-4px);
  }
}

@media (max-width: 640px) {
  /* Keep text and image side by side on phones, just more compact: never
     wrap (nowrap), tighter gap/padding, and smaller min-heights so two
     columns fit comfortably in both portrait and landscape. */
  .reader-panels {
    flex-wrap: nowrap;
    gap: 12px;
    margin-top: 16px;
  }
  .text-panel,
  .image-panel {
    min-height: 200px;
    padding: 14px;
  }
  #pageText {
    font-size: clamp(1.2rem, 4.8vw, 2rem);
  }
  .image-panel img {
    max-height: 60vh;
  }
  .reader-meta-right {
    justify-content: center;
  }
}

/* ---------- Footer ---------- */

.app-footer {
  text-align: center;
  padding: 28px 4px 12px;
  font-size: 0.8rem;
  color: #8a7c9d;
}

.app-footer p {
  margin: 0 0 4px;
}

.app-footer a {
  color: inherit;
}

.consent-contact,
.parent-contact {
  font-size: 0.9rem;
  color: #8a7c9d;
  line-height: 1.4;
}

.parent-contact {
  text-align: center;
}

:root[data-theme="calm"] .parent-contact {
  color: #a99fc4;
}

:root[data-theme="calm"] .app-footer {
  color: #a99fc4;
}

/* ---------- Calm mode (dark, low-stimulation) ---------- */
/* A softer, dimmer palette with the animations and hover bounces turned
   off, for kids who read better without bright colors and movement. */

:root[data-theme="calm"] {
  --bg: #1f1c2e;
  --panel: #2b2740;
  --ink: #ece8f5;
  --accent: #7f9fce;
  --accent-dark: #6f8dbb;
  --blue: #6d8fb4;
  --green: #6fae8e;
  --red: #d98a84;
}

:root[data-theme="calm"] body {
  background: var(--bg);
}

:root[data-theme="calm"] .icon-btn:hover,
:root[data-theme="calm"] .side-menu-item:hover,
:root[data-theme="calm"] .word:hover {
  background: #3a3556;
}

:root[data-theme="calm"] .buddy-face,
:root[data-theme="calm"] .deck-card img,
:root[data-theme="calm"] .deck-card .cover-placeholder {
  background: #3a3556;
}

:root[data-theme="calm"] .deck-count {
  color: #a99fc4;
}

:root[data-theme="calm"] .reader-meta,
:root[data-theme="calm"] .empty-note,
:root[data-theme="calm"] .parent-email {
  color: #a99fc4;
}

/* Low-stimulation: no pulsing, bouncing, or hover lifts. */
:root[data-theme="calm"] * {
  animation: none !important;
}

:root[data-theme="calm"] .deck-card:hover,
:root[data-theme="calm"] .tab-btn:hover,
:root[data-theme="calm"] .topup-btn:hover,
:root[data-theme="calm"] .nav-btn-small:not(:disabled):hover {
  transform: none;
}

/* Respect a system-level reduced-motion preference everywhere too. */
@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
  }
}
