/* ===== CSS Variables ===== */
:root {
  --primary: #FF6B6B;
  --primary-hover: #ee5a5a;
  --secondary: #4ECDC4;
  --secondary-hover: #3ebdb5;
  --accent: #FFE66D;
  --accent-hover: #f5dc5b;
  --bg: #F7FFF7;
  --card-bg: #ffffff;
  --text: #2d3436;
  --text-light: #636e72;
  --learned: #00b894;
  --unlearned: #d63031;
  --new: #636e72;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  --shadow-hover: 0 6px 30px rgba(0, 0, 0, 0.12);
  --radius: 16px;
  --radius-sm: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

/* ===== Reset ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-overflow-scrolling: touch;
}

/* ===== App Container ===== */
.app {
  max-width: 600px;
  margin: 0 auto;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ===== Header ===== */
.app-header {
  text-align: center;
  padding: 8px 0 4px;
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 22px;
  color: var(--primary);
  letter-spacing: 2px;
}

/* ===== Tab Bar ===== */
.tab-bar {
  display: flex;
  gap: 6px;
  margin: 8px 0 12px;
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 5px;
  box-shadow: var(--shadow);
  flex-shrink: 0;
}

.tab-btn {
  flex: 1;
  padding: 10px 6px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-light);
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 44px;
}

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

.tab-btn.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 10px rgba(255, 107, 107, 0.3);
}

/* ===== Tab Content ===== */
.tab-panel {
  display: none;
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.tab-panel.active {
  display: flex;
  flex-direction: column;
}

/* ===== Input Section ===== */
.input-section {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 14px;
  flex-shrink: 0;
}

.input-section label {
  display: block;
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--text);
}

.input-section textarea {
  width: 100%;
  height: 100px;
  border: 2px solid #e0e0e0;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 17px;
  font-family: inherit;
  resize: vertical;
  transition: border-color 0.2s;
  line-height: 1.6;
  -webkit-appearance: none;
}

.input-section textarea:focus {
  outline: none;
  border-color: var(--secondary);
}

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

/* ===== Stats Section ===== */
.stats-section {
  margin-bottom: 14px;
  flex-shrink: 0;
}

.stats-cards {
  display: flex;
  gap: 10px;
}

.stat-card {
  flex: 1;
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 14px 8px;
  text-align: center;
  box-shadow: var(--shadow);
}

.stat-value {
  display: block;
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
}

.stat-label {
  display: block;
  font-size: 12px;
  color: var(--text-light);
  margin-top: 2px;
}

.stat-learned .stat-value {
  color: var(--learned);
}

.stat-unlearned .stat-value {
  color: var(--unlearned);
}

/* ===== IO Section ===== */
.io-section {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.io-section .btn {
  flex: 1;
}

.import-btn {
  cursor: pointer;
  text-align: center;
}

/* ===== Character Card ===== */
.card-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-height: 0;
  padding-top: 8px;
}

.char-card {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 28px 36px;
  box-shadow: var(--shadow);
  text-align: center;
  width: 100%;
  max-width: 300px;
  flex-shrink: 0;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}

.pinyin {
  font-size: 28px;
  color: var(--secondary);
  font-weight: 600;
  margin-bottom: 8px;
  min-height: 36px;
  line-height: 36px;
}

.character {
  font-size: 130px;
  line-height: 1.15;
  color: var(--text);
  font-weight: 700;
  user-select: none;
  -webkit-user-select: none;
}

.char-status {
  margin-top: 8px;
  font-size: 14px;
  font-weight: 600;
  min-height: 20px;
}

.char-status.learned {
  color: var(--learned);
}

.char-status.unlearned {
  color: var(--unlearned);
}

/* ===== Progress ===== */
.progress {
  margin-top: 10px;
  font-size: 16px;
  color: var(--text-light);
  font-weight: 600;
  flex-shrink: 0;
}

/* ===== Card Actions (Learn Mode) ===== */
.card-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  justify-content: center;
  width: 100%;
  flex-shrink: 0;
}

/* ===== Navigation Actions ===== */
.nav-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  justify-content: center;
  width: 100%;
  flex-shrink: 0;
}

.nav-actions .btn {
  flex: 1;
  max-width: 120px;
}

/* ===== Quiz Mode ===== */
.quiz-stats {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.quiz-stats span {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.pinyin-hidden {
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

.pinyin-text {
  font-size: 28px;
  color: var(--secondary);
  font-weight: 600;
}

.quiz-actions {
  display: flex;
  gap: 14px;
  margin-top: 20px;
  justify-content: center;
  width: 100%;
  flex-shrink: 0;
}

.quiz-actions .btn {
  flex: 1;
  max-width: 160px;
  font-size: 18px;
  padding: 14px 16px;
}

/* ===== Empty Hint ===== */
.empty-hint {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-light);
  gap: 8px;
}

.empty-hint p:first-child {
  font-size: 20px;
}

.empty-hint p:last-child {
  font-size: 14px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 48px;
  font-family: inherit;
  white-space: nowrap;
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:active {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--secondary);
  color: #fff;
}

.btn-secondary:active {
  background: var(--secondary-hover);
}

.btn-danger {
  background: #eee;
  color: var(--unlearned);
}

.btn-danger:active {
  background: #e0e0e0;
}

.btn-nav {
  background: #f0f0f0;
  color: var(--text);
}

.btn-nav:active {
  background: #e0e0e0;
}

.btn-learned {
  background: #e8f8f5;
  color: var(--learned);
  border: 2px solid var(--learned);
}

.btn-learned:active {
  background: var(--learned);
  color: #fff;
}

.btn-unlearned {
  background: #fde8e8;
  color: var(--unlearned);
  border: 2px solid var(--unlearned);
}

.btn-unlearned:active {
  background: var(--unlearned);
  color: #fff;
}

.btn-speak {
  background: #fef9e7;
  color: #f39c12;
  border: 2px solid #f39c12;
}

.btn-speak:active {
  background: #f39c12;
  color: #fff;
}

.btn-reveal {
  background: var(--accent);
  color: var(--text);
  padding: 8px 18px;
  font-size: 14px;
  min-height: 36px;
  border-radius: 18px;
}

.btn-reveal:active {
  background: var(--accent-hover);
}

.btn-know {
  background: #e8f8f5;
  color: var(--learned);
  border: 2px solid var(--learned);
}

.btn-know:active {
  background: var(--learned);
  color: #fff;
}

.btn-dont-know {
  background: #fde8e8;
  color: var(--unlearned);
  border: 2px solid var(--unlearned);
}

.btn-dont-know:active {
  background: var(--unlearned);
  color: #fff;
}

.btn-small {
  padding: 6px 12px;
  font-size: 13px;
  min-height: 32px;
  border-radius: 8px;
  background: #f0f0f0;
  color: var(--text-light);
}

.btn-small:active {
  background: #e0e0e0;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(30px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: rgba(45, 52, 54, 0.9);
  color: #fff;
  padding: 10px 24px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  pointer-events: none;
  opacity: 0;
  transition: all 0.3s ease;
  z-index: 1000;
  white-space: nowrap;
  max-width: 90vw;
  text-overflow: ellipsis;
  overflow: hidden;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.success {
  background: rgba(0, 184, 148, 0.9);
}

.toast.error {
  background: rgba(214, 48, 49, 0.9);
}

/* ===== Swipe hint ===== */
.swipe-hint {
  font-size: 12px;
  color: var(--text-light);
  margin-top: 6px;
  opacity: 0.6;
  flex-shrink: 0;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 24px;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

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

.modal {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 28px 24px 20px;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.2);
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-body {
  font-size: 17px;
  font-weight: 600;
  text-align: center;
  line-height: 1.5;
  color: var(--text);
  margin-bottom: 24px;
}

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

.modal-actions .btn {
  flex: 1;
}

/* ===== Responsive: Tablet/Desktop (768px+) ===== */
@media (min-width: 768px) {
  .app {
    padding: 24px;
    height: auto;
    min-height: 100vh;
  }

  html, body {
    overflow: auto;
  }

  .tab-panel {
    overflow: visible;
  }

  .app-header h1 {
    font-size: 28px;
  }

  .tab-btn {
    font-size: 16px;
    padding: 12px 8px;
  }

  .input-section {
    padding: 24px;
  }

  .input-section label {
    font-size: 16px;
  }

  .input-section textarea {
    height: 120px;
    font-size: 18px;
  }

  .char-card {
    padding: 40px 48px;
    max-width: 340px;
  }

  .character {
    font-size: 160px;
  }

  .pinyin, .pinyin-text {
    font-size: 32px;
  }

  .pinyin {
    min-height: 42px;
    line-height: 42px;
  }

  .pinyin-hidden {
    min-height: 42px;
  }

  .stat-value {
    font-size: 28px;
  }

  .stat-label {
    font-size: 13px;
  }

  .card-actions {
    gap: 12px;
  }

  .nav-actions .btn {
    max-width: none;
  }

  .quiz-actions .btn {
    max-width: 160px;
    font-size: 20px;
  }

  .swipe-hint {
    display: none;
  }
}

/* ===== Responsive: Small phones (≤360px) ===== */
@media (max-width: 360px) {
  .app {
    padding: 8px 12px calc(8px + var(--safe-bottom));
  }

  .app-header h1 {
    font-size: 20px;
  }

  .tab-btn {
    font-size: 13px;
    padding: 8px 4px;
    min-height: 40px;
  }

  .char-card {
    padding: 20px 24px;
  }

  .character {
    font-size: 110px;
  }

  .pinyin, .pinyin-text {
    font-size: 24px;
  }

  .pinyin {
    min-height: 32px;
    line-height: 32px;
  }

  .pinyin-hidden {
    min-height: 32px;
  }

  .btn {
    padding: 10px 12px;
    font-size: 13px;
  }

  .stat-value {
    font-size: 20px;
  }

  .card-actions {
    gap: 8px;
  }

  .quiz-actions .btn {
    font-size: 16px;
    padding: 12px 10px;
  }
}

/* ===== Responsive: Tall phones (e.g. iPhone 14 Pro Max) ===== */
@media (min-height: 800px) and (max-width: 480px) {
  .character {
    font-size: 150px;
  }

  .pinyin, .pinyin-text {
    font-size: 30px;
  }

  .char-card {
    padding: 32px 40px;
  }
}

/* ===== Landscape on phones ===== */
@media (max-height: 500px) and (orientation: landscape) {
  .app {
    padding: 4px 16px;
  }

  .app-header {
    display: none;
  }

  .tab-bar {
    margin: 4px 0 6px;
  }

  .character {
    font-size: 80px;
  }

  .pinyin, .pinyin-text {
    font-size: 22px;
  }

  .pinyin {
    min-height: 28px;
    line-height: 28px;
    margin-bottom: 4px;
  }

  .pinyin-hidden {
    min-height: 28px;
    margin-bottom: 4px;
  }

  .char-card {
    padding: 12px 24px;
  }

  .card-actions, .nav-actions, .quiz-actions {
    margin-top: 8px;
    gap: 8px;
  }

  .btn {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 13px;
  }
}
