:root {
  --ink: #172026;
  --muted: #66737d;
  --paper: #f6f2e8;
  --panel: #fffaf1;
  --line: #ded5c5;
  --teal: #176b68;
  --teal-dark: #0f4f4c;
  --coral: #c94f3d;
  --coral-strong: #a92f22;
  --amber: #d59b2d;
  --green: #2f7d4f;
  --green-strong: #1f6b3b;
  --shadow: 0 22px 60px rgba(23, 32, 38, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(213, 155, 45, 0.22), transparent 28rem),
    linear-gradient(135deg, #f9f0db 0%, #eef3ef 52%, #f8e9e3 100%);
  color: var(--ink);
  font-size: 17px;
  font-family: Tahoma, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 18px;
}

.topbar h1,
.quiz-head h2,
.empty-state h2,
.result-card h2,
.stats-card h2 {
  margin: 0;
  line-height: 1.2;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--teal);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0;
}

.topbar-actions,
.quiz-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.primary-button,
.ghost-button {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 14px;
  min-height: 42px;
  transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease;
}

.primary-button {
  background: var(--teal);
  color: white;
  box-shadow: 0 10px 24px rgba(23, 107, 104, 0.2);
}

.ghost-button {
  background: rgba(255, 250, 241, 0.78);
  border-color: var(--line);
  color: var(--ink);
}

.primary-button:hover,
.ghost-button:hover {
  transform: translateY(-1px);
}

.primary-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.ghost-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  transform: none;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 350px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.piece-panel,
.quiz-card,
.result-card,
.stats-card,
.empty-state {
  background: rgba(255, 250, 241, 0.9);
  border: 1px solid rgba(222, 213, 197, 0.85);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.piece-panel {
  position: sticky;
  top: 18px;
  max-height: calc(100vh - 48px);
  padding: 16px;
  overflow: hidden;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-head h2 {
  margin: 0;
}

.panel-head span {
  color: var(--muted);
  font-weight: 700;
}

.search-box {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.86rem;
  margin-bottom: 12px;
}

.search-box input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  padding: 10px 12px;
  color: var(--ink);
}

.piece-list {
  display: grid;
  gap: 8px;
  max-height: calc(100vh - 180px);
  overflow: auto;
  padding-inline-end: 4px;
}

.piece-button {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  padding: 10px;
  text-align: right;
}

.piece-button strong {
  display: block;
  margin-bottom: 4px;
}

.piece-button span {
  color: var(--muted);
  font-size: 0.84rem;
}

.piece-button.active {
  border-color: var(--teal);
  background: #e8f3ef;
}

.quiz-stage {
  min-width: 0;
}

.empty-state,
.result-card,
.stats-card {
  padding: clamp(20px, 4vw, 42px);
}

.empty-state p:last-child {
  max-width: 620px;
  color: var(--muted);
  line-height: 1.8;
}

.quiz-card {
  padding: clamp(14px, 2.4vw, 26px);
}

.quiz-head {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.timer {
  min-width: 62px;
  height: 62px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--ink);
  color: white;
  font-size: 1.35rem;
  font-weight: 800;
}

.timer.warning {
  background: var(--coral);
}

.progress-track {
  height: 8px;
  background: #e8dfd2;
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 16px;
}

.progress-track div {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--teal), var(--amber));
  transition: width 0.25s ease;
}

.passage-frame {
  margin: 0 0 18px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.passage-frame img {
  display: block;
  width: 100%;
  max-height: 390px;
  object-fit: contain;
}

.passage-frame.empty {
  display: none;
}

.question-block {
  display: grid;
  gap: 14px;
}

.question-count {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.question-block h3 {
  margin: 0;
  font-size: clamp(1.35rem, 2.4vw, 1.9rem);
  line-height: 1.65;
}

.answers {
  display: grid;
  gap: 10px;
}

.answer-button {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  padding: 16px;
  text-align: right;
  font-size: clamp(1.08rem, 1.8vw, 1.32rem);
  font-weight: 700;
  line-height: 1.6;
  box-shadow: 0 8px 20px rgba(23, 32, 38, 0.05);
}

.answer-button.correct {
  border-color: var(--green-strong);
  background: #dff3e5;
  color: #174a2c;
  box-shadow: inset 6px 0 0 var(--green-strong), 0 10px 22px rgba(47, 125, 79, 0.16);
}

.answer-button.wrong {
  border-color: var(--coral-strong);
  background: #fde0da;
  color: #7d261c;
  box-shadow: inset 6px 0 0 var(--coral-strong), 0 10px 22px rgba(201, 79, 61, 0.14);
}

.answer-button:disabled {
  cursor: default;
}

.answer-feedback {
  min-height: 34px;
  margin: 0;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: clamp(1rem, 1.6vw, 1.2rem);
  font-weight: 800;
  line-height: 1.7;
}

.answer-feedback:empty {
  display: none;
}

.answer-feedback.correct {
  background: #dff3e5;
  color: #174a2c;
  border: 1px solid var(--green-strong);
}

.answer-feedback.wrong {
  background: #fde0da;
  color: #7d261c;
  border: 1px solid var(--coral-strong);
}

.answer-feedback.timeout {
  background: #fff1cf;
  color: #7c5612;
  border: 1px solid var(--amber);
}

.quiz-actions {
  justify-content: flex-start;
  margin-top: 18px;
}

.score-number {
  margin: 20px 0 8px;
  font-size: clamp(2.4rem, 8vw, 5rem);
  font-weight: 900;
  color: var(--teal-dark);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 18px 0;
}

.stat-tile {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  padding: 14px;
}

.stat-tile strong {
  display: block;
  font-size: 1.5rem;
  margin-bottom: 4px;
  color: var(--teal-dark);
}

.stat-tile span {
  color: var(--muted);
}

.hidden {
  display: none !important;
}

@media (max-width: 900px) {
  .app-shell {
    padding: 10px;
  }

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

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

  .piece-panel {
    position: static;
    max-height: none;
  }

  .piece-list {
    max-height: 230px;
  }

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

  .quiz-card {
    padding: 12px;
  }

  .quiz-head {
    align-items: center;
  }

  .timer {
    min-width: 54px;
    height: 54px;
    font-size: 1.2rem;
  }

  .passage-frame {
    padding: 6px;
  }

  .passage-frame img {
    max-height: 42vh;
  }

  .question-block {
    gap: 12px;
  }

  .answer-button {
    min-height: 58px;
    padding: 14px 12px;
  }

  .quiz-card .quiz-actions {
    position: sticky;
    bottom: 0;
    z-index: 5;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 16px -12px -12px;
    padding: 10px 12px;
    background: rgba(255, 250, 241, 0.96);
    border-top: 1px solid var(--line);
  }

  .quiz-card .quiz-actions .primary-button,
  .quiz-card .quiz-actions .ghost-button {
    width: 100%;
    min-height: 48px;
    padding: 9px 8px;
  }
}

@media (max-width: 520px) {
  body {
    font-size: 16px;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .topbar-actions button {
    width: 100%;
  }

  .piece-panel {
    padding: 12px;
  }

  .piece-button {
    padding: 12px;
  }

  .question-block h3 {
    font-size: 1.28rem;
  }

  .answer-button {
    font-size: 1.08rem;
  }
}
