/* ============================================================================
   Дизайн-система «Вселенная Бориса» — пиксель-нео-брутализм, тёмная тема.
   Стандарт стилистики: docs/ui-style.md. Один файл, без build-шага.
   Раскладка mobile-first: <900px — bottom sheet, ≥900px — боковая панель.
   ============================================================================ */

/* ---------- Шрифты (самохостинг, OFL; сабсеты с unicode-range) ---------- */
@font-face {
  font-family: 'Press Start 2P';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('/fonts/press-start-2p-cyrillic-ext.woff2') format('woff2');
  unicode-range: U+0460-052F, U+1C80-1C8A, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
}
@font-face {
  font-family: 'Press Start 2P';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('/fonts/press-start-2p-cyrillic.woff2') format('woff2');
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: 'Press Start 2P';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('/fonts/press-start-2p-latin-ext.woff2') format('woff2');
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
@font-face {
  font-family: 'Press Start 2P';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url('/fonts/press-start-2p-latin.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: 'Pixelify Sans';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/pixelify-sans-cyrillic.woff2') format('woff2');
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: 'Pixelify Sans';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/pixelify-sans-latin-ext.woff2') format('woff2');
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
@font-face {
  font-family: 'Pixelify Sans';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/pixelify-sans-latin.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* ---------- Токены ---------- */
:root {
  /* Палитра: тёмная Tokyo Night в пиксель-обвязке */
  --bg: #14121a;
  --bg-deep: #100e17;
  --panel: #1d1a26;
  --panel2: #262233;
  --panel3: #322c44;
  --text: #d8d4e8;
  --dim: #8b86a3;
  --accent: #7aa2f7;
  --accent-deep: #46609c;
  --good: #9ece6a;
  --warn: #e0af68;
  --bad: #f7768e;
  --border: #3a3550;
  --ink: #0a0910; /* цвет жёстких пиксель-теней и рамок */

  /* Типографика */
  --font-pixel: 'Press Start 2P', monospace;
  --font-body: 'Pixelify Sans', 'Segoe UI', system-ui, sans-serif;

  /* Пиксель-обвязка */
  --px-border: 3px;
  --px-shadow: 4px 4px 0 var(--ink);
  --px-shadow-sm: 2px 2px 0 var(--ink);

  /* Sheet */
  --sheet-h: 92vh;
  --sheet-h: 92dvh;
  --peek-h: 84px; /* высота видимой части в peek: язычок + вкладки */
}

/* ---------- База ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font-body);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
/* Длинные тексты — выделяемы */
.markdown, #claims, #episodes, #diary-list, .feed-item .txt { user-select: text; -webkit-user-select: text; }

::selection { background: var(--accent); color: var(--ink); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button, input, select { font: inherit; color: inherit; touch-action: manipulation; }

* { scrollbar-width: thin; scrollbar-color: var(--panel3) transparent; }
::-webkit-scrollbar { width: 10px; }
::-webkit-scrollbar-thumb { background: var(--panel3); border: 2px solid var(--ink); }
::-webkit-scrollbar-track { background: transparent; }

#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ---------- Topbar ---------- */
#topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--panel);
  border-bottom: var(--px-border) solid var(--ink);
}
.brand { font-family: var(--font-pixel); font-size: 10px; letter-spacing: 0.5px; white-space: nowrap; }
.clock { font-family: var(--font-pixel); font-size: 8px; color: var(--accent); white-space: nowrap; }
.activity { color: var(--dim); font-style: italic; font-size: 13px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spacer { flex: 1; }
.money { font-variant-numeric: tabular-nums; white-space: nowrap; }
.badge {
  font-family: var(--font-pixel); font-size: 8px;
  padding: 5px 8px; background: var(--panel2);
  border: 2px solid var(--border); color: var(--dim);
  white-space: nowrap;
}
.badge.ok { color: var(--good); border-color: var(--good); }
.badge.err { color: var(--bad); border-color: var(--bad); }

/* ---------- Statsbar ---------- */
#statsbar {
  display: flex; gap: 14px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: var(--px-border) solid var(--ink);
}
.stat { display: flex; align-items: center; gap: 7px; flex: 1; min-width: 0; }
.stat label { color: var(--dim); font-size: 12.5px; white-space: nowrap; display: inline-flex; align-items: center; gap: 5px; }
.stat label .sicon { image-rendering: pixelated; display: block; } /* иконки потребностей — этап 6 */
.bar {
  flex: 1; height: 16px; min-width: 30px;
  background: var(--panel2);
  border: var(--px-border) solid var(--ink);
  position: relative; overflow: hidden;
}
.fill { height: 100%; width: 0%; transition: width 0.5s steps(12); }
.fill.fatigue { background: var(--accent); }
.fill.hunger { background: var(--warn); }
.fill.boredom { background: var(--good); }
/* Сегментные деления каждые 10% — циферблат в пиксель-стиле */
.bar::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(90deg, transparent 0 calc(10% - 2px), var(--ink) calc(10% - 2px) 10%);
  opacity: 0.4;
}
.stat span { width: 30px; text-align: right; font-variant-numeric: tabular-nums; color: var(--dim); font-size: 12.5px; }

/* ---------- Main: сцена + панель ---------- */
main { flex: 1; min-height: 0; position: relative; display: flex; }
#stage {
  flex: 1; position: relative; min-width: 0; min-height: 0;
  background-color: var(--bg-deep);
  /* Едва заметные сканлайны фона сцены — CRT-акцент вокруг канваса */
  background-image: repeating-linear-gradient(0deg, rgba(216, 212, 232, 0.025) 0 1px, transparent 1px 4px);
}
#canvas-host { position: absolute; inset: 14px; }
#canvas-host canvas {
  image-rendering: pixelated;
  /* outline, а не border: Phaser сам считает размер канваса */
  outline: var(--px-border) solid var(--ink);
  box-shadow: var(--px-shadow);
  background: var(--bg-deep);
}

/* ---------- Bottom sheet (мобильная панель) ---------- */
#side {
  /* Мобильный persistent-sheet: фиксирован снизу, три снап-точки, тянет за язычок */
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: var(--sheet-h);
  display: flex; flex-direction: column;
  background: var(--panel);
  border-top: var(--px-border) solid var(--ink);
  box-shadow: 0 -5px 0 var(--ink);
  padding-bottom: env(safe-area-inset-bottom);
  transform: translateY(calc(100% - var(--peek-h))); /* peek по умолчанию до первого JS-тикa */
  transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
  will-change: transform;
}
#side.dragging { transition: none; }
#sheet-grip {
  flex: none; height: 30px;
  display: flex; align-items: center; justify-content: center;
  cursor: grab; touch-action: none;
}
.grip { width: 56px; height: 8px; background: var(--panel3); border: 2px solid var(--ink); }
#tabs { flex: none; display: flex; gap: 6px; padding: 0 10px 10px; border-bottom: var(--px-border) solid var(--ink); touch-action: none; }

/* ---------- Вкладки ---------- */
#tabs button {
  flex: 1; min-height: 40px; padding: 8px 4px;
  background: var(--panel2); border: var(--px-border) solid var(--ink);
  box-shadow: var(--px-shadow-sm);
  color: var(--dim);
  font-family: var(--font-pixel); font-size: 8px;
  cursor: pointer;
}
#tabs button.active {
  background: var(--accent); color: var(--ink);
  box-shadow: none; transform: translate(2px, 2px);
}
.tab { display: none; flex: 1 1 0; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 12px 14px; }
.tab.active { display: block; }
.hidden { display: none !important; }

/* ---------- Лента ---------- */
#feed { display: flex; flex-direction: column; gap: 8px; }
.feed-item {
  padding: 8px 10px;
  background: var(--panel2);
  border: 2px solid var(--border); border-left: 6px solid var(--border);
  font-size: 14px;
}
.feed-item .ts { color: var(--dim); font-size: 11px; font-variant-numeric: tabular-nums; margin-right: 6px; }
.feed-item.thought { border-left-color: var(--accent); }
.feed-item.thought .txt { font-style: italic; }
.feed-item.event { border-left-color: var(--border); }
.feed-item.decision { border-left-color: var(--accent-deep); }
.feed-item.diary { border-left-color: var(--warn); }
.feed-item.plan { border-left-color: var(--good); }
.feed-item.system { border-left-color: var(--bad); color: var(--dim); }

/* ---------- Дневник ---------- */
#diary-list .di {
  padding: 10px 12px;
  background: var(--panel2);
  border: 2px solid var(--border); border-left: 6px solid var(--warn);
  box-shadow: var(--px-shadow-sm);
  margin-bottom: 10px; cursor: pointer;
}
#diary-list .di .d { color: var(--dim); font-size: 12px; }
.markdown { font-size: 14.5px; }
.markdown h1, .markdown h2 {
  font-family: var(--font-pixel); font-size: 10px; letter-spacing: 0.5px;
  color: var(--accent); margin: 0.6em 0 0.5em;
}
.markdown ul { margin: 0.3em 0; padding-left: 18px; }
.markdown li { margin: 2px 0; }
.markdown em { color: var(--dim); }
.markdown strong { color: var(--warn); }
.markdown hr { border: none; border-top: 2px solid var(--border); }
.backlink {
  font-family: var(--font-pixel); font-size: 8px;
  color: var(--accent); cursor: pointer; margin-bottom: 10px; display: inline-block;
}

/* ---------- Память ---------- */
.mem-note { color: var(--dim); font-size: 12.5px; margin-bottom: 10px; }
#claims .claim {
  padding: 8px 10px;
  background: var(--panel2);
  border: 2px solid var(--border); border-left: 6px solid var(--accent-deep);
  margin-bottom: 8px; font-size: 14px;
}
#claims .claim .cat { font-size: 11.5px; color: var(--dim); }
#claims .claim.conflicted { border-color: var(--bad); border-left-color: var(--bad); }
#claims .claim .conf { color: var(--bad); font-size: 11.5px; }
#episodes .ep { color: var(--dim); font-size: 13px; padding: 3px 0; }

/* ---------- Формы и кнопки ---------- */
h4 {
  font-family: var(--font-pixel); font-size: 8px; letter-spacing: 1px;
  color: var(--dim); text-transform: uppercase;
  margin: 16px 0 8px;
}
.row { display: flex; align-items: center; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
.hint { color: var(--dim); font-size: 12.5px; margin: 4px 0 8px; }
.btn {
  min-height: 44px; padding: 10px 16px;
  background: var(--panel2); border: var(--px-border) solid var(--ink);
  box-shadow: var(--px-shadow-sm);
  color: var(--text); font-weight: 600; font-size: 14px;
  cursor: pointer;
}
.btn:active { transform: translate(2px, 2px); box-shadow: none; }
.btn.danger { color: var(--bad); }
select, input[type="number"] {
  min-height: 40px; padding: 6px 10px;
  background: var(--panel2); border: var(--px-border) solid var(--ink);
  border-radius: 0; color: var(--text);
}
input[type="range"] { flex: 1; accent-color: var(--accent); }
details { margin-top: 16px; }
summary { cursor: pointer; color: var(--dim); font-weight: 600; }
summary::marker { color: var(--accent); }

/* ---------- Десктоп: боковая панель вместо sheet ---------- */
@media (min-width: 900px) {
  #side {
    position: static; height: auto; width: 400px; flex: none;
    border-top: none; border-left: var(--px-border) solid var(--ink);
    box-shadow: none; padding-bottom: 0;
    transform: none;
  }
  #sheet-grip { display: none; }
  #tabs { padding: 10px; }
  #canvas-host { inset: 20px; }
}

/* ---------- Мобильная перекомпоновка topbar ---------- */
@media (max-width: 899px) {
  .brand { font-size: 8px; }
  .stat label span { display: none; } /* на узком экране — только иконки потребностей */
  #topbar { flex-wrap: wrap; row-gap: 2px; }
  .brand { order: 1; }
  #clock { order: 2; }
  .spacer { order: 3; }
  #conn { order: 4; }
  #mode { order: 5; }
  #activity { order: 6; flex: 1 1 auto; min-width: 200px; }
  #money { order: 7; }
  #food { order: 8; }
  /* Канвас не прячется под свёрнутым sheet */
  #canvas-host { inset: 10px 10px calc(var(--peek-h) + 12px + env(safe-area-inset-bottom)); }
}

/* ---------- Hover только на устройствах с курсором ---------- */
@media (hover: hover) and (pointer: fine) {
  #tabs button:hover:not(.active) { color: var(--text); background: var(--panel3); }
  .btn:hover { background: var(--panel3); }
  .btn.danger:hover { color: var(--bad); border-color: var(--bad); }
  #diary-list .di:hover { outline: 2px solid var(--accent); }
  select:hover, input[type="number"]:hover { border-color: var(--accent-deep); }
}

/* ---------- Уважение к reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  #side { transition: none; }
  .fill { transition: none; }
}
