:root {
  --bg: #18201c;
  --panel: #263128;
  --panel2: #303b32;
  --line: #526052;
  --text: #f3f5e9;
  --muted: #c8d1bf;
  --accent: #ffd15a;
  --bad: #d95d4f;
  --cell: #78be78;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

button, select, input {
  font: inherit;
}

button {
  border: 1px solid #111;
  border-radius: 8px;
  background: #435348;
  color: var(--text);
  min-height: 42px;
  cursor: pointer;
}

button:active { transform: translateY(1px); }
button.selected { outline: 4px solid #050505; }

.app {
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
}

.topbar {
  display: grid;
  grid-template-columns: 82px 74px 1fr 54px;
  gap: 8px;
  align-items: center;
}

.primary {
  background: #6d5930;
  border-color: #e1bc55;
  color: #fff4c8;
  font-weight: 700;
}

.money {
  min-height: 44px;
  display: grid;
  place-items: center;
  border: 2px solid #151515;
  border-radius: 8px;
  background: #111815;
  color: var(--accent);
  font-size: 24px;
  font-weight: 800;
}

.iconBtn {
  font-size: 25px;
  background: #1f2924;
}

.gameArea {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(310px, 1fr) 300px;
  gap: 8px;
}

.gridWrap {
  align-self: start;
  position: relative;
  width: min(100%, calc(100svh - 180px));
  max-width: 720px;
  aspect-ratio: 1;
  margin: 0 auto;
}

.grid {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.movingLayer {
  position: absolute;
  inset: 0;
  z-index: 20;
  pointer-events: none;
}

.cell {
  position: relative;
  border: 2px solid #2d392e;
  background: var(--cell);
  border-radius: 5px;
  overflow: hidden;
  touch-action: manipulation;
}

.cell.locked {
  background: #233027;
}

.cell.locked::after {
  content: "🔒";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: clamp(18px, 5vw, 36px);
  color: #d9e1d2;
}

.priceTag {
  position: absolute;
  left: 50%;
  bottom: 6px;
  transform: translateX(-50%);
  padding: 2px 5px;
  border-radius: 5px;
  background: rgba(0,0,0,.7);
  color: #ffd15a;
  font-size: clamp(10px, 2.4vw, 15px);
  font-weight: 800;
  z-index: 2;
}

.cell.selected {
  outline: 4px solid #080808;
  z-index: 1;
}

.cell.conveyor::before {
  content: ">>> >>> >>>";
  position: absolute;
  left: -45%;
  top: 50%;
  z-index: 1;
  width: 190%;
  color: rgba(255, 225, 105, .9);
  font-size: clamp(12px, 3.6vw, 22px);
  font-weight: 900;
  line-height: 1;
  text-shadow: 1px 1px #000;
  animation: beltMove .7s linear infinite;
  pointer-events: none;
}

.cell.conveyorDir0::before { transform: translateY(-50%) rotate(-90deg); }
.cell.conveyorDir1::before { transform: translateY(-50%) rotate(0deg); }
.cell.conveyorDir2::before { transform: translateY(-50%) rotate(90deg); }
.cell.conveyorDir3::before { transform: translateY(-50%) rotate(180deg); }

@keyframes beltMove {
  from { margin-left: -18%; }
  to { margin-left: 18%; }
}

.tileImg {
  position: absolute;
  inset: 7%;
  width: 86%;
  height: 86%;
  object-fit: contain;
  image-rendering: auto;
  z-index: 2;
}

.resource {
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,.45);
  z-index: 3;
}

.movingLayer .resource {
  width: calc((100% - 24px) / 7 * .34);
  height: calc((100% - 24px) / 7 * .34);
  transform: translate(-50%, -50%);
  transition: left .11s linear, top .11s linear;
}

.dir {
  position: absolute;
  right: 4px;
  bottom: 1px;
  font-weight: 900;
  color: #ffe169;
  text-shadow: 1px 1px #000;
}

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  gap: 8px;
}

.hint, .info, .inventory, .recipes {
  border: 2px solid #111;
  border-radius: 8px;
  background: var(--panel);
  padding: 8px;
}

.hint {
  min-height: 42px;
  color: var(--muted);
}

.guide {
  border-color: #ffd15a;
  background: #3a3420;
  color: #fff1bd;
  font-weight: 700;
}

.guideDone {
  border-color: #74c783;
  background: #223826;
  color: #dff7df;
  font-weight: 700;
}

.guideFocus {
  outline: 4px solid #ffd15a;
  box-shadow: 0 0 0 3px rgba(0,0,0,.65) inset;
}

.inventory {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.invItem, .shopItem, .recipe {
  display: grid;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel2);
  padding: 7px;
}

.invItem {
  grid-template-columns: 34px 1fr;
  min-height: 48px;
}

.invIcon, .resIcon {
  width: 34px;
  height: 34px;
  border-radius: 5px;
  border: 1px solid rgba(0,0,0,.4);
}

.recipe .resIcon {
  background-color: transparent;
  border-color: transparent;
  box-shadow: none;
}

.blockIcon,
.itemTexture {
  position: relative;
  overflow: hidden;
  background-color: #667078;
  box-shadow: inset 4px 4px rgba(255,255,255,.18), inset -4px -4px rgba(0,0,0,.25);
}

.block-computer,
.item-13 {
  background:
    linear-gradient(#80d2ff 0 0) 50% 23% / 58% 38% no-repeat,
    linear-gradient(#1c344a 0 0) 50% 23% / 72% 52% no-repeat,
    linear-gradient(#7a8790 0 0) 50% 72% / 78% 20% no-repeat,
    #334456;
}

.block-screen,
.item-16 {
  background:
    linear-gradient(#68d7ff 0 0) 50% 38% / 78% 54% no-repeat,
    linear-gradient(#172b38 0 0) 50% 38% / 90% 68% no-repeat,
    linear-gradient(#67737a 0 0) 50% 84% / 52% 12% no-repeat,
    #2c3b44;
}

.item-6 {
  background:
    radial-gradient(circle at 35% 35%, #a8adb2 0 18%, transparent 19%),
    radial-gradient(circle at 65% 42%, #777d83 0 20%, transparent 21%),
    radial-gradient(circle at 47% 66%, #555b61 0 18%, transparent 19%),
    #858a90;
}

.item-7 {
  background:
    linear-gradient(90deg, #ffe08a 0 22%, #b57623 23% 45%, transparent 46%) 20% 30% / 42% 18% no-repeat,
    linear-gradient(90deg, #ffe08a 0 22%, #b57623 23% 45%, transparent 46%) 55% 55% / 42% 18% no-repeat,
    #5b4b31;
}

.item-9 {
  background:
    repeating-linear-gradient(90deg, #20262b 0 16%, #66717a 17% 23%),
    #384149;
}

.item-10 {
  background:
    linear-gradient(#1c2024 0 0) 50% 48% / 78% 20% no-repeat,
    linear-gradient(125deg, transparent 0 42%, #1c2024 43% 65%, transparent 66%) 56% 70% / 45% 42% no-repeat,
    #55606a;
}

.item-11 {
  background:
    radial-gradient(circle at 25% 50%, #f08c49 0 16%, transparent 17%),
    radial-gradient(circle at 75% 50%, #f08c49 0 16%, transparent 17%),
    linear-gradient(90deg, #cd4b2e, #f6a142, #cd4b2e);
}

.item-12 {
  background:
    radial-gradient(circle, #333 0 28%, #9ca7ad 29% 43%, transparent 44%),
    linear-gradient(#64717a 0 0) 50% 72% / 68% 18% no-repeat,
    #42515c;
}

.item-14 {
  background:
    linear-gradient(#e1e655 0 0) 50% 14% / 28% 10% no-repeat,
    linear-gradient(90deg, #4caf62 0 70%, #30383c 71%) 50% 55% / 74% 62% no-repeat,
    #1d2b22;
}

.item-15 {
  background:
    radial-gradient(circle at 50% 38%, #fff9a0 0 24%, #f0bf30 25% 38%, transparent 39%),
    linear-gradient(#586068 0 0) 50% 78% / 32% 30% no-repeat,
    #24303a;
}

.item-17 {
  background:
    linear-gradient(#9fe58d 0 0) 50% 50% / 62% 62% no-repeat,
    repeating-linear-gradient(90deg, transparent 0 11%, #d5ffd0 12% 15%),
    #2f7a4e;
}

.item-18 {
  background:
    linear-gradient(#3f2f75 0 0) 50% 50% / 82% 54% no-repeat,
    radial-gradient(circle at 70% 50%, #9f80ff 0 15%, transparent 16%),
    #252336;
}

.item-19 {
  background:
    repeating-linear-gradient(90deg, #1f6b3d 0 16%, #d5d86a 17% 22%),
    #2d9a59;
}

.item-20 {
  background:
    linear-gradient(#3aa06b 0 0) 50% 50% / 80% 78% no-repeat,
    radial-gradient(circle at 35% 34%, #d5d86a 0 8%, transparent 9%),
    radial-gradient(circle at 65% 62%, #d5d86a 0 8%, transparent 9%),
    #174b35;
}

.recipes {
  overflow-y: auto;
  max-height: min(46svh, 390px);
}

.recipe {
  grid-template-columns: 1fr auto 1fr auto 1fr;
  margin-bottom: 7px;
  min-height: 84px;
}

.recipe .resIcon {
  width: 42px;
  height: 42px;
}

.recipe.active {
  outline: 4px solid #050505;
}

.recipeParts {
  display: flex;
  gap: 4px;
  justify-content: center;
}

.recipeName {
  grid-column: 1 / -1;
  color: #f3f5e9;
  font-size: 13px;
  font-weight: 800;
  text-align: center;
  line-height: 1.1;
  margin-top: -2px;
}

.sign {
  font-size: 24px;
  font-weight: 900;
  color: #f6d66b;
}

.quickbar {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 6px;
}

.quickbar button {
  min-height: 58px;
  padding: 4px;
  font-size: 12px;
}

.quickbar img {
  display: block;
  width: 28px;
  height: 28px;
  object-fit: contain;
  margin: 0 auto 2px;
}

.dialog {
  width: min(720px, calc(100vw - 20px));
  max-height: calc(100svh - 24px);
  border: 2px solid #111;
  border-radius: 10px;
  background: #222c27;
  color: var(--text);
}

.dialog.small {
  width: min(430px, calc(100vw - 20px));
}

.dialog::backdrop {
  background: rgba(0,0,0,.55);
}

.dialogHead {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
}

.dialogHead h2 {
  margin: 0;
}

.plain {
  background: #344138;
  padding: 0 12px;
}

.shopList {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 8px;
}

.shopItem {
  grid-template-columns: 40px 1fr;
  text-align: left;
}

.shopItem:disabled {
  opacity: .55;
}

.shopItem img {
  width: 36px;
  height: 36px;
  object-fit: contain;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin: 10px 0;
}

.wide {
  width: 100%;
  margin-top: 8px;
}

.drillModes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.drillModes button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 4px;
}

.tradeGrid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 6px;
  margin-top: 8px;
}

.danger {
  background: #63342f;
}

@media (max-width: 820px) {
  .gameArea {
    grid-template-columns: 1fr;
  }

  .gridWrap {
    width: min(100%, calc(100svh - 350px));
    min-width: 304px;
  }

  .panel {
    min-height: 0;
  }

  .inventory {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .recipes {
    max-height: 220px;
  }

  .quickbar {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 430px) {
  .app { padding-left: 6px; padding-right: 6px; }
  .topbar { grid-template-columns: 66px 58px 1fr 44px; gap: 5px; }
  .money { font-size: 20px; }
  .gridWrap { min-width: 0; width: 100%; }
  .inventory { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
