/* ═══════════════════════════════════════════
   WenYan Study — 文言文背词系统
   Modern clean design, responsive
   ═══════════════════════════════════════════ */

:root {
  --bg: #f7f8fa;
  --card-bg: #ffffff;
  --card-border: #e8ecf1;
  --text: #1a1c2e;
  --text-secondary: #7e8398;
  --text-light: #9ca0b0;
  --accent: #5b6fe6;
  --accent-hover: #4a5cd4;
  --accent-light: #eef1ff;
  --accent-ghost: #f5f6ff;
  --success: #34c759;
  --success-bg: #e8f9ed;
  --error: #e5484d;
  --error-bg: #fef2f2;
  --warning: #f59e0b;
  --warning-bg: #fffbeb;
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.04), 0 8px 40px rgba(0,0,0,0.06);
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04), 0 2px 8px rgba(0,0,0,0.04);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  --transition: 0.2s ease;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

/* ─── Layout Container ─── */
.app-container {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100dvh;
  background: var(--bg);
  position: relative;
}

@media (min-width: 768px) {
  .app-container {
    max-width: 880px;
  }
}

/* Desktop: flashcard 2-column layout */
@media (min-width: 768px) {
  .learn-flashcard {
    display: flex;
    flex-direction: row;
    gap: 32px;
    align-items: stretch;
    text-align: left;
    padding: 32px;
  }

  .learn-flashcard .word-block {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 160px;
    padding-right: 32px;
    border-right: 2px solid var(--card-border);
  }

  .learn-flashcard .meanings-block {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .learn-flashcard .word-char {
    font-size: 64px;
  }

  .learn-flashcard .meaning-item {
    font-size: 16px;
    padding: 10px 0;
  }
}

/* Desktop header adjustments */
@media (min-width: 768px) {
  .learn-header {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 24px;
  }

  .learn-header-label {
    margin-bottom: 0;
    white-space: nowrap;
  }

  .learn-header-count {
    margin-bottom: 0;
    order: 1;
  }

  .learn-header-bar {
    flex: 1;
    height: 6px;
  }

  .learn-footer {
    padding: 14px 24px;
  }

  .learn-card-scroll {
    padding: 24px;
  }
}

/* ─── Header ─── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--card-border);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(20px);
}

.app-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
}

.app-logo .icon {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, #5b6fe6, #8b5cf6);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: white;
}

.app-logo .title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.app-logo .subtitle {
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 400;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ─── User Badge ─── */
.user-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--bg);
  border-radius: 20px;
  font-size: 13px;
  text-decoration: none;
  color: var(--text);
}

.user-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5b6fe6, #a78bfa);
  color: white;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
}

.login-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
  font-size: 13px;
}

.logout-link {
  color: var(--error);
  background: none;
  border: none;
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}

.logout-link:hover { background: var(--error-bg); }

/* ─── Content Area ─── */
.page-content {
  padding: 20px;
  padding-bottom: 40px;
}

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  font-family: var(--font);
  border: none;
  cursor: pointer;
  transition: all var(--transition);
  text-decoration: none;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--card-border);
}
.btn-secondary:hover { background: #eef0f5; }

.btn-success {
  background: var(--success);
  color: white;
}

.btn-danger {
  background: var(--error);
  color: white;
}

.btn-outline {
  background: transparent;
  color: var(--accent);
  border: 1.5px solid var(--accent);
}
.btn-outline:hover { background: var(--accent-light); }

.btn-sm {
  padding: 8px 16px;
  font-size: 13px;
  border-radius: var(--radius-xs);
}

.btn-lg {
  padding: 16px 32px;
  font-size: 17px;
  border-radius: var(--radius);
}

.btn-block { width: 100%; }

.btn-icon {
  width: 40px; height: 40px;
  padding: 0;
  border-radius: 50%;
  font-size: 18px;
}

/* ─── Cards ─── */
.card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.card-flat {
  box-shadow: none;
}

/* ─── Dashboard Stats ─── */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 18px;
  text-align: center;
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition);
}

.stat-card:active { transform: scale(0.98); }

.stat-number {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -1px;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-number.accent { color: var(--accent); }
.stat-number.success { color: var(--success); }
.stat-number.warning { color: var(--warning); }
.stat-number.error { color: var(--error); }

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.stat-sub {
  font-size: 11px;
  color: var(--text-light);
  margin-top: 2px;
}

/* ─── Book Selector ─── */
.book-selector {
  display: flex;
  gap: 10px;
  margin-bottom: 24px;
  overflow-x: auto;
  padding: 2px;
  -webkit-overflow-scrolling: touch;
}

.book-selector::-webkit-scrollbar { display: none; }

.book-chip {
  flex: 0 0 auto;
  padding: 10px 18px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  background: var(--card-bg);
  border: 1.5px solid var(--card-border);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.book-chip.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.book-chip:hover:not(.active) {
  border-color: var(--accent);
  background: var(--accent-ghost);
}

/* ─── Flashcard ─── */
.flashcard-container {
  perspective: 1000px;
  margin-bottom: 20px;
}

.flashcard {
  background: var(--card-bg);
  border: 1.5px solid var(--card-border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: var(--shadow);
  text-align: center;
  min-height: 200px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
  overflow: hidden;
}

.flashcard:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(91,111,230,0.12);
}

.flashcard .word-char {
  font-size: 48px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
  margin-bottom: 4px;
  letter-spacing: 2px;
}

.flashcard .word-pinyin {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.flashcard .word-type-tag {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  background: var(--accent-light);
  color: var(--accent);
  margin-bottom: 16px;
}

.flashcard .meanings-list {
  text-align: left;
  list-style: none;
}

.flashcard .meaning-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--card-border);
  font-size: 15px;
  line-height: 1.5;
}

.flashcard .meaning-item:last-child { border-bottom: none; }

.flashcard .meaning-num {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  background: var(--bg);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
}

.flashcard .meaning-text {
  flex: 1;
}

.flashcard .meaning-pos {
  font-size: 11px;
  color: var(--text-light);
  font-weight: 500;
  margin-left: 4px;
}

.flashcard .example-count {
  font-size: 11px;
  color: var(--accent);
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--accent-ghost);
  white-space: nowrap;
  border: none;
  font-family: var(--font);
  transition: all var(--transition);
}

.flashcard .example-count:hover {
  background: var(--accent-light);
}

.flashcard .tap-hint {
  position: absolute;
  bottom: 10px;
  right: 16px;
  font-size: 11px;
  color: var(--text-light);
  opacity: 0.6;
}

/* ─── Example Modal (popover style - position stable) ─── */
.example-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.25);
  z-index: 200;
  justify-content: center;
  align-items: flex-end;
}

.example-overlay.show {
  display: flex;
}

.example-sheet {
  background: var(--card-bg);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 8px 20px 32px;
  box-shadow: 0 -4px 30px rgba(0,0,0,0.1);
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.example-sheet .sheet-handle {
  width: 40px; height: 4px;
  background: #d1d5db;
  border-radius: 2px;
  margin: 8px auto 16px;
}

.example-sheet .sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.example-sheet .sheet-title {
  font-size: 17px;
  font-weight: 700;
}

.example-sheet .sheet-close {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--bg);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.example-block {
  padding: 14px 0;
  border-bottom: 1px solid var(--card-border);
}

.example-block:last-child { border-bottom: none; }

.example-block .example-meaning {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.example-block .example-meaning-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
}

.example-block .example-sentence {
  font-size: 17px;
  color: var(--text);
  font-family: var(--font);
  line-height: 1.6;
}

.example-block .example-source {
  font-size: 12px;
  color: var(--text-light);
}

.example-block .example-translation {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
  padding-left: 8px;
  border-left: 2px solid var(--card-border);
}

/* ─── Quiz ─── */
.quiz-section {
  margin-top: 24px;
}

.quiz-card {
  background: var(--card-bg);
  border: 1.5px solid var(--accent);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow-sm);
}

.quiz-card.correct {
  border-color: var(--success);
  background: var(--success-bg);
}

.quiz-card.wrong {
  border-color: var(--error);
  background: var(--error-bg);
}

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

.quiz-header .quiz-badge {
  padding: 3px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  background: var(--accent-light);
  color: var(--accent);
}

.quiz-example {
  font-size: 16px;
  line-height: 1.7;
  padding: 16px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  margin-bottom: 16px;
}

.quiz-example .highlight-word {
  color: var(--accent);
  font-weight: 700;
}

.quiz-options {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.quiz-option {
  padding: 14px 16px;
  background: var(--bg);
  border: 1.5px solid var(--card-border);
  border-radius: var(--radius-xs);
  font-size: 14px;
  cursor: pointer;
  transition: all var(--transition);
  text-align: left;
  font-family: var(--font);
}

.quiz-option:hover {
  border-color: var(--accent);
  background: var(--accent-ghost);
}

.quiz-option.selected {
  border-color: var(--accent);
  background: var(--accent-light);
}

.quiz-option.correct-answer {
  border-color: var(--success);
  background: var(--success-bg);
  color: var(--success);
}

.quiz-option.wrong-answer {
  border-color: var(--error);
  background: var(--error-bg);
  color: var(--error);
}

.quiz-answer-feedback {
  text-align: center;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  margin-top: 12px;
}

/* ─── Progress Bar ─── */
.progress-bar-container {
  margin-bottom: 20px;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

.progress-bar {
  height: 6px;
  background: #e8ecf1;
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  border-radius: 3px;
  transition: width 0.3s ease;
}

/* ─── Learn Page Fullscreen Layout ─── */
#pageContent:has(.learn-layout) {
  padding: 0;
  height: calc(100dvh - 61px); /* subtract header */
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.learn-layout {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* Compact header-integrated progress bar */
.learn-header {
  flex-shrink: 0;
  padding: 10px 20px 8px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--card-border);
}

.learn-header-label {
  font-size: 11px;
  color: var(--text-light);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  display: block;
  margin-bottom: 4px;
}

.learn-header-count {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.5px;
  line-height: 1;
  display: block;
  margin-bottom: 8px;
}

.learn-header-total {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.learn-header-bar {
  height: 4px;
  background: #e8ecf1;
  border-radius: 2px;
  overflow: hidden;
}

.learn-header-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  border-radius: 2px;
  transition: width 0.35s ease;
}

/* Scrollable flashcard area */
.learn-card-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.learn-flashcard {
  margin: 0;
  min-height: auto;
  cursor: default;
}

.learn-flashcard:hover {
  transform: none;
  box-shadow: var(--shadow);
}

/* Fixed footer buttons */
.learn-footer {
  flex-shrink: 0;
  display: flex;
  gap: 12px;
  padding: 12px 20px;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
  background: var(--card-bg);
  border-top: 1px solid var(--card-border);
}

/* ─── Navigation ─── */
.nav-tabs {
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 4px;
  border-radius: 12px;
  margin-bottom: 20px;
}

.nav-tab {
  flex: 1;
  padding: 10px;
  border-radius: 10px;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font);
  transition: all var(--transition);
}

.nav-tab.active {
  background: var(--card-bg);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.nav-tab:hover:not(.active) {
  color: var(--text);
}

/* ─── Empty State ─── */
.empty-state {
  text-align: center;
  padding: 48px 20px;
}

.empty-state .empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state .empty-title {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 6px;
}

.empty-state .empty-desc {
  font-size: 14px;
  color: var(--text-secondary);
}

/* ─── Admin Panel ─── */
.admin-panel {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px;
}

.admin-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.admin-tabs .nav-tab { flex: 0 0 auto; white-space: nowrap; }

.admin-search {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.admin-search input {
  flex: 1;
  padding: 10px 14px;
  border: 1.5px solid var(--card-border);
  border-radius: var(--radius-xs);
  font-size: 14px;
  font-family: var(--font);
  background: var(--card-bg);
}

.admin-search input:focus {
  outline: none;
  border-color: var(--accent);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.admin-table th {
  text-align: left;
  padding: 10px 12px;
  background: var(--bg);
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.admin-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--card-border);
  vertical-align: top;
}

.admin-table tr:hover td { background: var(--accent-ghost); }

.admin-word-edit {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}

.admin-form-group {
  margin-bottom: 12px;
}

.admin-form-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.admin-form-group input,
.admin-form-group textarea,
.admin-form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--card-border);
  border-radius: var(--radius-xs);
  font-size: 14px;
  font-family: var(--font);
}

.admin-form-group textarea { min-height: 80px; resize: vertical; }

.admin-form-group input:focus,
.admin-form-group textarea:focus,
.admin-form-group select:focus {
  outline: none;
  border-color: var(--accent);
}

.admin-meaning-row {
  border: 1px solid var(--card-border);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 8px;
}

.admin-meaning-examples {
  margin-left: 20px;
  margin-top: 8px;
  padding-left: 12px;
  border-left: 2px solid var(--card-border);
}

.admin-example-row {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
  align-items: center;
}

.admin-example-row input { flex: 1; }

/* ─── Toast ─── */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 900;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius-xs);
  font-size: 14px;
  font-weight: 600;
  color: white;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s;
  max-width: 320px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.toast.success { background: var(--success); }
.toast.error { background: var(--error); }
.toast.info { background: var(--accent); }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}
@keyframes toastOut {
  from { opacity: 1; transform: translateX(0); }
  to { opacity: 0; transform: translateX(40px); }
}

/* ─── Loading ─── */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--card-border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Responsive ─── */
@media (max-width: 479px) {
  .page-content { padding: 16px; }
  .stats-grid { gap: 8px; }
  .stat-card { padding: 14px 10px; }
  .stat-number { font-size: 26px; }
  .flashcard { padding: 22px 16px; }
  .flashcard .word-char { font-size: 40px; }
  .flashcard .meaning-item { font-size: 14px; }
  .btn-lg { padding: 14px 24px; font-size: 16px; }
}

/* Scrollbar styling */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 2px; }

/* Utility */
.hidden { display: none !important; }
.text-center { text-align: center; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.mb-24 { margin-bottom: 24px; }
.gap-8 { gap: 8px; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.flex-1 { flex: 1; }
