:root {
  --page: #edf4f1;
  --page-deep: #dcebe6;
  --surface: #ffffff;
  --surface-soft: #f7faf8;
  --ink: #13201d;
  --muted: #64746f;
  --line: #cfdbd6;
  --teal: #0f766e;
  --teal-ink: #064e49;
  --rose: #c44b55;
  --amber: #b77a20;
  --blue: #315f96;
  --green: #3f7e4e;
  --shadow: 0 24px 70px rgba(19, 32, 29, 0.12);
  --soft-shadow: 0 12px 36px rgba(19, 32, 29, 0.08);
  --font-sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-han: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", var(--font-sans);
  font-family: var(--font-sans);
}

* {
  box-sizing: border-box;
}

html {
  width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  background: var(--page);
}

body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  overscroll-behavior-x: none;
  touch-action: pan-y;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(237, 244, 241, 0.96), rgba(229, 239, 236, 0.98)),
    linear-gradient(90deg, rgba(15, 118, 110, 0.06) 1px, transparent 1px),
    linear-gradient(180deg, rgba(49, 95, 150, 0.04) 1px, transparent 1px);
  background-size: auto, 76px 76px, 76px 76px;
}

button,
input,
textarea {
  font: inherit;
  font-size: 16px;
}

button,
a {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  cursor: pointer;
}

h1,
h2,
h3,
p {
  margin: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.app-shell {
  width: min(980px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 10px) 16px calc(env(safe-area-inset-bottom) + 24px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  padding: 0 2px;
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
}

h1 {
  color: var(--teal-ink);
  font-size: 1.02rem;
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: 0;
}

h2 {
  font-size: 1.55rem;
  line-height: 1.1;
  letter-spacing: 0;
}

.brand p,
.eyebrow,
.stat span,
label span,
.card-row-meta,
.card-meta,
.grade small,
.status-pill,
.import-result {
  color: var(--muted);
  font-size: 0.84rem;
}

.app-menu {
  position: relative;
  flex: 0 0 auto;
  z-index: 10;
}

.app-menu summary {
  list-style: none;
}

.app-menu summary::-webkit-details-marker {
  display: none;
}

.menu-summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 36px;
  border: 1px solid rgba(19, 32, 29, 0.12);
  border-radius: 999px;
  padding: 0 11px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  box-shadow: 0 8px 24px rgba(19, 32, 29, 0.08);
  font-size: 0.9rem;
  font-weight: 850;
}

.menu-summary svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-width: 2;
}

.menu-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: min(430px, calc(100vw - 32px));
  padding: 12px;
  border: 1px solid rgba(19, 32, 29, 0.13);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.tabs {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(68px, 1fr));
  gap: 6px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-soft);
}

.tab {
  min-height: 40px;
  border: 0;
  border-radius: 10px;
  color: var(--muted);
  background: transparent;
  font-weight: 800;
}

.tab.is-active {
  color: var(--surface);
  background: var(--ink);
}

.menu-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 12px;
}

.status-pill {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 10px;
  background: var(--surface-soft);
}

main {
  display: grid;
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.section-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  margin: 8px 0 16px;
}

.heading-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.eyebrow {
  margin-bottom: 5px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

#view-study {
  display: none;
}

#view-study.is-active {
  display: grid;
  gap: 12px;
}

.study-stage {
  display: grid;
  gap: 12px;
}

.shadowing-panel {
  display: grid;
  grid-column: 1 / -1;
  gap: 11px;
  padding: 13px;
  border: 1px solid rgba(49, 95, 150, 0.18);
  border-radius: 16px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.92), rgba(241, 248, 246, 0.94)),
    var(--surface);
  box-shadow: 0 12px 32px rgba(19, 32, 29, 0.07);
}

.shadowing-panel-large {
  gap: 14px;
  padding: 18px;
}

.shadowing-panel-large .shadow-transcript {
  max-height: 58svh;
}

.shadowing-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.shadowing-copy {
  min-width: 0;
}

.shadowing-copy h3 {
  color: var(--ink);
  font-size: 1.02rem;
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: 0;
}

.shadowing-copy p {
  margin-top: 4px;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 720;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.shadowing-controls {
  display: flex;
  flex: 0 0 auto;
  gap: 7px;
}

.shadowing-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  gap: 7px;
  border: 1px solid rgba(49, 95, 150, 0.2);
  border-radius: 12px;
  padding: 0 11px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.82);
  font-weight: 860;
}

.shadowing-button svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2.4;
}

.shadowing-button.primary-audio {
  border-color: rgba(49, 95, 150, 0.26);
  color: var(--surface);
  background: var(--blue);
}

.shadowing-button.is-active:not(:disabled) {
  border-color: rgba(15, 118, 110, 0.34);
  color: var(--teal-ink);
  background: rgba(15, 118, 110, 0.09);
}

.shadowing-button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.shadowing-progress {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) 42px;
  align-items: center;
  gap: 9px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 820;
  font-variant-numeric: tabular-nums;
}

.shadowing-seek {
  min-height: 24px;
  padding: 0;
  border: 0;
  border-radius: 0;
  accent-color: var(--teal);
  background: transparent;
  touch-action: pan-x;
}

.shadow-text-options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  border: 1px solid rgba(15, 118, 110, 0.13);
  border-radius: 13px;
  padding: 5px;
  background: rgba(255, 255, 255, 0.62);
}

.shadow-text-options[hidden] {
  display: none;
}

.shadow-text-options span {
  margin: 0 5px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 850;
  text-transform: uppercase;
}

.shadow-text-options button {
  min-height: 34px;
  border: 0;
  border-radius: 9px;
  padding: 0 10px;
  color: var(--muted);
  background: transparent;
  font-size: 0.86rem;
  font-weight: 850;
}

.shadow-text-options button.is-active {
  color: var(--surface);
  background: var(--teal-ink);
}

.shadow-transcript {
  display: grid;
  max-height: 38svh;
  overflow: auto;
  gap: 5px;
  padding: 12px;
  border: 1px solid rgba(15, 118, 110, 0.13);
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.7);
}

.shadow-transcript[data-mode="characters"] {
  gap: 8px;
}

.shadow-transcript[data-mode="characters"] .transcript-pinyin,
.shadow-transcript[data-mode="characters"] .transcript-english,
.shadow-transcript[data-mode="both"] .transcript-english {
  display: none;
}

.transcript-scene {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

.transcript-line {
  color: var(--ink);
  font-family: var(--font-han);
  font-size: 1rem;
  font-weight: 760;
  line-height: 1.48;
}

.transcript-line span {
  color: var(--teal-ink);
  font-weight: 900;
}

.transcript-pinyin {
  margin-bottom: 6px;
  color: var(--blue);
  font-size: 0.88rem;
  font-weight: 700;
  line-height: 1.35;
}

.transcript-english {
  margin: -1px 0 8px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 680;
  line-height: 1.38;
}

.transcript-scene-english {
  margin: -4px 0 5px;
}

.flashcard-wrap {
  position: relative;
}

.flashcard {
  display: grid;
  width: 100%;
  min-height: 58svh;
  grid-template-rows: auto minmax(220px, 1fr) auto;
  gap: 18px;
  padding: 26px;
  border: 1px solid rgba(19, 32, 29, 0.12);
  border-radius: 18px;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(247, 250, 248, 0.98)),
    var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
}

.flashcard:active {
  transform: translateY(1px);
}

.flashcard:focus-visible,
.menu-summary:focus-visible,
.tab:focus-visible,
.primary:focus-visible,
.secondary:focus-visible,
.grade:focus-visible,
.row-action:focus-visible,
.card-audio-button:focus-visible,
.shadowing-button:focus-visible {
  outline: 3px solid rgba(15, 118, 110, 0.28);
  outline-offset: 2px;
}

.card-audio-button {
  position: absolute;
  top: 18px;
  right: 18px;
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 0;
  border-radius: 999px;
  color: var(--blue);
  background: transparent;
  box-shadow: none;
  padding: 0;
}

.card-audio-button svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.card-audio-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 24px;
  gap: 62px;
  padding-right: 48px;
  text-align: left;
  font-weight: 760;
}

#cardDue {
  border: 1px solid rgba(15, 118, 110, 0.18);
  border-radius: 999px;
  padding: 5px 9px;
  color: var(--teal-ink);
  background: rgba(15, 118, 110, 0.08);
}

.card-body {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 12px;
  min-height: 220px;
}

.card-text {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  color: var(--ink);
  font-family: var(--font-han);
  font-size: 3rem;
  font-weight: 880;
  line-height: 1.08;
  letter-spacing: 0;
  text-align: center;
}

.card-text.is-rule-answer {
  width: min(640px, 100%);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 1.24rem;
  font-weight: 740;
  line-height: 1.38;
  text-align: left;
}

.card-text.is-pinyin-answer {
  color: var(--blue);
  font-family: var(--font-sans);
  font-size: 1.44rem;
  font-weight: 860;
  line-height: 1.24;
}

.card-characters {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  color: var(--teal-ink);
  font-family: var(--font-han);
  font-size: 3.3rem;
  font-weight: 900;
  line-height: 1.02;
  text-align: center;
}

.card-pinyin {
  display: block;
  max-width: 100%;
  overflow-wrap: anywhere;
  color: var(--blue);
  font-size: 1.22rem;
  font-weight: 780;
  line-height: 1.35;
  text-align: center;
}

.card-context {
  display: block;
  min-height: 28px;
  max-width: 680px;
  margin: 0 auto;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.5;
  text-align: center;
}

.card-context.is-answer-detail {
  max-width: 620px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 650;
  line-height: 1.42;
}

.review-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  opacity: 0.28;
  pointer-events: none;
}

.review-actions.is-ready {
  opacity: 1;
  pointer-events: auto;
}

.grade {
  min-height: 68px;
  padding: 9px 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  box-shadow: 0 8px 20px rgba(19, 32, 29, 0.06);
  font-weight: 850;
}

.grade span,
.grade small {
  display: block;
}

.grade small {
  margin-top: 4px;
}

.grade.again {
  border-color: rgba(196, 75, 85, 0.34);
}

.grade.hard {
  border-color: rgba(183, 122, 32, 0.36);
}

.grade.good {
  border-color: rgba(15, 118, 110, 0.3);
}

.grade.easy {
  border-color: rgba(49, 95, 150, 0.32);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 2px;
}

.stat {
  min-height: 66px;
  padding: 11px;
  border: 1px solid rgba(19, 32, 29, 0.1);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.58);
  backdrop-filter: blur(12px);
}

.stat strong {
  display: block;
  margin-top: 4px;
  color: var(--teal-ink);
  font-size: 1.42rem;
  line-height: 1;
}

.primary,
.secondary,
.link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border-radius: 12px;
  padding: 0 15px;
  font-weight: 850;
  text-decoration: none;
}

.primary {
  border: 1px solid var(--teal);
  color: var(--surface);
  background: var(--teal);
}

.secondary {
  border: 1px solid var(--line);
  color: var(--ink);
  background: rgba(255, 255, 255, 0.84);
}

.compact {
  min-height: 38px;
  padding: 0 12px;
}

.hidden {
  display: none !important;
}

[hidden] {
  display: none !important;
}

.search-field,
.card-form,
.import-tools {
  display: grid;
  gap: 12px;
}

label {
  display: grid;
  gap: 7px;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  outline: 0;
}

input {
  min-height: 46px;
  padding: 0 12px;
}

textarea {
  min-height: 118px;
  padding: 12px;
  resize: vertical;
  line-height: 1.45;
}

input:focus,
textarea:focus {
  border-color: rgba(15, 118, 110, 0.62);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.form-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.card-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.card-stack {
  overflow: hidden;
  border: 1px solid rgba(15, 118, 110, 0.14);
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.66);
}

.card-stack-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 62px;
  padding: 12px;
  cursor: pointer;
  list-style: none;
}

.card-stack-summary::-webkit-details-marker {
  display: none;
}

.card-stack-heading {
  display: grid;
  min-width: 0;
  gap: 4px;
}

.card-stack-title {
  overflow: hidden;
  color: var(--ink);
  font-weight: 900;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-stack-meta {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 760;
}

.stack-study-button {
  flex: 0 0 auto;
  min-height: 38px;
  border: 1px solid rgba(49, 95, 150, 0.22);
  border-radius: 11px;
  padding: 0 12px;
  color: var(--surface);
  background: var(--blue);
  font-weight: 860;
}

.card-stack-list {
  display: grid;
  gap: 8px;
  padding: 0 10px 10px;
}

.card-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
  min-height: 92px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.82);
}

.card-row-title {
  margin-bottom: 5px;
  overflow-wrap: anywhere;
  font-weight: 850;
  line-height: 1.25;
}

.card-row-answer {
  margin-bottom: 7px;
  overflow-wrap: anywhere;
  color: var(--muted);
  line-height: 1.35;
}

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

.row-actions {
  display: flex;
  gap: 7px;
}

.row-action {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  background: var(--surface);
}

.row-action svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.row-action.delete {
  color: #9f352d;
}

.file-field input {
  padding: 10px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.import-result {
  min-height: 22px;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: calc(env(safe-area-inset-bottom) + 18px);
  z-index: 20;
  max-width: min(360px, calc(100vw - 36px));
  transform: translateY(18px);
  border: 1px solid rgba(19, 32, 29, 0.12);
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--surface);
  background: var(--ink);
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.is-visible {
  transform: translateY(0);
  opacity: 1;
}

@media (min-width: 760px) {
  .study-stage {
    grid-template-columns: minmax(0, 1fr) 176px;
    align-items: stretch;
  }

  .review-actions {
    grid-template-columns: 1fr;
  }

}

@media (max-width: 620px) {
  .app-shell {
    padding: calc(env(safe-area-inset-top) + 8px) 10px calc(env(safe-area-inset-bottom) + 16px);
  }

  .topbar {
    align-items: center;
    margin-bottom: 8px;
  }

  h1 {
    font-size: 0.98rem;
  }

  .brand p {
    max-width: 230px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.78rem;
  }

  .menu-summary span {
    display: none;
  }

  .menu-summary {
    width: 38px;
    min-height: 38px;
    padding: 0;
  }

  .menu-panel {
    right: -2px;
    width: min(390px, calc(100vw - 20px));
    padding: 10px;
  }

  .tabs {
    gap: 4px;
    padding: 4px;
  }

  .tab {
    min-height: 38px;
    font-size: 0.9rem;
  }

  .shadowing-panel {
    gap: 9px;
    padding: 10px;
    border-radius: 14px;
  }

  .shadowing-main {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 9px;
  }

  .shadowing-copy p {
    white-space: normal;
  }

  .shadowing-controls {
    display: grid;
    width: 100%;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .shadowing-button {
    min-height: 38px;
    gap: 5px;
    padding: 0 8px;
    border-radius: 10px;
    font-size: 0.86rem;
  }

  .shadowing-button svg {
    width: 16px;
    height: 16px;
  }

  .shadowing-progress {
    grid-template-columns: 38px minmax(0, 1fr) 38px;
    gap: 7px;
  }

  .shadow-text-options {
    display: grid;
    grid-template-columns: auto 1fr 1fr;
    gap: 5px;
  }

  .shadow-text-options button {
    padding: 0 6px;
    font-size: 0.82rem;
  }

  .shadow-transcript {
    max-height: 35svh;
    padding: 10px;
  }

  .flashcard {
    min-height: 56svh;
    grid-template-rows: auto minmax(170px, 1fr) auto;
    gap: 13px;
    padding: 17px;
    border-radius: 16px;
  }

  .card-audio-button {
    top: 13px;
    right: 13px;
    width: 38px;
    height: 38px;
  }

  .card-meta {
    gap: 54px;
    padding-right: 42px;
  }

  .card-body {
    min-height: 170px;
  }

  .card-text {
    font-size: 2.42rem;
    line-height: 1.1;
  }

  .card-text.is-rule-answer {
    font-size: 1.02rem;
    line-height: 1.4;
  }

  .card-characters {
    font-size: 2.7rem;
  }

  .card-pinyin {
    font-size: 1.04rem;
  }

  .card-context {
    min-height: 20px;
    font-size: 0.9rem;
  }

  .review-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  .grade {
    min-height: 61px;
  }

  .stats-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .stat {
    min-height: 56px;
    padding: 8px;
    border-radius: 12px;
  }

  .stat span {
    font-size: 0.72rem;
  }

  .stat strong {
    font-size: 1.15rem;
  }

  .form-pair,
  .card-row {
    grid-template-columns: 1fr;
  }

  .card-stack-summary {
    align-items: stretch;
    min-height: 0;
  }

  .card-stack-title {
    white-space: normal;
  }

  .stack-study-button {
    min-width: 72px;
  }

  .row-actions {
    justify-content: end;
  }

}
