/* ═══════════════════════════════════════════════════════
   Ecology Lab — iPad-first, dark forest theme
   ═══════════════════════════════════════════════════════ */

:root {
  --bg: #0f1f1a;
  --bg-2: #142b24;
  --bg-3: #1c3a31;
  --ink: #e8f3ec;
  --ink-dim: #8aa89a;
  --green: #3fa074;
  --green-soft: rgba(63, 160, 116, 0.18);
  --amber: #f0b86e;
  --red: #e07b6b;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  --radius: 18px;
  --t-fast: 160ms ease-out;
  --t-mid: 320ms cubic-bezier(.2,.7,.2,1);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans", sans-serif;
  font-size: 17px;
  line-height: 1.5;
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

button { font: inherit; color: inherit; border: 0; background: transparent; cursor: pointer; }

/* ═══════════════ Screen system ═══════════════ */
.screen {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  padding: env(safe-area-inset-top, 0) 0 env(safe-area-inset-bottom, 0);
}
.screen.active { display: flex; animation: fadeIn var(--t-mid); }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ═══════════════ HOME ═══════════════ */
.home-hero {
  padding: 48px 32px 24px;
  text-align: center;
}
.hero-emoji { font-size: 72px; line-height: 1; }
.home-hero h1 {
  margin: 12px 0 4px;
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -0.5px;
}
.subtitle { margin: 0; color: var(--ink-dim); font-size: 16px; }

.missions-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 18px;
  padding: 16px 32px;
  align-content: start;
}

.mission-card {
  background: var(--bg-2);
  border-radius: var(--radius);
  padding: 22px 22px 20px;
  display: flex; flex-direction: column;
  gap: 8px;
  border: 2px solid transparent;
  transition: transform var(--t-fast), border-color var(--t-fast);
  position: relative;
  overflow: hidden;
}
.mission-card:active { transform: scale(0.97); }
.mission-card.locked { opacity: 0.45; }
.mission-card.done { border-color: var(--green); }
.mission-card .m-icon { font-size: 44px; line-height: 1; }
.mission-card .m-name { font-size: 22px; font-weight: 600; margin-top: 4px; }
.mission-card .m-concept { font-size: 13px; color: var(--ink-dim); }
.mission-card .m-zh { font-size: 12px; color: var(--ink-dim); opacity: 0.7; margin-top: 2px; }
.mission-card .m-status {
  margin-top: 10px;
  display: flex; align-items: center; gap: 6px;
  font-size: 13px;
  color: var(--green);
}
.mission-card .m-status.empty { color: var(--ink-dim); }

.home-footer {
  display: flex; justify-content: space-between;
  padding: 20px 32px env(safe-area-inset-bottom, 20px);
  gap: 12px;
}
.btn-ghost {
  background: var(--bg-2);
  padding: 14px 20px;
  border-radius: 12px;
  color: var(--ink);
  font-size: 15px;
  transition: background var(--t-fast);
}
.btn-ghost:active { background: var(--bg-3); }
.btn-ghost.danger { color: var(--red); }

/* ═══════════════ MISSION ═══════════════ */
.mission-header {
  display: flex; align-items: center; gap: 16px;
  padding: 16px 24px;
  background: var(--bg);
  border-bottom: 1px solid var(--bg-3);
}
.btn-back {
  font-size: 17px;
  padding: 8px 12px;
  border-radius: 10px;
  color: var(--ink-dim);
  min-width: 70px; text-align: left;
}
.btn-back:active { background: var(--bg-2); }
.mission-title {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 600;
  display: flex; justify-content: center; align-items: center; gap: 10px;
}
.m-emoji { font-size: 26px; }
.mission-progress {
  min-width: 100px;
  text-align: right;
  font-size: 13px;
  color: var(--ink-dim);
}
.bar {
  width: 80px; height: 6px;
  background: var(--bg-3);
  border-radius: 3px;
  margin-bottom: 4px;
  margin-left: auto;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  width: 0;
  background: var(--green);
  border-radius: 3px;
  transition: width var(--t-mid);
}

.mission-body {
  flex: 1;
  overflow-y: auto;
  padding: 28px 32px;
  display: flex; flex-direction: column;
  gap: 24px;
}

.mission-footer {
  padding: 12px 24px env(safe-area-inset-bottom, 12px);
  border-top: 1px solid var(--bg-3);
  display: flex; justify-content: space-between; align-items: center;
}
.streak-area { font-size: 18px; color: var(--amber); font-weight: 600; }

/* ═══════════════ Task cards ═══════════════ */
.task-card {
  background: var(--bg-2);
  border-radius: var(--radius);
  padding: 26px;
  box-shadow: var(--shadow);
  animation: slideUp var(--t-mid);
}
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: none; } }

.task-prompt {
  font-size: 20px;
  line-height: 1.4;
  margin: 0 0 22px;
  font-weight: 500;
}

.task-options {
  display: flex; flex-direction: column;
  gap: 12px;
}
.opt {
  background: var(--bg-3);
  padding: 16px 18px;
  border-radius: 14px;
  font-size: 17px;
  text-align: left;
  border: 2px solid transparent;
  transition: all var(--t-fast);
  min-height: 56px;
  display: flex; align-items: center;
  position: relative;
}
.opt:active:not(.locked) { transform: scale(0.98); background: #234a3e; }
.opt.selected { border-color: var(--green); background: var(--green-soft); }
.opt.correct {
  border-color: var(--green); background: var(--green-soft);
  color: #b8e8cf;
}
.opt.correct::after {
  content: '✓'; position: absolute; right: 18px;
  color: var(--green); font-weight: 700; font-size: 22px;
}
.opt.wrong {
  border-color: var(--red); background: rgba(224, 123, 107, 0.12);
}
.opt.wrong::after {
  content: '✗'; position: absolute; right: 18px;
  color: var(--red); font-weight: 700; font-size: 22px;
}
.opt.locked { pointer-events: none; }

.explain {
  margin-top: 18px;
  padding: 14px 18px;
  background: var(--bg-3);
  border-radius: 12px;
  border-left: 3px solid var(--amber);
  font-size: 15px;
  line-height: 1.5;
  color: #d5e6dc;
  animation: fadeIn var(--t-mid);
}

.task-actions {
  margin-top: 22px;
  display: flex; justify-content: flex-end; gap: 12px;
}
.btn-primary {
  background: var(--green);
  color: #0b1813;
  font-weight: 600;
  padding: 14px 28px;
  border-radius: 14px;
  font-size: 17px;
  min-width: 120px;
  transition: transform var(--t-fast), background var(--t-fast);
}
.btn-primary:active { transform: scale(0.97); background: #4ab588; }
.btn-primary:disabled { opacity: 0.4; }

/* ═══════════════ CLASSIFY (drag to buckets) ═══════════════ */
.classify-area { display: flex; flex-direction: column; gap: 18px; }
.classify-items {
  display: flex; flex-wrap: wrap; gap: 10px;
  padding: 16px;
  background: var(--bg-3);
  border-radius: 12px;
  min-height: 60px;
}
.chip {
  background: var(--bg-2);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 15px;
  cursor: grab;
  border: 2px solid var(--bg-3);
  touch-action: none;
  transition: transform var(--t-fast);
}
.chip.dragging { opacity: 0.4; }
.chip.placed-correct { border-color: var(--green); }
.chip.placed-wrong { border-color: var(--red); animation: shake 320ms; }
@keyframes shake { 0%,100% { transform: none; } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.buckets {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
}
.bucket {
  background: var(--bg-3);
  border: 2px dashed var(--bg-3);
  border-radius: 14px;
  padding: 14px;
  min-height: 120px;
  transition: border-color var(--t-fast), background var(--t-fast);
}
.bucket.drop-active { border-color: var(--green); background: var(--green-soft); }
.bucket .b-title {
  font-size: 14px; font-weight: 600;
  color: var(--ink-dim);
  text-transform: uppercase; letter-spacing: 1px;
  margin-bottom: 10px;
}
.bucket .b-items {
  display: flex; flex-wrap: wrap; gap: 8px;
}

/* ═══════════════ SORT (food chain ladder) ═══════════════ */
.sort-area {
  display: flex; flex-direction: column-reverse;
  gap: 6px;
  padding: 14px;
  background: var(--bg-3);
  border-radius: 14px;
}
.sort-slot {
  min-height: 56px;
  background: var(--bg);
  border: 2px dashed transparent;
  border-top: 2px dashed var(--bg-2);
  border-radius: 10px;
  display: flex; align-items: center; padding: 0 14px;
  position: relative;
  font-size: 17px;
  transition: all var(--t-fast);
}
.sort-slot.drop-active { border-color: var(--green); background: var(--green-soft); }
.sort-slot.level-label {
  background: transparent; border: 0; min-height: auto;
  color: var(--ink-dim); font-size: 12px; padding: 4px 8px;
  text-transform: uppercase; letter-spacing: 1px;
}

.sort-pool {
  display: flex; flex-wrap: wrap; gap: 10px;
  margin-top: 14px;
  padding: 14px;
  background: var(--bg-3);
  border-radius: 12px;
  min-height: 60px;
}

/* ═══════════════ KEYSTONE (cascade sim) ═══════════════ */
.keystone-stage {
  background: linear-gradient(180deg, #1c4060 0%, #0f2538 100%);
  border-radius: var(--radius);
  padding: 30px 20px;
  min-height: 360px;
  position: relative;
  overflow: hidden;
  display: flex; flex-direction: column; align-items: center;
  gap: 14px;
}
.keystone-stage.dead { background: linear-gradient(180deg, #2a1a18 0%, #1a0e0c 100%); }
.species {
  display: flex; flex-direction: column; align-items: center;
  gap: 4px;
  transition: all 600ms cubic-bezier(.4,0,.2,1);
}
.species .sp-emoji { font-size: 56px; line-height: 1; transition: all 600ms; }
.species.boom .sp-emoji { transform: scale(1.6); filter: hue-rotate(20deg); }
.species.dying .sp-emoji { opacity: 0.2; filter: grayscale(1); transform: scale(0.7); }
.species .sp-name {
  font-size: 13px; color: rgba(255,255,255,0.8);
}
.species.removed {
  opacity: 0; transform: translateY(-30px) scale(0.5);
  pointer-events: none;
}
.arrow-down {
  font-size: 22px; color: rgba(255,255,255,0.4);
  line-height: 0.5;
}

.cascade-log {
  margin-top: 16px;
  display: flex; flex-direction: column; gap: 6px;
  min-height: 120px;
}
.cascade-line {
  background: rgba(0,0,0,0.4);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  color: #f0d8c8;
  border-left: 2px solid var(--amber);
  animation: slideUp var(--t-mid);
}

.btn-danger {
  background: var(--red); color: white; font-weight: 600;
  padding: 14px 24px; border-radius: 12px; font-size: 16px;
  min-width: 200px;
}
.btn-danger:active { transform: scale(0.97); }
.btn-danger:disabled { opacity: 0.3; }

/* ═══════════════ NOTEBOOK ═══════════════ */
.notebook-body {
  flex: 1; overflow-y: auto;
  padding: 28px 32px;
}
.notebook-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}
.nb-card {
  background: var(--bg-2);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
  border: 2px solid transparent;
}
.nb-card.unlocked { border-color: var(--green); }
.nb-card.locked { opacity: 0.3; }
.nb-emoji { font-size: 56px; line-height: 1; }
.nb-name { font-size: 17px; font-weight: 600; margin: 8px 0 6px; }
.nb-fact { font-size: 13px; color: var(--ink-dim); line-height: 1.5; }
.nb-card.locked .nb-fact::before { content: '???'; }
.nb-card.locked .nb-fact { color: transparent; }

/* ═══════════════ COMPLETE SCREEN ═══════════════ */
#screen-complete {
  display: none;
  justify-content: center; align-items: center;
  padding: 32px;
}
#screen-complete.active { display: flex; }
.complete-card {
  background: var(--bg-2);
  border-radius: 24px;
  padding: 40px 30px;
  max-width: 480px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
  animation: popIn 500ms cubic-bezier(.2,1.4,.4,1);
}
@keyframes popIn { from { opacity: 0; transform: scale(0.85); } to { opacity: 1; transform: none; } }
.big-emoji { font-size: 88px; line-height: 1; }
.complete-card h2 { font-size: 28px; margin: 14px 0 8px; }
.complete-card .stats { color: var(--ink-dim); font-size: 15px; }
.unlocks {
  display: flex; justify-content: center; gap: 16px;
  margin: 20px 0;
  flex-wrap: wrap;
}
.unlock-pill {
  background: var(--green-soft);
  border: 2px solid var(--green);
  padding: 10px 16px;
  border-radius: 12px;
  display: flex; align-items: center; gap: 8px;
  font-size: 16px;
  animation: popIn 600ms cubic-bezier(.2,1.4,.4,1);
}
.unlock-pill .ue { font-size: 28px; }

/* ═══════════════ Toast ═══════════════ */
.toast {
  position: fixed;
  top: 84px; left: 50%; transform: translateX(-50%) translateY(-120px);
  background: rgba(15, 31, 26, 0.95);
  backdrop-filter: blur(10px);
  padding: 12px 22px;
  border-radius: 14px;
  font-size: 15px;
  border: 1px solid var(--bg-3);
  z-index: 1000;
  transition: transform var(--t-mid);
  pointer-events: none;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { border-color: var(--green); color: #b8e8cf; }
.toast.fail { border-color: var(--red); color: #f0c0b8; }

/* ═══════════════ Phone fallback ═══════════════ */
@media (max-width: 640px) {
  .missions-grid { grid-template-columns: 1fr; }
  .home-hero { padding: 32px 20px 16px; }
  .home-hero h1 { font-size: 30px; }
  .mission-body { padding: 20px; }
  .task-card { padding: 20px; }
  .task-prompt { font-size: 18px; }
  .buckets { grid-template-columns: 1fr; }
}
