:root {
  --bg: #fff9f0;
  --card-bg: #ffffff;
  --text: #3d2c2c;
  --text-soft: #7a6a6a;
  --accent: #ff8c42;
  --accent-hover: #e67a30;
  --blue: #5b9bd5;
  --blue-hover: #4a89c4;
  --green: #6dbd78;
  --green-hover: #5aa865;
  --pink: #f08ca0;
  --yellow: #ffd56b;
  --purple: #b39ddb;
  --shadow: 0 6px 20px rgba(0,0,0,0.08);
  --radius: 22px;
  --transition: 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  background: linear-gradient(135deg, #fff3e0 0%, #fce4ec 30%, #e8f5e9 60%, #e3f2fd 100%);
  background-attachment: fixed;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.container {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 20px;
  height: 100%;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  text-align: center;
  padding: 10px 0 20px;
  flex-shrink: 0;
}

header h1 {
  font-size: 2.2rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.02em;
}

header p {
  font-size: 1rem;
  color: var(--text-soft);
  margin-top: 4px;
}

.admin-link {
  display: none;
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 50;
  padding: 8px 16px;
  border-radius: 20px;
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  text-decoration: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  transition: transform var(--transition), background var(--transition);
}

.admin-link:hover { background: var(--accent-hover); transform: scale(1.05); }
.admin-link:active { transform: scale(0.95); }

.back-btn {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 50;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 2px solid #ddd;
  background: rgba(255,255,255,0.92);
  font-size: 1.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,0.1);
  transition: transform var(--transition);
}

.back-btn:hover { transform: scale(1.08); }
.back-btn:active { transform: scale(0.95); }

.fullscreen-btn {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 50;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 2px solid #ddd;
  background: rgba(255,255,255,0.92);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,0.1);
  transition: transform var(--transition);
}

.fullscreen-btn svg {
  width: 22px;
  height: 22px;
  fill: var(--text);
}

.fullscreen-btn:hover { transform: scale(1.08); }
.fullscreen-btn:active { transform: scale(0.95); }

.view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.view[hidden] { display: none !important; }

.category-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 18px;
  padding: 10px 0;
  overflow-y: auto;
}

.category-tile {
  min-height: 150px;
  border-radius: var(--radius);
  border: 2px solid transparent;
  background: var(--card-bg);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0px;
  padding: 20px 14px;
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition);
  position: relative;
  overflow: hidden;
}

.category-tile::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 5px;
  border-radius: var(--radius) var(--radius) 0 0;
}

.category-tile:nth-child(5n+1)::before { background: var(--accent); }
.category-tile:nth-child(5n+2)::before { background: var(--blue); }
.category-tile:nth-child(5n+3)::before { background: var(--green); }
.category-tile:nth-child(5n+4)::before { background: var(--pink); }
.category-tile:nth-child(5n+5)::before { background: var(--purple); }

.category-tile:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 28px rgba(0,0,0,0.12);
}

.category-tile:active {
  transform: scale(0.96);
  transition: transform 80ms ease;
}

.category-tile .placeholder {
  font-size: 3.2rem;
  line-height: 1;
}

.category-tile .thumbnail {
  width: 100%;
  height: 100px;
  object-fit: cover;
  border-radius: 8px;
}

.category-tile .name {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  text-align: center;
}

.category-tile .card-count {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted, #888);
  text-align: center;
}

.flashcard-view {
  position: relative;
  contain: content;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 10px 0;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.flashcard-stage {
  position: relative;
  flex: 1;
  align-self: stretch;
  min-height: 0;
  display: grid;
  grid-template-areas: "stack";
  grid-template-rows: 1fr;
}

.flashcard-card {
  grid-area: stack;
  width: 100%;
  max-width: 480px;
  justify-self: center;
  border-radius: 28px;
  background: var(--card-bg);
  box-shadow: 0 10px 36px rgba(0,0,0,0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform var(--transition), opacity var(--transition);
  will-change: opacity, transform;
}

.flashcard-card.card-next {
  opacity: 0;
  pointer-events: none;
  transform: none;
  transition: none;
}

.flashcard-card.swipe-out {
  opacity: 0;
  transform: translateX(-60px);
}

.flashcard-card .card-image {
  width: 100%;
  flex: 1 1 0;
  min-height: 80px;
  object-fit: contain;
}

.card-image[hidden],
.card-image-placeholder[hidden] {
  display: none !important;
}

.flashcard-card .card-image-placeholder {
  width: 100%;
  flex: 1 1 0;
  min-height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(3rem, 8vw, 5rem);
}

.flashcard-card .card-body {
  padding: 12px 20px 16px;
  text-align: center;
  display: grid;
  gap: 6px;
  flex-shrink: 0;
}

.flashcard-card .card-word {
  font-size: clamp(2rem, 5vw, 3.5rem);
  font-weight: 800;
  color: var(--accent);
  line-height: 1.15;
  text-transform: capitalize;
}

.flashcard-card .card-syllables {
  font-size: 2rem;
  color: var(--blue);
  font-weight: 600;
}

.flashcard-card .card-sentence {
  font-size: 2rem;
  color: var(--text-soft);
  background: #faf7f2;
  border-radius: 14px;
  padding: 10px 14px;
  line-height: 1.5;
}

.audio-btns {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-shrink: 0;
  padding: 4px 0;
}

.audio-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 2px solid #e8e0d8;
  background: var(--card-bg);
  font-size: 2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
  transition: transform var(--transition), border-color var(--transition), background var(--transition);
  color: var(--accent);
}

.audio-btn:hover { transform: scale(1.08); border-color: var(--accent); }
.audio-btn:active { transform: scale(0.92); }

.audio-btn.playing {
  border-color: var(--accent);
  background: #fff2e8;
  animation: pulse 0.8s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

.nav-btns {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  margin-top: 10px;
  flex-shrink: 0;
}

.nav-arrow {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 2px solid #ddd;
  background: rgba(255,255,255,0.92);
  font-size: 1.8rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0,0,0,0.08);
  transition: transform var(--transition), background var(--transition);
  color: var(--text);
}

.nav-arrow:hover { background: #fff; transform: scale(1.1); }
.nav-arrow:active { transform: scale(0.9); }
.nav-arrow:disabled { opacity: 0.35; cursor: not-allowed; }

#prevBtn, #nextBtn {
  position: fixed;
  top: 50%;
  z-index: 50;
}

#prevBtn { left: 16px; transform: translateY(-50%); }
#nextBtn { right: 16px; transform: translateY(-50%); }

#prevBtn:hover, #nextBtn:hover { background: #fff; transform: translateY(-50%) scale(1.1); }
#prevBtn:active, #nextBtn:active { transform: translateY(-50%) scale(0.9); }
#prevBtn:disabled, #nextBtn:disabled { opacity: 0.35; cursor: not-allowed; transform: translateY(-50%); }

.card-counter {
  font-size: 1rem;
  color: var(--text-soft);
  font-weight: 600;
  min-width: 40px;
  text-align: center;
}

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-soft);
  font-size: 1.25rem;
  line-height: 1.6;
}

@media (min-width: 600px) {
  .category-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
  .flashcard-card { max-width: 520px; }
}

@media (orientation: portrait) {
  .container { padding-left: 4px; padding-right: 4px; }

  .flashcard-view {
    justify-content: space-around;
    gap: 12px;
  }

  .flashcard-stage {
    flex: none;
    min-height: 350px;
  }

  .flashcard-card {
    align-self: center;
    width: 92%;
    max-width: stretch;
    height: 100%;
    justify-content: center;
    align-items: center;
    gap: 24px;
    padding: 24px;
  }

  .flashcard-card .card-image {
    flex: none;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 38vh;
    object-fit: contain;
  }

  .flashcard-card .card-image-placeholder {
    flex: none;
    width: auto;
    height: auto;
    min-height: 0;
    font-size: clamp(4.5rem, 16vw, 7rem);
  }

  .flashcard-card .card-body {
    padding: 0;
  }

  #prevBtn, #nextBtn { display: none; }
}

@media (orientation: landscape) and (max-height: 500px) {
  .container { padding: 8px 16px; }

  header { display: none; }

  .flashcard-view {
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
  }

  .flashcard-stage {
    flex: 1;
    align-self: stretch;
    height: auto;
    min-height: 0;
  }

  .flashcard-card {
    flex-direction: row;
    text-align: left;
    width: 100%;
    max-width: none;
    height: 100%;
    gap: 16px;
    padding: 16px;
  }

  .flashcard-card .card-image,
  .flashcard-card .card-image-placeholder {
    flex: 0 0 50%;
    width: 50%;
    height: 100%;
    max-height: none;
    object-fit: contain;
  }

  .flashcard-card .card-image-placeholder {
    font-size: clamp(2.5rem, 10vh, 4rem);
  }

  .flashcard-card .card-body {
    flex: 1;
    text-align: left;
    align-self: center;
    padding: 0 64px 0 0;
  }

  .audio-btns {
    position: absolute;
    top: 50%;
    right: 28px;
    transform: translateY(-50%);
    flex-direction: column;
    gap: 12px;
    padding: 0;
    flex-shrink: 0;
    z-index: 10;
  }
  .audio-btn { width: 48px; height: 48px; font-size: 1.4rem; }

  .nav-btns { display: none; }

  #prevBtn, #nextBtn { display: none; }

  body.is-fullscreen .fullscreen-btn {
    top: 12px;
    right: 12px;
    bottom: auto;
    width: 40px;
    height: 40px;
  }

  body.is-fullscreen .fullscreen-btn svg {
    width: 18px;
    height: 18px;
  }
}

@media (max-width: 400px) {
  .category-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .category-tile { min-height: 130px; }
  .category-tile .placeholder { font-size: 2.6rem; }
  .audio-btn { width: 60px; height: 60px; font-size: 1.6rem; }
  .nav-arrow { width: 56px; height: 56px; }
}

/* ---------- Fullscreen ---------- */

body.is-fullscreen .back-btn,
body.is-fullscreen .admin-link {
  display: none !important;
}

body.is-fullscreen {
  overflow: hidden;
}

#sceneRoot.fake-fullscreen,
#sceneRoot:fullscreen,
#sceneRoot:-webkit-full-screen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100dvh;
  max-width: none;
  min-height: 0;
  margin: 0;
  z-index: 9999;
  background: var(--bg);
}

/* ---------- Subscribe Overlay ---------- */

.subscribe-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.45);
  backdrop-filter: blur(6px);
  padding: 24px;
}

.subscribe-overlay[hidden] { display: none !important; }

.subscribe-overlay-card {
  background: var(--card-bg);
  border-radius: 28px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  padding: 40px 28px 32px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.18);
  animation: overlayFadeIn 0.35s ease;
}

@keyframes overlayFadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.subscribe-overlay-icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.subscribe-overlay-title {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--accent);
  margin-bottom: 10px;
}

.subscribe-overlay-text {
  font-size: 1.05rem;
  color: var(--text-soft);
  line-height: 1.6;
  margin-bottom: 24px;
}

.subscribe-overlay-btn {
  display: block;
  width: 100%;
  padding: 14px 24px;
  border: none;
  border-radius: 16px;
  background: var(--accent);
  color: #fff;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(255, 140, 66, 0.35);
  transition: transform var(--transition), background var(--transition);
  margin-bottom: 12px;
}

.subscribe-overlay-btn:hover { background: var(--accent-hover); transform: scale(1.03); }
.subscribe-overlay-btn:active { transform: scale(0.97); }
.subscribe-overlay-btn:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }

.subscribe-overlay-dismiss {
  display: inline-block;
  background: none;
  border: none;
  color: var(--text-soft);
  font-size: 0.95rem;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 12px;
  transition: background var(--transition);
}

.subscribe-overlay-dismiss:hover { background: #f0ebe3; }

.ctx-menu {
  position: fixed;
  z-index: 200;
  min-width: 180px;
  padding: 6px 0;
  background: var(--card-bg);
  border-radius: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
}

.ctx-menu[hidden] { display: none !important; }

.ctx-menu-item {
  display: block;
  width: 100%;
  border: none;
  background: none;
  padding: 10px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: background var(--transition);
}

.ctx-menu-item:hover { background: #fff2e8; }
.ctx-menu-item:active { background: #ffe0cc; }
