:root {
  --brand-primary: #c83e3b;
  --brand-primary-dark: #a32c2a;
  --brand-soft: #fef2f2;
  --surface-bg: #f6f3ed;
  --surface-card: #ffffff;
  --border-subtle: #e7e2d8;
  --tone-1: #dc2626; /* Thanh 1: đỏ */
  --tone-2: #d97706; /* Thanh 2: cam/vàng đậm */
  --tone-3: #15803d; /* Thanh 3: xanh lá */
  --tone-4: #2563eb; /* Thanh 4: xanh dương */
  --tone-5: #64748b; /* Thanh nhẹ: xám */
}

body {
  font-family: 'Be Vietnam Pro', system-ui, -apple-system, sans-serif;
  background-color: var(--surface-bg);
  color: #1f2937;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button, a, [role="tab"] {
  touch-action: manipulation;
}

.font-hanzi {
  font-family: 'Noto Sans SC', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

/* Native <dialog> modal styling + Modern Top-Layer Entry/Exit Animations */
dialog:modal {
  overscroll-behavior: contain;
  border: 1px solid var(--border-subtle);
  border-radius: 1.25rem;
  padding: 0;
  max-width: min(94vw, 48rem);
  width: 100%;
  max-height: 90dvh;
  box-shadow: 0 25px 50px -12px rgba(15, 23, 42, 0.28);
  background: var(--surface-card);
  color: #1f2937;
  margin: auto;
}

dialog {
  opacity: 0;
  transition-property: opacity, transform, display, overlay;
  transition-duration: 0.22s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}

@media (prefers-reduced-motion: no-preference) {
  dialog {
    transform: translateY(8px) scale(0.97);
  }
}

dialog[open]:not([data-closing]) {
  display: flex;
  flex-direction: column;
  opacity: 1;
}

@media (prefers-reduced-motion: no-preference) {
  dialog[open]:not([data-closing]) {
    transform: translateY(0) scale(1);
  }
}

@starting-style {
  dialog[open]:not([data-closing]) {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: no-preference) {
  @starting-style {
    dialog[open]:not([data-closing]) {
      transform: translateY(8px) scale(0.97);
    }
  }
}

dialog::backdrop {
  background: rgba(15, 23, 42, 0);
  backdrop-filter: blur(0px);
  overflow: hidden;
  overscroll-behavior: contain;
  transition-property: background-color, backdrop-filter, display, overlay;
  transition-duration: 0.22s;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;
}

dialog[open]:not([data-closing])::backdrop {
  background: rgba(15, 23, 42, 0.52);
  backdrop-filter: blur(3px);
}

@starting-style {
  dialog[open]:not([data-closing])::backdrop {
    background: rgba(15, 23, 42, 0);
    backdrop-filter: blur(0px);
  }
}

@media (prefers-reduced-motion: reduce) {
  dialog,
  dialog::backdrop {
    transition-duration: 0.1s;
  }
}

dialog.dialog-sm:modal {
  max-width: min(94vw, 32rem);
}

dialog.dialog-lg:modal {
  max-width: min(96vw, 60rem);
}

/* Accessible single-row scrollable Tablist with scroll-driven edge fades */
@property --fade-start {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

@property --fade-end {
  syntax: "<length>";
  inherits: false;
  initial-value: 0px;
}

.tablist-scroller {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: inline proximity;
  scroll-padding-inline: 2rem;
  gap: 0.25rem;
  --fade-size: 2.25rem;
  mask:
    linear-gradient(black, black) no-clip subtract,
    linear-gradient(to right, black, transparent) 0 0 / var(--fade-start) 100% no-repeat no-clip,
    linear-gradient(to right, transparent, black) 100% 0 / var(--fade-end) 100% no-repeat no-clip;
  animation: reveal-fade-start linear both, hide-fade-end linear both;
  animation-timeline: scroll(self inline);
  animation-range: 0 var(--fade-size), calc(100% - var(--fade-size)) 100%;
}

.tablist-scroller::-webkit-scrollbar {
  display: none;
}

@keyframes reveal-fade-start {
  to {
    --fade-start: var(--fade-size);
  }
}

@keyframes hide-fade-end {
  from {
    --fade-end: var(--fade-size);
  }
}

.tab-btn {
  position: relative;
  flex: none;
  scroll-snap-align: start;
  white-space: nowrap;
}

.tab-btn[aria-selected="true"] {
  background-color: #ffffff;
  color: #1c1917;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

.tab-btn[aria-selected="true"]::after {
  content: "";
  position: absolute;
  inset: auto 0.75rem 0.15rem;
  border-block-end: 3px solid var(--brand-primary);
  border-radius: 999px;
}

/* Accessible focus rings & :user-invalid styling */
:focus-visible {
  outline: 3px solid #c83e3b;
  outline-offset: 2px;
}

input:user-invalid,
textarea:user-invalid,
select:user-invalid {
  border-color: #dc2626;
  background-color: #fef2f2;
}

/* Tone color classes */
.tone-1 { color: var(--tone-1); }
.tone-2 { color: var(--tone-2); }
.tone-3 { color: var(--tone-3); }
.tone-4 { color: var(--tone-4); }
.tone-5 { color: var(--tone-5); }

/* Sidebar collapsible deck groups */
.deck-group summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
}

.deck-group summary::-webkit-details-marker {
  display: none;
}

.deck-group summary .group-chevron {
  transition: transform 0.18s ease;
}

.deck-group[open] summary .group-chevron {
  transform: rotate(90deg);
}

/* Ruby / Interlinear Sentence Reader */
.ruby-token {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: 0.2rem 0.28rem;
  border-radius: 0.5rem;
  transition: background-color 0.15s;
}

.ruby-token.is-hanzi {
  cursor: pointer;
}

.ruby-token.is-hanzi:hover {
  background-color: #fef2f2;
}

.ruby-pinyin {
  font-size: 0.72rem;
  line-height: 1.1;
  font-weight: 600;
  min-height: 0.9rem;
}

.ruby-char {
  font-size: 1.5rem;
  line-height: 1.25;
  font-weight: 600;
}

/* Star button active state */
.star-btn.is-starred {
  color: #d97706;
  background-color: #fef3c7;
  border-color: #fcd34d;
}

/* Quiz option buttons */
.quiz-option-btn {
  transition: transform 0.1s ease, border-color 0.15s ease, background-color 0.15s ease;
}

.quiz-option-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: #c83e3b;
  background-color: #fffaf9;
}

.quiz-option-btn.is-correct {
  border-color: #16a34a !important;
  background-color: #f0fdf4 !important;
  color: #14532d !important;
}

.quiz-option-btn.is-wrong {
  border-color: #dc2626 !important;
  background-color: #fef2f2 !important;
  color: #7f1d1d !important;
}

/* Recording pulse ring */
@keyframes micPulse {
  0% { box-shadow: 0 0 0 0 rgba(200, 62, 59, 0.55); }
  70% { box-shadow: 0 0 0 18px rgba(200, 62, 59, 0); }
  100% { box-shadow: 0 0 0 0 rgba(200, 62, 59, 0); }
}

.is-recording {
  animation: micPulse 1.3s infinite;
  background-color: #dc2626 !important;
  color: #ffffff !important;
}

/* Waveform canvas */
#waveform-canvas {
  width: 100%;
  height: 56px;
  display: block;
  border-radius: 0.75rem;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
}

/* Responsive table with sticky headers & container-query stacking */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(560px, 68vh);
  container-type: inline-size;
  border: 1px solid var(--border-subtle);
  border-radius: 0.875rem;
  background: #ffffff;
}

.responsive-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  text-align: left;
  font-size: 0.9375rem;
}

.responsive-table thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 2;
  background: #f5f2eb;
}

.responsive-table th,
.responsive-table td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #efeae0;
  vertical-align: middle;
}

.responsive-table tbody tr:hover {
  background-color: #faf8f4;
}

@container (width < 640px) {
  .responsive-table,
  .responsive-table thead,
  .responsive-table tbody,
  .responsive-table tr,
  .responsive-table th,
  .responsive-table td {
    display: block;
  }

  .responsive-table thead {
    display: none;
  }

  .responsive-table tr {
    margin: 0.75rem;
    border: 1px solid var(--border-subtle);
    border-radius: 0.75rem;
    background: #ffffff;
    overflow: hidden;
  }

  .responsive-table tbody th {
    background: #f5f2eb;
    font-weight: 700;
  }

  .responsive-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    border-bottom: 1px solid #f3efe6;
  }

  .responsive-table td::before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: #6b7280;
    flex-shrink: 0;
  }
}

.visually-hidden {
  position: absolute;
  clip-path: inset(50%);
  overflow: hidden;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  white-space: nowrap;
}

/* =========================================================================
   Interactive Pinyin Matrix (汉语普通话声韵母拼合表) - Vertical Column Mode
   ========================================================================= */
.pinyin-matrix-wrapper {
  overflow: auto;
  max-width: 100%;
  max-height: min(640px, 72vh);
  border: 1px solid var(--border-subtle);
  border-radius: 1rem;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.pinyin-matrix-table {
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.8125rem;
  width: max-content;
}

.pinyin-matrix-table th,
.pinyin-matrix-table td {
  border-right: 1px solid #ece7dd;
  border-bottom: 1px solid #ece7dd;
  padding: 0;
  text-align: center;
  min-width: 56px;
  height: 36px;
}

.pinyin-matrix-table thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: #f5f2eb;
  font-weight: 700;
  color: #374151;
}

.pinyin-matrix-table thead th.corner-cell {
  left: 0;
  z-index: 5;
  min-width: 48px;
  background: #ebe5d8;
}

.pinyin-matrix-table tbody th.row-header {
  position: sticky;
  left: 0;
  z-index: 2;
  min-width: 48px;
  background: #faf7f0;
  font-weight: 700;
  color: #4b5563;
}

.pinyin-col-header-btn {
  width: 100%;
  height: 100%;
  padding: 0.35rem 0.4rem;
  font-weight: 700;
  cursor: pointer;
  background: transparent;
  border: 0;
  transition: background-color 0.15s, color 0.15s;
}

.pinyin-col-header-btn:hover {
  background: #fee2e2;
  color: #991b1b;
}

.pinyin-col-header-btn.is-selected-col {
  background: #c83e3b;
  color: #ffffff;
}

.pinyin-cell-btn {
  width: 100%;
  height: 100%;
  padding: 0.25rem 0.3rem;
  border: 0;
  background: transparent;
  cursor: pointer;
  font-weight: 500;
  color: #1f2937;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
  transition: background-color 0.12s, transform 0.1s;
}

.pinyin-cell-btn:hover {
  background: #fef2f2;
  color: #991b1b;
}

td.is-col-highlight {
  background-color: #fff7ed;
}

td.is-cell-active .pinyin-cell-btn {
  background-color: #c83e3b !important;
  color: #ffffff !important;
  font-weight: 700;
  box-shadow: inset 0 0 0 2px #7f1d1d;
}

td.is-cell-passed .pinyin-cell-btn {
  background-color: #bbf7d0;
  color: #14532d;
  font-weight: 700;
  box-shadow: inset 0 0 0 1px #4ade80;
}

td.is-cell-passed .pinyin-cell-btn:hover {
  background-color: #86efac;
  color: #14532d;
}

td.is-cell-passed.is-cell-active .pinyin-cell-btn {
  background-color: #15803d !important;
  color: #ffffff !important;
  box-shadow: inset 0 0 0 2px #14532d;
}

td.is-cell-failed .pinyin-cell-btn {
  background-color: #fef3c7;
  color: #92400e;
  font-weight: 700;
}

td.is-empty-cell {
  background: #fcfbf9;
}

/* Tianzige (田字格) writing grid for Hanzi Writer */
.tianzige-box {
  width: 190px;
  height: 190px;
  border: 2px solid #c83e3b;
  border-radius: 0.75rem;
  background-color: #fffdf9;
  background-image:
    linear-gradient(to right, transparent 49.5%, rgba(200, 62, 59, 0.22) 49.5%, rgba(200, 62, 59, 0.22) 50.5%, transparent 50.5%),
    linear-gradient(to bottom, transparent 49.5%, rgba(200, 62, 59, 0.22) 49.5%, rgba(200, 62, 59, 0.22) 50.5%, transparent 50.5%),
    linear-gradient(45deg, transparent 49.6%, rgba(200, 62, 59, 0.12) 49.6%, rgba(200, 62, 59, 0.12) 50.4%, transparent 50.4%),
    linear-gradient(-45deg, transparent 49.6%, rgba(200, 62, 59, 0.12) 49.6%, rgba(200, 62, 59, 0.12) 50.4%, transparent 50.4%);
  touch-action: none;
  cursor: crosshair;
}

/* =========================================================================
   Mobile First Enhancements (Drawer Sidebar, Bottom Bar, Safe Areas, Touch)
   ========================================================================= */
#mobile-sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(15, 23, 42, 0.48);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}

#mobile-sidebar-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

@media (max-width: 1023px) {
  #sidebar-panel {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    width: min(88vw, 350px);
    height: 100dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--surface-bg);
    padding: 0.875rem 0.875rem calc(1.5rem + env(safe-area-inset-bottom));
    box-shadow: 0 25px 50px -12px rgba(15, 23, 42, 0.35);
    transform: translateX(-105%);
    transition: transform 0.24s cubic-bezier(0.22, 1, 0.36, 1);
    display: block !important;
  }

  #sidebar-panel.is-open {
    transform: translateX(0);
  }

  #sidebar-panel #lesson-list {
    max-height: none;
  }
}

/* Mobile Bottom App Navigation Bar */
#mobile-bottom-nav {
  padding-bottom: max(0.4rem, env(safe-area-inset-bottom));
}

.mobile-nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  min-height: 50px;
  color: #57534e;
  font-size: 11px;
  font-weight: 600;
  border-radius: 0.75rem;
  transition: color 0.15s ease, background-color 0.15s ease;
}

.mobile-nav-btn.is-active {
  color: var(--brand-primary);
  background-color: var(--brand-soft);
}

/* Ngăn iOS Safari tự zoom khi bấm vào ô input */
@media (max-width: 767px) {
  input[type="text"],
  input[type="search"],
  input[type="email"],
  input[type="password"],
  textarea,
  select {
    font-size: 16px !important;
  }

  #waveform-canvas {
    width: 100%;
    max-width: 100%;
  }
}

/* Modal dạng Bottom-Sheet trên màn hình điện thoại nhỏ */
@media (max-width: 639px) {
  dialog:modal,
  dialog.dialog-sm:modal,
  dialog.dialog-lg:modal {
    max-width: 100vw;
    width: 100vw;
    max-height: 92dvh;
    margin: auto 0 0 0;
    border-radius: 1.25rem 1.25rem 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
}
