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

:root {
  --bg: #0c0a08;
  --bg-2: #12100d;
  --panel: #151210;
  --panel-2: #1b1714;
  --line: rgba(255,255,255,0.08);
  --line-bright: rgba(255,255,255,0.18);
  --fg: #ededed;
  --fg-dim: #8a857f;
  --fg-faint: #4a453f;

  --accent: #d9925a;
  --accent-dim: #b87640;
  --accent-warm: #e5a570;

  --fam-igneous: #c25a3a;
  --fam-sedimentary: #b89068;
  --fam-metamorphic: #8a7a9a;
}

html, body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Tajawal', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-weight: 300;
}

body::before {
  content: "";
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background:
    radial-gradient(ellipse 70% 50% at 50% 100%, rgba(194,90,58,0.06) 0%, transparent 70%),
    radial-gradient(ellipse 60% 60% at 20% 10%, rgba(184,144,104,0.04) 0%, transparent 70%);
}

.app {
  position: relative; z-index: 1;
  display: grid;
  grid-template-columns: 280px 1fr 340px;
  grid-template-rows: 56px 1fr;
  grid-template-areas:
    "top   top    top"
    "left  stage  right";
  height: 100vh;
  width: 100vw;
}

/* ---------- TOP ---------- */
.topbar {
  grid-area: top;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1.25rem;
  gap: 1rem;
}
.back {
  color: var(--fg-dim); text-decoration: none; font-size: 0.85rem;
  letter-spacing: 0.02em; transition: color 0.2s;
}
.back:hover { color: var(--fg); }

.brand { display: flex; align-items: baseline; gap: 0.6rem; }
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff 0%, var(--accent) 50%, #5c2a14 100%);
  box-shadow: 0 0 10px rgba(217,146,90,0.5);
  align-self: center;
}
.brand-title { font-size: 0.95rem; font-weight: 500; }
.brand-sub { font-size: 0.7rem; color: var(--fg-dim); letter-spacing: 0.15em; }

.model-note {
  font-size: 0.72rem;
  color: var(--fg-dim);
  letter-spacing: 0.05em;
}

/* ---------- PANELS ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 1rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}
.panel-left { grid-area: left; border-top: 0; border-right: 0; border-bottom: 0; }
.panel-right { grid-area: right; border-top: 0; border-left: 0; border-bottom: 0; }

.section { display: flex; flex-direction: column; gap: 0.6rem; }
.section-head {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 0.72rem;
  letter-spacing: 0.15em;
  color: var(--fg-dim);
  text-transform: uppercase;
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.4rem;
}
.section-sub { font-size: 0.65rem; color: var(--fg-faint); letter-spacing: 0.1em; }

/* ---------- TREE ---------- */
.tree {
  display: flex; flex-direction: column;
  gap: 0.1rem;
  font-size: 0.85rem;
}
.tree-family { display: flex; flex-direction: column; }
.tree-fam-head,
.tree-sub-head,
.tree-leaf {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  background: transparent;
  color: inherit;
  font-family: inherit;
  text-align: start;
  width: 100%;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.tree-fam-head {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--fg);
}
.tree-sub-head {
  padding-inline-start: 1.6rem;
  font-size: 0.8rem;
  color: var(--fg-dim);
}
.tree-leaf {
  padding-inline-start: 2.8rem;
  font-size: 0.78rem;
  color: var(--fg-dim);
}
.tree-fam-head:hover,
.tree-sub-head:hover,
.tree-leaf:hover {
  background: rgba(217,146,90,0.06);
  color: var(--fg);
}
.tree-fam-head.active {
  background: rgba(217,146,90,0.12);
  border-color: var(--accent-dim);
  color: var(--accent);
}
.tree-sub-head.active {
  background: rgba(217,146,90,0.10);
  color: var(--accent);
}
.tree-leaf.active {
  color: var(--accent);
  background: rgba(217,146,90,0.10);
}
.tree-caret {
  display: inline-flex;
  width: 0.85rem;
  justify-content: center;
  color: var(--fg-faint);
  font-size: 0.75rem;
  transition: transform 0.2s;
}
.tree-caret.open { transform: rotate(90deg); }
html[dir="rtl"] .tree-caret { transform: rotate(180deg); }
html[dir="rtl"] .tree-caret.open { transform: rotate(90deg); }
.tree-fam-dot {
  width: 8px; height: 8px; border-radius: 50%;
  flex: 0 0 auto;
}
.tree-fam-dot.ig { background: var(--fam-igneous); }
.tree-fam-dot.sd { background: var(--fam-sedimentary); }
.tree-fam-dot.mt { background: var(--fam-metamorphic); }
.tree-fam-en,
.tree-sub-en,
.tree-leaf-en {
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: 0.68rem;
  color: var(--fg-faint);
  letter-spacing: 0.05em;
  margin-inline-start: auto;
}
.tree-children { display: flex; flex-direction: column; gap: 0.08rem; }
.tree-children.collapsed { display: none; }

.tree-reset {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  padding: 0.45rem 0.6rem;
  font-family: inherit;
  font-size: 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
  margin-top: 0.3rem;
}
.tree-reset:hover { color: var(--fg); border-color: var(--line-bright); }

/* ---------- LEGEND ---------- */
.legend { margin-top: auto; }
.legend-row {
  display: flex; align-items: flex-start; gap: 0.6rem;
  font-size: 0.78rem;
  color: var(--fg-dim);
  line-height: 1.6;
}
.legend-row strong { color: var(--fg); font-weight: 500; }
.legend-icon {
  flex: 0 0 auto;
  width: 1.4rem; height: 1.4rem;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--accent);
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: 0.85rem;
}
.legend-note {
  font-size: 0.68rem;
  color: var(--fg-faint);
  margin-top: 0.4rem;
  line-height: 1.7;
}

/* ---------- STAGE ---------- */
.stage {
  grid-area: stage;
  overflow-y: auto;
  padding: 1.25rem 1.5rem 2rem;
  display: flex; flex-direction: column; gap: 1rem;
}
.stage-head {
  display: flex; align-items: baseline; gap: 0.75rem;
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--line);
}
.stage-title {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--fg);
}
.stage-count {
  font-size: 0.72rem;
  color: var(--fg-faint);
  letter-spacing: 0.1em;
  font-variant-numeric: tabular-nums;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.8rem;
}
.rock-card {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.2s, box-shadow 0.2s;
  display: flex; flex-direction: column;
}
.rock-card:hover {
  border-color: var(--accent-dim);
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
}
.rock-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent-dim), 0 6px 18px rgba(0,0,0,0.5);
}
.rock-card-swatch {
  width: 100%;
  aspect-ratio: 16 / 10;
  display: block;
}
.rock-card-body {
  padding: 0.55rem 0.7rem 0.7rem;
  display: flex; flex-direction: column; gap: 0.15rem;
}
.rock-card-name {
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--fg);
}
.rock-card-en {
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: 0.68rem;
  color: var(--fg-faint);
  letter-spacing: 0.05em;
}
.rock-card-fam {
  font-size: 0.66rem;
  color: var(--fg-dim);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-top: 0.2rem;
}
.rock-card-fam::before {
  content: "";
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  margin-inline-end: 0.35rem;
  vertical-align: middle;
}
.rock-card-fam[data-fam="igneous"]::before { background: var(--fam-igneous); }
.rock-card-fam[data-fam="sedimentary"]::before { background: var(--fam-sedimentary); }
.rock-card-fam[data-fam="metamorphic"]::before { background: var(--fam-metamorphic); }

.gallery-empty {
  grid-column: 1 / -1;
  color: var(--fg-faint);
  font-size: 0.85rem;
  padding: 2rem 0;
  text-align: center;
}

/* ---------- DETAIL CARD ---------- */
.detail-card {
  display: flex; flex-direction: column; gap: 0.8rem;
  font-size: 0.85rem;
  line-height: 1.7;
}
.detail-swatch {
  width: 100%;
  aspect-ratio: 16 / 10;
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg-2);
  box-shadow: 0 4px 14px rgba(0,0,0,0.5);
}
.detail-swatch svg { width: 100%; height: 100%; display: block; }

.detail-title {
  font-size: 1.1rem;
  font-weight: 500;
  color: var(--fg);
}
.detail-en {
  font-size: 0.75rem;
  color: var(--fg-dim);
  letter-spacing: 0.08em;
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
}
.detail-fam-line {
  display: flex; align-items: center; gap: 0.45rem;
  font-size: 0.72rem;
  color: var(--fg-dim);
  letter-spacing: 0.05em;
}
.detail-fam-dot {
  width: 9px; height: 9px; border-radius: 50%;
  display: inline-block;
}
.detail-fam-dot.ig { background: var(--fam-igneous); }
.detail-fam-dot.sd { background: var(--fam-sedimentary); }
.detail-fam-dot.mt { background: var(--fam-metamorphic); }

.detail-row {
  display: flex; flex-direction: column; gap: 0.25rem;
  padding-top: 0.55rem;
  border-top: 1px solid var(--line);
}
.detail-row-label {
  font-size: 0.68rem;
  color: var(--fg-dim);
  letter-spacing: 0.12em;
}
.detail-row-body {
  color: var(--fg);
  font-size: 0.88rem;
  line-height: 1.8;
}

.detail-fact {
  position: relative;
  background: linear-gradient(135deg, rgba(217,146,90,0.10) 0%, rgba(217,146,90,0.04) 100%);
  border: 1px solid rgba(217,146,90,0.45);
  border-radius: 6px;
  padding: 0.7rem 0.85rem 0.8rem;
  display: flex; flex-direction: column; gap: 0.3rem;
  box-shadow: 0 1px 0 rgba(217,146,90,0.08) inset;
}
.detail-fact::before {
  content: "";
  position: absolute;
  inset-inline-end: -1px;
  top: -1px; bottom: -1px;
  width: 3px;
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-dim) 100%);
  border-radius: 0 6px 6px 0;
}
.fact-label {
  font-size: 0.68rem;
  color: var(--accent);
  font-weight: 500;
  letter-spacing: 0.12em;
}
.fact-body {
  font-size: 0.92rem;
  line-height: 1.75;
  color: var(--fg);
}

.detail-athar {
  padding-top: 0.55rem;
  border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 0.35rem;
}
.athar-head {
  font-size: 0.68rem;
  color: var(--fg-dim);
  letter-spacing: 0.12em;
}
.athar-body {
  display: flex; flex-wrap: wrap; gap: 0.35rem;
}
.athar-chip {
  font-size: 0.72rem;
  color: var(--fg-dim);
  border: 1px solid var(--line);
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: var(--panel-2);
}

/* ---------- WELCOME (EMPTY STATE) ---------- */
.welcome {
  display: flex; flex-direction: column; gap: 1rem;
  font-size: 0.85rem;
  line-height: 1.7;
  padding: 0.2rem 0;
}
.welcome-title {
  font-size: 1rem;
  font-weight: 500;
  color: var(--fg);
}
.welcome-sub {
  font-size: 0.75rem;
  color: var(--fg-dim);
  line-height: 1.75;
}
.welcome-list {
  list-style: none;
  display: flex; flex-direction: column; gap: 0.55rem;
  padding: 0.6rem 0.2rem;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.welcome-list li {
  display: flex; gap: 0.6rem; align-items: flex-start;
  font-size: 0.83rem;
  color: var(--fg);
  line-height: 1.6;
}
.wl-icon {
  flex: 0 0 auto;
  width: 1.5rem; height: 1.5rem;
  border-radius: 4px;
  background: rgba(217,146,90,0.12);
  color: var(--accent);
  font-size: 0.85rem;
  display: inline-flex; align-items: center; justify-content: center;
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  margin-top: 0.05rem;
}
.welcome-start {
  background: linear-gradient(180deg, rgba(217,146,90,0.16), rgba(217,146,90,0.06));
  border: 1px solid var(--accent-dim);
  color: var(--accent);
  padding: 0.7rem 0.9rem;
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex; flex-direction: column; align-items: stretch; gap: 0.15rem;
  text-align: start;
}
.welcome-start:hover {
  background: linear-gradient(180deg, rgba(217,146,90,0.24), rgba(217,146,90,0.10));
  color: var(--accent-warm);
}
.ws-sub {
  font-size: 0.7rem;
  color: var(--fg-dim);
  font-weight: 300;
}
.welcome-start:hover .ws-sub { color: #c89a78; }

/* ---------- RESPONSIVE ---------- */
@media (max-width: 960px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: 56px auto auto auto;
    grid-template-areas:
      "top"
      "left"
      "stage"
      "right";
    height: auto;
    min-height: 100vh;
  }
  html, body { height: auto; overflow: auto; }
  .panel-left, .panel-right {
    max-height: none;
    border: 0;
    border-bottom: 1px solid var(--line);
  }
  .panel-right { border-top: 1px solid var(--line); border-bottom: 0; }
  .stage { overflow: visible; }
}

@media (max-width: 480px) {
  .topbar { padding: 0 0.8rem; gap: 0.5rem; }
  .brand-sub { display: none; }
  .model-note { font-size: 0.62rem; }
  .stage { padding: 1rem 0.85rem 2rem; }
  .gallery {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.6rem;
  }
  .rock-card-name { font-size: 0.82rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
