:root {
  --bg: #f4f5f4;
  --surface: #ffffff;
  --ink: #17201d;
  --muted: #5d6965;
  --line: #dfe3e1;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e3f2ef;
  --warn: #9a5b00;
  --warn-soft: #fff4dc;
  --bad: #b42318;
  --bad-soft: #fde9e7;
  --ok: #157f3b;
  --ok-soft: #e4f5ea;
  --info: #1d4ed8;
  --info-soft: #e7eefe;
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color: var(--ink);
  font-size: 14px;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }

.shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar { background: #12201c; color: #cfe0db; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 8px 24px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 17px; color: #fff; margin-right: 14px; }
.brand-mark { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); box-shadow: inset 0 0 0 5px #12201c, inset 0 0 0 7px #5eead4; }
.main-tabs { display: flex; flex-wrap: wrap; gap: 4px; flex: 1 1 auto; }
.main-tabs a { color: #cfe0db; text-decoration: none; padding: 9px 14px; border-radius: 8px; font-weight: 500; }
.main-tabs a:hover { background: #1c2f2a; color: #fff; }
.main-tabs a.active { background: var(--accent); color: #fff; font-weight: 600; }
.top-user { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; font-size: 12.5px; }
.top-user b { color: #fff; font-weight: 600; }
.top-user a { color: #5eead4; }
.top-user .top-warn { color: #fde68a; }
.subnav { background: var(--surface); border-bottom: 1px solid var(--line); padding: 0 24px; display: flex; flex-wrap: wrap; gap: 2px; }
.subnav a { padding: 12px 14px; color: var(--muted); text-decoration: none; border-bottom: 3px solid transparent; font-weight: 500; }
.subnav a:hover { color: var(--ink); }
.subnav a.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.table-wrap { overflow-x: auto; }

main { padding: 18px 24px 60px; min-width: 0; width: 100%; max-width: 1560px; margin: 0 auto; flex: 1; }
h2 { margin: 0 0 16px; font-size: 21px; }
h3 { margin: 0 0 10px; font-size: 15px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card + .card { margin-top: 12px; }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--surface); padding: 8px 13px; border-radius: 8px; cursor: pointer; white-space: nowrap; text-decoration: none; color: var(--ink); }
.btn:hover { border-color: #b9c2bf; background: #fafbfa; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #0b5f58; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 5px 9px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

label.f { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); }
label.f > span { font-weight: 600; }
input[type=text], input[type=password], input[type=number], input[type=date], input:not([type]), select, textarea {
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; color: var(--ink); font-size: 14px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
textarea { resize: vertical; min-height: 64px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 13.5px; color: var(--ink); cursor: pointer; }

.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: #eef0ef; color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }

.alert { border-radius: 8px; padding: 9px 11px; font-size: 13px; line-height: 1.4; background: var(--warn-soft); color: #6b4000; }
.alert + .alert { margin-top: 6px; }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.info { background: var(--info-soft); color: #173aa8; }

/* ----- Накладна ----- */
.order-bar { display: flex; gap: 10px; align-items: center; margin: -18px -24px 16px; padding: 12px 24px; flex-wrap: wrap; position: sticky; top: 0; z-index: 20; background: rgba(244, 245, 244, .96); backdrop-filter: blur(6px); border-bottom: 1px solid var(--line); }
.bar-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-left: auto; }
.bar-title { margin: 0; font-size: 18px; }
.order-bar form { display: flex; gap: 8px; }
.order-bar input { width: 230px; font-size: 16px; padding: 10px 12px; }
.order-bar .btn { padding: 10px 16px; font-size: 15px; }
.inv-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; align-items: start; }
.preview { overflow: auto; padding-bottom: 10px; }
.preview .paper { margin: 0 auto 18px; box-shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 28px rgba(16,32,28,.10); }
.panel { position: sticky; top: 84px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 100px); overflow: auto; padding-right: 2px; }
.panel .card { padding: 14px; }
.panel .card + .card { margin-top: 0; }
.panel h3 { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { justify-content: center; }
.actions .wide { grid-column: span 2; }
.vlist { display: flex; flex-direction: column; font-size: 12.5px; max-height: 240px; overflow: auto; }
.vlist button { text-align: left; border: 0; background: none; padding: 7px 6px; border-radius: 6px; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.vlist button:hover { background: #f1f4f3; }
.vlist .v-r { color: var(--ink); font-weight: 600; }
.vlist .v-d, .vlist .v-f { color: var(--muted); }
.receipt-input { display: flex; gap: 6px; margin-top: 8px; }
.empty { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty .big { font-size: 18px; color: var(--ink); margin-bottom: 6px; }
pre.raw { background: #0f1a17; color: #c7e8df; padding: 12px; border-radius: 8px; font-size: 12px; max-height: 360px; overflow: auto; white-space: pre-wrap; }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.spinner { width: 18px; height: 18px; border: 2px solid #cfe0db; border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ----- Таблиці довідників ----- */
table.list { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
table.list th, table.list td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: left; font-size: 13.5px; vertical-align: middle; }
table.list th { background: #f8f9f8; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
table.list tr:last-child td { border-bottom: 0; }
table.list tr.click { cursor: pointer; }
table.list tr.click:hover td { background: #f6f8f7; }
table.list td select, table.list td input { padding: 6px 8px; }
.head-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.head-row h2 { margin: 0; }

.fop-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 12px; }
.fop-card { cursor: pointer; display: flex; flex-direction: column; gap: 6px; }
.fop-card:hover { border-color: #a9c9c2; }
.fop-card .name { font-weight: 600; font-size: 15px; }
.fop-card .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.fop-card.inactive { opacity: .55; }

.form-page { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; align-items: start; }
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.img-field { display: flex; gap: 12px; align-items: center; }
.img-prev { width: 120px; height: 70px; border: 1px dashed var(--line); border-radius: 8px; display: grid; place-items: center; background: #fbfcfb repeating-conic-gradient(#f1f3f2 0 25%, transparent 0 50%) 50% / 14px 14px; overflow: hidden; }
.img-prev img { max-width: 100%; max-height: 100%; }
.tpl-preview { transform-origin: top left; }
.tpl-preview-wrap { overflow: hidden; border-radius: 8px; }

/* ----- Модальне вікно ----- */
.modal-bg { position: fixed; inset: 0; background: rgba(12, 22, 19, .45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--surface); border-radius: 12px; width: min(920px, 100%); max-height: calc(100vh - 40px); display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.modal-head h3 { margin: 0; }
.modal-body { padding: 16px 18px; overflow: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.items-edit { width: 100%; border-collapse: collapse; margin-top: 8px; }
.items-edit th { font-size: 12px; color: var(--muted); text-align: left; padding: 4px; }
.items-edit td { padding: 3px 4px; }
.items-edit input { padding: 6px 8px; }

#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #12201c; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13.5px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.bad { background: var(--bad); }

#print-root { display: none; }

@media (max-width: 1150px) {
  .inv-layout, .form-page { grid-template-columns: minmax(0, 1fr); }
  .panel { position: static; max-height: none; }
}
@media (max-width: 760px) {
  .topbar, .subnav { padding-left: 16px; padding-right: 16px; }
  main { padding: 14px 16px 40px; }
  .order-bar { margin: -14px -16px 14px; padding: 10px 16px; }
  .bar-actions { margin-left: 0; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
}

@media print {
  body { background: #fff; }
  .shell, #toast, .modal-bg { display: none !important; }
  #print-root { display: block; }
}

/* ----- Вхід ----- */
body.auth-mode .shell { display: none; }
#auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(circle at 20% 10%, #dcefea, transparent 50%), var(--bg); }
.auth-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 26px; display: flex; flex-direction: column; gap: 14px; box-shadow: 0 10px 40px rgba(16,32,28,.08); }
.auth-card h2 { margin: 0; }
.auth-card .btn { justify-content: center; padding: 10px; }
.brand.dark { color: var(--ink); padding: 0; }
.brand.dark .brand-mark { box-shadow: inset 0 0 0 5px #fff, inset 0 0 0 7px var(--accent); }


/* ----- Спільне: списки на телефоні, бейджі ----- */
table.list th.r, table.list td.r { text-align: right; }
.badge.muted { background: #eef0ef; color: var(--muted); }
@media (max-width: 760px) {
  table.list { display: block; overflow-x: auto; }
  .head-row { flex-wrap: wrap; }
}
/* ===== Доступність і зручність ===== */
/* Видимий фокус з клавіатури для всіх кнопок, посилань і рядків таблиць */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.topbar :focus-visible, .main-tabs a:focus-visible { outline-color: #5eead4; }
table.list tr.click:focus-visible { outline-offset: -2px; }
table.list tr.click:focus-visible td { background: #f6f8f7; }
/* Кнопка з іконкою (✕) — квадратна */
.btn.icon { justify-content: center; min-width: 32px; min-height: 32px; padding: 0; }
/* Підтвердження незворотної дії */
.btn.danger.solid { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.danger.solid:hover { background: #8f1c13; }
/* Сенсорні екрани: області натискання не менше 44 px */
@media (pointer: coarse) {
  .btn, .chip, .main-tabs a, .subnav a { min-height: 44px; }
  .btn.icon { min-width: 44px; }
}
/* Менше руху для тих, хто так налаштував систему */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; animation-duration: 0s !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
  .tile:hover { transform: none; }
  .spinner { animation: spin 1.5s linear infinite !important; }
}

