:root {
  --bg: #f3f1ec;
  --panel: #ffffff;
  --panel-2: #faf8f3;
  --ink: #1a1a1a;
  --ink-dim: #6b6b6b;
  --line: #d9d4ca;
  --line-strong: #b8b1a3;
  --accent: #d23434;
  --accent-2: #2e5fbf;
  --asphalt: #b9b4a8;
  --asphalt-2: #a8a294;
  --line-paint: #ffffff;
  --grass: #9eb886;
  --planter: #c7b89a;
  --shadow: rgba(0,0,0,0.18);
  --hl: rgba(210, 52, 52, 0.18);
  --grid: rgba(0,0,0,0.05);
}

[data-theme="dark"] {
  --bg: #14161a;
  --panel: #1c1f25;
  --panel-2: #232730;
  --ink: #ececec;
  --ink-dim: #9aa0ab;
  --line: #2f343f;
  --line-strong: #444b58;
  --accent: #ff5a5a;
  --accent-2: #6aa3ff;
  --asphalt: #3a3f49;
  --asphalt-2: #2e333c;
  --line-paint: #d8d4c8;
  --grass: #3e5a3a;
  --planter: #5a4d3a;
  --shadow: rgba(0,0,0,0.5);
  --hl: rgba(255, 90, 90, 0.22);
  --grid: rgba(255,255,255,0.04);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--ink);
  font-family: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 56px 1fr;
  height: 100vh;
  width: 100vw;
}

.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; letter-spacing: 0.2px;
  font-size: 14px;
}
.brand .mark {
  width: 22px; height: 22px; border-radius: 5px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center;
  color: white; font-size: 13px;
}
.brand small { color: var(--ink-dim); font-weight: 500; }

.seg {
  display: inline-flex; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 3px;
}
.seg button {
  appearance: none; border: 0; background: transparent;
  color: var(--ink-dim); padding: 6px 12px; border-radius: 6px;
  font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
  letter-spacing: 0.2px;
}
.seg button.on {
  background: var(--ink); color: var(--bg);
}

.spacer { flex: 1; }

.btn {
  appearance: none; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--ink);
  padding: 8px 12px; border-radius: 8px;
  font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { border-color: var(--line-strong); }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--ink); color: var(--bg); border-color: var(--ink);
}
.btn.icon { padding: 8px; }
.btn svg { width: 14px; height: 14px; }
.kbd {
  font-family: ui-monospace, Menlo, monospace; font-size: 10px;
  padding: 1px 5px; border-radius: 4px;
  background: var(--bg); border: 1px solid var(--line);
  color: var(--ink-dim);
}

/* Sidebar */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  min-height: 0;
}
.sidebar h3 {
  margin: 0; padding: 14px 14px 6px;
  font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 1.2px;
  color: var(--ink-dim);
}
.sidebar .search {
  margin: 6px 12px 8px;
  display: flex; align-items: center; gap: 6px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 10px;
}
.sidebar .search input {
  background: transparent; border: 0; outline: 0;
  color: var(--ink); font: inherit; font-size: 12px; width: 100%;
}
.sidebar .scroll {
  overflow-y: auto; padding: 0 8px 14px;
  flex: 1;
}
.cat {
  padding: 10px 6px 4px;
  font-size: 10px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 1px;
  color: var(--ink-dim);
}
.palette {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.item {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  aspect-ratio: 1;
  display: grid; place-items: center;
  cursor: grab;
  position: relative;
  transition: border-color 0.12s, transform 0.05s, background 0.12s;
}
.item:hover {
  border-color: var(--line-strong);
  background: var(--bg);
}
.item:active { cursor: grabbing; }
.item canvas { width: 78%; height: 78%; }
.item .lbl {
  position: absolute; bottom: 3px; left: 0; right: 0;
  font-size: 9px; color: var(--ink-dim);
  text-align: center; pointer-events: none;
  font-weight: 500;
}

/* Canvas wrap */
.stage {
  position: relative; min-width: 0; min-height: 0;
  background: var(--bg);
  overflow: hidden;
}
#canvas {
  display: block; width: 100%; height: 100%;
  background:
    linear-gradient(var(--grid) 1px, transparent 1px) 0 0 / 40px 40px,
    linear-gradient(90deg, var(--grid) 1px, transparent 1px) 0 0 / 40px 40px;
  cursor: default;
}
#canvas.placing { cursor: copy; }
#canvas.dragging { cursor: grabbing; }

/* Floating panels */
.float {
  position: absolute;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 8px 24px var(--shadow);
  padding: 8px;
  z-index: 10;
}
.context {
  display: none;
  bottom: 14px; left: 50%; transform: translateX(-50%);
  flex-direction: row; gap: 4px; align-items: center;
}
.context.show { display: flex; }
.context .sep { width: 1px; height: 20px; background: var(--line); margin: 0 4px; }
.context .info { font-size: 11px; color: var(--ink-dim); padding: 0 6px; min-width: 90px; }
.context input[type="range"] { width: 100px; }

.legend {
  bottom: 14px; right: 14px;
  font-size: 11px; color: var(--ink-dim);
  padding: 10px 12px; line-height: 1.7;
}
.legend b { color: var(--ink); }

.zoomctl {
  top: 14px; right: 14px;
  display: flex; gap: 4px;
}

/* 3D modal */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: none; align-items: stretch; justify-content: stretch;
  z-index: 100;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}
.modal.show { display: flex; }
.modal-inner {
  position: relative; flex: 1; background: var(--bg);
  display: flex; flex-direction: column;
}
.modal-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; background: var(--panel);
  border-bottom: 1px solid var(--line);
}
#three-host { flex: 1; position: relative; }
#three-host canvas { display: block; width: 100%; height: 100%; }
.crosshair {
  position: absolute; left: 50%; top: 50%;
  width: 6px; height: 6px; margin: -3px;
  border-radius: 50%; background: white;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.5);
  pointer-events: none; opacity: 0; transition: opacity 0.2s;
}
.crosshair.show { opacity: 0.85; }
.hint {
  position: absolute; left: 50%; bottom: 30px;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.7); color: white;
  padding: 10px 16px; border-radius: 10px;
  font-size: 13px; line-height: 1.5;
  pointer-events: none;
  text-align: center;
}
.hint .kbd { background: rgba(255,255,255,0.15); color: white; border-color: rgba(255,255,255,0.2); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg);
  padding: 8px 14px; border-radius: 20px;
  font-size: 12px; font-weight: 600;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Confirm modal */
.confirm-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55);
  display: none; align-items: center; justify-content: center;
  z-index: 300;
}
.confirm-mask.show { display: flex; }
.confirm-box {
  background: var(--panel); color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 60px var(--shadow);
  padding: 22px;
  min-width: 320px; max-width: 440px;
}
.confirm-box h4 { margin: 0 0 8px; font-size: 15px; font-weight: 700; }
.confirm-box p { margin: 0 0 18px; font-size: 13px; color: var(--ink-dim); line-height: 1.5; }
.confirm-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* Mobile sidebar */
.sidebar-toggle {
  display: none;
}
@media (max-width: 760px) {
  .app {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: absolute;
    top: 56px; bottom: 0; left: 0;
    width: 78%; max-width: 320px;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 6px 0 24px var(--shadow);
  }
  .sidebar.open { transform: none; }
  .sidebar-toggle { display: inline-flex; }
  .topbar { gap: 6px; padding: 0 10px; overflow-x: auto; }
  .topbar .brand small { display: none; }
  .topbar .btn { padding: 7px 10px; font-size: 11px; white-space: nowrap; }
  .seg button { padding: 5px 9px; font-size: 11px; }
  .legend { display: none; }
  .context { left: 8px; right: 8px; transform: none; bottom: 8px; flex-wrap: wrap; }
  .context input[type=range] { width: 80px; }
}

/* Touch look area (covers the 3D view; sits BEHIND joystick/buttons via z-index) */
.look-area {
  position: absolute; inset: 0;
  display: none;
  touch-action: none;
  z-index: 1;
}
.look-area.show { display: block; }

/* Touch joystick */
.joystick {
  position: absolute;
  width: 130px; height: 130px;
  border-radius: 50%;
  background: rgba(255,255,255,0.1);
  border: 2px solid rgba(255,255,255,0.3);
  touch-action: none;
  display: none;
  z-index: 5;
}
.joystick.show { display: block; }
.joystick .stick {
  position: absolute; left: 50%; top: 50%;
  width: 56px; height: 56px;
  margin: -28px;
  border-radius: 50%;
  background: rgba(255,255,255,0.85);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  pointer-events: none;
}
.joystick.left { left: 18px; bottom: 18px; }
.joystick.right { right: 18px; bottom: 18px; }
.joystick .lbl {
  position: absolute; left: 0; right: 0; top: -22px;
  text-align: center; color: white; font-size: 11px; font-weight: 600;
  text-shadow: 0 1px 2px rgba(0,0,0,0.7);
  pointer-events: none;
}

/* Touch action buttons (run / jump) */
.touch-btns {
  position: absolute;
  right: 18px; bottom: 18px;
  display: none;
  flex-direction: column;
  gap: 10px;
  z-index: 5;
  touch-action: none;
}
.touch-btns.show { display: flex; }
.tbtn {
  appearance: none;
  width: 78px; height: 78px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.45);
  background: rgba(255,255,255,0.18);
  color: white;
  font-weight: 800;
  font-size: 11px;
  letter-spacing: 0.6px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  cursor: pointer;
  backdrop-filter: blur(4px);
}
.tbtn.active, .tbtn:active {
  background: rgba(255,255,255,0.42);
  transform: scale(0.94);
}
#btnJump { background: rgba(110, 170, 255, 0.28); }
#btnRun  { background: rgba(255, 180, 90, 0.28); }
#btnRun.active {
  background: rgba(255, 180, 90, 0.95);
  border-color: #ffd47a;
  color: #2a1a05;
  text-shadow: none;
  box-shadow:
    0 0 0 4px rgba(255,180,90,0.28),
    0 0 22px rgba(255,180,90,0.85);
  animation: runPulse 1.2s ease-in-out infinite;
}
@keyframes runPulse {
  50% { box-shadow:
    0 0 0 6px rgba(255,180,90,0.18),
    0 0 30px rgba(255,200,120,1.0); }
}

/* Mini 3D toolbar on small */
@media (max-width: 760px) {
  .modal-bar { gap: 6px; padding: 8px; flex-wrap: wrap; }
  .modal-bar strong { font-size: 12px; }
}

/* Drag ghost */
.drag-ghost {
  position: fixed; pointer-events: none; z-index: 999;
  width: 72px; height: 72px;
  display: grid; place-items: center;
  opacity: 0.85;
  transform: translate(-50%, -50%);
}
.drag-ghost canvas { width: 100%; height: 100%; }

/* Scrollbar slim */
.scroll::-webkit-scrollbar { width: 8px; }
.scroll::-webkit-scrollbar-thumb { background: var(--line); border-radius: 8px; }
.scroll::-webkit-scrollbar-track { background: transparent; }

/* ───────────────────────── INTRO SCREEN ───────────────────────── */
.intro {
  position: fixed; inset: 0;
  z-index: 1000;
  background: #0e1116;
  overflow: hidden;
  animation: introIn 0.4s ease both;
}
.intro.out {
  animation: introOut 0.55s ease both;
  pointer-events: none;
}
@keyframes introIn  { from { opacity: 0; } to { opacity: 1; } }
@keyframes introOut { to   { opacity: 0; transform: scale(1.02); } }

.intro-canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
  filter: saturate(1.05);
}
.intro-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  /* glassy diffuse */
  background:
    radial-gradient(120% 80% at 50% 35%, rgba(20,30,50,0.20), rgba(10,15,25,0.78) 70%),
    linear-gradient(180deg, rgba(0,0,0,0.15), rgba(0,0,0,0.55));
  backdrop-filter: blur(8px) saturate(1.1);
  -webkit-backdrop-filter: blur(8px) saturate(1.1);
  padding: 24px;
}
.intro-card {
  text-align: center;
  color: white;
  max-width: 560px;
  animation: introCardIn 0.7s 0.15s ease both;
}
@keyframes introCardIn {
  from { opacity: 0; transform: translateY(14px) scale(0.985); }
  to   { opacity: 1; transform: none; }
}
.intro-badge {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 12px; letter-spacing: 1.6px; font-weight: 700;
  text-transform: uppercase;
  color: rgba(255,255,255,0.85);
  padding: 7px 14px;
  border-radius: 999px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.18);
  margin-bottom: 22px;
  backdrop-filter: blur(6px);
}
.intro-badge .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #ff5a5a;
  box-shadow: 0 0 10px #ff5a5a, 0 0 0 3px rgba(255,90,90,0.18);
  animation: introDot 1.6s ease-in-out infinite;
}
@keyframes introDot { 50% { opacity: 0.45; transform: scale(0.85); } }

.intro-title {
  margin: 0;
  font-size: clamp(46px, 11vw, 112px);
  line-height: 1.05;
  letter-spacing: -0.03em;
  font-weight: 800;
  display: flex; flex-direction: column;
  text-shadow: 0 8px 40px rgba(0,0,0,0.5);
}
.intro-title span {
  /* Padding so descenders (g, p) aren't clipped by background-clip:text */
  padding-bottom: 0.08em;
}
.intro-title span:first-child {
  background: linear-gradient(95deg, #ffffff 30%, #ffd0d0 100%);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.intro-title span:last-child {
  background: linear-gradient(95deg, #ff7a7a 0%, #ff5a5a 70%, #d23434 100%);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
.intro-sub {
  margin: 22px auto 30px;
  max-width: 460px;
  font-size: 15px; line-height: 1.55;
  color: rgba(255,255,255,0.78);
  font-weight: 500;
}
.intro-btn {
  appearance: none; cursor: pointer;
  border: 0;
  background: linear-gradient(180deg, #ffffff 0%, #ececec 100%);
  color: #14161a;
  font: inherit; font-weight: 800; font-size: 15px;
  padding: 14px 32px;
  border-radius: 999px;
  letter-spacing: 0.4px;
  box-shadow: 0 12px 36px rgba(255,255,255,0.18), 0 2px 0 rgba(0,0,0,0.2) inset;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.intro-btn:hover { transform: translateY(-1px); box-shadow: 0 18px 44px rgba(255,255,255,0.24); }
.intro-btn:active { transform: translateY(1px); }

/* ───────────────────────── MOBILE PORTRAIT POLISH ───────────────────────── */
@media (max-width: 760px) {
  .topbar {
    height: auto;
    padding: 8px 8px;
    flex-wrap: wrap;
    row-gap: 6px;
  }
  .app { grid-template-rows: auto 1fr; }
  .brand { font-size: 13px; }
  .brand .mark { width: 20px; height: 20px; font-size: 11px; }
  .topbar .btn {
    padding: 6px 9px; font-size: 11px;
    border-radius: 7px;
  }
  .topbar .btn.icon { padding: 6px 8px; }
  .seg { padding: 2px; }
  .seg button { padding: 5px 8px; font-size: 10.5px; }

  /* compact: hide labels, keep icons-ish */
  #seedBtn::after { content: ""; }
  /* the spacer doesn't expand multi-line, drop it */
  .spacer { display: none; }
  .topbar > .btn, .topbar > .seg { flex: 0 0 auto; }

  /* place zoom controls on the LEFT so they don't clash with context bar */
  .zoomctl { top: 8px; right: 8px; }
  .legend { display: none; }

  .context {
    left: 8px; right: 8px; bottom: 8px;
    transform: none; flex-wrap: wrap;
    padding: 6px;
  }
  .context input[type=range] { flex: 1 1 90px; width: auto; min-width: 80px; }
  .context .info { flex: 1 1 100%; padding: 2px 4px 4px; min-width: 0; }
  .context .sep { display: none; }

  /* Smaller joystick / buttons on phones */
  .joystick { width: 110px; height: 110px; }
  .joystick .stick { width: 48px; height: 48px; margin: -24px; }
  .joystick.left { left: 14px; bottom: 14px; }
  .tbtn { width: 64px; height: 64px; font-size: 10.5px; }
  .touch-btns { right: 14px; bottom: 14px; gap: 8px; }

  /* hide controls hint on mobile in 3D */
  #hint { display: none !important; }

  .modal-bar { padding: 8px 10px; gap: 6px; }
  .modal-bar strong { font-size: 12px; flex: 1 1 100%; }
  .modal-bar .seg { order: 2; }
  .modal-bar .btn { order: 3; }
}

/* Very tall narrow phones */
@media (max-width: 420px) {
  .intro-sub { display: none; }
  .topbar { padding: 6px; gap: 4px; }
  .topbar .btn { padding: 5px 7px; }
}
