:root {
  --bg: #f5f3ef;
  --surface: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --border: #e5e7eb;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

header {
  text-align: center;
  margin-bottom: 24px;
}

.home-link {
  display: inline-block;
  margin-bottom: 12px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
}

.home-link:hover { color: var(--primary); }

header h1 {
  font-size: 1.75rem;
  font-weight: 700;
}

.subtitle {
  color: var(--muted);
  font-size: 0.95rem;
  margin-top: 4px;
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface);
  padding: 4px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow-x: auto;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 8px;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--muted);
  white-space: nowrap;
  transition: all 0.2s;
}

.tab:hover { color: var(--text); }
.tab.active {
  background: var(--primary);
  color: white;
  font-weight: 600;
}

.panel { display: none; }
.panel.active { display: block; }

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

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  text-align: center;
}

.stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--primary);
}

.stat-label {
  font-size: 0.85rem;
  color: var(--muted);
  margin-top: 2px;
}

.workflow-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.workflow-card h3 {
  font-size: 1rem;
  margin-bottom: 8px;
}

.workflow-steps {
  padding-left: 1.2rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.workflow-steps li { margin-bottom: 4px; }
.workflow-steps strong { color: var(--text); }

.mastery-bar {
  display: flex;
  height: 28px;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 16px;
  background: var(--border);
}

.mastery-seg {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
}

.mastery-seg span { padding: 0 4px; }
.mastery-seg.mastery-0 { background: #fecaca; color: #991b1b; }
.mastery-seg.mastery-1 { background: #fde68a; color: #92400e; }
.mastery-seg.mastery-2 { background: #bfdbfe; color: #1e40af; }
.mastery-seg.mastery-3 { background: #bbf7d0; color: #166534; }
.mastery-seg.mastery-4 { background: #6ee7b7; color: #065f46; }

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

.hint-inline {
  display: block;
  font-weight: 400;
  color: var(--muted);
  font-size: 0.8rem;
  margin: 4px 0 6px;
}

select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  background: white;
}

.setup h3 { margin-bottom: 8px; }

.card-meta {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 8px;
  min-height: 1.2em;
}

.mode-badge {
  display: inline-block;
  background: #eff6ff;
  color: var(--primary);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 999px;
  margin-bottom: 12px;
}

.spell-hint {
  color: var(--muted);
  margin-bottom: 16px;
}

.spell-meaning {
  margin-top: 16px;
  color: var(--text);
  font-size: 1.05rem;
}

.speak-btn.large {
  width: auto;
  height: auto;
  border-radius: 10px;
  padding: 12px 20px;
  gap: 8px;
}

.spell-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.spell-answer-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.spell-answer-row input {
  flex: 1;
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.08em;
  font-weight: 600;
  text-align: center;
  background: #f8fafc;
  cursor: default;
}

.spell-answer-row input.spell-shake {
  animation: spell-shake 0.35s ease;
  border-color: var(--danger);
}

@keyframes spell-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

.spell-letters {
  display: grid;
  grid-template-columns: repeat(var(--spell-cols, 10), minmax(0, 1fr));
  gap: 8px;
  padding: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: 56px;
}

.letter-btn {
  width: 100%;
  aspect-ratio: 1;
  height: auto;
  max-height: 52px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: white;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  text-transform: lowercase;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.letter-btn:hover:not(:disabled) {
  border-color: var(--primary);
  background: #eff6ff;
  color: var(--primary);
  transform: translateY(-1px);
}

.letter-btn:active:not(:disabled) {
  transform: scale(0.96);
}

.letter-btn.used,
.letter-btn:disabled {
  opacity: 0.35;
  cursor: default;
  transform: none;
}

.spell-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.spell-actions .btn.primary {
  min-width: 100px;
}

.wrong-list {
  text-align: left;
  background: #fff7ed;
  border-radius: 8px;
  padding: 12px 16px;
  margin: 16px 0;
}

.wrong-list h4 { margin-bottom: 8px; font-size: 0.95rem; }
.wrong-item { font-size: 0.9rem; margin-bottom: 4px; color: var(--muted); }
.wrong-item strong { color: var(--text); }

.done-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
}

.pos-tag {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  background: #eff6ff;
  padding: 1px 6px;
  border-radius: 4px;
  margin-left: 6px;
}

.phonetic-inline {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.9rem;
  margin-left: 6px;
}

.word-item-unit {
  font-size: 0.8rem;
  color: var(--primary);
  margin-top: 2px;
}

.icon-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 4px 6px;
}

.icon-btn:hover { color: var(--primary); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  width: min(480px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

.modal-card h3 { margin-bottom: 12px; }

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 8px;
}

.words-header select {
  width: auto;
  min-width: 120px;
  margin: 0;
}

.action-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  cursor: pointer;
  text-align: left;
  transition: all 0.2s;
}

.card-btn:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow);
}

.card-btn.primary {
  border-color: var(--primary);
  background: #eff6ff;
}

.card-icon { font-size: 1.5rem; margin-bottom: 4px; }
.card-title { font-size: 1.1rem; font-weight: 600; }
.card-desc { font-size: 0.85rem; color: var(--muted); margin-top: 2px; }

.form-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.form-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
}

.form-tab.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.form-panel { display: none; }
.form-panel.active { display: block; }

label {
  display: block;
  margin-bottom: 12px;
  font-size: 0.9rem;
  font-weight: 500;
}

input, textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
}

input:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.btn {
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  background: var(--border);
  color: var(--text);
  transition: background 0.2s;
}

.btn:hover { filter: brightness(0.95); }
.btn.primary { background: var(--primary); color: white; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.correct { background: var(--success); color: white; }
.btn.wrong { background: var(--danger); color: white; }

.message {
  margin-top: 12px;
  padding: 12px;
  border-radius: 8px;
  font-size: 0.9rem;
}

.message.success { background: #dcfce7; color: #166534; }
.message.error { background: #fee2e2; color: #991b1b; }
.hidden { display: none !important; }

.setup {
  background: var(--surface);
  padding: 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.setup p { margin-bottom: 16px; color: var(--muted); }

.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  margin-bottom: 8px;
  overflow: hidden;
}

.progress-bar > div {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  transition: width 0.3s;
  width: 0%;
}

.progress-text {
  text-align: center;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 20px;
}

.flashcard {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 48px 24px;
  text-align: center;
  cursor: pointer;
  min-height: 220px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-bottom: 20px;
  user-select: none;
}

.card-word-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.card-word {
  font-size: 2.2rem;
  font-weight: 700;
}

.speak-btn {
  border: none;
  background: #eff6ff;
  color: var(--primary);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.speak-btn:hover {
  background: var(--primary);
  color: white;
  transform: scale(1.05);
}

.speak-btn:active {
  transform: scale(0.95);
}

.word-item .speak-btn {
  width: 36px;
  height: 36px;
  font-size: 1rem;
}

.card-phonetic {
  color: var(--muted);
  font-size: 1.1rem;
  margin-top: 8px;
}

.card-meaning {
  font-size: 1.5rem;
  color: var(--primary);
  font-weight: 600;
}

.card-example {
  margin-top: 12px;
  color: var(--muted);
  font-style: italic;
}

.flip-hint {
  margin-top: 24px;
  font-size: 0.85rem;
  color: var(--muted);
}

.actions {
  display: flex;
  gap: 12px;
}

.actions .btn { flex: 1; padding: 14px; font-size: 1.05rem; }

.done {
  text-align: center;
  padding: 32px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.done h2 { margin-bottom: 12px; }

.test-question {
  background: var(--surface);
  padding: 32px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
  margin-bottom: 16px;
}

.options {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.option-btn {
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-size: 1rem;
  cursor: pointer;
  text-align: left;
  transition: all 0.2s;
}

.option-btn:hover:not(:disabled) {
  border-color: var(--primary);
}

.option-btn.correct-answer {
  border-color: var(--success);
  background: #dcfce7;
}

.option-btn.wrong-answer {
  border-color: var(--danger);
  background: #fee2e2;
}

.feedback {
  text-align: center;
  padding: 12px;
  border-radius: 8px;
  margin-bottom: 12px;
  font-weight: 600;
}

.feedback.correct { background: #dcfce7; color: #166534; }
.feedback.wrong { background: #fee2e2; color: #991b1b; }

.score {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--primary);
  margin: 16px 0;
}

.words-header {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 16px;
}

.words-header input { flex: 1; margin: 0; }

.badge {
  background: var(--primary);
  color: white;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.word-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.word-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.word-item-main { flex: 1; min-width: 0; }

.word-item-word {
  font-weight: 600;
  font-size: 1.05rem;
}

.word-item-meaning {
  color: var(--muted);
  font-size: 0.9rem;
}

.word-item-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.mastery-tag {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
}

.mastery-0 { background: #fee2e2; color: #991b1b; }
.mastery-1 { background: #fef3c7; color: #92400e; }
.mastery-2 { background: #dbeafe; color: #1e40af; }
.mastery-3 { background: #dcfce7; color: #166534; }
.mastery-4 { background: #d1fae5; color: #065f46; }

.delete-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.2rem;
  padding: 4px 8px;
}

.delete-btn:hover { color: var(--danger); }

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--muted);
}

.public-list { display: flex; flex-direction: column; gap: 16px; margin-bottom: 16px; }
.public-chapter {
  background: var(--surface, #fff);
  border: 1px solid var(--border, #e2e8f0);
  border-radius: var(--radius, 12px);
  overflow: hidden;
}
.public-chapter-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  background: rgba(148, 163, 184, 0.12);
}
.public-chapter-title { font-weight: 600; font-size: 0.95rem; }
.public-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px 14px;
  border-top: 1px solid var(--border, #e2e8f0);
  cursor: pointer;
  margin-bottom: 0;
}
.public-item:last-child { border-bottom: none; }
.public-item.already-added { opacity: 0.55; }
.public-item input[type="checkbox"] {
  margin-top: 4px;
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  cursor: pointer;
  display: inline-block;
}
.public-item input[type="checkbox"]:disabled { cursor: not-allowed; }
.public-item-body { flex: 1; min-width: 0; }
.public-item-title { font-weight: 600; margin: 2px 0 4px; }
.public-item-content {
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
  white-space: normal;
}
.public-import-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 8px;
}
.public-import-bar #public-selected-count { flex: 1; color: var(--muted); font-size: 0.9rem; }
.btn.small {
  padding: 4px 10px;
  font-size: 0.8rem;
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .card-word { font-size: 1.8rem; }
  .form-row { grid-template-columns: 1fr; }
  .words-header { flex-wrap: wrap; }
  .letter-btn { font-size: 1.05rem; max-height: 44px; }
  .public-import-bar { flex-wrap: wrap; }
  .public-import-bar #public-selected-count { flex-basis: 100%; }
}
