:root {
  --bg: #161c16;
  --bg-elev: #1f281f;
  --bg-elev-2: #283328;
  --border: #3a4a38;
  --olive: #4b5d3a;
  --olive-light: #7a9463;
  --tan: #c9b487;
  --text: #ece7da;
  --text-dim: #a9b2a1;
  --good: #5c9a5c;
  --bad: #b5453f;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: linear-gradient(180deg, #1c241c 0%, #161c16 100%);
  border-bottom: 1px solid var(--border);
  padding: calc(env(safe-area-inset-top) + 12px) 16px 0;
}

.app-header h1 {
  margin: 0 0 12px;
  font-size: 1.3rem;
  letter-spacing: 0.02em;
}

.tabs {
  display: flex;
  gap: 4px;
}

.tab {
  flex: 1;
  padding: 10px 6px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.tab.active {
  color: var(--tan);
  border-bottom-color: var(--olive-light);
}

main#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: 48px;
}

.group-section {
  margin-bottom: 28px;
}

.group-title {
  font-size: 1.05rem;
  margin: 0 0 4px;
  color: var(--tan);
}

.group-info {
  margin: 0 0 12px;
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.4;
}

.rank-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.rank-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  cursor: pointer;
  text-align: center;
  transition: transform 0.1s ease, border-color 0.15s ease;
}

.rank-card:active {
  transform: scale(0.97);
}

.rank-card:hover {
  border-color: var(--olive-light);
}

.rank-card img {
  width: 100%;
  aspect-ratio: 167 / 113;
  object-fit: cover;
  border-radius: 6px;
  display: block;
  margin-bottom: 6px;
  background: #000;
}

.rank-card .rank-name {
  font-size: 0.82rem;
  font-weight: 600;
  line-height: 1.25;
}

/* Detail overlay */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
}

.overlay.hidden { display: none; }

.detail-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  width: 100%;
  max-width: 720px;
  max-height: 88vh;
  overflow-y: auto;
  padding: 20px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  position: relative;
  animation: slideUp 0.18s ease-out;
}

@media (min-width: 640px) {
  .overlay { align-items: center; }
  .detail-card { border-radius: 18px; }
}

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

.close-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
}

.detail-content img {
  width: 100%;
  max-width: 340px;
  display: block;
  margin: 0 auto 16px;
  border-radius: 10px;
  background: #000;
}

.detail-content .detail-group {
  color: var(--olive-light);
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 2px;
}

.detail-content h2 {
  margin: 0 0 12px;
  font-size: 1.4rem;
}

.detail-block {
  margin-bottom: 14px;
}

.detail-block h3 {
  margin: 0 0 4px;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}

.detail-block p {
  margin: 0;
  line-height: 1.5;
}

/* Units (organizational structure) */

.unit-ladder {
  display: flex;
  flex-direction: column;
}

.unit-card {
  display: flex;
  gap: 14px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.unit-badge {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--olive);
  color: #fff;
  font-weight: 700;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.unit-body h3 {
  margin: 0 0 6px;
  font-size: 1.1rem;
}

.unit-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.unit-meta-label {
  color: var(--tan);
  font-weight: 600;
}

.unit-body p {
  margin: 0;
  line-height: 1.5;
  font-size: 0.92rem;
}

.unit-connector {
  width: 2px;
  height: 16px;
  background: var(--border);
  margin-left: 32px;
}

/* English: alphabet + dictionary */

.lang-toggle {
  margin-bottom: 20px;
}

.alpha-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 10px;
}

.alpha-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 6px;
  text-align: center;
}

.alpha-letter {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--tan);
}

.alpha-code {
  font-size: 0.85rem;
  font-weight: 600;
  margin-top: 2px;
}

.alpha-pron {
  font-size: 0.72rem;
  color: var(--text-dim);
  margin-top: 2px;
}

.dict-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.dict-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
}

.dict-en {
  font-weight: 700;
}

.dict-sk {
  color: var(--text-dim);
  text-align: right;
}

/* Test / quiz */
.quiz-intro {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.quiz-intro h2 { margin-top: 0; }

.field-label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-dim);
  margin: 16px 0 8px;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
}

.chip.selected {
  background: var(--olive);
  border-color: var(--olive-light);
  color: #fff;
}

.primary-btn {
  margin-top: 20px;
  width: 100%;
  padding: 14px;
  border-radius: 10px;
  border: none;
  background: var(--olive-light);
  color: #10160f;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.primary-btn:active { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.5; }

.secondary-btn {
  margin-top: 10px;
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}

.quiz-progress {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  color: var(--text-dim);
  font-size: 0.85rem;
}

.progress-bar {
  height: 6px;
  background: var(--bg-elev-2);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 20px;
}

.progress-bar-fill {
  height: 100%;
  background: var(--olive-light);
  transition: width 0.2s ease;
}

.quiz-image-wrap {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  margin-bottom: 18px;
}

.quiz-image-wrap img {
  width: 100%;
  max-width: 320px;
  border-radius: 10px;
  background: #000;
}

.quiz-text-prompt {
  font-size: 1.7rem;
  font-weight: 800;
  color: var(--tan);
  padding: 16px 8px;
}

.quiz-text-prompt-small {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  text-align: left;
  padding: 4px;
}

.quiz-passage {
  text-align: left;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--text-dim);
  margin: 0 0 4px;
}

.listen-btn {
  padding: 16px 28px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--olive);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.quiz-question-text {
  margin: 12px 0 0;
  color: var(--text-dim);
  font-size: 0.9rem;
}

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

.choice-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  font-size: 1rem;
  text-align: left;
  cursor: pointer;
}

.choice-btn .choice-letter {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--bg-elev-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
}

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

.choice-btn.correct {
  border-color: var(--good);
  background: rgba(92, 154, 92, 0.18);
}

.choice-btn.correct .choice-letter {
  background: var(--good);
  color: #fff;
}

.choice-btn.incorrect {
  border-color: var(--bad);
  background: rgba(181, 69, 63, 0.18);
}

.choice-btn.incorrect .choice-letter {
  background: var(--bad);
  color: #fff;
}

.quiz-results {
  text-align: center;
  padding: 30px 16px;
}

.quiz-results .score-big {
  font-size: 3rem;
  font-weight: 800;
  color: var(--tan);
  margin: 8px 0;
}

.mistake-list {
  text-align: left;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mistake-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
}

.mistake-item img {
  width: 70px;
  border-radius: 6px;
  background: #000;
  flex-shrink: 0;
}

.mistake-item .mistake-text {
  font-size: 0.88rem;
  line-height: 1.4;
}

.mistake-item .mistake-text .right {
  color: var(--good);
  font-weight: 700;
}

.mistake-item .mistake-text .wrong {
  color: var(--bad);
  text-decoration: line-through;
}

.answer-correct {
  border-color: var(--good);
}

/* History */

.history-item {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.history-item-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 12px;
}

.history-item-date {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 2px;
}

.history-item-score {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--tan);
  white-space: nowrap;
}

.history-detail {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-detail.hidden {
  display: none;
}
