:root {
  --bg: var(--tg-theme-bg-color, #17212b);
  --bg2: var(--tg-theme-secondary-bg-color, #232e3c);
  --text: var(--tg-theme-text-color, #f5f5f5);
  --hint: var(--tg-theme-hint-color, #8a9aa9);
  --accent: var(--tg-theme-button-color, #5288c1);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e35d5d;
  --radius: 12px;
}

* { -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
}

#screens {
  padding: 12px 12px calc(72px + env(safe-area-inset-bottom)) 12px;
}

.screen { display: none; }
.screen.active { display: block; }

.hidden { display: none !important; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.pad-b { padding-bottom: 10px; }

/* Заголовок текущего пространства (тап — выбор цветной/чёрный) */
.space-head {
  width: 100%;
  background: none;
  border: none;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
  padding: 0 0 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.space-head .chev { font-size: 11px; opacity: 0.8; }

/* Большая плитка (основной металл, напр. Чёрный) */
#big-tiles:empty { display: none; }
.big-tile {
  width: 100%;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: var(--radius);
  padding: 22px 12px;
  margin-bottom: 8px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.big-tile:active { filter: brightness(1.1); }
.big-tile .bt-name { font-size: 23px; font-weight: 800; }
.big-tile .bt-price { font-size: 14px; font-weight: 500; opacity: 0.9; }

/* Плитки металлов */
.tiles {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
#tiles:empty { display: none; }
.tile {
  background: var(--bg2);
  border: none;
  border-radius: var(--radius);
  color: var(--text);
  padding: 12px 4px 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.tile:active { filter: brightness(1.25); }
.tile .price {
  font-size: 12px;
  font-weight: 400;
  color: var(--hint);
}

/* Перетаскивание плиток */
.tile {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}
.tile.drag-placeholder { opacity: 0.3; filter: none; }
.tile.drag-ghost {
  position: fixed;
  z-index: 100;
  margin: 0;
  pointer-events: none;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  transform: scale(1.07);
  opacity: 0.95;
}

/* Накладная: плитки / операции / итог — три блока с разделителями */
#invoice-box {
  margin-top: 14px;
  border-top: 1px solid var(--bg2);
  padding-top: 12px;
}
.line {
  display: flex;
  align-items: center;
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 6px;
  gap: 10px;
}
.line .l-name { font-weight: 600; flex: 0 0 auto; }
.line .l-calc { color: var(--hint); font-size: 13px; flex: 1 1 auto; text-align: left; }
.line .l-sum { font-weight: 600; white-space: nowrap; }
.line .l-del {
  background: none;
  border: none;
  color: var(--hint);
  font-size: 17px;
  padding: 2px 4px;
  cursor: pointer;
}
.total-row {
  display: flex;
  justify-content: space-between;
  font-size: 18px;
  font-weight: 700;
  padding: 10px 12px;
  border-top: 1px solid var(--bg2);
  margin-top: 4px;
}
.empty-note { text-align: center; color: var(--hint); padding: 18px 0; }

/* Сводка за день (история чёрного): сколько каждого металла принято */
.day-summary {
  background: var(--bg2);
  border-radius: 10px;
  padding: 8px 12px 10px;
  margin-bottom: 10px;
}
.day-summary .ds-head {
  font-size: 12px;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 0 6px;
}
.day-summary .ds-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 3px 0;
  font-size: 14px;
}
.day-summary .ds-name { font-weight: 600; flex: 1 1 auto; }
.day-summary .ds-qty { color: var(--hint); white-space: nowrap; }
.day-summary .ds-sum { font-weight: 600; white-space: nowrap; min-width: 72px; text-align: right; }
.day-summary .ds-total {
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  margin-top: 4px;
  padding-top: 7px;
  font-weight: 700;
}
.day-summary .ds-total .ds-name { font-weight: 700; }

/* Строка клиента (история чёрного) */
.client-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg2);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 6px;
  cursor: pointer;
}
.client-row:active { filter: brightness(1.25); }
.client-row .cr-main { flex: 1 1 auto; min-width: 0; }
.client-row .cr-title { font-weight: 600; font-size: 14px; }
.client-row .cr-sub {
  color: var(--hint);
  font-size: 12px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.client-row .cr-total { font-weight: 600; white-space: nowrap; }
.client-row .cr-del {
  background: none;
  border: none;
  color: var(--hint);
  font-size: 16px;
  padding: 4px 5px;
  cursor: pointer;
}

/* Списки (цены, разница, история) */
.rows .row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 6px;
  cursor: pointer;
  gap: 8px;
}
.rows .row:active { filter: brightness(1.25); }
.row .r-name { font-weight: 600; }
.row .r-sub { color: var(--hint); font-size: 12px; margin-top: 2px; }
.row .r-val { font-weight: 600; white-space: nowrap; }
.row .r-val .eff { color: var(--accent-text); }

.prices-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 10px;
}

.btn {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  padding: 10px 14px;
  cursor: pointer;
}
.btn:active { filter: brightness(1.15); }
.btn:disabled { opacity: 0.6; }
.btn.full { width: 100%; padding: 13px; font-size: 17px; margin-top: 6px; }
.btn.secondary { background: var(--bg2); color: var(--text); }
.btn.small { padding: 8px; font-size: 14px; font-weight: 500; }
/* .warn после .secondary, чтобы красный цвет подтверждения побеждал */
.btn.warn { background: var(--danger); color: #fff; }

/* Журнал операций («Подробнее») */
#sheet-body { max-height: 62vh; overflow-y: auto; }
.op {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg2);
  border-radius: 10px;
  padding: 9px 10px;
  margin-bottom: 6px;
}
.op .o-time { color: var(--hint); font-size: 12px; white-space: nowrap; }
.op .o-name { font-weight: 600; font-size: 14px; }
.op .o-calc { color: var(--hint); font-size: 13px; flex: 1 1 auto; }
.op .o-sum { font-weight: 600; font-size: 14px; white-space: nowrap; }
.op button {
  background: none;
  border: none;
  color: var(--hint);
  font-size: 16px;
  padding: 4px 5px;
  cursor: pointer;
}
.day-actions { display: flex; gap: 8px; padding: 2px 0 10px; }
.day-actions .btn { flex: 1; padding: 9px; font-size: 14px; margin: 0; }

/* История: раскрытый день */
.day-lines { padding: 4px 2px 10px; }
.day-lines .line { background: transparent; padding: 6px 10px; margin: 0; }

/* Нижние вкладки */
#tabs {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--bg2);
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 1px solid rgba(255,255,255,0.06);
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--hint);
  font-size: 19px;
  padding: 8px 0 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}
.tab span { font-size: 11px; }
.tab.active { color: var(--text); }

/* Попап выбора пространства (над вкладкой «Накладная») */
#space-pop {
  position: fixed;
  z-index: 60;
  background: var(--bg2);
  border-radius: 12px;
  padding: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 150px;
}
#space-pop button {
  background: none;
  border: none;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  text-align: left;
  padding: 11px 14px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
#space-pop button.active { background: var(--accent); color: var(--accent-text); }
#space-pop button:active { background: rgba(255, 255, 255, 0.08); }
#space-pop .dot { width: 9px; height: 9px; border-radius: 50%; }
#space-pop .dot.color { background: #e0a23c; }
#space-pop .dot.black { background: #6b7682; }

/* Модалка */
#overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: flex-end;
  z-index: 50;
}
#sheet {
  background: var(--bg);
  width: 100%;
  border-radius: 16px 16px 0 0;
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
  box-sizing: border-box;
}
#sheet-title {
  font-size: 19px;
  font-weight: 700;
  text-align: center;
  padding-bottom: 14px;
}
.field { margin-bottom: 10px; }
.field label {
  display: block;
  font-size: 13px;
  color: var(--hint);
  margin-bottom: 4px;
}
.field input {
  width: 100%;
  box-sizing: border-box;
  background: var(--bg2);
  border: 1px solid transparent;
  border-radius: 10px;
  color: var(--text);
  font-size: 20px;
  padding: 11px 12px;
  outline: none;
}
.field input:focus { border-color: var(--accent); }
.field input[readonly] { opacity: 0.85; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.8);
  color: #fff;
  font-size: 14px;
  padding: 9px 16px;
  border-radius: 10px;
  z-index: 99;
  max-width: 86vw;
  text-align: center;
}
