/* ============================================================
   HomeBox — тёмная тема
   ============================================================ */
:root {
  --bg: #0d1117;
  --bg-soft: #101623;
  --panel: #151c27;
  --panel-2: #1b2433;
  --border: #263140;
  --border-soft: #1f2937;
  --text: #e8edf4;
  --muted: #93a1b5;
  --faint: #5c6b80;
  --accent: #4d8dff;
  --accent-hi: #6ba3ff;
  --accent-soft: rgba(77, 141, 255, .15);
  --danger: #e5534b;
  --danger-soft: rgba(229, 83, 75, .15);
  --ok: #37b26c;
  --radius: 12px;
  --shadow: 0 12px 34px rgba(0, 0, 0, .5);
  font-size: 15px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background:
    radial-gradient(1100px 560px at 82% -12%, #15233f 0%, transparent 62%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
}
.hidden { display: none !important; }

/* ---------- Форма, поля ---------- */
input, select {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder { color: var(--faint); }
select { cursor: pointer; }

/* ---------- Кнопки ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hi); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); }
.btn.block { width: 100%; }

.mini {
  font-size: 12.5px;
  padding: 5px 10px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
  transition: color .12s, border-color .12s;
}
.mini:hover { color: var(--text); border-color: var(--accent); }
.mini.danger:hover { color: var(--danger); border-color: var(--danger); }

/* ---------- Экран входа ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 340px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 38px 32px 30px;
  text-align: center;
  box-shadow: var(--shadow);
}
.login-logo { font-size: 46px; }
.login-card h1 { margin: 10px 0 2px; font-size: 27px; letter-spacing: .5px; }
.login-sub { color: var(--muted); margin: 0 0 24px; font-size: 13.5px; }
#pin {
  width: 100%;
  font-size: 27px;
  letter-spacing: 14px;
  text-align: center;
  padding: 12px;
  margin-bottom: 14px;
  border-radius: 10px;
}
.login-error { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 16px; }

/* ---------- Шапка ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 13px 24px;
  border-bottom: 1px solid var(--border-soft);
  background: rgba(13, 17, 23, .82);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-size: 17px; white-space: nowrap; }
.brand-sub { color: var(--faint); font-size: 12px; margin-left: 8px; }
.tabs { display: flex; gap: 6px; flex: 1; overflow-x: auto; }
.tab-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 14.5px;
  font-weight: 600;
  font-family: inherit;
  padding: 8px 14px;
  border-radius: 9px;
  cursor: pointer;
  white-space: nowrap;
}
.tab-btn:hover { color: var(--text); background: var(--panel-2); }
.tab-btn.active { color: var(--text); background: var(--accent-soft); }

/* ---------- Контент ---------- */
.content { padding: 22px 24px 70px; max-width: 1100px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; animation: fadein .18s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } }

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.toolbar .search { flex: 1; min-width: 160px; }
.h2 { margin: 0; font-size: 17px; flex: 1; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { min-width: 260px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  overflow: hidden;
}

/* ---------- Таблицы ---------- */
.tbl { width: 100%; border-collapse: collapse; }
.tbl th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--faint);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.tbl td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
  font-size: 14px;
  vertical-align: middle;
}
.tbl tbody tr:last-child td { border-bottom: none; }
.tbl tbody tr:hover { background: rgba(255, 255, 255, .02); }
.tbl .num { text-align: right; }
.tbl .right { text-align: right; }
td.name { font-weight: 600; }

.chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
}
.loc { font-weight: 600; color: var(--accent-hi); }
.mov-arrow { color: var(--faint); text-align: center; }
.empty { padding: 44px; text-align: center; color: var(--faint); }
.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- Списки (объекты / категории) ---------- */
.res-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.res-row:last-child { border-bottom: none; }
.res-row:hover { background: rgba(255, 255, 255, .02); }
.res-main { flex: 1; display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.res-main small { color: var(--faint); white-space: nowrap; }

/* ---------- История (в модалке) ---------- */
.hist { list-style: none; margin: 0; padding: 0; }
.hist li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 2px;
  border-bottom: 1px solid var(--border-soft);
  font-size: 14px;
}
.hist li:last-child { border-bottom: none; }
.hist-when { color: var(--faint); white-space: nowrap; }
.hist-path .to { color: var(--accent-hi); font-weight: 600; }

/* ---------- Модалки ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 12, .68);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 9vh 16px 16px;
  z-index: 50;
}
.modal {
  width: 100%;
  max-width: 460px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  animation: pop .16s ease;
}
@keyframes pop {
  from { transform: translateY(10px) scale(.98); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 20px;
  border-bottom: 1px solid var(--border-soft);
}
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-x {
  background: none;
  border: none;
  color: var(--faint);
  font-size: 15px;
  cursor: pointer;
  padding: 4px;
}
.modal-x:hover { color: var(--text); }
.modal-body { padding: 18px 20px; max-height: 62vh; overflow-y: auto; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border-soft);
}

/* ---------- Формы внутри модалок ---------- */
.vform { display: flex; flex-direction: column; gap: 14px; }
.vform label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
  font-weight: 600;
}
.vform input, .vform select { width: 100%; }
.hint { color: var(--faint); font-weight: 400; font-size: 12px; line-height: 1.4; }
.confirm-text { line-height: 1.55; margin: 0; }
.move-from { color: var(--muted); margin: 0 0 14px; }
.move-from b { color: var(--text); }
.loading { color: var(--faint); padding: 24px; text-align: center; }

/* ---------- Всплывающие уведомления ---------- */
#toast-root {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
}
.toast {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--ok);
  padding: 11px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  box-shadow: var(--shadow);
  animation: slidein .18s ease;
  max-width: 340px;
}
.toast.err { border-left-color: var(--danger); }
@keyframes slidein { from { transform: translateX(14px); opacity: 0; } }

/* ---------- Мелкие экраны ---------- */
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .tabs { order: 3; width: 100%; }
  .content { padding: 16px 14px 50px; }
  .inline-form { width: 100%; }
  .inline-form input { min-width: 0; flex: 1; }
  .tbl .th-num, .tbl td:nth-child(3) { display: none; }
}

/* ---------- Адресное хранение / инвентарные номера ---------- */
.inv {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  font-weight: 700;
  letter-spacing: .3px;
  color: var(--text);
  white-space: nowrap;
}
.addr { margin-top: 3px; color: var(--muted); font-size: 12px; }
.addr.muted { color: var(--faint); }
.address-form { margin-bottom: 14px; }
.address-form input { flex: 1; min-width: 0; }
.address-list { border: 1px solid var(--border-soft); border-radius: 10px; overflow: hidden; }
.empty.compact { padding: 24px; }

/* ---------- Списание / система ---------- */
textarea {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 10px 12px;
  min-height: 110px;
  resize: vertical;
  font: inherit;
  outline: none;
}
textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea::placeholder { color: var(--faint); }
.writeoff-comment { max-width: 360px; white-space: pre-wrap; color: var(--muted); }
.system-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.system-card { padding: 20px; overflow: visible; }
.system-card h3 { margin: 0 0 8px; font-size: 16px; }
.system-card p { margin: 0 0 18px; color: var(--muted); line-height: 1.55; }
.system-card input[type="file"] { display: block; width: 100%; margin-bottom: 12px; }
@media (max-width: 720px) { .system-grid { grid-template-columns: 1fr; } }

/* ---------- Выпадающее меню действий ТМЦ ---------- */
.action-cell { position: relative; }
.action-dropdown { position: relative; display: inline-block; text-align: left; }
.action-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 34px;
}
.action-trigger svg { width: 14px; height: 14px; }
.action-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  width: 190px;
  padding: 6px;
  background: #111824;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 40;
  display: none;
}
.action-dropdown.open .action-menu { display: block; animation: menuin .12s ease; }
@keyframes menuin { from { opacity: 0; transform: translateY(-4px); } }
.action-menu button {
  appearance: none;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  border-radius: 7px;
  padding: 9px 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  font: inherit;
  font-size: 13.5px;
  cursor: pointer;
}
.action-menu button:hover { background: var(--panel-2); }
.action-menu button.warn { color: #f0b35a; }
.action-menu button.danger { color: var(--danger); }
.action-menu svg {
  width: 17px;
  height: 17px;
  flex: 0 0 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.action-separator { height: 1px; background: var(--border-soft); margin: 5px 4px; }

/* Меню должно иметь возможность выходить за границы панели */
.panel { overflow: visible; }
.panel::after { content: ''; display: block; clear: both; }

/* ---------- Мобильная адаптация ---------- */
@media (max-width: 760px) {
  :root { font-size: 16px; }
  body { background: var(--bg); }

  input, select, .btn {
    min-height: 44px;
    font-size: 16px; /* предотвращает auto-zoom Safari */
  }

  .topbar {
    padding: 10px 12px 0;
    gap: 8px;
    align-items: center;
    background: rgba(13, 17, 23, .96);
  }
  .brand { flex: 1; font-size: 18px; }
  .brand-sub { display: none; }
  #logout-btn { min-height: 38px; padding: 7px 11px; font-size: 13px; }
  .tabs {
    order: 3;
    flex-basis: calc(100% + 24px);
    width: calc(100% + 24px);
    margin: 2px -12px 0;
    padding: 4px 12px 9px;
    gap: 4px;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab-btn {
    min-height: 42px;
    padding: 9px 13px;
    font-size: 13.5px;
  }

  .content { padding: 14px 12px 60px; }
  .toolbar { gap: 8px; margin-bottom: 10px; }
  .toolbar .select,
  .toolbar .search { width: 100%; flex: 1 1 100%; }
  #btn-add-item { width: 100%; }
  .h2 { flex-basis: 100%; font-size: 19px; margin-bottom: 2px; }

  .inline-form {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
  }
  .inline-form input { min-width: 0; width: 100%; }

  .panel {
    background: transparent;
    border: 0;
    border-radius: 0;
  }

  /* Таблицы становятся карточками */
  .tbl, .tbl tbody, .tbl tr, .tbl td { display: block; width: 100%; }
  .tbl thead { display: none; }
  .tbl tbody { display: grid; gap: 10px; }
  .tbl tbody tr {
    position: relative;
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: 13px;
    padding: 11px 12px;
    box-shadow: 0 5px 18px rgba(0, 0, 0, .14);
  }
  .tbl tbody tr:hover { background: var(--panel); }
  .tbl td {
    border: 0 !important;
    padding: 5px 0;
    min-height: 28px;
    display: grid;
    grid-template-columns: 108px minmax(0, 1fr);
    gap: 10px;
    align-items: start;
    text-align: left !important;
    font-size: 14px;
  }
  .tbl td::before {
    content: attr(data-label);
    color: var(--faint);
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .45px;
    padding-top: 2px;
  }
  .tbl td.name { font-size: 15.5px; }
  .tbl td.mov-arrow { display: none; }
  .tbl td.action-cell {
    display: block;
    padding-top: 9px;
    margin-top: 5px;
    border-top: 1px solid var(--border-soft) !important;
  }
  .tbl td.action-cell::before { display: none; }
  .action-dropdown, .action-trigger { width: 100%; }
  .action-trigger { min-height: 42px; }
  .action-menu {
    position: static;
    width: 100%;
    margin-top: 6px;
    box-shadow: none;
    background: var(--bg-soft);
  }
  .action-menu button { min-height: 43px; font-size: 14px; }

  .empty {
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: 13px;
    padding: 34px 18px;
  }

  /* Объекты и категории */
  .res-row {
    background: var(--panel);
    border: 1px solid var(--border-soft);
    border-radius: 12px;
    margin-bottom: 9px;
    padding: 13px;
    align-items: flex-start;
    flex-direction: column;
  }
  .res-row:last-child { border-bottom: 1px solid var(--border-soft); }
  .res-row .actions { width: 100%; justify-content: flex-start; }
  .res-row .mini { min-height: 38px; flex: 1 1 auto; }

  /* Модалки как нижняя панель */
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }
  .modal {
    max-width: none;
    max-height: 92dvh;
    border-radius: 18px 18px 0 0;
    border-left: 0;
    border-right: 0;
    border-bottom: 0;
  }
  .modal-head { padding: 15px 16px; }
  .modal-body { padding: 16px; max-height: 68dvh; }
  .modal-foot {
    padding: 12px 16px max(12px, env(safe-area-inset-bottom));
    gap: 8px;
  }
  .modal-foot .btn { flex: 1; }
  .modal-x { min-width: 38px; min-height: 38px; font-size: 18px; }

  .hist li {
    flex-direction: column;
    gap: 4px;
  }
  .hist-when { white-space: normal; font-size: 12px; }

  #toast-root {
    left: 12px;
    right: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
  }
  .toast { max-width: none; width: 100%; }

  .system-grid { display: grid; gap: 10px; }
  .system-card { background: var(--panel); border: 1px solid var(--border-soft); padding: 16px; }
  .system-card .btn, .system-card input[type="file"] { width: 100%; }
}

@media (max-width: 390px) {
  .tbl td { grid-template-columns: 92px minmax(0, 1fr); gap: 8px; }
  .inline-form { grid-template-columns: 1fr; }
  .inline-form .btn { width: 100%; }
}
@media (max-width: 720px) {
  /* Отменяем старое правило, скрывавшее третью колонку на телефонах. */
  .tbl td:nth-child(3) { display: grid; }
}

/* ---------- Карточки ТМЦ, прямые ссылки и пагинация ---------- */
.item-link {
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
}
.item-link:hover { color: var(--accent-hi); text-decoration: underline; }
.pagination {
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 12px 4px 0;
  color: var(--muted);
  font-size: 13px;
}
.pagination .mini { min-width: 38px; min-height: 32px; }
.pagination .mini:disabled { opacity: .35; cursor: default; border-color: var(--border); color: var(--faint); }
.mobile-only { display: none; }

.item-card-detail { display: grid; gap: 18px; }
.item-card-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.inv.big { font-size: 16px; }
.status-badge { border-radius: 999px; padding: 5px 9px; font-size: 12px; font-weight: 700; }
.status-badge.active { color: #65d995; background: rgba(55, 178, 108, .14); }
.status-badge.written { color: #f0b35a; background: rgba(240, 179, 90, .14); }
.item-facts { margin: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; background: var(--border-soft); border: 1px solid var(--border-soft); border-radius: 12px; overflow: hidden; }
.item-facts > div { background: var(--panel); padding: 13px 14px; min-width: 0; }
.item-facts > div.wide { grid-column: 1 / -1; }
.item-facts dt { margin: 0 0 5px; color: var(--faint); font-size: 11px; text-transform: uppercase; letter-spacing: .45px; font-weight: 700; }
.item-facts dd { margin: 0; color: var(--text); line-height: 1.45; overflow-wrap: anywhere; }
.item-card-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.item-card-actions .btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.item-card-actions svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- QR-сканер ---------- */
.scanner-wrap { position: relative; display: grid; gap: 12px; }
#qr-video { display: block; width: 100%; min-height: 280px; max-height: 58dvh; object-fit: cover; background: #05070a; border-radius: 14px; }
.scanner-frame { position: absolute; left: 18%; right: 18%; top: 18%; aspect-ratio: 1; border: 2px solid var(--accent-hi); border-radius: 18px; box-shadow: 0 0 0 999px rgba(0,0,0,.22); pointer-events: none; }
.scanner-hint { margin: 0; color: var(--muted); text-align: center; line-height: 1.45; }

@media (max-width: 760px) {
  .mobile-only { display: inline-flex; }
  #btn-scan-qr, #btn-add-item { flex: 1 1 calc(50% - 4px); width: auto; }

  /* Название — заголовок мобильной карточки, независимо от порядка колонок на desktop. */
  .tbl tbody tr { display: flex; flex-direction: column; }
  .tbl td.name {
    order: -20;
    display: block;
    font-size: 17px;
    font-weight: 700;
    padding: 2px 0 10px;
    margin-bottom: 3px;
    border-bottom: 1px solid var(--border-soft) !important;
  }
  .tbl td.name::before { display: none; }
  .tbl td.name .item-link { display: block; line-height: 1.3; }
  .tbl td[data-label="Инв. №"] { order: -10; }

  .pagination { padding-top: 10px; padding-bottom: 4px; }
  .pagination .mini { min-height: 42px; min-width: 48px; }

  .item-facts { grid-template-columns: 1fr; }
  .item-facts > div.wide { grid-column: auto; }
  .item-card-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .item-card-actions .btn { min-height: 46px; }
  .item-card-actions .btn.primary { grid-column: 1 / -1; }
  #qr-video { min-height: 52dvh; border-radius: 12px; }
}

@media (max-width: 390px) {
  #btn-scan-qr, #btn-add-item { flex-basis: 100%; }
  .item-card-actions { grid-template-columns: 1fr; }
  .item-card-actions .btn.primary { grid-column: auto; }
}
