*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --color-primary: #1a4d7a;
  --color-primary-light: #2a6da8;
  --color-primary-dark: #0f3355;
  --color-bg: #f5f7fa;
  --color-surface: #ffffff;
  --color-text: #1a1a2e;
  --color-text-light: #5a5a7a;
  --color-success: #2d8a4e;
  --color-success-bg: #e8f5ec;
  --color-error: #c0392b;
  --color-error-bg: #fde8e6;
  --color-border: #dde1e7;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --transition: 0.2s ease;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Header */
.app-header {
  background: var(--color-primary);
  color: #fff;
  padding: 14px 16px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.app-header__title {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

/* Main container */
#app {
  padding: 16px;
  max-width: 640px;
  margin: 0 auto;
  opacity: 1;
  transition: opacity var(--transition);
}

#app.fade-out {
  opacity: 0;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  min-width: 48px;
  padding: 12px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), transform 0.1s ease;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}

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

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

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

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

.btn--outline {
  background: transparent;
  color: var(--color-primary);
  border: 2px solid var(--color-primary);
}

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

.btn--large {
  width: 100%;
  min-height: 56px;
  font-size: 1.1rem;
  border-radius: 12px;
}

.btn--back {
  background: transparent;
  color: var(--color-text-light);
  border: 1px solid var(--color-border);
  font-size: 0.9rem;
  min-height: 44px;
  padding: 8px 16px;
}

/* Cards */
.card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 12px;
}

/* Species grid */
.species-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 16px 0;
}

.species-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px 8px;
  background: var(--color-surface);
  border: 2px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
  -webkit-tap-highlight-color: transparent;
  min-height: 48px;
}

.species-btn:active {
  border-color: var(--color-primary);
  background: #eef3f9;
}

.species-btn__icon {
  font-size: 2rem;
}

.species-btn__name {
  color: var(--color-text);
}

.species-btn__count {
  font-size: 0.75rem;
  color: var(--color-text-light);
}

/* Section headings */
.section-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text-light);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 20px 0 10px;
}

/* Stats bar */
.stats-bar {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  margin-top: 20px;
}

.stats-bar__label {
  font-size: 0.8rem;
  color: var(--color-text-light);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 6px;
}

.stats-bar__progress {
  width: 100%;
  height: 8px;
  background: var(--color-border);
  border-radius: 4px;
  overflow: hidden;
}

.stats-bar__fill {
  height: 100%;
  background: var(--color-success);
  border-radius: 4px;
  transition: width 0.4s ease;
}

.stats-bar__text {
  font-size: 0.85rem;
  color: var(--color-text);
  margin-top: 4px;
  text-align: right;
}

/* Test view */
.test-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}

.test-header__title {
  font-size: 1.1rem;
  font-weight: 600;
}

.test-row {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
  overflow: hidden;
  border-left: 4px solid transparent;
  transition: border-color var(--transition), background var(--transition);
}

.test-row--correct {
  border-left-color: var(--color-success);
  background: var(--color-success-bg);
}

.test-row--wrong {
  border-left-color: var(--color-error);
  background: var(--color-error-bg);
}

.test-row__image-wrap {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.test-row__image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.test-row__body {
  padding: 12px;
}

.test-row__number {
  font-size: 0.8rem;
  color: var(--color-text-light);
  margin-bottom: 6px;
}

.test-row__select {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  font-size: 1rem;
  border: 2px solid var(--color-border);
  border-radius: 6px;
  background: #fff;
  color: var(--color-text);
  appearance: auto;
  -webkit-appearance: auto;
}

.test-row__select:focus {
  border-color: var(--color-primary);
  outline: none;
}

.test-row__select--correct {
  border-color: var(--color-success);
  background: var(--color-success-bg);
}

.test-row__select--wrong {
  border-color: var(--color-error);
  background: var(--color-error-bg);
}

.test-row__feedback {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 0.9rem;
  display: none;
}

.test-row__feedback--correct {
  display: block;
  background: var(--color-success-bg);
  color: var(--color-success);
}

.test-row__feedback--wrong {
  display: block;
  background: var(--color-error-bg);
  color: var(--color-error);
}

.test-row__features {
  margin-top: 8px;
  padding: 8px 10px;
  background: #f0f4f8;
  border-radius: 6px;
  font-size: 0.85rem;
  color: var(--color-text-light);
  display: none;
}

.test-row__features.visible {
  display: block;
}

.test-row__features ul {
  list-style: none;
  padding: 0;
}

.test-row__features li::before {
  content: "• ";
  color: var(--color-primary);
}

/* Result banner */
.result-banner {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  text-align: center;
  margin-bottom: 16px;
}

.result-banner__score {
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-primary);
}

.result-banner__label {
  font-size: 0.9rem;
  color: var(--color-text-light);
  margin-top: 4px;
}

/* Placeholder image */
.placeholder-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 20px;
  text-align: center;
  color: var(--color-text-light);
  height: 100%;
}

.placeholder-card img {
  width: 48px;
  height: 48px;
  opacity: 0.5;
}

.placeholder-card__link {
  color: var(--color-primary);
  font-size: 0.85rem;
  text-decoration: underline;
}

/* Action bar */
.action-bar {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.action-bar .btn {
  flex: 1;
  min-width: 120px;
}

/* Stub view */
.stub-view {
  text-align: center;
  padding: 40px 16px;
}

.stub-view__icon {
  font-size: 3rem;
  margin-bottom: 12px;
}

.stub-view__title {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.stub-view__text {
  color: var(--color-text-light);
  margin-bottom: 24px;
}

/* Mode selection */
.mode-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 20px;
}

/* Flashcard (Learn + SRS Review) */
.flashcard {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.flashcard__image {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--color-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

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

.flashcard__question {
  padding: 14px 16px 8px;
  font-size: 1.1rem;
  font-weight: 600;
  text-align: center;
}

.flashcard__options {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 16px 16px;
}

.flashcard__option {
  width: 100%;
  min-height: 48px;
  padding: 12px 16px;
  border: 2px solid var(--color-border);
  border-radius: 6px;
  background: #fff;
  color: var(--color-text);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: border-color var(--transition), background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.flashcard__option:active:not(:disabled) {
  border-color: var(--color-primary);
  background: #eef3f9;
}

.flashcard__option:disabled {
  cursor: default;
  opacity: 0.85;
}

.flashcard__option--correct {
  border-color: var(--color-success) !important;
  background: var(--color-success-bg) !important;
  opacity: 1 !important;
}

.flashcard__option--wrong {
  border-color: var(--color-error) !important;
  background: var(--color-error-bg) !important;
  opacity: 1 !important;
}

.flashcard__reveal {
  padding: 16px;
  border-top: 1px solid var(--color-border);
}

.flashcard__result {
  font-size: 1.1rem;
  font-weight: 700;
  padding: 10px;
  border-radius: 6px;
  text-align: center;
  margin-bottom: 12px;
}

.flashcard__result--correct {
  background: var(--color-success-bg);
  color: var(--color-success);
}

.flashcard__result--wrong {
  background: var(--color-error-bg);
  color: var(--color-error);
}

.flashcard__name {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.flashcard__desc {
  font-size: 0.9rem;
  color: var(--color-text-light);
  margin-bottom: 12px;
  line-height: 1.5;
}

.flashcard__features {
  background: #f0f4f8;
  padding: 10px 12px;
  border-radius: 6px;
  font-size: 0.85rem;
  margin-bottom: 12px;
}

.flashcard__features ul {
  list-style: none;
  padding: 4px 0 0;
}

.flashcard__features li::before {
  content: "• ";
  color: var(--color-primary);
}

.flashcard__comparison {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.flashcard__compare-col {
  background: #f0f4f8;
  padding: 10px;
  border-radius: 6px;
  font-size: 0.8rem;
}

.flashcard__compare-col ul {
  list-style: none;
  padding: 4px 0 0;
}

.flashcard__compare-col li::before {
  content: "• ";
  color: var(--color-text-light);
}

.flashcard__compare-label {
  display: block;
  font-size: 0.85rem;
  margin-bottom: 4px;
}

.flashcard__compare-label--correct { color: var(--color-success); }
.flashcard__compare-label--wrong { color: var(--color-error); }

.flashcard__actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.flashcard__actions .btn {
  flex: 1;
  min-height: 48px;
  font-size: 0.9rem;
}

/* Danger outline variant */
.btn--outline.btn--danger {
  color: var(--color-error);
  border-color: var(--color-error);
}

.btn--outline.btn--danger:active {
  background: var(--color-error);
  color: #fff;
}

/* Type filter list */
.type-filter-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

/* Stats view */
.stats-detail {
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--color-text-light);
}

.stats-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  background: var(--color-surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.stats-table th,
.stats-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

.stats-table th {
  background: var(--color-bg);
  font-weight: 600;
  color: var(--color-text-light);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stats-table td:nth-child(n+2) { text-align: center; }
.stats-table th:nth-child(n+2) { text-align: center; }

.stats-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 0.8rem;
  font-weight: 600;
}

.stats-badge--green { background: var(--color-success-bg); color: var(--color-success); }
.stats-badge--yellow { background: #fef3cd; color: #856404; }
.stats-badge--red { background: var(--color-error-bg); color: var(--color-error); }

/* Race rows (heatmap) */
.race-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.race-row:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.race-row:last-child { border-radius: 0 0 var(--radius) var(--radius); border-bottom: none; }

.race-row__info {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 120px;
}

.race-row__name {
  font-size: 0.8rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 90px;
}

.race-row__species {
  font-size: 0.75rem;
}

.race-row__seen {
  font-size: 0.7rem;
  color: var(--color-text-light);
}

.race-row__bar-wrap {
  flex: 1;
  height: 8px;
  background: var(--color-border);
  border-radius: 4px;
  overflow: hidden;
}

.race-row__bar {
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s ease;
}

.race-row__bar--green { background: var(--color-success); }
.race-row__bar--yellow { background: #f0ad4e; }
.race-row__bar--red { background: var(--color-error); }

.race-row__pct {
  font-size: 0.8rem;
  font-weight: 600;
  min-width: 36px;
  text-align: right;
  color: var(--color-text-light);
}

/* Image spinner */
.img-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
}

.img-spinner__ring {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Install banner */
.install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--color-surface);
  box-shadow: 0 -2px 12px rgba(0,0,0,0.12);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 200;
}

.install-banner__text {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 500;
}

.install-banner__btn {
  min-height: 40px;
  padding: 8px 18px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.install-banner__close {
  background: none;
  border: none;
  font-size: 1.2rem;
  color: var(--color-text-light);
  cursor: pointer;
  padding: 8px;
  min-width: 40px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Confetti */
.result-banner--perfect {
  position: relative;
  overflow: hidden;
}

.confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(circle, #f0ad4e 2px, transparent 2px),
    radial-gradient(circle, #c0392b 2px, transparent 2px),
    radial-gradient(circle, #2d8a4e 2px, transparent 2px),
    radial-gradient(circle, #1a4d7a 2px, transparent 2px),
    radial-gradient(circle, #e74c3c 1.5px, transparent 1.5px),
    radial-gradient(circle, #27ae60 1.5px, transparent 1.5px);
  background-size: 60px 60px, 80px 80px, 70px 70px, 90px 90px, 50px 50px, 65px 65px;
  background-position: 5px 5px, 30px 15px, 55px 35px, 10px 45px, 40px 55px, 70px 25px;
  animation: confetti-fall 2s ease-out forwards;
  opacity: 0.7;
}

@keyframes confetti-fall {
  0% { transform: translateY(-100%); opacity: 0.8; }
  100% { transform: translateY(100%); opacity: 0; }
}

/* Desktop */
@media (min-width: 480px) {
  #app {
    padding: 24px;
  }

  .species-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 768px) {
  .app-header__title {
    font-size: 1.3rem;
  }

  .species-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  :root {
    --color-primary: #4a9fd8;
    --color-primary-light: #6ab5e8;
    --color-primary-dark: #2a7fb8;
    --color-bg: #121620;
    --color-surface: #1c2030;
    --color-text: #e0e4ec;
    --color-text-light: #8a90a4;
    --color-success: #4caf6a;
    --color-success-bg: #1a2e22;
    --color-error: #e06050;
    --color-error-bg: #2e1a1a;
    --color-border: #2a3040;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  }

  .app-header {
    background: #0d1220;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
  }

  .test-row__select,
  .flashcard__option {
    background: var(--color-surface);
    color: var(--color-text);
  }

  .flashcard__features,
  .flashcard__compare-col,
  .test-row__features {
    background: #161a28;
  }

  .stats-table th { background: #161a28; }
  .stats-badge--yellow { background: #2e2a1a; color: #d4a017; }

  .install-banner { background: var(--color-surface); }
}
