:root {
  color-scheme: light;
  --ink: #172033;
  --muted: #64748b;
  --line: #d8e1ec;
  --page: #f5f8fc;
  --primary: #137177;
  --primary-dark: #0d555a;
  --danger: #b42318;
  --soft: #e8f6f4;
  --shadow: 0 18px 45px rgba(26, 45, 68, 0.13);
}

* {
  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;
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 0%, rgba(217, 149, 34, 0.18), transparent 28rem),
    radial-gradient(circle at 100% 15%, rgba(19, 113, 119, 0.12), transparent 24rem),
    var(--page);
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0 14px;
  cursor: pointer;
  font-weight: 750;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

button.primary {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

button.primary:hover {
  background: var(--primary-dark);
}

button.secondary {
  color: var(--ink);
  background: #fff;
  border-color: var(--line);
}

button.secondary:hover {
  background: #f7fafc;
}

button.edit {
  color: #0f5f9f;
  background: #eef7ff;
  border-color: #b8d9f3;
}

button.edit:hover {
  background: #dff0ff;
}

button.danger {
  color: var(--danger);
  background: #fff;
  border-color: #f0b8b2;
}

button.icon {
  width: 42px;
  padding: 0;
}

main {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 34px 0 48px;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 18px;
  align-items: start;
  margin-bottom: 22px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  max-width: 760px;
  font-size: clamp(2rem, 4.8vw, 4.1rem);
  line-height: 1;
  letter-spacing: 0;
}

.lead {
  max-width: 760px;
  margin-top: 12px;
  color: var(--muted);
  line-height: 1.62;
  font-size: 1.04rem;
}

.hero-actions,
.voice-panel,
form,
.card-body {
  display: grid;
}

.hero-actions {
  gap: 12px;
  justify-items: end;
}

.mode-switch {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  width: min(360px, 100%);
  padding: 6px;
  background: #e6edf3;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.mode-button {
  color: var(--muted);
  background: transparent;
}

.mode-button.active {
  color: #fff;
  background: var(--primary);
  box-shadow: 0 8px 18px rgba(19, 113, 119, 0.22);
}

.voice-panel {
  gap: 10px;
  min-width: min(360px, 100%);
  padding: 14px;
  background: rgba(255, 255, 255, 0.82);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.voice-grid,
.app-grid,
.library-tools,
.classification-grid,
.form-actions,
.listen-row,
.card-actions {
  display: grid;
  gap: 12px;
}

.voice-grid {
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.classification-grid {
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 700;
}

select,
input[type="text"],
input[type="password"],
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #fbfdff;
  outline: none;
}

select,
input[type="text"],
input[type="password"] {
  min-height: 42px;
  padding: 0 11px;
}

textarea {
  min-height: 88px;
  resize: vertical;
  padding: 12px;
  line-height: 1.5;
}

select:focus,
input[type="text"]:focus,
input[type="password"]:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(19, 113, 119, 0.16);
}

.app-grid {
  grid-template-columns: 360px 1fr;
  align-items: start;
  gap: 18px;
}

body.kid-mode .app-grid {
  grid-template-columns: 1fr;
  grid-template-areas: none;
}

body.parent-mode .app-grid {
  grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
  grid-template-areas:
    "sidebar creator"
    "library library";
}

body.parent-mode.admin-locked .app-grid {
  grid-template-columns: minmax(280px, 420px);
  grid-template-areas: "sidebar";
  justify-content: center;
}

body.kid-mode .parent-only {
  display: none;
}

body.admin-locked .admin-only {
  display: none;
}

body.parent-mode.admin-locked .admin-gated-library {
  display: none;
}

body.admin-authenticated .guest-only {
  display: none;
}

.panel {
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.creator {
  position: sticky;
  top: 16px;
  overflow: hidden;
}

body.parent-mode .creator {
  grid-area: creator;
  position: static;
}

.parent-sidebar {
  position: sticky;
  top: 16px;
  display: grid;
  gap: 14px;
}

body.parent-mode .parent-sidebar {
  grid-area: sidebar;
  position: static;
}

body.parent-mode.admin-locked .parent-sidebar {
  position: static;
  width: min(420px, 100%);
}

.auth-panel,
.password-panel {
  overflow: hidden;
}

.parent-sidebar .voice-panel {
  min-width: 0;
  gap: 0;
  padding: 0;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.92);
}

.panel-header {
  display: grid;
  gap: 6px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

.panel-header.compact {
  padding: 14px;
}

.panel-header h2 {
  font-size: 1.18rem;
}

.panel-header p {
  color: var(--muted);
  line-height: 1.45;
  font-size: 0.94rem;
}

form {
  gap: 14px;
  padding: 18px;
}

.compact-form {
  gap: 12px;
  padding: 14px;
}

.card-form {
  gap: 16px;
}

.stack-actions {
  display: grid;
  gap: 8px;
}

.sidebar-voice-grid {
  grid-template-columns: 1fr;
  padding: 14px 14px 8px;
}

.sidebar-message {
  padding: 0 14px 14px;
}

.upload-box {
  display: grid;
  gap: 12px;
}

.image-input {
  display: grid;
  place-items: center;
  min-height: 188px;
  border: 2px dashed #bfd1dd;
  border-radius: 8px;
  background: #f8fbfd;
  overflow: hidden;
  cursor: pointer;
}

.image-input input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.image-help {
  display: grid;
  place-items: center;
  gap: 6px;
  min-height: 180px;
  padding: 16px;
  text-align: center;
  color: var(--muted);
}

.image-help strong {
  color: var(--ink);
}

.preview {
  display: none;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.preview.active {
  display: block;
}

.form-actions {
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.message {
  min-height: 20px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.35;
}

.library {
  min-height: 580px;
  overflow: hidden;
}

body.parent-mode .library {
  grid-area: library;
}

.library-tools {
  grid-template-columns: minmax(180px, 1fr) minmax(150px, auto) minmax(150px, auto) auto auto;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}

body.kid-mode:not(.kid-lesson-active) .library-tools,
body.kid-mode:not(.kid-lesson-active) .cards,
body.kid-mode:not(.kid-lesson-active) .pagination,
body.kid-mode:not(.kid-lesson-active) .empty {
  display: none;
}

body.parent-mode .category-home,
body.parent-mode .lesson-bar {
  display: none;
}

.category-home {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px;
  padding: 18px;
}

.category-card {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
  min-height: 260px;
  padding: 14px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 10px 22px rgba(26, 45, 68, 0.08);
  text-align: left;
  transition: transform 160ms ease, box-shadow 160ms ease, border-color 160ms ease;
}

.category-card:hover {
  transform: translateY(-3px);
  border-color: #9ec9ce;
  box-shadow: 0 18px 34px rgba(26, 45, 68, 0.14);
}

.category-art {
  display: grid;
  place-items: center;
  min-height: 130px;
  border-radius: 8px;
  overflow: hidden;
}

.category-art svg {
  width: 100%;
  max-width: 180px;
  height: 120px;
}

.category-copy {
  display: grid;
  gap: 6px;
}

.category-copy strong {
  font-size: 1.28rem;
  line-height: 1.15;
}

.category-copy span {
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.4;
}

.lesson-bar {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 14px;
  align-items: center;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  background: #f8fbfd;
}

.lesson-bar p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.94rem;
  line-height: 1.35;
}

body.kid-mode .library-tools {
  grid-template-columns: minmax(180px, 1fr) minmax(150px, auto) minmax(150px, auto) auto;
}

.manage-toggle {
  display: inline-grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 0.94rem;
  font-weight: 750;
  cursor: pointer;
}

.manage-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 14px;
  padding: 18px;
}

body.kid-mode .cards {
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 18px;
}

.card {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  min-width: 0;
  overflow: hidden;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  transition: transform 160ms ease, box-shadow 160ms ease, border-color 160ms ease;
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 16px 32px rgba(26, 45, 68, 0.16);
}

.card.speaking-english,
.card.speaking-vietnamese,
.card.speaking-both {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(19, 113, 119, 0.14), var(--shadow);
  animation: happy-pop 520ms ease;
}

@keyframes happy-pop {
  0% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.025);
  }
  100% {
    transform: scale(1);
  }
}

.card-image-button {
  width: 100%;
  height: auto;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--soft);
}

.card-image {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.generated-art {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 4 / 3;
  padding: 18px;
  color: #fff;
  background: linear-gradient(135deg, rgba(0, 0, 0, 0.1), transparent 45%), var(--primary);
  text-align: center;
}

.generated-art span {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  font-size: clamp(1.4rem, 7vw, 2.2rem);
  line-height: 1.08;
}

.card-body {
  gap: 10px;
  padding: 14px;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.meta-pill {
  width: fit-content;
  padding: 5px 8px;
  border: 1px solid #c9d8e6;
  border-radius: 999px;
  color: #37536b;
  background: #f3f8fb;
  font-size: 0.78rem;
  font-weight: 800;
}

body.kid-mode .card-body {
  gap: 12px;
  padding: 16px;
}

.word-button {
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  text-align: left;
  background: transparent;
  color: inherit;
}

.word-button:hover {
  color: var(--primary-dark);
}

.english {
  font-size: 1.34rem;
  line-height: 1.18;
  font-weight: 850;
}

body.kid-mode .english {
  font-size: clamp(1.65rem, 6vw, 2.35rem);
}

.vietnamese {
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.35;
  font-weight: 700;
}

body.kid-mode .vietnamese {
  font-size: 1.18rem;
}

.listen-row {
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 0 14px 14px;
}

.listen-button {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
  min-width: 0;
  min-height: 54px;
  padding: 0 12px;
  color: #fff;
  border: 0;
  text-align: left;
}

.listen-button .speaker {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.22);
  font-size: 1.16rem;
}

.listen-button .label {
  display: grid;
  gap: 1px;
  line-height: 1.1;
  min-width: 0;
  white-space: nowrap;
}

.listen-button small {
  font-size: 0.73rem;
  font-weight: 800;
  opacity: 0.86;
}

.listen-english {
  background: #2563eb;
}

.listen-english:hover {
  background: #1d4ed8;
}

.listen-vietnamese {
  background: #d97706;
}

.listen-vietnamese:hover {
  background: #b45309;
}

.card-actions {
  grid-template-columns: 1fr;
  gap: 8px;
  padding: 0 14px 14px;
}

.card-actions.manage {
  grid-template-columns: 1fr auto auto;
}

.empty {
  display: none;
  padding: 44px 18px;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

.empty.active {
  display: block;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  padding: 0 18px 18px;
}

.pagination[hidden] {
  display: none;
}

.pagination span {
  color: var(--muted);
  font-weight: 800;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  z-index: 5;
  transform: translateX(-50%) translateY(80px);
  max-width: min(620px, calc(100% - 24px));
  padding: 12px 14px;
  color: #fff;
  background: #172033;
  border-radius: 8px;
  opacity: 0;
  transition: 180ms ease;
  pointer-events: none;
}

.toast.active {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 980px) {
  .topbar,
  .app-grid {
    grid-template-columns: 1fr;
  }

  body.parent-mode .app-grid,
  body.parent-mode.admin-locked .app-grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "sidebar"
      "creator"
      "library";
  }

  .hero-actions {
    justify-items: stretch;
  }

  .creator,
  .parent-sidebar {
    position: static;
  }
}

@media (max-width: 760px) {
  .listen-row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 620px) {
  main {
    width: min(100% - 24px, 1180px);
    padding-top: 24px;
  }

  .voice-grid,
  .library-tools,
  .classification-grid,
  .listen-row,
  .form-actions {
    grid-template-columns: 1fr;
  }

  .cards {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .category-home {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .lesson-bar {
    grid-template-columns: 1fr;
  }
}
