/* Вигляд підлаштовується автоматично під ширину екрана:
   телефон (< 700px) · планшет вертикально (700–1023px) · планшет горизонтально й комп'ютер (≥ 1024px). */

:root {
  --bg: #1b1b1c;
  --panel: #252527;
  --panel-2: #2e2e31;
  --line: #3d3d42;
  --text: #f2f2f2;
  --muted: #a6a6ad;
  --accent: #ee9a99;
  --accent-ink: #2a1414;
  --ok: #6fcf8e;
  --warn: #ff7b72;
  --radius: 12px;
  --side-w: 220px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 18px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; }

/* ---------- Поля й кнопки ---------- */
.lbl { display: block; margin: 18px 0 8px; color: var(--muted); font-size: 16px; }
.inp {
  width: 100%; min-height: 56px; padding: 12px 14px;
  background: var(--panel); color: var(--text);
  border: 1.5px solid var(--line); border-radius: var(--radius);
  font: inherit; font-size: 19px; outline: none;
  -webkit-appearance: none; appearance: none;
}
.inp:focus { border-color: var(--accent); }
select.inp {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 22px) 50%, calc(100% - 16px) 50%;
  background-size: 6px 6px; background-repeat: no-repeat; padding-right: 40px;
}
.btn {
  min-height: 56px; padding: 0 18px; border-radius: var(--radius);
  background: var(--panel-2); border: 1.5px solid var(--line);
  cursor: pointer; touch-action: manipulation; user-select: none;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--warn); border-color: #6a3330; background: transparent; }
.btn.big { width: 100%; min-height: 60px; font-size: 20px; margin-top: 22px; }
.icon-btn {
  width: 48px; height: 48px; flex: none; display: grid; place-items: center;
  background: none; border: 0; color: var(--text); border-radius: 50%; cursor: pointer;
}
.icon-btn:active, .icon-btn:hover { background: var(--panel-2); }
.hint { color: var(--muted); font-size: 15px; margin-top: 18px; }

/* ---------- Вхід ---------- */
.screen { min-height: 100%; display: flex; align-items: flex-start; justify-content: center; padding: calc(32px + var(--safe-t)) 18px 32px; }
.login-box { width: 100%; max-width: 420px; }
.login-box h1 { margin: 6px 0 10px; font-size: 28px; }
.logo { width: 72px; height: 72px; border-radius: 18px; display: block; }

/* ---------- Каркас: телефон ---------- */
#main { min-height: 100%; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + var(--safe-t)) 10px 8px 18px;
  background: #1e1e1e; border-bottom: 1px solid var(--line);
}
.brand { display: none; font-weight: 700; font-size: 20px; color: var(--accent); }
.who { flex: 1; min-width: 0; }
.who-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-role { color: var(--muted); font-size: 14px; }
.demo-badge { background: #4a3a10; color: #ffd36b; font-size: 12px; padding: 3px 8px; border-radius: 6px; font-weight: 700; }
main { width: 100%; max-width: 560px; margin: 0 auto; padding: 14px 16px calc(100px + var(--safe-b)); }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; background: #1e1e1e; border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px 4px; background: none; border: 0; color: var(--muted); font-size: 14px; cursor: pointer;
}
.tab.active { color: var(--accent); }

.warning { margin-bottom: 12px; padding: 12px 14px; border-radius: var(--radius); background: #4a3a10; color: #ffe2a0; font-size: 15px; }

/* ---------- Новий запис ---------- */
.scan-btn {
  width: 100%; min-height: 76px; display: flex; align-items: center; justify-content: center; gap: 12px;
  font-size: 21px; font-weight: 650; background: var(--panel-2); border-color: var(--accent); color: var(--accent);
}
.scanrow { display: flex; gap: 8px; margin-top: 10px; }
.scanrow .inp { flex: 1; min-width: 0; font-size: 17px; }
.find-btn { min-width: 64px; }

.item-card {
  margin-top: 14px; padding: 14px 16px; border-radius: var(--radius);
  background: var(--panel); border-left: 5px solid var(--accent);
}
.item-name { font-size: 20px; font-weight: 650; }
.item-meta { margin-top: 6px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; color: var(--muted); }
.item-meta b { color: var(--text); font-size: 22px; }
.refreshing { font-size: 14px; }
.badge { display: inline-block; font-size: 14px; font-weight: 700; padding: 3px 10px; border-radius: 99px; white-space: nowrap; }
.badge.ok { background: #183b26; color: var(--ok); }
.badge.order { background: #4a1f1d; color: var(--warn); }
.msg { margin-top: 14px; padding: 14px 16px; border-radius: var(--radius); background: #4a1f1d; color: #ffd0cc; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 8px; }
.seg .btn { min-height: 64px; padding: 0 4px; font-size: 17px; overflow-wrap: anywhere; }
.seg .btn.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }

.qty { display: flex; gap: 8px; }
.qbtn { width: 72px; min-width: 72px; font-size: 32px; line-height: 1; }
.qinp { flex: 1; min-width: 0; text-align: center; font-size: 26px; font-weight: 650; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips .btn { min-height: 54px; }
.chips .btn.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }

.save-btn { min-height: 68px; font-size: 22px; }

.record-side { margin-top: 26px; }
.side-title { color: var(--muted); font-size: 15px; margin-bottom: 8px; }
.recent { display: flex; flex-direction: column; gap: 6px; }
.recent-row { padding: 10px 12px; background: var(--panel); border-radius: 10px; font-size: 15px; }
.recent-row b { font-weight: 650; }
.recent-row .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.recent-row.out { border-left: 4px solid var(--warn); }
.recent-row.in { border-left: 4px solid var(--ok); }
.recent-row.ret { border-left: 4px solid #7fb2ff; }
.recent-row.failed { background: #3a1f1d; }
.recent-row .st { display: inline-block; width: 20px; font-weight: 700; }
.recent-row .st.pending { color: #ffd36b; }
.recent-row .st.ok { color: var(--ok); }
.recent-row .st.error { color: var(--warn); }
.rec-err { margin-top: 6px; color: #ffd0cc; font-size: 14px; }
.link-btn { background: none; border: 0; color: var(--accent); font-size: 14px; font-weight: 600; padding: 6px 8px; cursor: pointer; text-decoration: underline; }
.sync-badge { background: #4a3a10; color: #ffd36b; border: 0; border-radius: 99px; padding: 6px 12px; font-size: 15px; font-weight: 700; cursor: pointer; }
/* На телефоні показуємо лише 3 останні записи. */
.recent-row:nth-child(n+4) { display: none; }

/* ---------- Таблиці (Залишки, Витрати) ---------- */
.toolbar { display: flex; gap: 6px; align-items: center; }
.toolbar .inp { flex: 1; }
.check { display: flex; align-items: center; gap: 10px; margin: 14px 2px 4px; color: var(--muted); }
.check input { width: 24px; height: 24px; accent-color: var(--accent); }
.summary { color: var(--muted); font-size: 15px; margin: 8px 2px; }
.list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.row-btn {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; width: 100%;
  padding: 14px; background: var(--panel); border: 0; border-radius: var(--radius); text-align: left; cursor: pointer;
}
.row-btn:hover { background: var(--panel-2); }
.row-title { display: block; font-weight: 600; }
.row-sub { display: block; color: var(--muted); font-size: 14px; }
.phone-only { display: none; }

.dtable { width: 100%; border-collapse: separate; border-spacing: 0 8px; margin-top: 4px; }
.dtable th { text-align: left; color: var(--muted); font-weight: 500; font-size: 14px; padding: 6px 12px; white-space: nowrap; cursor: pointer; user-select: none; }
.dtable th.sorted::after { content: " ▲"; font-size: 11px; }
.dtable th.sorted.desc::after { content: " ▼"; }
.dtable td { background: var(--panel); padding: 12px; vertical-align: middle; }
.dtable td:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.dtable td:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
.dtable .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.dtable td.big { font-size: 22px; font-weight: 700; }
.dtable tr.order td { background: #2f2222; }
.dtable tr.total td { background: transparent; font-weight: 700; font-size: 19px; border-top: 1px solid var(--line); }
.dtable tr.click { cursor: pointer; }
.dtable tr.click:hover td { background: var(--panel-2); }
.dtable .sub { color: var(--muted); font-size: 14px; }

/* Телефон: рядки таблиць стають картками. */
@media (max-width: 699px) {
  .dtable thead { display: none; }
  .dtable, .dtable tbody { display: block; }
  .dtable tr {
    display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
    background: var(--panel); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 8px;
  }
  .dtable tr.order { background: #2f2222; box-shadow: inset 0 0 0 1px #6a3330; }
  .dtable tr.total { background: transparent; }
  .dtable td { background: none !important; padding: 0; border-radius: 0 !important; }
  .dtable td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: 13px; }
  .dtable td.cell-main { grid-column: 1; font-weight: 600; }
  .dtable td.cell-num { grid-column: 2; grid-row: 1 / span 3; text-align: right; }
  .dtable td.cell-sub { grid-column: 1; color: var(--muted); font-size: 14px; }
  .dtable .col-wide, .dtable .col-mid { display: none; }
  .phone-only { display: inline; }
}
@media (min-width: 1024px) {
  .list { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }
}

/* ---------- Звичайна таблиця (Витрати): однаковий шрифт, підсумок у нижньому рядку ---------- */
.table-wrap { margin-top: 14px; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.grid { width: 100%; border-collapse: collapse; font-size: 16px; font-variant-numeric: tabular-nums; }
.grid th, .grid td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--line); }
.grid thead th { background: var(--panel-2); color: var(--muted); font-weight: 600; white-space: nowrap; }
.grid tbody tr:nth-child(even) td { background: #212123; }
.grid tbody tr.click { cursor: pointer; }
.grid tbody tr.click:hover td { background: var(--panel-2); }
.grid tbody tr.click td:first-child { color: var(--accent); }
.grid tfoot td { background: var(--panel-2); font-weight: 700; border-bottom: 0; }
.grid tfoot td.sum { color: var(--accent); }
.grid .num { text-align: right; white-space: nowrap; }
.grid .empty { color: var(--muted); text-align: center; padding: 22px; }
@media (max-width: 699px) {
  .grid { font-size: 15px; }
  .grid th, .grid td { padding: 10px 10px; }
  .grid .col-wide, .grid .col-mid { display: none; }
}

.stock-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }
.orders-btn { display: inline-flex; align-items: center; gap: 10px; margin-top: 10px; border-color: var(--accent); color: var(--accent); }
@media (max-width: 699px) { .orders-btn { width: 100%; justify-content: center; } }

/* ---------- Сканер ---------- */
.scanner {
  position: fixed; inset: 0; z-index: 20; background: #000;
  display: flex; flex-direction: column; padding: calc(12px + var(--safe-t)) 14px calc(14px + var(--safe-b));
}
.scanner-top { text-align: center; padding: 8px 0 12px; font-size: 18px; }
#reader { width: 100%; max-width: 900px; margin: 0 auto; flex: 1; min-height: 0; overflow: hidden; border-radius: var(--radius); background: #111; }
#reader video { width: 100% !important; height: 100% !important; object-fit: cover; }
.scanner-msg { min-height: 24px; color: #ffd0cc; text-align: center; margin-top: 10px; }
.scanner .btn.big { margin: 10px auto 0; max-width: 520px; }

/* ---------- Діалог, сповіщення ---------- */
.modal { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.6); display: flex; align-items: flex-end; justify-content: center; }
.modal-box {
  width: 100%; max-width: 560px; max-height: 92vh; overflow: auto;
  background: var(--panel); border-radius: 18px 18px 0 0; padding: 20px 18px calc(20px + var(--safe-b));
}
.modal-box h2 { margin: 0 0 6px; font-size: 22px; }
.modal-box .btn.big { margin-top: 12px; }
.modal-box .btn.big:first-of-type { margin-top: 22px; }
.modal-box .hint.top { margin-top: 0; }
.temp-pass { font: 700 30px/1.2 ui-monospace, Menlo, Consolas, monospace; letter-spacing: 2px; text-align: center; padding: 16px; background: var(--bg); border-radius: var(--radius); margin: 14px 0; user-select: all; }
.menu-item { display: block; width: 100%; text-align: left; padding: 18px 6px; background: none; border: 0; border-bottom: 1px solid var(--line); font-size: 19px; cursor: pointer; }
.menu-list { margin-top: 12px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 40;
  bottom: calc(90px + var(--safe-b)); width: calc(100% - 32px); max-width: 520px;
  padding: 16px 18px; border-radius: var(--radius); font-size: 18px; font-weight: 600;
  background: #183b26; color: #c9f5d6; box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.toast.err { background: #4a1f1d; color: #ffd0cc; }
.loader { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.35); display: grid; place-items: center; }
.spinner { width: 46px; height: 46px; border: 4px solid #555; border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Планшет вертикально ---------- */
@media (min-width: 700px) {
  main { max-width: 760px; padding-left: 24px; padding-right: 24px; }
  .tab { font-size: 15px; padding: 12px 4px; }
  .modal { align-items: center; }
  .modal-box { border-radius: 18px; }
  .recent-row:nth-child(n+4) { display: block; }
  .recent-row:nth-child(n+6) { display: none; }
  .add-user { max-width: 360px; }
}

/* ---------- Планшет горизонтально й комп'ютер ---------- */
@media (min-width: 1024px) {
  .brand { display: block; width: calc(var(--side-w) - 18px); }
  .tabs {
    top: 65px; bottom: 0; right: auto; width: var(--side-w);
    flex-direction: column; gap: 4px; padding: 14px 10px;
    border-top: 0; border-right: 1px solid var(--line);
  }
  .tab { flex: none; flex-direction: row; gap: 12px; padding: 14px 14px; border-radius: 10px; font-size: 17px; text-align: left; }
  .tab.active { background: var(--panel-2); }
  .tab:hover { background: var(--panel); }
  main { width: auto; max-width: none; margin: 0; margin-left: var(--side-w); padding: 22px 32px 40px; }
  #main.no-tabs main { margin-left: auto; margin-right: auto; max-width: 1100px; }
  .toast { bottom: 30px; }

  .record-layout { display: grid; grid-template-columns: minmax(0, 620px) minmax(280px, 1fr); gap: 32px; align-items: start; max-width: 1200px; }
  .record-side { margin-top: 0; position: sticky; top: 90px; }
  .recent-row:nth-child(n+4) { display: block; }
  .recent-row:nth-child(n+11) { display: none; }

  #view-stock, #view-expenses, #view-users { max-width: 1200px; }
  .toolbar .inp { max-width: 520px; }
  .add-user { max-width: 320px; margin-top: 0; }
}
