:root {
  --bg: #ece6d6;
  --surface: #f8f4ea;
  --ink: #1f1c16;
  --muted: #655f4f;
  --line: #d4cab3;
  --c-minerals: #226a67;
  --c-igneous: #b2431f;
  --c-sedimentary: #8f6a1c;
  --c-metamorphic: #3f5f86;
  --shadow: 0 1px 2px rgba(40, 30, 10, 0.12), 0 10px 26px -12px rgba(40, 30, 10, 0.32);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15130f;
    --surface: #211e18;
    --ink: #eee8d9;
    --muted: #a39c89;
    --line: #38342b;
    --c-minerals: #5cc0bb;
    --c-igneous: #ee8a63;
    --c-sedimentary: #d9b25a;
    --c-metamorphic: #8fb0dd;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 12px 28px -12px rgba(0, 0, 0, 0.7);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15130f;
  --surface: #211e18;
  --ink: #eee8d9;
  --muted: #a39c89;
  --line: #38342b;
  --c-minerals: #5cc0bb;
  --c-igneous: #ee8a63;
  --c-sedimentary: #d9b25a;
  --c-metamorphic: #8fb0dd;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 12px 28px -12px rgba(0, 0, 0, 0.7);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }

[data-set="minerals"] { --accent: var(--c-minerals); }
[data-set="igneous"] { --accent: var(--c-igneous); }
[data-set="sedimentary"] { --accent: var(--c-sedimentary); }
[data-set="metamorphic"] { --accent: var(--c-metamorphic); }
.d-credit { margin: 12px 0 0; font-size: 12px; color: var(--muted); }

/* ---------- header ---------- */
.top {
  border-bottom: 1px solid var(--line);
  padding: calc(14px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}
.top-in {
  max-width: 1240px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "brand theme" "views views" "bar bar";
  gap: 12px 12px;
  align-items: center;
}
.brand { grid-area: brand; }
.eyebrow { margin: 0; font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
h1 { margin: 0; font: 600 28px/1.1 var(--serif); letter-spacing: -0.01em; }
.theme { grid-area: theme; }
.views { grid-area: views; display: flex; gap: 22px; border-bottom: 1px solid var(--line); }
.views button {
  background: none; border: 0; padding: 8px 0 9px; margin-bottom: -1px;
  border-bottom: 2px solid transparent; color: var(--muted); font-weight: 500;
  transition: color 0.2s, border-color 0.2s;
}
.views button:hover { color: var(--ink); }
.views button[aria-current="page"] { color: var(--ink); border-bottom-color: var(--ink); }
.bar { grid-area: bar; display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; justify-content: space-between; }
.chips { display: flex; gap: 8px; overflow-x: auto; max-width: 100%; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; background: none; border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 11px; font-size: 14px; color: var(--muted);
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.chip b { font-weight: 600; color: var(--ink); margin-left: 5px; }
.chip:hover { border-color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.chip[aria-pressed="true"] b { color: var(--bg); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn {
  display: inline-flex; align-items: center; gap: 7px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 6px; padding: 7px 13px; font-size: 14px; font-weight: 500;
  transition: border-color 0.2s, transform 0.15s;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: scale(0.97); }
.btn[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center; background: none;
  border: 1px solid var(--line); border-radius: 6px;
}
.icon-btn:hover { border-color: var(--muted); }
body[data-view="focus"] .only-grid, body[data-view="study"] .only-grid,
body[data-view="grid"] .only-study, body[data-view="focus"] .only-study { display: none; }

@media (min-width: 900px) {
  .top-in {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "brand views theme" "bar bar bar";
    gap: 14px 28px;
  }
  .views { border-bottom: 0; justify-content: center; }
  .views button { padding: 9px 2px; }
  h1 { font-size: 32px; }
}

/* ---------- stage ---------- */
#stage {
  flex: 1; width: 100%; max-width: 1240px; margin: 0 auto; outline: 0;
  padding: 22px max(16px, env(safe-area-inset-right)) 28px max(16px, env(safe-area-inset-left));
}
.foot { padding: 4px 16px calc(24px + env(safe-area-inset-bottom)); text-align: center; color: var(--muted); font-size: 13px; }
.foot p { margin: 0 auto; max-width: 560px; }
.nojs { padding: 24px; text-align: center; }

/* ---------- grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
@media (min-width: 700px) { .grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 18px; } }
.cell { position: relative; }

/* ---------- the flip card ---------- */
.card {
  position: relative; aspect-ratio: 5 / 6; perspective: 1200px; container-type: inline-size;
  cursor: pointer; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent;
  transition: transform 0.25s;
}
.card:not(.flipped):hover { transform: translateY(-3px); }
.inner {
  position: absolute; inset: 0; transform-style: preserve-3d;
  transition: transform 0.7s cubic-bezier(0.34, 1.28, 0.5, 1);
}
.card.flipped .inner { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; border-radius: 12px; overflow: hidden;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--surface); box-shadow: var(--shadow);
}
.front img { width: 100%; height: 100%; object-fit: cover; display: block; -webkit-user-drag: none; }
.tag {
  position: absolute; left: 8px; bottom: 8px; padding: 3px 8px; border-radius: 4px;
  background: rgba(18, 16, 12, 0.7); color: #f4efe2; font-size: 12px; font-weight: 500; letter-spacing: 0.02em;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.back { transform: rotateY(180deg); border: 1px solid var(--line); padding: 6cqw 6.5cqw; display: flex; flex-direction: column; }
.b-head { padding-right: 9cqw; }
.back h3 { margin: 0 0 1.5cqw; font: 600 max(15px, 9cqw)/1.08 var(--serif); letter-spacing: -0.01em; }
.cls { margin: 0 0 3.5cqw; font-size: max(10.5px, 5.2cqw); line-height: 1.25; font-weight: 600; color: var(--accent); }
.b-props p {
  margin: 0 0 1.6cqw; font-size: max(10.5px, 6cqw); line-height: 1.25;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.b-props b { font-weight: 600; color: var(--muted); }
.open {
  position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 6px; color: var(--ink);
  opacity: 0; pointer-events: none; transition: opacity 0.2s 0.25s;
}
.cell.up .open { opacity: 1; pointer-events: auto; }
.open:hover { border-color: var(--muted); }

/* ---------- one at a time ---------- */
.focus { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.f-stage { width: min(100%, 480px); touch-action: pan-y; }
.card.big { aspect-ratio: auto; width: 100%; height: clamp(440px, calc(100svh - 300px), 640px); }
.card.big:not(.flipped):hover { transform: none; }
.card.big .face { border-radius: 16px; }
.card.big .back { padding: 0; }
.card.big .scroll { overflow-y: auto; padding: 26px 26px 30px; height: 100%; overscroll-behavior: auto; }
.card.big .tag { font-size: 13px; left: 12px; bottom: 12px; padding: 4px 10px; }
.controls { width: min(100%, 480px); display: grid; grid-template-columns: 52px 1fr 52px; align-items: center; gap: 16px; }
.nav {
  height: 52px; display: grid; place-items: center; background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; transition: border-color 0.2s, transform 0.15s;
}
.nav:hover { border-color: var(--muted); }
.nav:active { transform: scale(0.94); }
.f-meta { text-align: center; font-size: 14px; color: var(--muted); }
.f-meta .count { display: block; margin-bottom: 7px; }
.progress { display: block; height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--ink); transition: width 0.35s; }
.hint { margin: 0; font-size: 13px; color: var(--muted); text-align: center; }
@media (hover: none) { .hint .kb { display: none; } }

/* ---------- details (shared by the big card and the study sheet) ---------- */
.d-name { margin: 0; font: 600 30px/1.08 var(--serif); letter-spacing: -0.015em; }
.d-cls { margin: 6px 0 0; font-size: 14px; font-weight: 600; color: var(--accent); }
.d-formula { margin: 2px 0 0; font-size: 14px; color: var(--muted); }
.d-note { margin: 10px 0 14px; font-family: var(--serif); font-style: italic; font-size: 17px; line-height: 1.35; }
.props { margin: 0; display: grid; grid-template-columns: minmax(0, 8.2rem) minmax(0, 1fr); gap: 8px 14px; border-top: 1px solid var(--line); padding-top: 12px; }
.props dt { font-size: 13px; font-weight: 600; color: var(--muted); line-height: 1.5; }
.props dd { margin: 0; font-size: 15px; line-height: 1.45; }
.card.big .props { grid-template-columns: minmax(0, 6.6rem) minmax(0, 1fr); gap: 7px 12px; }
.card.big .props dd { font-size: 14.5px; line-height: 1.4; }
.card.big .d-note { margin: 8px 0 12px; font-size: 16px; }
.card.big .d-name { font-size: 28px; }
@media (max-width: 420px) {
  .card.big .props { grid-template-columns: 1fr; gap: 0; }
  .card.big .props dt { margin-top: 9px; }
}

/* ---------- study sheet ---------- */
.study { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: 34px 24px; }
.fig { margin: 0; display: flex; flex-direction: column; }
.photo { position: relative; aspect-ratio: 1 / 1; border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); background: var(--surface); }
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; -webkit-user-drag: none; }
.fig figcaption { padding: 16px 2px 0; flex: 1; }
.fig .d-name { font-size: 26px; }
.reveal {
  width: 100%; padding: 22px 12px; background: var(--surface); border: 1px dashed var(--muted);
  border-radius: 8px; font-weight: 500; color: var(--muted);
}
.reveal:hover { color: var(--ink); border-color: var(--ink); }
.fig.is-hidden .photo { cursor: pointer; }

@media (prefers-reduced-motion: reduce) {
  .inner { transition-duration: 0.01s; }
  .card, .progress i { transition: none; }
}

/* touch targets: 44px minimum on phones */
@media (pointer: coarse), (max-width: 700px) {
  .views button { min-height: 44px; padding-top: 0; padding-bottom: 0; }
  .chip, .btn { min-height: 44px; padding-left: 14px; padding-right: 14px; }
  .icon-btn { width: 44px; height: 44px; }
  .open { width: 40px; height: 40px; top: 4px; right: 4px; }
  .b-head { padding-right: 36px; }
}
body { overflow-x: clip; }

/* the photo side must never swallow a vertical drag: the page scrolls, only a horizontal swipe is ours */
.f-stage, .card.big, .card.big .face, .card.big .front img { touch-action: pan-y pinch-zoom; }
.card.big .front img { pointer-events: none; -webkit-touch-callout: none; }

/* iOS still hit-tests the hidden face; its scroller was eating drags meant for the page */
.card:not(.flipped) .back, .card.flipped .front { pointer-events: none; }
.card:not(.flipped) .back .scroll { overflow: hidden; }
