: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 { display: grid; grid-template-columns: 210px 1fr; min-height: 100vh; }
.side { background: #12201c; color: #cfe0db; padding: 18px 12px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: #fff; padding: 4px 10px 18px; }
.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; }
.side a { color: #cfe0db; text-decoration: none; padding: 9px 10px; border-radius: 8px; }
.side a:hover { background: #1c2f2a; color: #fff; }
.side a.active { background: var(--accent); color: #fff; }
.side-sep { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: #7d948e; padding: 16px 10px 6px; }
.side-foot { margin-top: auto; font-size: 12px; color: #8fa8a1; padding: 10px; line-height: 1.4; }
.side-foot b { color: #fff; font-weight: 600; }

main { padding: 22px 26px 60px; min-width: 0; }
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 #99d5cc; 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: 11.5px; 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-bottom: 18px; flex-wrap: wrap; }
.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: 16px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 32px); 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: 700; 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: 1fr; }
  .panel { position: static; max-height: none; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; }
  .side-sep, .side-foot { display: none; }
  main { padding: 16px; }
  .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); }

.side-user { display: flex; flex-direction: column; margin-top: 10px; }
.side-user span { color: #8fa8a1; }
.side-links { display: flex; gap: 12px; margin-top: 6px; }
.side .side-links a { padding: 0; color: #5eead4; font-size: 12px; background: none; }

/* ----- Рахунки: плитки ----- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; }
.tile { display: flex; gap: 16px; align-items: flex-start; padding: 22px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); text-decoration: none; color: var(--ink); transition: border-color .15s, box-shadow .15s, transform .15s; }
.tile:hover { border-color: var(--accent); box-shadow: 0 6px 24px rgba(15,118,110,.12); transform: translateY(-1px); }
.tile-ico { width: 46px; height: 46px; flex: none; border-radius: 12px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-size: 22px; font-weight: 700; }
.tile-title { font-weight: 700; font-size: 16px; margin-bottom: 4px; }
.tile-text { color: var(--muted); font-size: 13px; line-height: 1.45; }

/* ----- Рахунки: рядки товарів (назва на всю ширину, під нею кількість і ціна) ----- */
.ri { display: grid; grid-template-columns: 18px 1fr 1fr 30px; grid-template-areas: "n name name name" ". qty price del"; gap: 5px; align-items: center; padding: 7px 0; border-bottom: 1px dashed var(--line); }
.ri input { padding: 6px 8px; font-size: 13px; }
.ri .ri-n { grid-area: n; font-size: 11px; color: var(--muted); text-align: right; }
.ri [data-k=name] { grid-area: name; }
.ri [data-k=qty] { grid-area: qty; }
.ri [data-k=price] { grid-area: price; }
.ri [data-rdel] { grid-area: del; padding: 5px 0; justify-content: center; }
.ri-head { grid-template-areas: ". qty price ."; font-size: 11px; color: var(--muted); font-weight: 600; padding: 0 0 2px; border: 0; }
.ri-head span:nth-child(2) { grid-area: qty; }
.ri-head span:nth-child(3) { grid-area: price; }
