:root {
  --ink: #17202a;
  --muted: #5d6978;
  --line: #d9e2ec;
  --paper: #f7f8fb;
  --card: #ffffff;
  --blue: #2f6fed;
  --blue-dark: #174fb7;
  --green: #16865d;
  --red: #d04444;
  --yellow: #f7c948;
  --shadow: 0 16px 40px rgba(23, 32, 42, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

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

button {
  cursor: pointer;
}

.auth-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: linear-gradient(135deg, #12213a, #2257be 62%, #16865d);
}

.auth-card {
  width: min(420px, 100%);
  border-radius: 12px;
  background: #fff;
  box-shadow: var(--shadow);
  padding: 30px;
}

.auth-brand {
  width: 54px;
  height: 54px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: var(--yellow);
  color: #12213a;
  font-size: 30px;
  font-weight: 900;
  margin-bottom: 14px;
}

.auth-card h1 {
  margin: 0 0 8px;
  font-size: 28px;
}

.auth-card p {
  margin: 0 0 18px;
  color: var(--muted);
  line-height: 1.55;
}

.auth-card label {
  display: grid;
  gap: 7px;
  margin: 14px 0;
  color: var(--ink);
  font-weight: 800;
}

.auth-card input {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 13px;
}

.auth-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}

.auth-actions button {
  border: 0;
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--blue);
  color: #fff;
  font-weight: 900;
}

.auth-actions .ghost-auth {
  background: #edf3ff;
  color: var(--blue-dark);
}

.auth-message {
  min-height: 24px;
  margin-top: 14px;
  color: var(--blue-dark);
  font-weight: 700;
  line-height: 1.5;
}

.auth-message.error {
  color: var(--red);
}

.app-shell {
  min-height: 100vh;
  display: none;
  grid-template-columns: 300px 1fr;
}

body.logged-in .auth-screen {
  display: none;
}

body.logged-in .app-shell {
  display: grid;
}

.sidebar {
  padding: 24px;
  background: #12213a;
  color: #fff;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.brand {
  display: flex;
  gap: 14px;
  align-items: center;
}

.brand-mark {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: var(--yellow);
  color: #12213a;
  font-size: 28px;
  font-weight: 900;
}

.brand h1 {
  margin: 0;
  font-size: 22px;
}

.brand p,
.tip-card p {
  margin: 4px 0 0;
  color: #b9c7d8;
  line-height: 1.55;
}

.unit-picker {
  display: grid;
  gap: 8px;
  color: #d8e1ed;
  font-size: 14px;
  font-weight: 700;
}

.unit-picker select {
  width: 100%;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 8px;
  padding: 10px 12px;
  background: #fff;
  color: var(--ink);
}

.user-panel {
  display: grid;
  gap: 10px;
  padding: 14px;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 8px;
  background: rgba(255,255,255,.07);
}

.user-panel span {
  display: block;
  color: #b9c7d8;
  font-size: 12px;
}

.user-panel strong {
  display: block;
  margin-top: 2px;
  color: var(--yellow);
  font-size: 18px;
}

.user-panel button {
  border: 0;
  border-radius: 8px;
  padding: 10px 13px;
  background: var(--yellow);
  color: #12213a;
  font-weight: 900;
}

.user-panel p {
  margin: 0;
  color: #b9c7d8;
  font-size: 12px;
  line-height: 1.45;
}

.user-panel .sync-status {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 9px;
  border-radius: 8px;
  background: rgba(255,255,255,.08);
  color: #e7eef8;
}

.sync-status::before {
  content: "";
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  border-radius: 999px;
  background: #f59e0b;
}

.sync-status[data-state="synced"]::before {
  background: #22c55e;
}

.sync-status[data-state="syncing"]::before {
  background: #38bdf8;
}

.sync-status[data-state="offline"]::before {
  background: #f59e0b;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.stats-grid div {
  padding: 12px 8px;
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 8px;
  background: rgba(255,255,255,.06);
  text-align: center;
}

.stats-grid strong {
  display: block;
  font-size: 24px;
  color: var(--yellow);
}

.stats-grid span {
  font-size: 12px;
  color: #d8e1ed;
}

.nav-tabs {
  display: grid;
  gap: 8px;
}

.tab {
  border: 0;
  border-radius: 8px;
  padding: 12px 14px;
  color: #e6edf7;
  background: transparent;
  text-align: left;
}

.tab.active,
.tab:hover {
  color: #12213a;
  background: #fff;
}

.tip-card {
  margin-top: auto;
  padding: 16px;
  border-radius: 8px;
  background: rgba(255,255,255,.08);
}

.workspace {
  padding: 28px;
  overflow: auto;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.hero-panel,
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 20px;
}

.hero-panel {
  padding: 26px;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(120deg, #2257be, #16865d);
  box-shadow: var(--shadow);
}

.eyebrow {
  display: inline-block;
  margin-bottom: 8px;
  color: inherit;
  opacity: .78;
  font-size: 13px;
  font-weight: 700;
}

h2,
h3,
p {
  margin-top: 0;
}

h2 {
  margin-bottom: 8px;
  font-size: 30px;
  line-height: 1.2;
}

.hero-panel p,
.section-title p {
  margin-bottom: 0;
  line-height: 1.6;
}

.progress-ring {
  flex: 0 0 138px;
  height: 138px;
  border: 10px solid rgba(255,255,255,.34);
  border-radius: 50%;
  display: grid;
  place-items: center;
  text-align: center;
}

.progress-ring span {
  display: block;
  font-size: 24px;
  font-weight: 900;
}

.progress-ring small {
  display: block;
  font-size: 12px;
}

.mission-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

.card,
.word-card,
.dialogue-line,
.wrong-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.card {
  padding: 20px;
}

.wide {
  grid-column: 1 / -1;
}

.card-head span {
  color: var(--blue);
  font-weight: 800;
  font-size: 13px;
}

.card-head h3,
.card h3 {
  margin: 4px 0 12px;
  font-size: 20px;
}

.question-label {
  min-height: 48px;
  color: var(--muted);
  line-height: 1.55;
}

.mission-word-list,
.mission-review-list {
  display: grid;
  gap: 10px;
}

.mission-word-list {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.mission-word-item,
.mission-review-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fbff;
  padding: 12px;
}

.mission-word-item strong,
.mission-review-item strong {
  display: block;
  color: var(--ink);
  font-size: 18px;
  margin-bottom: 4px;
}

.mission-word-item span,
.mission-review-item span {
  color: var(--muted);
  font-size: 13px;
}

.mission-word-item em {
  display: inline-block;
  margin-top: 8px;
  color: var(--blue);
  font-style: normal;
  font-weight: 800;
  font-size: 12px;
}

.mission-word-item p {
  min-height: 54px;
  margin: 10px 0 0;
  padding: 9px;
  border-radius: 8px;
  background: #fff8dc;
  color: #6f5a1f;
  font-size: 12px;
  line-height: 1.45;
}

.mission-word-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.mission-review-item p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.mission-review-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
}

.mission-review-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.primary-action,
.secondary-action,
.spell-row button,
.word-actions button,
.mission-word-actions button,
.mission-review-actions button,
.line-actions button,
.wrong-item button,
.ghost {
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--blue);
  color: #fff;
  font-weight: 700;
}

.secondary-action,
.ghost,
.word-actions button,
.mission-word-actions button,
.mission-review-actions button,
.line-actions button,
.wrong-item button {
  background: #edf3ff;
  color: var(--blue-dark);
}

.choice-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
}

.choice-grid button {
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-weight: 800;
}

.choice-grid.compact {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.choice-grid button.right {
  border-color: var(--green);
  background: #e8f7ef;
}

.choice-grid button.wrong {
  border-color: var(--red);
  background: #fff0f0;
}

.feedback {
  min-height: 30px;
  margin-top: 12px;
  color: var(--blue-dark);
  font-weight: 700;
}

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

.spell-row input,
.word-tools input,
.word-tools select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  background: #fff;
}

.hint-row {
  margin-top: 10px;
  display: flex;
  gap: 10px;
}

.sentence-box {
  padding: 18px;
  border-radius: 8px;
  background: #f0f7f4;
  color: #12352a;
  font-size: 22px;
  font-weight: 900;
}

.word-tools {
  display: grid;
  grid-template-columns: 1fr 180px 150px 180px;
  gap: 12px;
  margin-bottom: 16px;
}

.control-label,
.loop-row {
  display: grid;
  gap: 8px;
  margin: 12px 0;
  color: var(--muted);
  font-weight: 700;
}

.control-label select {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: #fff;
}

.loop-row {
  grid-template-columns: auto 1fr;
  align-items: center;
}

.recorder-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.recorder-actions button,
button.recording {
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--green);
  color: #fff;
  font-weight: 800;
}

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

.speech-toast {
  position: fixed;
  z-index: 200;
  left: 50%;
  bottom: 24px;
  max-width: min(520px, calc(100vw - 28px));
  transform: translate(-50%, 20px);
  padding: 12px 16px;
  border-radius: 12px;
  background: #17202a;
  color: #fff;
  box-shadow: 0 16px 36px rgba(23, 32, 42, .22);
  font-weight: 800;
  line-height: 1.45;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}

.speech-toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.speech-toast.listening {
  background: var(--blue);
}

.speech-toast.error {
  background: var(--red);
}

.word-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.word-card {
  padding: 16px;
}

.word-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.word-top h3 {
  margin: 0 0 5px;
  font-size: 21px;
  line-height: 1.2;
}

.word-top p,
.example,
.cn {
  color: var(--muted);
  line-height: 1.55;
}

.word-top span {
  align-self: flex-start;
  padding: 4px 8px;
  border-radius: 999px;
  background: #f5e6a8;
  color: #573f00;
  font-weight: 800;
}

.hidden-cn {
  filter: blur(5px);
  user-select: none;
}

.word-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.word-actions.six {
  grid-template-columns: repeat(3, 1fr);
}

.challenge-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 10px 0 12px;
}

.challenge-actions button {
  flex: 1 1 140px;
}

.memory-line,
.mnemonic-line,
.score-line,
.weak-line {
  margin: 8px 0 0;
  color: var(--muted);
  line-height: 1.5;
  font-size: 14px;
}

.mnemonic-line {
  padding: 8px 10px;
  border-radius: 8px;
  background: #fff6d8;
  color: #6b4a00;
}

.score-line {
  color: var(--green);
  font-weight: 800;
}

.weak-line {
  color: #a65f00;
  font-weight: 800;
}

.inline-audio {
  display: inline-flex;
  align-items: center;
  margin-left: 8px;
  padding: 5px 9px;
  border-radius: 999px;
  font-size: 12px;
  line-height: 1;
  background: #eaf5ff;
  color: #1769aa;
  border: 1px solid #b8ddff;
  vertical-align: middle;
}

.reading-layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 18px;
}

.dialogue-list {
  display: grid;
  gap: 12px;
}

.dialogue-line {
  display: grid;
  grid-template-columns: 110px 1fr auto;
  gap: 16px;
  align-items: center;
  padding: 14px;
}

.dialogue-line.read-done {
  border-color: #9bd9bb;
  background: #f1fbf6;
}

.line-speaker {
  color: var(--blue-dark);
  font-weight: 900;
}

.line-main strong {
  display: block;
  font-size: 17px;
  line-height: 1.4;
}

.line-main p {
  margin: 5px 0 0;
  color: var(--muted);
}

.line-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.segmented {
  display: flex;
  padding: 4px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
}

.segmented button {
  border: 0;
  border-radius: 6px;
  padding: 9px 16px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.segmented button.active {
  color: #fff;
  background: var(--blue);
}

.read-panel {
  position: sticky;
  top: 20px;
  align-self: start;
}

.read-panel li {
  margin-bottom: 8px;
  line-height: 1.5;
}

.read-status {
  margin-top: 14px;
  padding: 12px;
  border-radius: 8px;
  background: #f5f7fb;
  color: var(--muted);
  line-height: 1.5;
}

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

.empty-state {
  padding: 28px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: #fff;
}

.wrong-item {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: center;
  padding: 16px;
}

.wrong-item strong {
  display: block;
  font-size: 20px;
}

.wrong-item span,
.wrong-item p {
  color: var(--muted);
}

.wrong-item div:last-child {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.parent-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

.level-row {
  display: grid;
  grid-template-columns: 46px 1fr 34px;
  gap: 10px;
  align-items: center;
  margin: 11px 0;
}

.level-row div {
  height: 12px;
  border-radius: 999px;
  background: #edf1f7;
  overflow: hidden;
}

.level-row i {
  display: block;
  height: 100%;
  background: var(--green);
}

#weakWords p {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

#reportText {
  width: 100%;
  min-height: 260px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  resize: vertical;
  line-height: 1.6;
}

@media (max-width: 1000px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
  }

  .nav-tabs,
  .stats-grid,
  .mission-layout,
  .word-grid,
  .word-tools,
  .reading-layout,
  .parent-grid {
    grid-template-columns: 1fr;
  }

  .choice-grid.compact,
  .choice-grid {
    grid-template-columns: 1fr;
  }

  .mission-word-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

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

  .mission-review-actions {
    justify-content: flex-start;
  }

  .dialogue-line {
    grid-template-columns: 1fr;
  }

  .line-actions,
  .challenge-actions,
  .word-actions,
  .word-actions.six {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 640px) {
  html,
  body {
    width: 100%;
    overflow-x: hidden;
  }

  body {
    background: #eef3f8;
  }

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

  .auth-screen {
    padding: 16px;
    align-items: start;
  }

  .auth-card {
    margin-top: 28px;
    padding: 22px;
    border-radius: 10px;
  }

  .auth-card h1 {
    font-size: 24px;
  }

  .app-shell {
    display: none;
  }

  body.logged-in .app-shell {
    display: block;
  }

  .sidebar {
    padding: 12px 12px 10px;
    gap: 12px;
    border-radius: 0 0 16px 16px;
    box-shadow: 0 12px 24px rgba(18, 33, 58, .18);
  }

  .brand {
    gap: 10px;
  }

  .brand-mark {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    font-size: 24px;
  }

  .brand h1 {
    font-size: 18px;
    line-height: 1.25;
  }

  .brand p {
    font-size: 12px;
    line-height: 1.35;
  }

  .unit-picker {
    font-size: 13px;
  }

  .unit-picker select,
  .auth-card input,
  .word-tools input,
  .word-tools select,
  .spell-row input,
  .control-label select {
    min-height: 46px;
  }

  .user-panel {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    padding: 10px 12px;
  }

  .user-panel strong {
    font-size: 16px;
  }

  .user-panel button {
    min-height: 40px;
    padding: 8px 12px;
    white-space: nowrap;
  }

  .user-panel p,
  .tip-card {
    display: none;
  }

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

  .stats-grid div {
    padding: 8px 4px;
  }

  .stats-grid strong {
    font-size: 19px;
  }

  .stats-grid span {
    display: block;
    font-size: 11px;
    line-height: 1.25;
  }

  .nav-tabs {
    position: fixed;
    z-index: 50;
    left: 0;
    right: 0;
    bottom: 0;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, .96);
    border-top: 1px solid var(--line);
    box-shadow: 0 -10px 24px rgba(23, 32, 42, .12);
  }

  .tab {
    min-height: 48px;
    padding: 6px 2px;
    border-radius: 10px;
    color: #39485a;
    background: #edf3ff;
    text-align: center;
    font-size: 12px;
    line-height: 1.25;
    font-weight: 900;
  }

  .tab.active,
  .tab:hover {
    color: #12213a;
    background: var(--yellow);
  }

  .workspace {
    padding: 14px 12px 88px;
    overflow: visible;
  }

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

  .challenge-actions button {
    width: 100%;
    min-height: 44px;
    padding: 10px 8px;
  }

  .hero-panel,
  .section-title {
    display: grid;
    gap: 12px;
    margin-bottom: 12px;
  }

  .hero-panel {
    padding: 16px;
    border-radius: 10px;
  }

  .hero-panel h2,
  .section-title h2 {
    font-size: 22px;
    line-height: 1.25;
  }

  .hero-panel p,
  .section-title p,
  .question-label {
    font-size: 14px;
    line-height: 1.55;
  }

  .progress-ring {
    width: 92px;
    height: 92px;
    flex-basis: auto;
    border-width: 7px;
    justify-self: start;
  }

  .progress-ring span {
    font-size: 18px;
  }

  .mission-layout,
  .reading-layout,
  .parent-grid,
  .word-grid {
    gap: 12px;
  }

  .card,
  .word-card,
  .dialogue-line,
  .wrong-item {
    border-radius: 10px;
    box-shadow: 0 8px 20px rgba(23, 32, 42, .07);
  }

  .card,
  .word-card {
    padding: 14px;
  }

  .card-head h3,
  .card h3 {
    font-size: 18px;
    line-height: 1.3;
  }

  .question-label {
    min-height: 0;
  }

  .mission-word-list {
    grid-template-columns: 1fr;
  }

  .mission-word-item {
    padding: 12px;
  }

  .mission-word-item p {
    min-height: 0;
    font-size: 13px;
  }

  .mission-review-item {
    padding: 12px;
  }

  .mission-review-actions,
  .line-actions,
  .wrong-item div:last-child,
  .recorder-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .primary-action,
  .secondary-action,
  .spell-row button,
  .word-actions button,
  .mission-word-actions button,
  .mission-review-actions button,
  .line-actions button,
  .wrong-item button,
  .ghost,
  .recorder-actions button {
    min-height: 44px;
    padding: 10px 12px;
    border-radius: 10px;
  }

  .choice-grid,
  .choice-grid.compact {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .choice-grid button {
    min-height: 50px;
    border-radius: 10px;
  }

  .spell-row,
  .hint-row {
    display: grid;
    grid-template-columns: 1fr;
  }

  .sentence-box {
    padding: 14px;
    font-size: 18px;
    line-height: 1.35;
  }

  .word-tools {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .word-tools input,
  .word-tools button {
    grid-column: 1 / -1;
  }

  .word-top {
    align-items: start;
  }

  .word-top h3 {
    font-size: 20px;
    word-break: break-word;
  }

  .word-actions,
  .word-actions.six {
    grid-template-columns: 1fr 1fr;
  }

  .segmented {
    width: 100%;
  }

  .segmented button {
    flex: 1;
    padding: 10px 8px;
  }

  .read-panel {
    position: static;
  }

  .dialogue-line {
    gap: 10px;
    padding: 12px;
  }

  .line-main strong {
    font-size: 16px;
  }

  .wrong-item {
    display: grid;
    gap: 12px;
  }

  #weakWords p {
    display: grid;
    gap: 4px;
  }

  #reportText {
    min-height: 220px;
  }
}
