/* ---------------------------------------------------------
   Strata — design tokens
--------------------------------------------------------- */
:root {
  color-scheme: dark;
  --bg: #101220;
  --bg-elevated: #171a2b;
  --bg-card: #1b1e33;
  --border: #2a2e47;
  --text: #eef0fb;
  --text-dim: #9ea3c4;
  --text-faint: #6b6f92;

  --magma-1: #c2542f;
  --magma-2: #e8955a;
  --teal-1: #2fb8a6;
  --teal-2: #7fe0d0;

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 9px;

  --header-h: 56px;
  --footer-h: 64px;

  /* device safe-area insets, with sane fallbacks */
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

/* Theme is resolved to "light" or "dark" by js/theme.js (light/dark/system
   toggle) and stamped onto <html data-theme>, applied pre-paint via an
   inline script in <head> so there's no flash of the wrong theme. */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f4f1;
  --bg-elevated: #ffffff;
  --bg-card: #ffffff;
  --border: #e4e2db;
  --text: #191a22;
  --text-dim: #55586e;
  --text-faint: #8c8fa3;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

a { color: inherit; text-decoration: none; }
ul, ol { padding-left: 1.25em; }
li + li { margin-top: 0.4em; }

/* ---------------------------------------------------------
   App shell / safe-area aware header & footer
--------------------------------------------------------- */
.app-shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  height: calc(var(--header-h) + var(--sat));
  padding: var(--sat) max(16px, var(--sar)) 0 max(16px, var(--sal));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border-bottom: 1px solid var(--border);
}

.back-btn {
  appearance: none;
  border: none;
  background: none;
  color: var(--text);
  display: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  margin-left: -8px;
  border-radius: 50%;
  cursor: pointer;
}
.back-btn:not([hidden]) { display: inline-flex; }
.back-btn:active { background: var(--border); }

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}
.brand-mark {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  display: block;
}
.header-spacer { flex: 1; }

/* ---------------------------------------------------------
   Animated strata logo
--------------------------------------------------------- */
.strata-logo { display: block; overflow: visible; }
.strata-logo .stratum {
  transform-box: fill-box;
  transform-origin: center;
  animation: strata-drift 5s ease-in-out infinite;
}
.strata-logo .stratum-1 { animation-delay: 0s; }
.strata-logo .stratum-2 { animation-delay: 0.25s; }
.strata-logo .stratum-3 { animation-delay: 0.5s; }
.strata-logo .stratum-4 { animation-delay: 0.75s; }
.strata-logo .stratum-5 { animation-delay: 1s; }
@keyframes strata-drift {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-1px); }
}
/* Fault-line crack: a faint energy pulse traveling along it, like the
   "harvest now, decrypt later" tension the app's own Fault Lines post
   describes — a crack that's always quietly active. */
.strata-logo .fault-crack {
  stroke-dasharray: 3 5;
  animation: fault-pulse 2.4s linear infinite;
}
@keyframes fault-pulse {
  to { stroke-dashoffset: -16; }
}
@media (prefers-reduced-motion: reduce) {
  .strata-logo .stratum,
  .strata-logo .fault-crack { animation: none; }
}

.hero-logo { margin-bottom: 10px; }
.hero-logo .strata-logo { width: 48px; height: 48px; }

/* ---------------------------------------------------------
   Header action icons (theme / install / share)
--------------------------------------------------------- */
.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}
.icon-btn {
  appearance: none;
  border: none;
  background: none;
  color: var(--text-dim);
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:active { background: var(--border); }
.icon-btn:hover { color: var(--text); }

.icon-sun, .icon-moon { display: none; }
:root[data-theme="light"] .icon-sun { display: block; }
:root[data-theme="dark"] .icon-moon { display: block; }

.menu-wrap { position: relative; }
.theme-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 148px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
  z-index: 30;
}
.theme-menu button {
  appearance: none;
  border: none;
  background: none;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}
.theme-menu button:hover { background: var(--border); }
.theme-menu button[aria-checked="true"] { color: var(--magma-2); }
.theme-menu button[aria-checked="true"]::after {
  content: "✓";
  margin-left: auto;
  font-weight: 700;
}

.app-main {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 20px max(16px, var(--sar)) calc(var(--footer-h) + var(--sab) + 24px) max(16px, var(--sal));
  outline: none;
}

.app-footer {
  position: sticky;
  bottom: 0;
  z-index: 20;
  display: flex;
  gap: 4px;
  height: calc(var(--footer-h) + var(--sab));
  /* Leave breathing room above the home-indicator / gesture swipe area
     instead of letting nav targets sit flush against it. */
  padding: 6px max(10px, var(--sar)) max(10px, var(--sab)) max(10px, var(--sal));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border-top: 1px solid var(--border);
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border-radius: var(--radius-sm);
  color: var(--text-faint);
  font-size: 0.68rem;
  font-weight: 600;
  transition: color 0.15s ease, background 0.15s ease;
  /* keep tap targets away from screen edges (swipe-back / control-center gestures) */
  margin: 0 2px;
}
.nav-item:active { background: var(--border); }
.nav-item.active { color: var(--text); }
.nav-item.active svg path { stroke: var(--magma-2); }

/* ---------------------------------------------------------
   Home view
--------------------------------------------------------- */
.hero {
  margin-bottom: 24px;
}
.hero h1 {
  font-size: 1.7rem;
  margin: 0 0 6px;
  letter-spacing: -0.01em;
}
.hero p {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.95rem;
  line-height: 1.5;
}

.section-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  margin: 28px 0 12px;
}
.section-label:first-of-type { margin-top: 0; }

.core-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
@media (max-width: 420px) {
  .core-grid { grid-template-columns: 1fr; }
}

.core-card {
  position: relative;
  overflow: hidden;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--bg-card);
  padding: 18px 16px;
  display: block;
  min-height: 128px;
}
.core-card::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.16;
}
.core-card[data-theme="magma-orange"]::before { background: linear-gradient(160deg, var(--magma-1), transparent 65%); }
.core-card[data-theme="basalt-teal"]::before { background: linear-gradient(160deg, var(--teal-1), transparent 65%); }

.core-card .core-name {
  position: relative;
  font-weight: 700;
  font-size: 1.05rem;
  margin: 0 0 6px;
}
.core-card .core-desc {
  position: relative;
  color: var(--text-dim);
  font-size: 0.82rem;
  line-height: 1.45;
  margin: 0 0 10px;
}
.core-card .core-count {
  position: relative;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-faint);
}

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

.post-card {
  display: block;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-card);
  padding: 14px 16px;
}
.post-card .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.post-card h3 { margin: 0 0 6px; font-size: 1rem; line-height: 1.35; }
.post-card .tags { color: var(--text-faint); font-size: 0.76rem; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 3px 8px;
  border-radius: 100px;
  border: 1px solid var(--border);
  color: var(--text-dim);
}
.badge.depth-bedrock { color: var(--magma-2); border-color: color-mix(in srgb, var(--magma-1) 40%, var(--border)); }
.badge.depth-fault_lines { color: var(--teal-2); border-color: color-mix(in srgb, var(--teal-1) 40%, var(--border)); }
.badge.status-crystallized { color: var(--teal-2); }
.badge.status-raw_sediment { color: var(--text-faint); }

/* ---------------------------------------------------------
   Core detail view
--------------------------------------------------------- */
.core-header {
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  padding: 20px 18px;
  margin-bottom: 22px;
  position: relative;
  overflow: hidden;
}
.core-header[data-theme="magma-orange"] { background: linear-gradient(160deg, color-mix(in srgb, var(--magma-1) 22%, var(--bg-card)), var(--bg-card) 70%); }
.core-header[data-theme="basalt-teal"] { background: linear-gradient(160deg, color-mix(in srgb, var(--teal-1) 22%, var(--bg-card)), var(--bg-card) 70%); }
.core-header h1 { margin: 0 0 8px; font-size: 1.4rem; }
.core-header p { margin: 0; color: var(--text-dim); font-size: 0.9rem; line-height: 1.5; }

/* ---------------------------------------------------------
   Post detail view
--------------------------------------------------------- */
.post-header { margin-bottom: 18px; }
.post-header .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.post-header h1 { font-size: 1.4rem; line-height: 1.3; margin: 0 0 10px; }
.post-header .tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tag-chip {
  font-size: 0.72rem;
  color: var(--text-dim);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 100px;
  padding: 3px 9px;
}

.post-body {
  font-size: 0.96rem;
  line-height: 1.65;
  color: var(--text);
}
.post-body h3 {
  font-size: 1.05rem;
  margin: 28px 0 10px;
  color: var(--magma-2);
}
.post-body p { margin: 0 0 14px; }
.post-body ul, .post-body ol { margin: 0 0 14px; }
.post-body strong { color: var(--text); }
.post-body em { color: var(--text-dim); }

.reactions {
  display: flex;
  gap: 8px;
  margin: 26px 0;
  flex-wrap: wrap;
}
.reaction-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-dim);
  border-radius: 100px;
  padding: 7px 13px;
  font-size: 0.82rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.15s ease, color 0.15s ease;
}
.reaction-btn:active { transform: scale(0.96); }
.reaction-btn.active {
  color: var(--magma-2);
  border-color: color-mix(in srgb, var(--magma-1) 45%, var(--border));
  background: color-mix(in srgb, var(--magma-1) 12%, var(--bg-card));
}

.related-strata {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.related-strata h4 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-faint); margin: 0 0 12px; }
.related-strata .post-list { margin: 0; }

.empty-state {
  color: var(--text-faint);
  font-size: 0.9rem;
  padding: 24px 0;
  text-align: center;
}

.install-hint {
  margin: 0 0 22px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--border);
  color: var(--text-dim);
  font-size: 0.8rem;
  line-height: 1.5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.install-hint button {
  appearance: none;
  border: none;
  background: var(--magma-1);
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
  padding: 7px 12px;
  border-radius: 100px;
  cursor: pointer;
  flex-shrink: 0;
}

.install-hint + .install-hint { margin-top: -12px; }

/* ---------------------------------------------------------
   Comments
--------------------------------------------------------- */
.comments {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.comments h4 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin: 0 0 14px;
}

.comment-list {
  list-style: none;
  margin: 0 0 18px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.comment-item {
  border: 1px solid var(--border);
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}
.comment-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 4px;
}
.comment-name { font-weight: 700; font-size: 0.82rem; }
.comment-time { font-size: 0.7rem; color: var(--text-faint); flex-shrink: 0; }
.comment-text { margin: 0; font-size: 0.88rem; line-height: 1.5; color: var(--text-dim); }
.comment-status { color: var(--text-faint); font-size: 0.85rem; padding: 4px 0; }

.comment-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.comment-form input,
.comment-form textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font: inherit;
  font-size: 0.88rem;
  resize: vertical;
}
.comment-form input:focus,
.comment-form textarea:focus {
  outline: 2px solid var(--teal-1);
  outline-offset: 1px;
}
.comment-form-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}
.comment-error {
  font-size: 0.76rem;
  color: var(--magma-2);
  flex: 1;
}
.comment-form button {
  appearance: none;
  border: none;
  background: var(--teal-1);
  color: #06201c;
  font-weight: 700;
  font-size: 0.82rem;
  padding: 9px 16px;
  border-radius: 100px;
  cursor: pointer;
  flex-shrink: 0;
}
.comment-form button:disabled { opacity: 0.6; cursor: default; }

/* ---------------------------------------------------------
   Bottom sheet (install instructions / share menu)
--------------------------------------------------------- */
.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(6, 7, 14, 0.55);
  display: none;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s ease;
}
/* [hidden] alone loses to the class selector above on specificity, so the
   overlay would otherwise stay display:flex (and eat every click/scroll,
   invisibly, across the whole app) even while "hidden". */
.sheet-overlay:not([hidden]) { display: flex; }
.sheet-overlay.open { opacity: 1; }

.sheet {
  width: 100%;
  max-width: 480px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 22px 22px 0 0;
  padding: 10px 20px calc(24px + var(--sab)) 20px;
  transform: translateY(100%);
  transition: transform 0.24s cubic-bezier(0.32, 0.72, 0, 1);
  max-height: 82vh;
  overflow-y: auto;
}
.sheet-overlay.open .sheet { transform: translateY(0); }

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 100px;
  background: var(--border);
  margin: 0 auto 14px;
}

.sheet-title { margin: 0 0 8px; font-size: 1.1rem; }
.sheet-text { margin: 0 0 4px; color: var(--text-dim); font-size: 0.88rem; line-height: 1.5; }
.share-preview-title { color: var(--text); font-weight: 600; margin-bottom: 16px; }

.sheet-steps {
  margin: 0;
  padding-left: 1.3em;
  color: var(--text-dim);
  font-size: 0.88rem;
  line-height: 1.7;
}
.sheet-steps strong { color: var(--text); }

.sheet-cta {
  appearance: none;
  border: none;
  width: 100%;
  background: var(--magma-1);
  color: #fff;
  font-weight: 700;
  font-size: 0.92rem;
  padding: 13px;
  border-radius: 100px;
  cursor: pointer;
  margin: 6px 0 4px;
}

.share-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 16px;
}
.share-item {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-dim);
  border-radius: var(--radius-md);
  padding: 14px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}
.share-item:active { background: var(--border); }
.share-icon { font-size: 1.3rem; }
.share-item:disabled { opacity: 0.6; cursor: default; }

/* ---------------------------------------------------------
   Toast
--------------------------------------------------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--footer-h) + var(--sab) + 14px);
  transform: translate(-50%, 8px);
  background: var(--text);
  color: var(--bg);
  font-size: 0.84rem;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 100px;
  z-index: 60;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
