/* ── The Reub Goldberg ─────────────────────────────────────────────── */
:root {
  --bg: #12141d;
  --panel: #1c1f2b;
  --panel-2: #242838;
  --ink: #eef0f7;
  --ink-dim: #9aa1b5;
  --accent: #ffb32b;
  --accent-2: #ff7847;
  --green: #3ecf6e;
  --line: #323749;
  --radius: 14px;
  font-size: 16px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", "Trebuchet MS", system-ui, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}
#app { display: flex; flex-direction: column; height: 100vh; }

/* ── Top bar ── */
#topbar {
  display: flex; align-items: center; gap: 18px;
  padding: 10px 18px;
  background: linear-gradient(180deg, #232738, #1a1d29);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .gear { font-size: 30px; animation: spin 9s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.brand-text h1 {
  font-size: 20px; letter-spacing: 0.5px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  line-height: 1.1;
}
.brand-text .tagline { font-size: 11px; color: var(--ink-dim); letter-spacing: 1.5px; text-transform: uppercase; }
#level-title { flex: 1; text-align: center; font-size: 17px; }
#level-num {
  background: var(--accent); color: #201400; font-weight: 700;
  border-radius: 8px; padding: 2px 9px; margin-right: 8px; font-size: 14px;
}
#level-name { font-weight: 600; }
.controls { display: flex; gap: 8px; }

.btn {
  font: inherit; font-weight: 600; color: var(--ink);
  background: var(--panel-2); border: 1px solid var(--line);
  padding: 8px 16px; border-radius: 10px; cursor: pointer;
  transition: transform .08s, background .15s, box-shadow .15s;
}
.btn:hover { background: #2d3247; transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn.play {
  background: linear-gradient(180deg, #46d97a, #2cab58); color: #06230f;
  border-color: #2cab58; box-shadow: 0 3px 10px rgba(62, 207, 110, .35);
}
.btn.play:hover { background: linear-gradient(180deg, #55e689, #35b862); }
.btn.play.stop {
  background: linear-gradient(180deg, #ff6b5e, #d94437); color: #2b0703; border-color: #d94437;
  box-shadow: 0 3px 10px rgba(217, 68, 55, .35);
}
.btn.big { font-size: 20px; padding: 14px 34px; border-radius: 14px; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .4; cursor: default; transform: none; }

/* ── Main layout ── */
#main { flex: 1; display: flex; min-height: 0; padding: 14px; gap: 14px; }
#stage-wrap {
  flex: 1; position: relative; display: flex; flex-direction: column;
  align-items: center; justify-content: center; min-width: 0; min-height: 0;
  gap: 8px;
}
#stage {
  background: #0d0f16;
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0,0,0,.5), inset 0 0 0 1px var(--line);
  max-width: 100%; max-height: 100%;
  flex: 0 1 auto; min-height: 0;
  aspect-ratio: 10 / 7;
  touch-action: none;
  cursor: default;
}
#stage.grabbing { cursor: grabbing; }
#goal-banner {
  flex: 0 0 auto;
  background: linear-gradient(180deg, rgba(43, 37, 18, .95), rgba(28, 24, 13, .95));
  backdrop-filter: blur(6px);
  border: 1.5px solid rgba(255, 179, 43, .6); color: var(--ink);
  padding: 10px 24px; border-radius: 999px;
  font-size: 16.5px; font-weight: 600; letter-spacing: .2px;
  box-shadow: 0 4px 22px rgba(255, 179, 43, .18), 0 6px 24px rgba(0, 0, 0, .45);
  pointer-events: none; white-space: nowrap; max-width: 92%;
  overflow: hidden; text-overflow: ellipsis;
}
#goal-banner b { color: var(--accent); }

/* level intro splash */
#level-intro {
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center; pointer-events: none;
  background: rgba(16, 17, 26, .88); backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 179, 43, .5);
  border-radius: 20px; padding: 26px 48px;
  box-shadow: 0 18px 70px rgba(0, 0, 0, .6);
  animation: intro-in .45s cubic-bezier(.2, 1.6, .4, 1);
  transition: opacity .5s;
}
#level-intro.fading { opacity: 0; }
@keyframes intro-in {
  from { transform: translate(-50%, -50%) scale(.6); opacity: 0; }
}
#level-intro .intro-num {
  color: var(--accent); font-size: 13px; font-weight: 800;
  letter-spacing: 3px; text-transform: uppercase;
}
#level-intro .intro-name { font-size: 30px; font-weight: 700; margin: 4px 0 12px; }
#level-intro .intro-goal {
  font-size: 17px; color: #ffe1a6;
  background: rgba(255, 179, 43, .12);
  border: 1px solid rgba(255, 179, 43, .35);
  border-radius: 12px; padding: 9px 18px;
}
#level-intro .intro-goal b { color: var(--accent); }
#hint-pop {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: #2a2503; border: 1px solid #6b5d10; color: #ffe9a3;
  padding: 10px 18px; border-radius: 12px; font-size: 14px; max-width: 70%;
  box-shadow: 0 6px 20px rgba(0,0,0,.4);
}

/* ── Toolbox ── */
#toolbox {
  flex: 0 0 240px; display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden;
}
.toolbox-head { padding: 12px 14px 8px; border-bottom: 1px solid var(--line); }
.toolbox-head h2 { font-size: 15px; letter-spacing: 1px; text-transform: uppercase; color: var(--accent); }
.toolbox-tip { font-size: 11.5px; color: var(--ink-dim); }
#toolbox-items { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.tool-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; cursor: grab;
  transition: background .12s, border-color .12s;
  touch-action: none;   /* drag out of the bin without scrolling the page */
}
.tool-item:hover { background: #2e3349; border-color: #4a5170; }
.tool-item.empty { opacity: .35; cursor: default; }
.tool-item canvas { width: 54px; height: 40px; flex: 0 0 auto; }
.tool-meta { flex: 1; min-width: 0; }
.tool-name { font-size: 13.5px; font-weight: 600; }
.tool-desc { font-size: 11px; color: var(--ink-dim); line-height: 1.25; }
.tool-count {
  font-weight: 700; font-size: 14px; color: #131625;
  background: var(--accent); border-radius: 8px; padding: 2px 8px;
}
.tool-item.empty .tool-count { background: #454a5e; color: #999; }
.toolbox-help {
  padding: 10px 14px; border-top: 1px solid var(--line);
  font-size: 11.5px; color: var(--ink-dim); line-height: 1.7;
}
.toolbox-help b { color: var(--ink); }

/* ── floating part-editing toolbar ── */
#sel-tools {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 10px;
  background: rgba(22, 24, 35, .92); backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 179, 43, .5);
  border-radius: 999px; padding: 8px 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .5);
  animation: fadein .15s ease;
}
#sel-tools button {
  width: 46px; height: 46px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel-2);
  color: var(--ink); font-size: 22px; line-height: 1; cursor: pointer;
  transition: background .12s, transform .08s;
  touch-action: manipulation;
}
#sel-tools button:hover { background: #323952; }
#sel-tools button:active { transform: scale(.92); }
#sel-tools button:disabled { opacity: .3; }
#sel-tools button.danger { color: #ff8177; border-color: #6b2f2a; }

/* ── Overlays ── */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(8, 9, 14, .72); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  animation: fadein .2s ease;
}
@keyframes fadein { from { opacity: 0; } }
.hidden { display: none !important; }
.panel {
  background: linear-gradient(180deg, #232738, #1a1d29);
  border: 1px solid #3a4058; border-radius: 18px;
  padding: 28px 32px; max-width: 640px; width: min(92vw, 640px);
  box-shadow: 0 24px 80px rgba(0,0,0,.6);
  max-height: 88vh; overflow-y: auto;
}
.panel.wide { max-width: 860px; width: min(94vw, 860px); }
.panel h2 { margin-bottom: 16px; font-size: 22px; }
.panel-foot { margin-top: 20px; display: flex; align-items: center; gap: 12px; justify-content: space-between; }
.panel-foot.center { justify-content: center; }
#progress-note { color: var(--ink-dim); font-size: 13px; }

/* Level grid */
#level-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 8px; }
.lvl-tile {
  aspect-ratio: 1; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--ink);
  font: inherit; font-weight: 700; font-size: 15px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 2px;
  transition: transform .08s, background .12s;
}
.lvl-tile:hover:not(.locked) { transform: scale(1.08); background: #323952; }
.lvl-tile.done { background: linear-gradient(180deg, #2b5e3c, #1f4a2e); border-color: #3ecf6e; }
.lvl-tile.done::after { content: "★"; font-size: 11px; color: var(--accent); }
.lvl-tile.locked { opacity: .35; cursor: default; }
.lvl-tile.current { outline: 2px solid var(--accent); }

/* Win panel */
.win-panel { text-align: center; }
.win-stamp {
  display: inline-block; font-size: 34px; font-weight: 800; letter-spacing: 2px;
  color: var(--green); border: 4px solid var(--green); border-radius: 12px;
  padding: 4px 22px; transform: rotate(-4deg); margin-bottom: 14px;
  animation: stamp .4s cubic-bezier(.2, 2.4, .4, 1);
}
@keyframes stamp { from { transform: rotate(-4deg) scale(2.4); opacity: 0; } }
#win-sub { color: var(--ink-dim); margin-top: 6px; }

/* Title panel */
.title-panel { text-align: center; }
.title-gears { font-size: 44px; margin-bottom: 6px; }
.title-gears span { display: inline-block; animation: spin 8s linear infinite; }
.title-gears .g2 { animation-direction: reverse; animation-duration: 5s; font-size: 30px; }
.title-gears .g3 { animation: none; }
.big-title {
  font-size: 44px; line-height: 1.05; margin: 4px 0 2px;
  background: linear-gradient(90deg, #ffd34d, #ff7847);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.subtitle { color: var(--ink-dim); letter-spacing: 3px; text-transform: uppercase; font-size: 13px; margin-bottom: 16px; }
.title-blurb { color: var(--ink-dim); font-size: 14.5px; line-height: 1.6; max-width: 460px; margin: 0 auto; }

/* ══ Mobile & small screens ═══════════════════════════════ */
@media (max-width: 900px), (max-height: 560px) {
  #topbar { padding: 6px 10px; gap: 8px; }
  .brand .gear { font-size: 22px; }
  .brand-text h1 { font-size: 15px; }
  .brand-text .tagline { display: none; }
  #level-title { font-size: 13px; }
  #level-name { display: inline-block; max-width: 26vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; }
  .btn { padding: 8px 10px; }
  .btn-label { display: none; }
  #main { padding: 8px; gap: 8px; }
  #goal-banner { font-size: 13px; padding: 7px 14px; max-width: 96%; }
  #level-intro { padding: 18px 22px; width: 86%; }
  #level-intro .intro-name { font-size: 22px; }
  #level-intro .intro-goal { font-size: 14px; }
  #level-grid { grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); }
  .panel { padding: 18px 16px; }
}

/* portrait phones/tablets: stack the parts bin under the machine */
@media (max-width: 820px) and (orientation: portrait) {
  #main { flex-direction: column; min-height: 0; justify-content: flex-start; }
  #stage-wrap { flex: 0 1 auto; min-height: 0; justify-content: flex-start; gap: 6px; }
  #stage { max-height: 100%; }
  #toolbox {
    flex: 0 0 auto; width: 100%;
    flex-direction: row; align-items: stretch;
  }
  .toolbox-head { display: none; }
  #toolbox-items {
    flex-direction: row; overflow-x: auto; overflow-y: hidden;
    padding: 8px; gap: 8px;
    -webkit-overflow-scrolling: touch;
  }
  .tool-item { flex: 0 0 auto; flex-direction: column; gap: 4px; padding: 6px 8px; width: 92px; text-align: center; }
  .tool-item canvas { width: 54px; height: 40px; }
  .tool-desc { display: none; }
  .tool-name { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 84px; }
  .tool-count { font-size: 12px; padding: 1px 7px; }
  .toolbox-help { display: none; }
  #sel-tools { bottom: 8px; }
  #hint-pop { max-width: 92%; font-size: 13px; white-space: normal; }
}

/* small landscape (phones on their side): keep the sidebar, slim it down */
@media (max-height: 560px) and (orientation: landscape) {
  #toolbox { flex-basis: 168px; }
  .tool-item { gap: 6px; padding: 6px; }
  .tool-item canvas { width: 40px; height: 30px; }
  .tool-desc { display: none; }
  .tool-name { font-size: 11.5px; }
  .toolbox-help { display: none; }
  .toolbox-head h2 { font-size: 12px; }
  .toolbox-tip { display: none; }
  #sel-tools { bottom: 6px; padding: 5px 8px; gap: 7px; }
  #sel-tools button { width: 40px; height: 40px; font-size: 19px; }
}

/* very narrow: gear only, no wordmark */
@media (max-width: 520px) {
  .brand-text { display: none; }
  #level-title { text-align: left; }
  .controls { gap: 5px; }
  .btn { padding: 7px 9px; font-size: 14px; }
}

/* touch devices: swap the keyboard help for touch help */
@media (pointer: coarse) {
  .help-keys { display: none !important; }
}
@media (pointer: fine) {
  .help-touch { display: none !important; }
}
