:root {
  --bg-main: #EBF7FC;
  --bg-gradient: linear-gradient(135deg, #E0F4FF 0%, #F0FAFF 100%);
  --bg-card: #FFFFFF;
  --primary: #1CA7EC;
  --primary-dark: #0F82C0;
  --primary-light: #E1F5FE;
  --accent: #00C4B4;
  --sunshine: #FFB703;
  --text-dark: #1E3A4C;
  --text-muted: #5C7C8F;
  --border: #C5E6F6;
  --correct-bg: #E6FBF5;
  --correct-border: #10B981;
  --correct-text: #067647;
  --wrong-bg: #FFF5F5;
  --wrong-border: #F87171;
  --wrong-text: #B91C1C;
  --exp-bg: #F4FBFF;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  /* 繁中優先：避免 Zen Maru（日文圓體）缺字造成「同一句混兩種字」 */
  --font-zh: 'Noto Sans TC', "PingFang TC", "Microsoft JhengHei", "Noto Sans CJK TC", sans-serif;
  --font-display: 'Zen Maru Gothic', var(--font-zh);
}

* {
  box-sizing: border-box;
  font-family: var(--font-zh);
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  background: var(--bg-gradient);
  background-color: var(--bg-main);
  color: var(--text-dark);
  margin: 0;
  padding: 16px;
  padding-top: calc(16px + var(--safe-top));
  padding-bottom: calc(16px + var(--safe-bottom));
  line-height: 1.7;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

.app-wrapper {
  max-width: 880px;
  width: 100%;
  margin: 0 auto;
  background: var(--bg-card);
  border-radius: 24px;
  box-shadow: 0 10px 30px rgba(28, 167, 236, 0.12);
  padding: 28px;
  border: 2px solid var(--border);
}

.header {
  text-align: center;
  padding-bottom: 16px;
  margin-bottom: 18px;
  border-bottom: 2px dashed var(--border);
}

.header-badge {
  display: inline-block;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  color: #fff;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 5px 16px;
  border-radius: 30px;
  margin-bottom: 10px;
  box-shadow: 0 4px 10px rgba(28, 167, 236, 0.25);
  max-width: 100%;
  word-break: keep-all;
  font-family: var(--font-display);
}

.header h1 {
  margin: 0 0 8px 0;
  font-size: 1.65rem;
  font-weight: 900;
  color: var(--primary-dark);
  letter-spacing: 0.5px;
  line-height: 1.35;
  /* 標題仍以繁中完整字體為主，避免標題缺字 */
  font-family: var(--font-zh);
}

.header p {
  margin: 0;
  font-size: 0.95rem;
  color: var(--text-muted);
  font-weight: 500;
}

.cheer-banner {
  background: linear-gradient(90deg, #E1F5FE 0%, #E0FBF6 100%);
  border: 1.5px solid #9EE0F8;
  border-radius: 16px;
  padding: 14px 18px;
  margin-bottom: 22px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  user-select: none;
}

.cheer-banner:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(28, 167, 236, 0.15);
}

.cheer-text {
  font-size: 1rem;
  font-weight: 700;
  color: var(--primary-dark);
  flex: 1 1 260px;
  line-height: 1.55;
  text-align: left;
}

.cheer-hint {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--primary-dark);
  white-space: nowrap;
  background: #ffffff;
  padding: 5px 12px;
  border-radius: 20px;
  border: 1px solid #B3E5FC;
  margin-left: auto;
}

.subject-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 16px;
}

.subject-card {
  background: #F8FDFF;
  border: 2px solid var(--border);
  border-radius: 18px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.25s ease;
  text-align: left;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 120px;
}

.subject-card:hover,
.subject-card:focus-visible {
  border-color: var(--primary);
  background: var(--primary-light);
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(28, 167, 236, 0.18);
  outline: none;
}

.subject-card:active {
  transform: translateY(0);
}

.subject-tag {
  font-size: 0.8rem;
  color: var(--primary-dark);
  background: #fff;
  padding: 4px 12px;
  border-radius: 20px;
  display: inline-block;
  width: fit-content;
  margin-bottom: 10px;
  font-weight: 700;
  border: 1.5px solid var(--border);
}

.subject-title {
  font-size: 1.12rem;
  font-weight: 900;
  color: var(--text-dark);
  margin-bottom: 8px;
  line-height: 1.4;
}

.subject-meta {
  font-size: 0.88rem;
  color: var(--primary-dark);
  font-weight: 700;
}

.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--primary-light);
  padding: 10px 16px;
  border-radius: 14px;
  margin-bottom: 12px;
  font-size: 0.94rem;
  font-weight: 700;
  color: var(--primary-dark);
  flex-wrap: wrap;
  gap: 6px 12px;
}

.progress-track {
  width: 100%;
  height: 10px;
  background: #E2F1F8;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 20px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  width: 0%;
  transition: width 0.35s ease;
  border-radius: 10px;
}

.q-badge {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 3px 12px;
  border-radius: 20px;
  margin-bottom: 10px;
  max-width: 100%;
  word-break: break-word;
  line-height: 1.4;
}

.question-box {
  font-size: 1.14rem;
  font-weight: 700;
  margin-bottom: 18px;
  color: var(--text-dark);
  white-space: pre-line;
  word-break: break-word;
  overflow-wrap: anywhere;
  line-height: 1.65;
}

.options-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.opt-btn {
  background: #fff;
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-dark);
  text-align: left;
  cursor: pointer;
  transition: all 0.2s ease;
  line-height: 1.55;
  word-break: break-word;
  overflow-wrap: anywhere;
  width: 100%;
  min-height: 48px;
}

.opt-btn:hover:not(.disabled) {
  border-color: var(--primary);
  background: var(--exp-bg);
}

.opt-btn.disabled {
  cursor: default;
}

.opt-btn.correct {
  background: var(--correct-bg);
  border-color: var(--correct-border);
  color: var(--correct-text);
  font-weight: 700;
}

.opt-btn.wrong {
  background: var(--wrong-bg);
  border-color: var(--wrong-border);
  color: var(--wrong-text);
  font-weight: 700;
}

.exp-box {
  margin-top: 20px;
  padding: 18px;
  border-radius: 16px;
  background: var(--exp-bg);
  border-left: 6px solid var(--primary);
  border-top: 1px solid var(--border);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  display: none;
  animation: fadeIn 0.3s ease;
}

.exp-box.wrong-alert {
  border-left-color: var(--sunshine);
  background: #FFFDF7;
  border-color: #FDE68A;
}

.exp-cheer {
  font-size: 1.02rem;
  font-weight: 900;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--border);
  line-height: 1.5;
}

.exp-content {
  font-size: 0.96rem;
  color: var(--text-dark);
  white-space: pre-line;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-weight: 500;
  line-height: 1.65;
}

.nav-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 2px dashed var(--border);
  flex-wrap: wrap;
}

.btn {
  background: linear-gradient(90deg, var(--primary), #1592D6);
  color: #fff;
  border: none;
  padding: 12px 22px;
  border-radius: 30px;
  font-size: 0.98rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(28, 167, 236, 0.25);
  transition: all 0.2s ease;
  text-align: center;
  min-height: 48px;
}

.btn:hover {
  background: var(--primary-dark);
  transform: translateY(-2px);
}

.btn:disabled {
  opacity: 0.55;
  cursor: wait;
  transform: none;
}

.btn-outline {
  background: #fff;
  color: var(--primary-dark);
  border: 2px solid var(--border);
  box-shadow: none;
}

.btn-outline:hover {
  background: var(--primary-light);
  border-color: var(--primary);
}

.result-box {
  text-align: center;
  padding: 12px 0;
}

.score-circle {
  width: 135px;
  height: 135px;
  border-radius: 50%;
  background: linear-gradient(135deg, #E1F5FE 0%, #E0FBF6 100%);
  border: 5px solid var(--primary);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: 0 auto 18px auto;
  color: var(--primary-dark);
  box-shadow: 0 8px 20px rgba(28, 167, 236, 0.18);
}

.score-num {
  font-size: 2.3rem;
  font-weight: 900;
  line-height: 1;
}

.score-label {
  font-size: 0.88rem;
  font-weight: 700;
  margin-top: 6px;
}

.review-list {
  text-align: left;
  margin-top: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.review-item {
  border: 2px solid var(--border);
  border-radius: 16px;
  padding: 16px;
  background: #fff;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.review-item-success {
  text-align: center;
  color: var(--correct-text);
  font-weight: 700;
  font-size: 1.1rem;
}

.review-title {
  margin: 12px 0 6px 0;
  color: var(--primary-dark);
}

.review-q {
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 10px;
}

.review-yours {
  color: var(--wrong-text);
  font-size: 0.95rem;
  margin-bottom: 4px;
}

.review-correct {
  color: var(--correct-text);
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 12px;
}

.review-exp {
  background: var(--exp-bg);
  padding: 14px;
  border-radius: 12px;
  font-size: 0.95rem;
  white-space: pre-line;
  border-left: 5px solid var(--primary);
}

.result-heading {
  margin: 0 0 10px 0;
  color: var(--primary-dark);
}

.result-summary {
  color: var(--text-dark);
  font-weight: 700;
  font-size: 1.08rem;
  margin-bottom: 24px;
  white-space: pre-line;
}

.result-actions {
  display: flex;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
}

.loading-panel,
.error-panel {
  text-align: center;
  padding: 28px 16px;
  border: 2px dashed var(--border);
  border-radius: 18px;
  background: #F8FDFF;
  color: var(--primary-dark);
  font-weight: 700;
  margin-bottom: 16px;
}

.error-panel {
  border-color: #FDE68A;
  background: #FFFDF7;
  color: #92400E;
}

.home-tip {
  text-align: center;
  font-size: 0.88rem;
  color: var(--text-muted);
  font-weight: 500;
  margin-top: 8px;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 手機與小螢幕 */
@media (max-width: 600px) {
  body {
    padding: 0;
    padding-bottom: var(--safe-bottom);
  }

  .app-wrapper {
    max-width: 100%;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px 14px calc(20px + var(--safe-bottom));
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-top: none;
    box-shadow: none;
  }

  .header {
    margin-bottom: 14px;
    padding-bottom: 12px;
  }

  .header h1 {
    font-size: 1.28rem;
  }

  .header p {
    font-size: 0.84rem;
  }

  .header-badge {
    font-size: 0.76rem;
    padding: 4px 12px;
  }

  .cheer-banner {
    flex-direction: column;
    align-items: stretch;
    padding: 12px 14px;
    gap: 8px;
    margin-bottom: 16px;
  }

  .cheer-text {
    flex: none;
    width: 100%;
    font-size: 0.94rem;
  }

  .cheer-hint {
    align-self: flex-end;
    font-size: 0.78rem;
    padding: 4px 10px;
  }

  .subject-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .subject-card {
    padding: 16px;
    min-height: 108px;
  }

  .status-bar {
    padding: 10px 12px;
    font-size: 0.88rem;
  }

  #currentSubName {
    width: 100%;
    border-bottom: 1px dashed #B3E5FC;
    padding-bottom: 4px;
    margin-bottom: 2px;
    font-size: 0.92rem;
  }

  .question-box {
    font-size: 1.05rem;
  }

  .opt-btn {
    padding: 13px 14px;
    font-size: 0.95rem;
  }

  /* 底部操作列：實心底，避免虛線壓在詳解文字上 */
  .nav-controls {
    flex-direction: column-reverse;
    align-items: stretch;
    position: sticky;
    bottom: 0;
    background: #fff;
    border-top: 2px dashed var(--border);
    box-shadow: 0 -12px 24px rgba(255, 255, 255, 0.92);
    padding-top: 14px;
    padding-bottom: calc(12px + var(--safe-bottom));
    margin-top: 20px;
    margin-bottom: 0;
    z-index: 5;
  }

  /* 詳解與選項底部留白，捲動時最後幾行不會被 sticky 列遮住 */
  #quizView .exp-box {
    margin-bottom: 8px;
  }

  #quizView .options-list {
    padding-bottom: 4px;
  }

  .nav-controls .btn {
    width: 100%;
    padding: 14px 16px;
  }

  .score-circle {
    width: 120px;
    height: 120px;
  }

  .result-box .btn {
    width: 100%;
  }

  .result-actions {
    flex-direction: column;
  }
}

@media (max-width: 380px) {
  .header h1 {
    font-size: 1.16rem;
  }

  .opt-btn,
  .question-box {
    font-size: 0.92rem;
  }
}
