:root {
  --bg: #0f1a14;
  --panel: rgba(18, 28, 22, 0.94);
  --panel-2: rgba(36, 52, 42, 0.9);
  --line: rgba(255, 255, 255, 0.1);
  --text: #f1ece0;
  --muted: #a9b5a6;
  --gold: #ffd54f;
  --green: #8be36b;
  --red: #ef5350;
  --blue: #90caf9;
  --orange: #ffb74d;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 14px/1.35 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; user-select: none; -webkit-user-select: none; }
button { font: inherit; color: inherit; cursor: pointer; }
canvas#world { position: fixed; inset: 0; width: 100vw; height: 100vh; touch-action: none; display: block; }
/* Затемнение краёв и красная пульсация при атаке — средствами CSS, без перерисовки canvas. */
#vignette { position: fixed; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at center, rgba(0,0,0,0) 45%, rgba(8,14,10,0.55) 100%); }
#vignette::after { content: ''; position: absolute; inset: 0; opacity: 0;
  background: radial-gradient(ellipse at center, rgba(0,0,0,0) 40%, rgba(140,10,10,0.6) 100%); }
body.attack #vignette::after { animation: attack 1s ease-in-out infinite alternate; }
@keyframes attack { from { opacity: 0.4; } to { opacity: 1; } }

/* ---------- верхняя панель ---------- */
#hud { position: fixed; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; gap: 6px;
  padding: calc(8px + var(--safe-t)) 8px 0; pointer-events: none; flex-wrap: wrap; }
#hud .res, #hud .right { display: flex; gap: 6px; flex-wrap: wrap; }
#hud .right { margin-left: auto; }
.pill { pointer-events: auto; display: inline-flex; align-items: center; gap: 5px; height: 32px; padding: 0 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; white-space: nowrap; }
.pill b { font-variant-numeric: tabular-nums; }
.pill.btn { border-color: rgba(255, 213, 79, 0.35); }
.pill.btn:active { transform: scale(0.95); }
.mini { display: inline-block; width: 22px; height: 4px; border-radius: 2px; background: rgba(255,255,255,0.15); overflow: hidden; }
.mini i { display: block; height: 100%; width: 0; background: var(--blue); }
.hpbar { display: inline-block; width: 46px; height: 7px; border-radius: 4px; background: rgba(255,255,255,0.15); overflow: hidden; }
.hpbar i { display: block; height: 100%; background: var(--green); }
.hpbar i.mid { background: var(--orange); }
.hpbar i.low { background: var(--red); }

/* ---------- счётчик FPS ---------- */
#perf { position: fixed; left: 50%; transform: translateX(-50%); top: calc(44px + var(--safe-t)); z-index: 6; max-width: calc(100vw - 16px);
  padding: 3px 8px; border-radius: 8px; background: rgba(0,0,0,0.75); color: #b9f6ca; font: 11px/1.3 ui-monospace, Consolas, monospace;
  pointer-events: none; text-align: center; }
#perf[hidden] { display: none; }

/* ---------- задание ---------- */
#quest { position: fixed; left: 8px; top: calc(48px + var(--safe-t)); width: min(300px, calc(100vw - 16px));
  background: var(--panel); border: 1px solid rgba(255, 213, 79, 0.3); border-radius: 12px; padding: 8px 10px; }
.q-row { display: flex; justify-content: space-between; gap: 8px; cursor: pointer; }
.q-title { color: var(--gold); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.q-reward { font-size: 12px; color: var(--gold); }
.q-text { font-weight: 600; margin-top: 2px; cursor: pointer; }
.q-hint { color: var(--muted); font-size: 13px; margin-top: 4px; }
.q-go { margin-top: 6px; padding: 5px 12px; border-radius: 8px; border: 1px solid rgba(255,213,79,.5); background: rgba(255,213,79,.12); }

/* ---------- тосты ---------- */
#toasts { position: fixed; right: 8px; top: calc(48px + var(--safe-t)); width: min(320px, calc(100vw - 16px));
  display: flex; flex-direction: column; gap: 6px; pointer-events: none; z-index: 5; }
.toast { pointer-events: auto; background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--blue);
  border-radius: 10px; padding: 8px 10px; animation: in .25s ease-out; transition: opacity .4s, transform .4s; cursor: pointer; }
.toast.good { border-left-color: var(--green); }
.toast.warn { border-left-color: var(--orange); }
.toast.bad { border-left-color: var(--red); background: rgba(60, 18, 18, 0.94); }
.toast.gone { opacity: 0; transform: translateX(20px); }
@keyframes in { from { opacity: 0; transform: translateY(-8px); } }

/* На узком экране тосты уходят вниз, чтобы не закрывать задание. */
@media (max-width: 640px) {
  #toasts { top: auto; bottom: calc(76px + var(--safe-b)); right: 8px; left: 8px; width: auto; flex-direction: column-reverse; }
  body.sheet-open #toasts { top: calc(44px + var(--safe-t)); bottom: auto; flex-direction: column; }
}

/* ---------- нижняя панель ---------- */
#bar { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(8px + var(--safe-b)); display: flex; gap: 6px; z-index: 5; }
#bar button { position: relative; display: flex; flex-direction: column; align-items: center; gap: 1px; min-width: 70px; padding: 6px 8px 5px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; font-size: 20px; }
#bar button span { font-size: 12px; color: var(--muted); }
#bar button:active { transform: scale(0.95); }
#bar button.on { border-color: rgba(255, 213, 79, 0.7); background: rgba(70, 60, 20, 0.92); }
.badge { position: absolute; top: -6px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  background: var(--red); color: #fff; font: 700 12px/20px system-ui, sans-serif; font-style: normal; animation: blink 1s infinite; }
.badge[hidden] { display: none; }
.badge.gold { background: var(--gold); color: #3a2a00; animation: none; }
.rating { color: var(--gold); font-weight: 600; margin-left: 4px; }

/* ---------- шторка ---------- */
#sheet { position: fixed; left: 50%; bottom: calc(70px + var(--safe-b)); width: min(560px, calc(100vw - 8px)); transform: translate(-50%, 130%);
  transition: transform .22s ease-out; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; max-height: 50vh; overflow-y: auto; padding: 12px 14px 14px; z-index: 4;
  touch-action: pan-y; overscroll-behavior: contain; }
#sheet.open { transform: translate(-50%, 0); }
/* На широком экране — панель справа, карта остаётся видна. */
@media (min-width: 900px) {
  #sheet { left: auto; right: 8px; top: calc(48px + var(--safe-t)); bottom: 8px; width: 380px; max-height: none;
    transform: translateX(110%); }
  #sheet.open { transform: none; }
  body.sheet-open #bar { left: calc((100vw - 396px) / 2); }
  body.sheet-open #bar button { min-width: 62px; }
  body.sheet-open #toasts { right: 404px; }
}
.sh-head { display: flex; align-items: center; gap: 8px; }
.sh-title { font-size: 18px; font-weight: 700; }
.x.back { margin-left: 0; }
.x { margin-left: auto; width: 34px; height: 34px; border-radius: 17px; border: 1px solid var(--line); background: var(--panel-2); flex: none; }
.sh-sub { color: var(--muted); margin: 3px 0 6px; }
.chip { font-size: 12px; padding: 2px 8px; border-radius: 10px; background: rgba(255,255,255,0.1); white-space: nowrap; }
.chip.ok { background: rgba(139,227,107,.18); color: var(--green); }
.chip.safe { background: rgba(144,202,249,.18); color: var(--blue); }
.chip.warn { background: rgba(255,183,77,.18); color: var(--orange); }
.chip.sick { background: rgba(160,230,110,.22); color: #c5f59a; }
.chip.bad { background: rgba(239,83,80,.25); color: #ff8a80; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .6; } }

.stat { display: grid; grid-template-columns: 82px 1fr 52px; align-items: center; gap: 8px; margin: 6px 0; }
.stat span { color: var(--muted); }
.stat em { font-style: normal; text-align: right; font-variant-numeric: tabular-nums; }
.bar { height: 8px; border-radius: 4px; background: rgba(255,255,255,0.12); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--text); }
.bar.food i { background: #e57373; }
.bar.warn i { background: var(--orange); }
.bar.sick i { background: var(--green); }
.bar.safe i { background: var(--blue); }
.bar.hp i { background: #a1887f; }

.acts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.acts.three { grid-template-columns: repeat(3, 1fr); }
.acts.four { grid-template-columns: repeat(2, 1fr); }
.act { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 10px 12px; text-align: left;
  background: var(--panel-2); border: 1px solid rgba(255,255,255,0.14); border-radius: 12px; min-height: 54px; }
.act b { font-weight: 650; }
.act small { color: var(--muted); font-size: 12px; }
.act.primary { border-color: rgba(239,83,80,.6); background: rgba(120,30,30,.45); }
.act.wide { width: 100%; margin-top: 6px; }
.act.danger { border-color: rgba(239,83,80,.9); background: rgba(150,30,30,.75); }
.act.muted { opacity: .85; }
.warn-text { color: var(--orange); }
.act:not([disabled]):active { transform: scale(0.97); }
.act[disabled] { opacity: .5; cursor: default; }
.acts.three .act { padding: 8px; }
.hint { color: var(--muted); font-size: 13px; margin-top: 10px; }
.sec { margin: 14px 0 4px; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--gold); }
.bld { padding: 8px 0; border-bottom: 1px solid var(--line); }
.bld-h { font-weight: 650; }
.bld-h span { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 4px; }
.bld-e { color: var(--muted); font-size: 13px; }
.bld-e.max { margin-top: 4px; color: var(--green); }
.dlist { margin-top: 8px; }
.list { margin-top: 4px; }
.row { display: flex; align-items: center; gap: 6px; padding: 5px 0; border-bottom: 1px solid var(--line); }
.row.threat .rname { color: #ff8a80; }
.row.order .rname { color: var(--gold); }
.row.empty { color: var(--muted); font-size: 13px; padding: 8px 0; }
.rname i { font-style: normal; }
.trait { margin: 2px 0 6px; font-size: 13px; color: var(--muted); }
.trait b { color: var(--text); }
.bar.gold i { background: var(--gold); }
.rname { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; padding: 2px 0; border: 0; background: none; text-align: left; font-weight: 650; }
.rname small { color: var(--muted); font-weight: 400; font-size: 12px; display: flex; align-items: center; gap: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.dot { width: 8px; height: 8px; border-radius: 4px; background: var(--muted); flex: none; }
.dot.ok { background: var(--green); } .dot.safe { background: var(--blue); } .dot.warn { background: var(--orange); }
.dot.sick { background: #c5f59a; } .dot.bad { background: var(--red); }
.ib { flex: none; min-width: 52px; height: 36px; padding: 0 9px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.16);
  background: var(--panel-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.ib.gold, .act.gold { border-color: rgba(255,213,79,.8); background: rgba(110,85,10,.55); }
.ib.primary { border-color: rgba(239,83,80,.6); background: rgba(120,30,30,.5); }
.ib[disabled] { opacity: .4; cursor: default; }
.ib:not([disabled]):active { transform: scale(0.95); }
.link-btn { margin-left: 8px; border: 0; background: none; color: var(--muted); text-transform: none; letter-spacing: 0; font-weight: 400; text-decoration: underline; }
.opt { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; }
.opt > span { color: var(--muted); }
.segs { display: flex; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg { padding: 8px 12px; border: 0; background: var(--panel-2); }
.seg.on { background: rgba(139,195,74,.35); color: #fff; font-weight: 700; }
.drow { display: flex; justify-content: space-between; padding: 8px 2px; border-bottom: 1px solid var(--line); }
.drow[data-act] { cursor: pointer; }
.drow em { font-style: normal; color: var(--muted); }
.drow em.out, .drow em.work { color: var(--gold); }
.drow em.repair { color: var(--red); }

/* ---------- окна ---------- */
#modal { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; padding: 16px;
  background: rgba(5, 10, 8, 0.6); backdrop-filter: blur(3px); z-index: 10; }
#modal.open { display: flex; }
.card { width: min(420px, 100%); max-height: 100%; overflow-y: auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: 20px; padding: 22px 20px; text-align: center; }
.card h1 { margin: 6px 0 8px; font-size: 30px; letter-spacing: .01em; }
.card h2 { margin: 0 0 12px; }
.lead { color: var(--muted); margin: 0 0 18px; }
.logo { display: flex; justify-content: center; }
.big { display: block; width: 100%; padding: 13px; margin-top: 10px; border-radius: 14px; border: 0; font-weight: 700; font-size: 16px;
  background: linear-gradient(180deg, #8bc34a, #5d9134); color: #10200c; }
.big.ghost { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.big.danger { background: #b23a35; color: #fff; }
.link { margin-top: 12px; background: none; border: 0; color: var(--muted); text-decoration: underline; }
.help { text-align: left; padding-left: 0; list-style: none; margin: 0; }
.help li { margin: 0 0 10px; }
.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.summary div { background: var(--panel-2); border-radius: 12px; padding: 10px; }
.summary b { display: block; font-size: 22px; }
.summary span { color: var(--muted); font-size: 12px; }

@media (max-width: 480px) {
  #bar button { min-width: 0; width: calc((100vw - 40px) / 5); padding-left: 2px; padding-right: 2px; }
  #bar button span { font-size: 11px; }
  .rating { display: none; }
  #hud { flex-wrap: nowrap; gap: 4px; padding-left: 6px; padding-right: 6px; }
  #hud .res, #hud .right { gap: 4px; flex-wrap: nowrap; }
  .pill { height: 30px; padding: 0 6px; gap: 2px; font-size: 12.5px; }
  #hud .day, #hpText, .mini { display: none; }
  .hpbar { width: 30px; }
  #quest { top: calc(44px + var(--safe-t)); left: 6px; width: calc(100vw - 12px); padding: 6px 9px; }
  .q-text { font-size: 13px; }
  .q-hint { font-size: 12px; }
  .acts.three { grid-template-columns: 1fr; }
}
