:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --ink: #1f2937;
  --muted: #64748b;
  --line: #d8dee9;
  --panel: #ffffff;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --green: #16a34a;
  --green-soft: #dcfce7;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --gold: #d97706;
  --gold-soft: #fef3c7;
  --purple: #7c3aed;
  --purple-soft: #ede9fe;
  --white: #ffffff;
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(37, 99, 235, 0.08), transparent 300px),
    var(--bg);
}

button {
  font: inherit;
}

.app-shell {
  width: min(960px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0;
}

.topbar,
.nav-row,
.question-head,
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.topbar {
  margin-bottom: 20px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 30px;
  line-height: 1.2;
}

.topbar p,
.sheet-header p {
  margin-top: 6px;
  color: var(--muted);
}

.icon-button,
.nav-button,
.close-button {
  border: 1px solid var(--line);
  background: var(--white);
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.icon-button {
  min-height: 44px;
  padding: 0 16px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

.sheet-icon {
  width: 18px;
  height: 18px;
  display: inline-block;
  border: 2px solid currentColor;
  border-radius: 4px;
  box-shadow: inset 0 -5px 0 rgba(37, 99, 235, 0.18);
}

.review-icon {
  width: 18px;
  height: 18px;
  display: inline-block;
  border: 2px solid currentColor;
  border-radius: 3px;
  position: relative;
}

.review-icon::before,
.review-icon::after {
  content: "";
  position: absolute;
  left: 3px;
  right: 3px;
  height: 2px;
  background: currentColor;
}

.review-icon::before {
  top: 4px;
}

.review-icon::after {
  top: 10px;
}

.codex-icon {
  width: 18px;
  height: 18px;
  display: inline-block;
  border: 2px solid currentColor;
  border-radius: 4px;
  position: relative;
}

.codex-icon::before,
.codex-icon::after {
  content: "";
  position: absolute;
  background: currentColor;
}

.codex-icon::before {
  inset: 3px auto 3px 7px;
  width: 2px;
}

.codex-icon::after {
  left: 3px;
  right: 3px;
  top: 7px;
  height: 2px;
}

.reward-strip {
  margin-bottom: 16px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr)) auto;
  gap: 12px;
  align-items: center;
  box-shadow: 0 10px 32px rgba(15, 23, 42, 0.06);
}

.reward-stat {
  min-height: 54px;
  padding: 9px 12px;
  border: 1px solid #e6ebf3;
  border-radius: 8px;
  background: var(--white);
}

.reward-stat span,
.card-rarity {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.reward-stat strong {
  display: block;
  margin-top: 3px;
  font-size: 18px;
  line-height: 1.2;
}

.draw-button {
  min-height: 54px;
  padding: 0 18px;
  border: 1px solid var(--purple);
  border-radius: 8px;
  background: var(--purple);
  color: var(--white);
  cursor: pointer;
  font-weight: 900;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.draw-button:not(:disabled):hover {
  transform: translateY(-1px);
}

.draw-button:disabled {
  border-color: var(--line);
  background: #e2e8f0;
  color: var(--muted);
  cursor: not-allowed;
}

.reward-popup {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 20;
  width: min(360px, calc(100vw - 32px));
  min-height: 88px;
  padding: 14px 16px;
  border: 1px solid #f59e0b;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(255, 251, 235, 0.96), rgba(237, 233, 254, 0.96)),
    var(--white);
  color: #78350f;
  box-shadow: 0 22px 60px rgba(120, 53, 15, 0.24);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  overflow: hidden;
}

.reward-popup[hidden] {
  display: none;
}

.reward-popup::after {
  content: "";
  position: absolute;
  inset: -40% auto -40% -30%;
  width: 36%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.76), transparent);
  transform: rotate(18deg);
  animation: rewardShine 1.7s ease-in-out infinite;
}

.reward-popup.showing {
  animation: rewardPopIn 0.42s cubic-bezier(0.2, 0.9, 0.2, 1.15) both;
}

.reward-popup.hiding {
  animation: rewardPopOut 0.24s ease-in both;
}

.reward-popup-badge {
  width: 54px;
  height: 54px;
  flex: 0 0 54px;
  border-radius: 8px;
  background:
    radial-gradient(circle at 50% 42%, #ffffff 0 13%, transparent 14%),
    conic-gradient(from 18deg, #f59e0b, #7c3aed, #2563eb, #f59e0b);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.72), 0 10px 20px rgba(124, 58, 237, 0.18);
  position: relative;
}

.reward-popup-badge::before,
.reward-popup-badge::after {
  content: "";
  position: absolute;
  background: rgba(255, 255, 255, 0.9);
}

.reward-popup-badge::before {
  left: 13px;
  right: 13px;
  top: 25px;
  height: 4px;
}

.reward-popup-badge::after {
  top: 13px;
  bottom: 13px;
  left: 25px;
  width: 4px;
}

.reward-popup-copy {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.reward-popup-copy strong {
  font-size: 18px;
  line-height: 1.25;
}

.reward-popup-copy span {
  color: #6b21a8;
  font-size: 14px;
  font-weight: 800;
  line-height: 1.4;
}

@keyframes rewardPopIn {
  0% { opacity: 0; transform: translate(-50%, -42%) scale(0.88); }
  62% { opacity: 1; transform: translate(-50%, -52%) scale(1.04); }
  100% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@keyframes rewardPopOut {
  to { opacity: 0; transform: translate(-50%, -44%) scale(0.94); }
}

@keyframes rewardShine {
  0%, 25% { transform: translateX(0) rotate(18deg); opacity: 0; }
  45% { opacity: 1; }
  78%, 100% { transform: translateX(520%) rotate(18deg); opacity: 0; }
}

.quiz-panel {
  min-height: 430px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 55px rgba(15, 23, 42, 0.08);
}

.question-head {
  margin-bottom: 22px;
}

.question-number {
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  background: #e0ecff;
  color: var(--blue-dark);
  font-weight: 800;
}

.state-pill {
  min-width: 76px;
  padding: 7px 12px;
  border-radius: 999px;
  color: var(--muted);
  background: #f1f5f9;
  text-align: center;
  font-size: 14px;
}

.state-pill.correct {
  color: var(--green);
  background: var(--green-soft);
}

.state-pill.wrong {
  color: var(--red);
  background: var(--red-soft);
}

.question-text {
  min-height: 120px;
  margin-bottom: 24px;
  font-size: 25px;
  line-height: 1.65;
  font-weight: 800;
}

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

.option {
  width: 100%;
  min-height: 66px;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--white);
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
}

.option:hover {
  border-color: var(--blue);
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.12);
}

.option-key {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  background: #eef2ff;
  color: var(--blue-dark);
  font-weight: 900;
}

.option-word {
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 20px;
  font-weight: 700;
}

.option.selected.correct {
  border-color: var(--green);
  background: var(--green-soft);
}

.option.selected.wrong {
  border-color: var(--red);
  background: var(--red-soft);
}

.option.answer {
  border-color: var(--green);
}

.feedback {
  min-height: 34px;
  margin-top: 20px;
  font-size: 20px;
  font-weight: 900;
}

.feedback.correct {
  color: var(--green);
}

.feedback.wrong {
  color: var(--red);
}

.flash-correct {
  animation: flashGreen 0.55s ease-in-out 2;
}

.flash-wrong {
  animation: flashRed 0.55s ease-in-out 2;
}

@keyframes flashGreen {
  0%, 100% { box-shadow: 0 18px 55px rgba(15, 23, 42, 0.08); }
  50% { box-shadow: 0 0 0 5px rgba(22, 163, 74, 0.3), 0 18px 55px rgba(22, 163, 74, 0.2); }
}

@keyframes flashRed {
  0%, 100% { box-shadow: 0 18px 55px rgba(15, 23, 42, 0.08); }
  50% { box-shadow: 0 0 0 5px rgba(220, 38, 38, 0.28), 0 18px 55px rgba(220, 38, 38, 0.18); }
}

.nav-row {
  margin-top: 18px;
}

.nav-button {
  min-width: 128px;
  min-height: 48px;
  padding: 0 20px;
  border-radius: 8px;
  font-weight: 800;
}

.nav-button.primary {
  border-color: var(--blue);
  background: var(--blue);
  color: var(--white);
}

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

.icon-button:hover,
.nav-button:not(:disabled):hover,
.close-button:hover {
  transform: translateY(-1px);
}

.overlay {
  position: fixed;
  inset: 0;
  padding: 24px;
  background: rgba(15, 23, 42, 0.55);
  display: grid;
  place-items: center;
  z-index: 10;
}

.overlay[hidden] {
  display: none;
}

.sheet-panel,
.review-panel,
.codex-panel {
  width: min(980px, 100%);
  max-height: min(760px, calc(100vh - 48px));
  padding: 24px;
  border-radius: 8px;
  background: var(--panel);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 30px 90px rgba(15, 23, 42, 0.28);
}

.review-panel {
  width: min(1120px, 100%);
}

.codex-panel {
  width: min(1180px, 100%);
  min-height: 0;
  overflow: auto;
}

.sheet-header {
  margin-bottom: 18px;
}

.codex-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.owned-gallery-button,
.help-button {
  border: 1px solid #cbd5e1;
  background: var(--white);
  color: var(--blue-dark);
  cursor: pointer;
}

.owned-gallery-button {
  min-height: 30px;
  padding: 0 10px;
  border-radius: 8px;
  font-weight: 900;
}

.help-button {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  font-weight: 900;
  line-height: 1;
}

.owned-gallery-button:hover,
.owned-gallery-button[aria-expanded="true"],
.help-button:hover,
.help-button[aria-expanded="true"] {
  border-color: var(--blue);
  background: #e0ecff;
}

.rules-panel {
  margin: -6px 0 16px;
  padding: 12px 14px;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  background: #eff6ff;
  color: #1e3a8a;
  display: grid;
  gap: 6px;
  line-height: 1.55;
}

.rules-panel[hidden] {
  display: none;
}

.rules-panel p {
  color: #334155;
}

.owned-gallery-panel {
  margin: -4px 0 16px;
  padding: 14px;
  border: 1px solid #fcd34d;
  border-radius: 8px;
  background: #fffbeb;
  display: grid;
  gap: 12px;
}

.owned-gallery-panel[hidden] {
  display: none;
}

.owned-gallery-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.owned-gallery-head span {
  color: #92400e;
  font-size: 14px;
  font-weight: 800;
}

.owned-gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 12px;
}

.owned-gallery-card {
  border: 1px solid #fde68a;
  border-radius: 8px;
  background: var(--white);
  overflow: hidden;
  box-shadow: 0 12px 28px rgba(146, 64, 14, 0.08);
}

.owned-gallery-card img {
  width: 100%;
  height: 260px;
  object-fit: contain;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.05), rgba(245, 158, 11, 0.08)),
    #f8fafc;
  display: block;
}

.owned-gallery-info {
  min-height: 62px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.owned-gallery-info strong,
.owned-gallery-info span {
  display: block;
}

.owned-gallery-info span {
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
}

.download-button {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 8px;
  background: var(--blue);
  color: var(--white);
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  font-weight: 900;
}

.owned-gallery-empty {
  padding: 18px;
  border: 1px dashed #f59e0b;
  border-radius: 8px;
  color: #92400e;
  background: rgba(255, 255, 255, 0.66);
  text-align: center;
}

.chapter-map-panel {
  margin: -4px 0 22px;
  padding: 16px;
  border: 1px solid #c4b5fd;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(237, 233, 254, 0.9), rgba(255, 251, 235, 0.86)),
    var(--white);
  display: grid;
  gap: 14px;
  overflow: visible;
  position: relative;
  z-index: 1;
}

.chapter-map-panel[hidden] {
  display: none;
}

.chapter-map-copy {
  display: grid;
  gap: 4px;
}

.chapter-map-copy strong {
  font-size: 20px;
  color: #4c1d95;
}

.chapter-map-copy span {
  color: #475569;
  line-height: 1.5;
}

.chapter-map {
  min-height: 210px;
  padding: 24px 22px 74px;
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.72), rgba(224, 236, 255, 0.55)),
    #f8fafc;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: start;
  gap: 16px;
  position: relative;
  overflow: hidden;
}

.map-node {
  min-height: 108px;
  padding: 14px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: var(--white);
  display: grid;
  justify-items: center;
  gap: 4px;
  position: relative;
  z-index: 1;
}

.map-node.passed {
  border-color: #86efac;
  background: #f0fdf4;
}

.map-node.current {
  border-color: #f59e0b;
  background: #fffbeb;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.14);
}

.map-node.next {
  border-color: #c4b5fd;
  background: #f5f3ff;
}

.map-dot {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #94a3b8;
  box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.72);
}

.map-node.current .map-dot {
  background: #f59e0b;
}

.map-node.next .map-dot {
  background: #7c3aed;
}

.map-node small {
  color: var(--muted);
  font-weight: 800;
}

.map-road {
  position: absolute;
  left: calc(50% + 16px);
  top: 36px;
  width: calc(100% + 1px);
  height: 4px;
  border-radius: 999px;
  background: repeating-linear-gradient(90deg, #c4b5fd 0 14px, transparent 14px 22px);
}

.map-avatar {
  position: absolute;
  left: calc(50% - 24px);
  bottom: 18px;
  width: 48px;
  height: 48px;
  border: 3px solid #ffffff;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 12px 24px rgba(76, 29, 149, 0.24);
  overflow: hidden;
  z-index: 2;
  transition: transform 0.8s ease-in-out;
}

.map-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chapter-map-panel.promoting .map-avatar {
  transform: translateX(min(340px, 30vw));
}

.promote-button {
  min-height: 44px;
  border: 1px solid var(--purple);
  border-radius: 8px;
  background: var(--purple);
  color: var(--white);
  cursor: pointer;
  font-weight: 900;
}

.promote-button:disabled {
  opacity: 0.7;
  cursor: wait;
}

.close-button {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  font-size: 26px;
  line-height: 1;
}

.answer-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 10px;
  overflow: auto;
  padding: 2px 4px 4px 2px;
}

.answer-dot {
  aspect-ratio: 1 / 1;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--white);
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}

.answer-dot.correct {
  border-color: var(--green);
  background: var(--green);
  color: var(--white);
}

.answer-dot.wrong {
  border-color: var(--red);
  background: var(--red);
  color: var(--white);
}

.answer-dot.current {
  outline: 3px solid rgba(37, 99, 235, 0.28);
}

.review-tools {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  margin-bottom: 16px;
}

.review-search {
  min-height: 44px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 14px;
  font: inherit;
  color: var(--ink);
  background: var(--white);
}

.filter-button {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: var(--white);
  cursor: pointer;
  font-weight: 800;
}

.filter-button.active {
  border-color: #f59e0b;
  background: #fef3c7;
  color: #92400e;
}

.review-list {
  overflow: auto;
  display: grid;
  gap: 10px;
  padding-right: 4px;
}

.review-item {
  display: grid;
  grid-template-columns: minmax(130px, 180px) minmax(90px, 130px) minmax(0, 1fr) auto;
  gap: 14px;
  align-items: start;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--white);
}

.review-item.marked {
  border-color: #f59e0b;
  background: #fffbeb;
}

.review-word {
  overflow-wrap: anywhere;
  font-size: 19px;
  font-weight: 900;
}

.review-phonetic {
  color: var(--blue-dark);
  font-weight: 700;
  overflow-wrap: anywhere;
}

.review-meaning {
  color: #334155;
  line-height: 1.55;
  overflow-wrap: anywhere;
}

.mark-button {
  min-width: 74px;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--white);
  color: var(--ink);
  cursor: pointer;
  font-weight: 800;
}

.mark-button.active {
  border-color: #f59e0b;
  background: #f59e0b;
  color: var(--white);
}

.review-empty {
  padding: 36px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: var(--white);
  text-align: center;
}

.draw-result {
  min-height: 118px;
  margin-bottom: 16px;
  padding: 14px;
  border: 1px solid #d8b4fe;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(124, 58, 237, 0.12), rgba(245, 158, 11, 0.14)),
    #fffbeb;
  display: flex;
  align-items: center;
  gap: 16px;
  color: #4c1d95;
  overflow: hidden;
  position: relative;
}

.draw-result[hidden] {
  display: none;
}

.draw-machine {
  width: 112px;
  height: 88px;
  border: 2px solid #6d28d9;
  border-radius: 8px;
  background:
    linear-gradient(180deg, #f8fafc, #ddd6fe 74%, #c4b5fd);
  box-shadow: inset 0 -8px 0 rgba(76, 29, 149, 0.14), 0 12px 26px rgba(76, 29, 149, 0.18);
  flex: 0 0 112px;
  position: relative;
}

.machine-window {
  position: absolute;
  inset: 12px 16px auto;
  height: 34px;
  border: 2px solid #4c1d95;
  border-radius: 8px;
  background: #fff7ed;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  padding: 5px;
  overflow: hidden;
}

.machine-window span {
  border-radius: 5px;
  background: linear-gradient(180deg, #f59e0b, #7c3aed, #2563eb, #f59e0b);
  background-size: 100% 320%;
  animation: slotSpin 0.36s linear infinite;
}

.machine-window span:nth-child(2) {
  animation-duration: 0.28s;
}

.machine-window span:nth-child(3) {
  animation-duration: 0.42s;
}

.machine-slot {
  position: absolute;
  left: 26px;
  right: 26px;
  bottom: 16px;
  height: 8px;
  border-radius: 999px;
  background: #4c1d95;
}

.machine-light {
  position: absolute;
  right: 10px;
  bottom: 11px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55);
  animation: machinePulse 0.55s ease-in-out infinite;
}

.draw-result.guaranteed .machine-light {
  background: #f59e0b;
  box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.62);
}

.draw-status {
  display: grid;
  gap: 6px;
}

.draw-status strong,
.draw-result > strong {
  font-size: 20px;
  line-height: 1.35;
}

.draw-status span {
  color: #6b21a8;
  font-weight: 700;
}

.draw-piece-preview {
  width: 92px;
  height: 74px;
  border: 2px solid #f59e0b;
  border-radius: 8px;
  background-repeat: no-repeat;
  box-shadow: 0 18px 38px rgba(146, 64, 14, 0.20);
  flex: 0 0 92px;
  animation: pieceBirth 0.72s cubic-bezier(0.2, 0.85, 0.2, 1.2) both;
  position: relative;
}

.draw-piece-preview::after {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: 12px;
  border: 2px solid rgba(245, 158, 11, 0.45);
  opacity: 0;
  animation: pieceGlow 0.85s ease-out 0.2s;
}

.draw-result.reveal {
  animation: resultSettle 0.28s ease-out;
}

.draw-result.new-piece {
  border-color: #f59e0b;
  color: #92400e;
}

@keyframes slotSpin {
  from { background-position: 0 0; }
  to { background-position: 0 100%; }
}

@keyframes machinePulse {
  0%, 100% { transform: scale(0.84); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.50); }
  50% { transform: scale(1); box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); }
}

@keyframes pieceBirth {
  0% {
    opacity: 0;
    transform: translateX(-44px) translateY(16px) scale(0.45) rotateY(82deg);
    filter: brightness(1.8) saturate(0.4);
  }
  60% {
    opacity: 1;
    transform: translateX(6px) translateY(-4px) scale(1.08) rotateY(-8deg);
    filter: brightness(1.25) saturate(1.15);
  }
  100% {
    opacity: 1;
    transform: translateX(0) translateY(0) scale(1) rotateY(0);
    filter: brightness(1) saturate(1.05);
  }
}

@keyframes pieceGlow {
  0% { opacity: 0.8; transform: scale(0.78); }
  100% { opacity: 0; transform: scale(1.22); }
}

@keyframes resultSettle {
  from { transform: translateY(-4px); }
  to { transform: translateY(0); }
}

.codex-grid {
  min-height: 0;
  overflow: visible;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  padding: 8px 4px 4px 2px;
  position: relative;
  z-index: 0;
}

.codex-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--white);
  overflow: hidden;
}

.codex-card.complete {
  border-color: #f59e0b;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.16);
}

.card-art-wrap {
  width: 100%;
  height: 168px;
  border: 0;
  padding: 0;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.10), rgba(124, 58, 237, 0.12)),
    #f8fafc;
  display: grid;
  place-items: center;
  overflow: hidden;
  cursor: zoom-in;
}

.card-mosaic,
.card-viewer-stage {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
}

.card-art {
  width: auto;
  height: auto;
  max-width: calc(100% - 18px);
  max-height: calc(100% - 18px);
  filter: saturate(0.92);
  transition: opacity 0.2s ease, filter 0.2s ease;
}

.mosaic-pieces {
  position: absolute;
  display: grid;
  gap: 2px;
  pointer-events: none;
  filter: drop-shadow(0 8px 12px rgba(15, 23, 42, 0.10));
}

.mosaic-piece {
  min-width: 0;
  min-height: 0;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.02);
}

.mosaic-piece.owned {
  background-repeat: no-repeat;
  opacity: 1;
  border-color: rgba(255, 255, 255, 0.72);
  box-shadow:
    inset 0 0 0 1px rgba(15, 23, 42, 0.12),
    inset 6px 6px 12px rgba(255, 255, 255, 0.16),
    0 3px 9px rgba(15, 23, 42, 0.22);
  transform: translateZ(0);
}

.mosaic-piece.owned:nth-child(odd) {
  border-top-left-radius: 10px;
  border-bottom-right-radius: 9px;
}

.mosaic-piece.owned:nth-child(even) {
  border-top-right-radius: 10px;
  border-bottom-left-radius: 9px;
}

.card-viewer {
  margin-bottom: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  display: grid;
  gap: 10px;
}

.card-viewer[hidden] {
  display: none;
}

.card-viewer-head {
  min-height: 36px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.card-viewer-stage {
  height: min(560px, 56vh);
  min-height: 260px;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.06), rgba(124, 58, 237, 0.08)),
    #ffffff;
  overflow: hidden;
}

.card-viewer img {
  width: auto;
  height: auto;
  max-width: calc(100% - 20px);
  max-height: calc(100% - 20px);
}

.viewer-close-button {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--white);
  color: var(--ink);
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
}

.codex-card.complete .card-art {
  filter: saturate(1.05);
}

.card-info {
  min-height: 64px;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.card-name {
  font-size: 18px;
  font-weight: 900;
}

.card-progress {
  color: var(--purple);
}

.piece-grid {
  padding: 0 12px 14px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.piece {
  min-height: 38px;
  aspect-ratio: 1.25 / 1;
  border: 1px dashed #cbd5e1;
  border-radius: 7px;
  display: grid;
  place-items: center;
  color: var(--muted);
  background: #f8fafc;
  font-size: 13px;
  font-weight: 800;
}

.piece.owned {
  border-style: solid;
  border-color: #f59e0b;
  background: var(--gold-soft);
  background-repeat: no-repeat;
  color: #92400e;
  box-shadow: inset 0 0 0 999px rgba(255, 255, 255, 0.08);
}

.reward-log-wrap {
  margin-top: 16px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.reward-log-wrap h3 {
  margin: 0 0 10px;
  font-size: 16px;
}

.reward-log {
  display: grid;
  gap: 8px;
  max-height: 130px;
  overflow: auto;
}

.reward-log-item,
.reward-empty {
  padding: 9px 11px;
  border-radius: 8px;
  background: #f8fafc;
  color: #334155;
  font-size: 14px;
}

@media (max-width: 680px) {
  .app-shell {
    width: min(100% - 20px, 960px);
    padding: 16px 0;
  }

  .topbar,
  .nav-row {
    align-items: stretch;
  }

  .topbar {
    flex-direction: column;
  }

  .top-actions {
    justify-content: stretch;
  }

  .top-actions .icon-button {
    flex: 1;
    justify-content: center;
  }

  .reward-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .draw-button {
    grid-column: 1 / -1;
  }

  .reward-popup {
    width: calc(100vw - 24px);
  }

  h1 {
    font-size: 24px;
  }

  .quiz-panel {
    min-height: 500px;
    padding: 20px;
  }

  .question-text {
    font-size: 21px;
  }

  .options {
    grid-template-columns: 1fr;
  }

  .option-word {
    font-size: 18px;
  }

  .nav-button {
    min-width: 0;
    flex: 1;
  }

  .overlay {
    padding: 10px;
  }

  .sheet-panel {
    max-height: calc(100vh - 20px);
    padding: 18px;
  }

  .review-panel {
    max-height: calc(100vh - 20px);
    padding: 18px;
  }

  .codex-panel {
    max-height: calc(100vh - 20px);
    padding: 18px;
  }

  .answer-grid {
    grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
    gap: 8px;
  }

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

  .review-item {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .mark-button {
    width: 100%;
  }

  .draw-result {
    align-items: stretch;
    flex-direction: column;
  }

  .draw-machine,
  .draw-piece-preview {
    align-self: center;
  }

  .codex-grid {
    grid-template-columns: 1fr;
  }

  #codexOverlay {
    padding: 0;
    place-items: stretch;
  }

  #codexOverlay .codex-panel {
    width: 100%;
    height: 100dvh;
    max-height: none;
    padding: 14px 12px calc(18px + env(safe-area-inset-bottom));
    border-radius: 0;
    overflow-y: auto;
  }

  #codexOverlay .sheet-header {
    position: sticky;
    top: -14px;
    z-index: 5;
    margin: -14px -12px 12px;
    padding: calc(14px + env(safe-area-inset-top)) 12px 12px;
    border-bottom: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.96);
    align-items: flex-start;
    backdrop-filter: blur(12px);
  }

  #codexOverlay .sheet-header > div {
    min-width: 0;
  }

  #codexOverlay .sheet-header h2 {
    font-size: 22px;
    line-height: 1.2;
  }

  #codexOverlay .sheet-header p {
    font-size: 13px;
    line-height: 1.45;
  }

  #codexOverlay .close-button {
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
  }

  .codex-title-row {
    gap: 6px;
  }

  .owned-gallery-button {
    min-height: 28px;
    padding: 0 8px;
    font-size: 13px;
  }

  .help-button {
    width: 28px;
    height: 28px;
    flex: 0 0 28px;
  }

  .rules-panel,
  .owned-gallery-panel,
  .chapter-map-panel,
  .card-viewer {
    margin-bottom: 12px;
  }

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

  .owned-gallery-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }

  .owned-gallery-grid {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    padding: 2px 2px 10px;
    -webkit-overflow-scrolling: touch;
  }

  .owned-gallery-card {
    flex: 0 0 min(82vw, 320px);
    scroll-snap-align: start;
  }

  .owned-gallery-card img {
    height: clamp(210px, 58vw, 280px);
  }

  .owned-gallery-info {
    min-height: 58px;
    padding: 9px 10px;
  }

  .download-button {
    min-height: 34px;
    padding: 0 10px;
  }

  .card-viewer {
    padding: 10px;
  }

  .card-viewer-stage {
    height: min(58vh, 420px);
    min-height: 220px;
  }

  .codex-grid {
    display: flex;
    grid-template-columns: none;
    gap: 12px;
    overflow-x: auto;
    overflow-y: visible;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    padding: 8px 2px 16px;
    -webkit-overflow-scrolling: touch;
  }

  .codex-card {
    flex: 0 0 min(82vw, 330px);
    scroll-snap-align: start;
  }

  .card-art-wrap {
    height: clamp(190px, 56vw, 240px);
  }

  .card-info {
    min-height: 56px;
    padding: 10px;
  }

  .card-name {
    font-size: 17px;
    line-height: 1.25;
  }

  .piece-grid {
    padding: 0 10px 12px;
    gap: 7px;
  }

  .piece {
    min-height: 34px;
  }

  .reward-log-wrap {
    margin-top: 10px;
  }

  .chapter-map {
    grid-template-columns: 1fr;
    min-height: 360px;
    padding: 18px 18px 78px;
  }

  .map-road {
    display: none;
  }

  .map-avatar {
    left: calc(50% - 24px);
    bottom: 8px;
  }

  .chapter-map-panel.promoting .map-avatar {
    transform: translateY(-86px);
  }
}
