:root {
  --bg: #0d100c;
  --panel: #171b16;
  --panel2: #21261f;
  --ink: #ece8dc;
  --muted: #9ca08f;
  --accent: #f0a83c;
  --accent-soft: rgba(240, 168, 60, 0.14);
  --teal: #59c3a8;
  --danger: #e06a4f;
  --border: rgba(236, 232, 220, 0.10);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  height: var(--tg-viewport-stable-height, 100dvh);
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color: var(--ink);
  background: radial-gradient(120% 90% at 50% 0%, #1d241b 0%, #0b0e0a 70%);
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}
[hidden] { display: none !important; }

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
}

/* ------------------------------------------------------------------ HUD */
.hud {
  flex: none;
  padding: 6px 10px 4px;
  background: linear-gradient(180deg, rgba(13, 16, 12, 0.94), rgba(13, 16, 12, 0.6));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.hud__row { display: flex; align-items: center; gap: 5px; height: 34px; }
.hud__brand {
  font-weight: 800; font-size: 12px; letter-spacing: 0.16em; color: var(--accent);
  white-space: nowrap;
}
.hud__seed { font-weight: 500; letter-spacing: 0; margin-left: 5px; font-size: 11px; color: var(--muted); }
.hud__spacer { flex: 1; }
.hud__power {
  display: flex; align-items: center; gap: 4px;
  font-size: 11px; color: var(--teal);
  background: rgba(89, 195, 168, 0.1);
  border: 1px solid rgba(89, 195, 168, 0.22);
  padding: 3px 8px; border-radius: 999px; white-space: nowrap;
}
.hud__power b { font-weight: 700; font-size: 11px; color: var(--ink); }
.hud__power.is-low { color: var(--danger); background: rgba(224, 106, 79, 0.1); border-color: rgba(224, 106, 79, 0.32); animation: lowPulse 1.6s ease-in-out infinite; }
@keyframes lowPulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }

.iconbtn {
  width: 34px; height: 34px; border-radius: 10px;
  background: transparent; border: none; color: var(--ink);
  display: grid; place-items: center; padding: 0;
  transition: background 0.15s, transform 0.12s var(--ease);
}
.iconbtn:active { background: rgba(255, 255, 255, 0.09); transform: scale(0.94); }

.hud__chips {
  display: flex; gap: 6px; overflow-x: auto;
  padding: 4px 0 2px; scrollbar-width: none;
}
.hud__chips::-webkit-scrollbar { display: none; }
.chip {
  display: flex; align-items: center; gap: 4px; flex: none;
  background: var(--panel2); border: 1px solid var(--border);
  border-radius: 999px; padding: 2px 8px 2px 3px;
  font-size: 11px; color: var(--muted); white-space: nowrap;
}
.chip b { color: var(--ink); font-weight: 700; font-size: 11px; }

/* ------------------------------------------------------------------ сцена */
.stage { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #090b08; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; z-index: 0; }
.stage::after {
  content: ""; position: absolute; inset: 0; z-index: 1; pointer-events: none;
  box-shadow: inset 0 0 90px 18px rgba(3, 5, 3, 0.55);
}

.fabs {
  position: absolute; right: 12px; bottom: 146px; z-index: 4;
  display: flex; flex-direction: column; gap: 10px;
}
.fab {
  width: 48px; height: 48px; border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(28, 33, 26, 0.92);
  backdrop-filter: blur(6px);
  color: var(--ink); display: grid; place-items: center;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.45);
  transition: transform 0.12s var(--ease), background 0.15s, color 0.15s;
}
.fab:active { transform: scale(0.93); background: var(--accent); color: #1d1406; }

.joystick { position: absolute; left: 16px; bottom: 16px; width: 120px; height: 120px; z-index: 4; touch-action: none; }
.joystick__ring {
  position: absolute; inset: 8px; border-radius: 50%;
  border: 1px solid rgba(236, 232, 220, 0.16);
  background: radial-gradient(circle at 50% 45%, rgba(240, 168, 60, 0.10), rgba(8, 10, 8, 0.4) 72%);
}
.joystick__knob {
  position: absolute; left: 50%; top: 50%; width: 46px; height: 46px;
  margin: -23px 0 0 -23px; border-radius: 50%;
  background: radial-gradient(circle at 38% 32%, #f6cc82, #cf8f2d);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  will-change: transform;
}

.hint {
  position: absolute; left: 12px; right: 12px; bottom: 150px; z-index: 4;
  background: linear-gradient(180deg, rgba(29, 34, 27, 0.97), rgba(19, 23, 17, 0.97));
  border: 1px solid rgba(240, 168, 60, 0.3);
  border-radius: 16px; padding: 11px 13px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  animation: hintIn 0.3s var(--ease);
}
@keyframes hintIn { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.hint__step { font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--accent); margin-bottom: 4px; }
.hint__text { font-size: 12.5px; line-height: 1.45; color: var(--ink); }
.hint__actions { display: flex; gap: 7px; margin-top: 9px; }

/* ------------------------------------------------------------------ тулбар */
.toolbar {
  flex: none; display: flex; gap: 7px; overflow-x: auto;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(20, 24, 18, 0.92), rgba(9, 12, 8, 0.99));
  border-top: 1px solid var(--border);
  scrollbar-width: none; z-index: 10;
}
.toolbar::-webkit-scrollbar { display: none; }
.tb {
  position: relative; flex: 0 0 auto; width: 66px; height: 64px;
  border-radius: 14px; border: 1px solid var(--border);
  background: var(--panel2); color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; padding: 4px; font-family: inherit;
  transition: transform 0.12s var(--ease), border-color 0.15s, background 0.15s, color 0.15s;
}
.tb__name {
  font-size: 9.5px; line-height: 1.1; text-align: center;
  max-width: 58px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tb__count {
  position: absolute; top: 3px; right: 3px; min-width: 16px; height: 16px;
  border-radius: 8px; background: rgba(0, 0, 0, 0.6);
  color: var(--accent); font-size: 9.5px; font-weight: 700;
  display: grid; place-items: center; padding: 0 3px;
}
.tb.is-active {
  border-color: var(--accent); color: var(--ink);
  background: linear-gradient(180deg, rgba(240, 168, 60, 0.24), rgba(240, 168, 60, 0.06));
  box-shadow: inset 0 0 0 1px var(--accent), 0 4px 14px rgba(240, 168, 60, 0.18);
  transform: translateY(-2px);
}
.tb.is-empty { opacity: 0.55; }
.tb.is-locked { opacity: 0.3; }
.tb:active { transform: scale(0.95); }
.tb__divider { flex: 0 0 1px; align-self: stretch; background: var(--border); margin: 8px 1px; }
.tb--mode { width: 62px; }

/* ------------------------------------------------------------------ кнопки */
.btn {
  border: 1px solid var(--border); background: var(--panel2); color: var(--ink);
  border-radius: 12px; padding: 11px 14px; font-size: 14px; font-weight: 600;
  font-family: inherit; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: transform 0.12s var(--ease), background 0.15s, border-color 0.15s;
}
.btn:active { transform: scale(0.97); background: #2b3128; }
.btn--primary {
  background: linear-gradient(180deg, #f2b355, #dc8f26);
  border-color: #f3bd6c; color: #241809;
  box-shadow: 0 4px 14px rgba(240, 168, 60, 0.25);
}
.btn--primary:active { background: #d9a13f; }
.btn--danger { color: #f4a292; border-color: rgba(224, 106, 79, 0.35); background: rgba(224, 106, 79, 0.12); }
.btn--sm { padding: 7px 11px; font-size: 12.5px; border-radius: 9px; }
.btn--block { width: 100%; margin-top: 8px; }
.btn:disabled { opacity: 0.4; }

/* ------------------------------------------------------------------ шторка */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none; transition: opacity 0.26s;
}
.sheet-backdrop.is-open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  display: flex; flex-direction: column;
  max-height: 76dvh;
  background: linear-gradient(180deg, #1d231b, #131711);
  border-radius: 22px 22px 0 0;
  border-top: 1px solid rgba(240, 168, 60, 0.22);
  box-shadow: 0 -20px 50px rgba(0, 0, 0, 0.55);
  transform: translate3d(0, 110%, 0);
  transition: transform 0.36s var(--ease);
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.is-open { transform: translate3d(0, 0, 0); }
.sheet.is-dragging { transition: none; }
.sheet__handle { width: 44px; height: 4px; border-radius: 2px; background: rgba(236, 232, 220, 0.25); margin: 8px auto 0; flex: none; }
.sheet__head { display: flex; align-items: center; gap: 10px; padding: 8px 16px 6px; flex: none; }
.sheet__head h2 { margin: 0; font-size: 16px; font-weight: 700; flex: 1; }
.sheet__close {
  width: 34px; height: 34px; border-radius: 50%; flex: none;
  border: 1px solid var(--border); background: var(--panel2); color: var(--ink);
  font-size: 20px; line-height: 1; display: grid; place-items: center;
}
.sheet__body { overflow-y: auto; padding: 4px 16px 18px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

/* ------------------------------------------------------------------ экраны */
.subscreen {
  position: fixed; inset: 0; z-index: 50;
  display: flex; flex-direction: column;
  background: linear-gradient(180deg, #141912, #0b0e0a);
  padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
  transform: translate3d(0, 100%, 0); opacity: 0;
  transition: transform 0.34s var(--ease), opacity 0.22s;
}
.subscreen.is-open { transform: translate3d(0, 0, 0); opacity: 1; }
.screen__top { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--border); flex: none; }
.screen__top h2 { margin: 0; font-size: 16px; font-weight: 700; }
.screen__back {
  width: 36px; height: 36px; border-radius: 10px; flex: none;
  border: 1px solid var(--border); background: var(--panel2); color: var(--ink);
  font-size: 18px; line-height: 1; display: grid; place-items: center;
}
.screen__pad { flex: 1; overflow-y: auto; padding: 14px 14px 30px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

/* ------------------------------------------------------------------ меню */
.menu { max-width: 440px; margin: 0 auto; }
.menu__hero { text-align: center; padding: 14px 0 20px; }
.menu__logo { margin-bottom: 8px; filter: drop-shadow(0 10px 24px rgba(240, 168, 60, 0.22)); }
.menu__title { font-size: 24px; font-weight: 800; letter-spacing: 0.01em; }
.menu__sub { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.menu__card {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.015));
  border: 1px solid var(--border); border-radius: 18px; padding: 14px;
}
.menu__row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.menu__t { font-size: 13px; font-weight: 600; }
.menu__d { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.menu__seed { font-size: 12px; color: var(--accent); background: var(--accent-soft); padding: 4px 9px; border-radius: 999px; }
.menu__grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.field { display: block; margin-bottom: 8px; }
.field span { display: block; font-size: 11px; color: var(--muted); margin-bottom: 5px; }
.field input {
  width: 100%; height: 46px; border-radius: 12px;
  border: 1px solid var(--border); background: #0f1310; color: var(--ink);
  padding: 0 12px; font-size: 16px; font-family: inherit; outline: none;
}
.field input:focus { border-color: var(--accent); }

/* ------------------------------------------------------------------ панели */
.panel__head { display: flex; gap: 12px; align-items: center; margin: 6px 0 8px; }
.panel__title { font-weight: 700; font-size: 15px; }
.panel__status { font-size: 11.5px; color: var(--teal); }
.panel__status.is-low { color: var(--danger); }
.panel__desc { color: var(--muted); font-size: 12.5px; line-height: 1.45; margin: 0 0 10px; }
.panel__label { font-size: 10.5px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); margin: 12px 0 6px; }
.listbox { background: rgba(255, 255, 255, 0.03); border: 1px solid var(--border); border-radius: 12px; padding: 2px 10px; }
.listrow { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 13px; }
.listrow span { flex: 1; color: var(--muted); }
.listrow b { font-weight: 700; }
.listempty { color: var(--muted); font-size: 12px; padding: 7px 0; }
.pbar { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.pbar__fill {
  height: 100%; width: calc(var(--p, 0) * 100%);
  background: linear-gradient(90deg, #f0a83c, #ffd27a);
  border-radius: 3px; transition: width 0.2s linear;
}
.panel__progress { margin-top: 12px; }
.panel__actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.panel__actions .btn { flex: 1 1 auto; }

.recipe-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px;
  max-height: 184px; overflow-y: auto; padding: 2px;
}
.recipe {
  display: flex; align-items: center; gap: 7px;
  background: var(--panel2); border: 1px solid var(--border);
  border-radius: 10px; padding: 7px; color: var(--muted);
  font-size: 11.5px; font-family: inherit; text-align: left;
}
.recipe.is-active { border-color: var(--accent); color: var(--ink); background: rgba(240, 168, 60, 0.12); }

/* ------------------------------------------------------------------ инвентарь */
.inv-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.inv-chip {
  display: flex; gap: 8px; align-items: center;
  background: rgba(255, 255, 255, 0.03); border: 1px solid var(--border);
  border-radius: 12px; padding: 7px; min-width: 0;
}
.inv-chip__n { font-size: 11px; color: var(--muted); line-height: 1.1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-chip__c { font-size: 13px; font-weight: 700; }

.craft-list { display: flex; flex-direction: column; gap: 6px; }
.craft-row {
  display: flex; align-items: center; gap: 8px;
  background: rgba(255, 255, 255, 0.03); border: 1px solid var(--border);
  border-radius: 12px; padding: 8px;
}
.craft-row__main { display: flex; gap: 8px; align-items: center; flex: 1; min-width: 0; }
.craft-row__n { font-size: 12.5px; font-weight: 600; }
.craft-row__in { display: flex; gap: 5px; margin-top: 3px; flex-wrap: wrap; }
.need { display: inline-flex; align-items: center; gap: 2px; font-size: 10.5px; color: var(--muted); }
.need.is-missing { color: var(--danger); }
.craft-row__btns { display: flex; gap: 5px; flex: none; }

/* ------------------------------------------------------------------ технологии */
.tech-list { display: flex; flex-direction: column; gap: 9px; max-width: 620px; margin: 0 auto; }
.tech-card {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.045), rgba(0, 0, 0, 0.12));
  border: 1px solid var(--border); border-radius: 14px; padding: 12px;
}
.tech-card.is-done { border-color: rgba(89, 195, 168, 0.35); }
.tech-card__top { display: flex; justify-content: space-between; gap: 10px; align-items: center; }
.tech-card__name { font-weight: 700; font-size: 13.5px; }
.tech-card__cost { font-size: 11px; color: var(--muted); margin-top: 2px; }
.tech-card__desc { font-size: 12px; color: var(--muted); line-height: 1.45; margin: 7px 0; }
.tech-req { font-size: 11px; color: var(--accent); margin-bottom: 6px; }
.tech-badge { font-size: 11px; padding: 4px 8px; border-radius: 999px; background: rgba(255, 255, 255, 0.07); color: var(--muted); white-space: nowrap; }
.tech-badge--done { background: rgba(89, 195, 168, 0.15); color: var(--teal); }
.tech-pts { font-size: 10.5px; color: var(--muted); margin-top: 4px; }
.tech-hint { font-size: 11.5px; color: var(--muted); line-height: 1.45; margin-top: 12px; padding: 10px; border-radius: 10px; border: 1px dashed var(--border); }

/* ------------------------------------------------------------------ настройки */
.settings { max-width: 560px; margin: 0 auto; }
.set-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px; border-radius: 12px; margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.035); border: 1px solid var(--border);
}
.set-row__t { font-size: 13.5px; font-weight: 600; }
.set-row__d { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.switch {
  width: 46px; height: 26px; border-radius: 999px; flex: none;
  border: 1px solid var(--border); background: rgba(255, 255, 255, 0.08);
  position: relative; transition: background 0.18s, border-color 0.18s;
}
.switch span {
  position: absolute; top: 2px; left: 2px; width: 20px; height: 20px;
  border-radius: 50%; background: #cfd2c6;
  transition: transform 0.2s var(--ease), background 0.2s;
}
.switch.is-on { background: rgba(240, 168, 60, 0.45); border-color: var(--accent); }
.switch.is-on span { transform: translateX(20px); background: #ffd89a; }
.set-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.selftest { margin-top: 10px; }
.selftest__sum { font-size: 12.5px; padding: 8px 10px; border-radius: 10px; margin-bottom: 6px; }
.selftest__sum.is-ok { background: rgba(89, 195, 168, 0.14); color: var(--teal); }
.selftest__sum.is-bad { background: rgba(224, 106, 79, 0.14); color: var(--danger); }
.selftest__row { font-size: 11.5px; color: var(--muted); padding: 4px 0; display: flex; gap: 6px; align-items: baseline; }
.selftest__row.is-ok span { color: var(--teal); }
.selftest__row.is-bad span { color: var(--danger); }
.selftest__row em { font-style: normal; font-size: 10.5px; margin-left: auto; color: var(--muted); }

/* ------------------------------------------------------------------ статистика */
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 4px; }
.stats-tile {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02));
  border: 1px solid var(--border); border-radius: 12px; padding: 10px 11px;
}
.stats-tile__v { font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--accent); }
.stats-tile__k { font-size: 10.5px; color: var(--muted); margin-top: 3px; line-height: 1.3; }

/* ------------------------------------------------------------------ тост */
.toast {
  position: fixed; left: 50%; z-index: 90;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  background: rgba(20, 25, 18, 0.97);
  border: 1px solid var(--border);
  color: var(--ink); padding: 9px 16px; border-radius: 999px;
  font-size: 13px; max-width: 86vw; text-align: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s, transform 0.22s var(--ease);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}
.toast.is-shown { opacity: 1; transform: translate(-50%, 0); }

body.kb-open .toolbar { display: none; }

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

@media (min-width: 720px) {
  .hud { padding-left: 18px; padding-right: 18px; }
  .toolbar { justify-content: center; }
  .joystick { left: 24px; bottom: 24px; }
  .fabs { right: 24px; bottom: 160px; }
}