/* ============================================================
   తెలుగులో — pastel palette, light + dark.
   Theme is set on <html data-theme="light|dark"> by an inline
   script in index.html (device preference, or the saved choice
   from the sun/moon button). Each topic picks one palette colour
   via --base and the .tint class derives everything else from it.
   ============================================================ */

:root {
  /* Palette */
  --pink: #F7A9C4;
  --lilac: #C5A7ED;
  --peach: #FFB47D;
  --green: #B8DCA6;
  --blue: #A8D7F2;
  --yellow: #FFE19B;
  --ivory: #FFF9F2;
  --plum: #49314F;
  --rose: #F28DAD;
  --dusty-purple: #806B83;
  --soft-lavender: #EDE1FA;
  --blush: #FCE0EC;
  --mint: #DDEFCF;
  --butter: #FBE1AE;
  --baby-pink: #F9D7E7;
  --powder-blue: #D8EAFB;
  --dusty-lt: color-mix(in srgb, var(--dusty-purple) 55%, var(--soft-lavender));

  --font: 'Nunito', 'Noto Sans Telugu', 'Telugu Sangam MN', 'Telugu MN', system-ui, sans-serif;

  /* Light theme (default) */
  color-scheme: light;
  --bg: var(--ivory);
  --ink: var(--plum);
  --muted: var(--dusty-purple);
  --chip: hsl(0 0% 100% / 0.55);
  --edge: hsl(0 0% 100% / 0.6);
  --shadow: 0 14px 30px -14px color-mix(in srgb, var(--plum) 55%, transparent);
  --toggle-bg: var(--soft-lavender);

  --g1: var(--rose);
  --g2: color-mix(in srgb, var(--peach) 85%, var(--plum));
  --g3: color-mix(in srgb, var(--green) 70%, var(--plum));
  --g4: color-mix(in srgb, var(--lilac) 85%, var(--plum));

  --blob1: var(--baby-pink);
  --blob2: var(--powder-blue);
  --blob3: var(--butter);
  --blob4: var(--mint);

  --tint-soft: 50%;  /* how much palette colour in the lightest surface */
  --tint-mid: 88%;
  --tint-base: var(--ivory);
  --c-ink: var(--plum);
  --c-on: var(--plum);
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #251a29;
  --ink: var(--ivory);
  --muted: color-mix(in srgb, var(--soft-lavender) 75%, var(--dusty-purple));
  --chip: hsl(0 0% 100% / 0.12);
  --edge: hsl(0 0% 100% / 0.14);
  --shadow: 0 16px 32px -14px hsl(0 0% 0% / 0.7);
  --toggle-bg: color-mix(in srgb, var(--plum) 80%, var(--lilac));

  --g1: var(--pink);
  --g2: var(--peach);
  --g3: var(--green);
  --g4: var(--lilac);

  --blob1: color-mix(in srgb, var(--rose) 32%, #251a29);
  --blob2: color-mix(in srgb, var(--blue) 26%, #251a29);
  --blob3: color-mix(in srgb, var(--yellow) 22%, #251a29);
  --blob4: color-mix(in srgb, var(--lilac) 30%, #251a29);

  --tint-soft: 55%;
  --tint-mid: 74%;
  --tint-base: #251a29;
  --c-ink: var(--ivory);
  --c-on: #2b1d30;
}

/* One palette colour in (--base), whole set out */
.tint {
  --c-soft: color-mix(in srgb, var(--base) var(--tint-soft), var(--tint-base));
  --c-mid: color-mix(in srgb, var(--base) var(--tint-mid), var(--tint-base));
  --c-alt: color-mix(in srgb, var(--c-soft) 70%, var(--soft-lavender));
  --c-strong: color-mix(in srgb, var(--base) 82%, var(--plum));
}
:root[data-theme='dark'] .tint {
  --c-strong: var(--base);
  --c-alt: color-mix(in srgb, var(--c-soft) 75%, var(--plum));
}

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.6;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--c-strong, var(--g4)); outline-offset: 3px; }

/* Drifting colour blobs behind everything */
.blobs { position: fixed; inset: 0; z-index: 0; pointer-events: none; overflow: hidden; }
.blobs i {
  position: absolute;
  width: 46vmax; height: 46vmax;
  border-radius: 50%;
  filter: blur(70px);
  opacity: 0.85;
  animation: drift 26s ease-in-out infinite alternate;
}
.blobs i:nth-child(1) { background: var(--blob1); top: -18vmax; left: -14vmax; }
.blobs i:nth-child(2) { background: var(--blob2); top: 10vmax; right: -20vmax; animation-delay: -6s; }
.blobs i:nth-child(3) { background: var(--blob3); bottom: -20vmax; left: 10vmax; animation-delay: -12s; }
.blobs i:nth-child(4) { background: var(--blob4); bottom: 0; right: -10vmax; width: 30vmax; height: 30vmax; animation-delay: -18s; }
@keyframes drift {
  to { transform: translate3d(6vmax, 4vmax, 0) scale(1.12); }
}

/* ---------- Home ---------- */

.home {
  position: relative;
  z-index: 1;
  max-width: 980px;
  margin: 0 auto;
  padding:
    max(24px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(40px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

.hero { text-align: center; padding: 20px 0 28px; }
.hero h1 {
  margin: 0;
  padding-bottom: 0.12em;
  font-size: clamp(3.2rem, 14vw, 6rem);
  font-weight: 800;
  line-height: 1.4;
  background: linear-gradient(90deg, var(--g1), var(--g2), var(--g3), var(--g4));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.hero p { margin: 0; font-size: clamp(1.15rem, 4vw, 1.5rem); color: var(--muted); font-weight: 600; }

.topics {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}
@media (min-width: 640px) { .topics { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; } }
@media (min-width: 900px) { .topics { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

.topic {
  display: flex;
  flex-direction: column;
  gap: 4px;
  aspect-ratio: 1 / 1; /* every tile is a perfect square */
  justify-content: flex-end;
  padding: 16px;
  border-radius: 28px;
  text-decoration: none;
  color: var(--c-ink);
  background: linear-gradient(150deg, var(--c-soft), var(--c-mid));
  box-shadow: var(--shadow);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.topic:hover { transform: translateY(-4px) rotate(-0.6deg); }
.topic:active { transform: scale(0.97); }

.topic .name { font-size: 1.5rem; font-weight: 800; line-height: 1.45; }
/* Phone: two squares per row, so tighten the type to keep everything inside */
@media (max-width: 639px) {
  .topics .topic { padding: 14px; gap: 2px; border-radius: 24px; }
  .topics .topic .name { font-size: 1.15rem; line-height: 1.4; }
  .topics .topic .meta { font-size: 0.8rem; line-height: 1.45; }
  .topics .topic .meta.known { font-size: 0.76rem; }
}
.topic .meta { font-size: 0.95rem; opacity: 0.85; font-weight: 600; }
.topic .meta.known { font-size: 0.85rem; }
.meter { display: block; height: 8px; margin: 4px 0 2px; border-radius: 99px; background: var(--chip); overflow: hidden; }
.meter i { display: block; width: var(--p); height: 100%; border-radius: inherit; background: var(--c-strong); transition: width 0.4s ease; }

/* ---------- Deck ---------- */

.deck {
  position: relative;
  z-index: 1;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding:
    max(14px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  color: var(--c-ink);
  background: linear-gradient(165deg, var(--c-soft) 0%, var(--c-alt) 60%, var(--bg) 140%);
}

.topbar {
  width: min(100%, 560px);
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
}
.topbar h2 { margin: 0; font-size: 1.2rem; font-weight: 800; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar > :last-child { justify-self: end; }
.topbar .right { display: flex; gap: 8px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 44px;
  padding: 6px 14px;
  border: 0;
  border-radius: 99px;
  background: var(--c-mid);
  color: var(--c-ink);
  font-weight: 700;
  font-size: 0.95rem;
  text-decoration: none;
  cursor: pointer;
  justify-self: start;
  transition: transform 0.15s ease;
}
.pill:active { transform: scale(0.95); }

.progress {
  width: min(100%, 560px);
  height: 10px;
  margin-top: 14px;
  border-radius: 99px;
  background: var(--c-mid);
  overflow: hidden;
}
.progress i { display: block; width: var(--p); height: 100%; border-radius: inherit; background: var(--c-strong); transition: width 0.35s ease; }
.count { margin: 8px 0 0; font-weight: 700; font-size: 1.05rem; opacity: 0.85; }

.stage {
  flex: 1;
  width: 100%;
  display: grid;
  place-items: center;
  padding: 14px 0;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}

/* Flip card */
.card {
  position: relative;
  width: min(100%, 440px);
  height: clamp(300px, 52dvh, 500px);
  perspective: 1400px;
  cursor: pointer;
  outline-offset: 6px;
  border-radius: 36px;
  -webkit-tap-highlight-color: transparent;
}
.card-inner {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  transition: transform 0.6s cubic-bezier(0.3, 0.8, 0.25, 1);
}
.card.flipped .card-inner { transform: rotateY(180deg); }

.face {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px;
  text-align: center;
  border-radius: 36px;
  border: 3px solid var(--edge);
  box-shadow: var(--shadow);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;       /* safety net: app.js shrinks the text so it never gets here */
  overflow-wrap: normal;  /* never split a word mid-letter; only wrap at spaces */
  word-break: normal;
}

.face-front { background: linear-gradient(145deg, var(--c-soft), var(--c-mid)); padding-bottom: 58px; }
.face-back { background: linear-gradient(145deg, var(--c-alt), var(--c-mid)); transform: rotateY(180deg); }

/* Everything on a face sits in .fit; app.js lowers .fit's font-size until it fits.
   All sizes inside are in em so they shrink together. */
.fit { width: 100%; display: flex; flex-direction: column; align-items: center; }
.face-front .fit { font-size: clamp(2.8rem, 12vw, 4.6rem); }
.face-front .fit.big { font-size: clamp(5.5rem, 26vw, 8.5rem); }
.face-front .word { font-weight: 800; line-height: 1.45; white-space: nowrap; }
.face-front .word.wrap { white-space: normal; }
.face-front .hint {
  position: absolute; bottom: 20px;
  font-size: 0.95rem; font-weight: 600; opacity: 0.65;
}

.face-back .fit { font-size: clamp(15px, 4.4vw, 18px); gap: 0.2em; }
.face-back .label { font-size: 0.85em; font-weight: 700; letter-spacing: 0.04em; opacity: 0.65; }
.face-back .rom { font-size: 2.2em; font-weight: 800; line-height: 1.25; }
.face-back .mean { font-size: 1.55em; font-weight: 700; line-height: 1.3; }
.face-back .divider { width: 3.4em; height: 0.3em; margin: 0.6em 0 0.4em; border-radius: 99px; background: var(--c-strong); }

/* Slide-in when moving between cards */
.card.in-next { animation: in-next 0.35s ease both; }
.card.in-prev { animation: in-prev 0.35s ease both; }
@keyframes in-next { from { opacity: 0; transform: translateX(40px) scale(0.96); } }
@keyframes in-prev { from { opacity: 0; transform: translateX(-40px) scale(0.96); } }

/* Controls */
.actions {
  width: min(100%, 440px);
  display: flex;
  align-items: center;
  gap: 10px;
}
.round, .big {
  min-height: 56px;
  border: 0;
  border-radius: 99px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.2s ease;
}
.round:active, .big:active { transform: scale(0.95); }
.round {
  flex: 0 0 56px;
  font-size: 2rem;
  line-height: 1;
  background: var(--c-mid);
  color: var(--c-ink);
}
.round:disabled { opacity: 0.35; cursor: default; }
.big {
  flex: 1;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 12px;
  font-size: 1.15rem;
  text-decoration: none;
  white-space: nowrap;
}
.big.know { background: var(--c-strong); color: var(--c-on); }
.big.again { background: var(--c-mid); color: var(--c-ink); }
.big.home { background: var(--c-soft); color: var(--c-ink); }

/* Done screen */
.done-card {
  flex: 1;
  width: min(100%, 440px);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px;
  text-align: center;
  padding: 24px 0;
}
.party { font-size: 4.5rem; animation: pop 0.7s cubic-bezier(0.3, 1.6, 0.5, 1) both; }
.done-card h3 { margin: 0; font-size: 2.4rem; font-weight: 800; line-height: 1.5; }
.done-card p { margin: 0; font-size: 1.2rem; font-weight: 600; }
.done-card .score { margin-top: 8px; font-size: 1.5rem; font-weight: 800; }
.done-actions { display: flex; flex-direction: column; gap: 12px; width: 100%; margin-top: 22px; }
.done-actions .big { flex: none; }
@keyframes pop { from { transform: scale(0.2) rotate(-25deg); opacity: 0; } }

/* ---------- Larger screens ---------- */

@media (min-width: 900px) {
  .deck { justify-content: center; }
  .stage { flex: none; padding: 26px 0; }
  .card { width: 480px; height: 440px; }
}

@media (max-width: 360px) {
  .topbar h2 { font-size: 1.1rem; }
  .pill { padding: 6px 10px; font-size: 0.85rem; }
  .big { font-size: 1rem; }
}

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

/* ---------- Sun / moon theme toggle ---------- */

.theme-toggle {
  position: relative;
  flex: 0 0 auto;
  width: 44px; height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--toggle-bg);
  color: var(--ink);
  cursor: pointer;
  display: grid; place-items: center;
  overflow: hidden;
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.15s ease;
}
.theme-toggle:active { transform: scale(0.92); }
.theme-toggle svg {
  grid-area: 1 / 1;
  width: 24px; height: 24px;
  transition: transform 0.5s cubic-bezier(0.3, 1.4, 0.5, 1), opacity 0.3s ease;
}
.theme-toggle .moon { transform: rotate(-90deg) scale(0.3); opacity: 0; }
:root[data-theme='dark'] .theme-toggle .sun { transform: rotate(90deg) scale(0.3); opacity: 0; }
:root[data-theme='dark'] .theme-toggle .moon { transform: none; opacity: 1; }
.home-top { display: flex; justify-content: flex-end; }

/* ---------- Paisley pattern ----------
   icons/paisley.png is a white-on-transparent mask (alpha only), so the
   colour comes from --pat and can follow each topic and theme. */

.tint { --pat: color-mix(in srgb, var(--base) 45%, var(--plum)); --pat-op: 0.22; }
:root[data-theme='dark'] .tint { --pat: var(--base); --pat-op: 0.2; }

.face::before,
.topic::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  background: var(--pat);
  opacity: var(--pat-op);
  -webkit-mask: url('../icons/paisley.png') center / cover no-repeat;
  mask: url('../icons/paisley.png') center / cover no-repeat;
  pointer-events: none;
}
.face > *, .topic > * { position: relative; z-index: 1; }
.face-front .hint { position: absolute; }
.topic { position: relative; overflow: hidden; isolation: isolate; }
.topic::before { mask-size: 280%; -webkit-mask-size: 280%; }


/* ---------- Category screen (sub-topic tiles) ---------- */
.cat-screen .topbar { width: 100%; }
.cat-screen .pill { background: var(--toggle-bg); color: var(--ink); }
.hero.small { padding: 10px 0 22px; }
.hero.small h1 {
  font-size: clamp(2.6rem, 11vw, 4.4rem);
  background: none;
  color: var(--pat);
}

/* ---------- Card back: word type + forms button ---------- */
.pos-tag {
  display: inline-block;
  margin-top: 0.7em;
  padding: 0.2em 0.85em;
  border-radius: 99px;
  font-size: 0.85em;
  font-weight: 700;
  background: var(--chip);
  color: var(--c-ink);
}
.forms-btn {
  margin-top: 0.7em;
  min-height: 2.6em;
  padding: 0.35em 1.1em;
  border: 0;
  border-radius: 99px;
  background: var(--c-strong);
  color: var(--c-on);
  font-weight: 800;
  font-size: 1em;
  cursor: pointer;
  transition: transform 0.15s ease;
}
.forms-btn:active { transform: scale(0.95); }
.card:not(.flipped) .face-back,
.card.flipped .face-front { pointer-events: none; }

.pill.shuffle { padding: 6px 12px; }
.pill.shuffle svg { width: 20px; height: 20px; }
.party {
  display: grid; place-items: center;
  width: 5.5rem; height: 5.5rem;
  border-radius: 50%;
  font-size: 2.8rem; font-weight: 800;
  background: var(--c-strong);
  color: var(--c-on);
}

/* ---------- Forms sheet ---------- */
.sheet-root { position: fixed; inset: 0; z-index: 50; }
.sheet-root[hidden] { display: none; }
body.sheet-open { overflow: hidden; }
.sheet-backdrop {
  position: absolute; inset: 0;
  background: hsl(0 0% 0% / 0.38);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  animation: fade-in 0.2s ease both;
}
.sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  max-height: 88dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px max(18px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  border-radius: 28px 28px 0 0;
  color: var(--c-ink);
  background: linear-gradient(170deg, var(--c-soft), var(--bg) 70%);
  box-shadow: 0 -12px 40px -12px hsl(0 0% 0% / 0.4);
  animation: sheet-up 0.32s cubic-bezier(0.3, 0.9, 0.3, 1) both;
}
@media (min-width: 700px) {
  .sheet {
    left: 50%; top: 50%; bottom: auto; right: auto;
    width: min(560px, 92vw);
    max-height: 86dvh;
    border-radius: 28px;
    transform: translate(-50%, -50%);
    animation: sheet-pop 0.25s ease both;
  }
  .grabber { display: none; }
}
@keyframes fade-in { from { opacity: 0; } }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes sheet-pop { from { opacity: 0; transform: translate(-50%, -46%) scale(0.97); } }

.grabber { width: 44px; height: 5px; margin: 0 auto 10px; border-radius: 99px; background: var(--chip); }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.sheet-word { font-size: clamp(2.2rem, 9vw, 3rem); font-weight: 800; line-height: 1.4; }
.sheet-sub { font-size: 1.05rem; font-weight: 600; opacity: 0.85; }
.sheet-head .pos-tag { margin-top: 8px; }
.sheet .close { flex: 0 0 48px; min-height: 48px; font-size: 1.8rem; }
.sheet h4 { margin: 22px 0 8px; font-size: 1.05rem; font-weight: 800; }

.tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; margin-top: 18px; }
@media (min-width: 520px) { .tabs { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.tabs button {
  min-height: 42px;
  padding: 6px 12px;
  border: 0;
  border-radius: 99px;
  background: var(--chip);
  color: var(--c-ink);
  font-weight: 700;
  font-size: 0.92rem;
  cursor: pointer;
}
.tabs button[aria-selected='true'] { background: var(--c-strong); color: var(--c-on); }
.tense-en { margin: 8px 2px 6px; font-size: 0.85rem; font-weight: 700; opacity: 0.65; }

.ftable { border-radius: 18px; background: var(--chip); overflow: hidden; }
.frow {
  display: grid;
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.4fr);
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
}
.frow + .frow { border-top: 1px solid color-mix(in srgb, var(--c-ink) 10%, transparent); }
.frow .who { font-weight: 700; font-size: 0.95rem; opacity: 0.85; }
.frow .who small { display: block; font-size: 0.75rem; font-weight: 600; opacity: 0.75; }
.frow .form { font-weight: 800; font-size: 1.15rem; line-height: 1.45; }
.frow .form small { display: block; font-size: 0.8rem; font-weight: 600; opacity: 0.7; }
