:root {
  --bg: #f4f6f9; --panel: #ffffff; --text: #1b2430; --muted: #66707d; --line: #e2e6ec;
  --accent: #1f3a5f; --accent-2: #2f6fb3; --accent-soft: #e8f0fa;
  --me: #e8f0fa; --them: #ffffff;
  --ok: #1e7b4a; --ok-bg: #e3f4ea; --warn: #9a5b00; --warn-bg: #fff1d6;
  --danger: #b42318; --danger-bg: #fde8e6; --info: #2f5b8a; --info-bg: #e6eef8; --muted-bg: #eef0f3;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a; --panel: #171e26; --text: #e6eaf0; --muted: #9aa4b1; --line: #26303b;
    --accent: #8fb4e3; --accent-2: #8fb4e3; --accent-soft: #1d2b3c;
    --me: #1d2b3c; --them: #1c242d;
    --ok: #6fcf97; --ok-bg: #16301f; --warn: #f2c66d; --warn-bg: #352a10;
    --danger: #ff8a80; --danger-bg: #3a1714; --info: #9cc3ee; --info-bg: #182a3e; --muted-bg: #222b35;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent-2); text-decoration: none; }
code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; }
h1 { font-size: 22px; margin: 0 0 16px; } h2 { font-size: 17px; margin: 0 0 12px; } h3 { font-size: 15px; margin: 0 0 10px; }
.muted { color: var(--muted); } .small { font-size: 13px; }
.inline { display: inline; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px;
  background: var(--accent); color: #fff; position: sticky; top: 0; z-index: 10; }
@media (prefers-color-scheme: dark) { .topbar { background: #14263d; } }
.brand { color: #fff; font-weight: 700; white-space: nowrap; }
.acct { font-weight: 500; opacity: .75; font-size: 13px; margin-left: 6px; }
.topbar nav { display: flex; gap: 4px; align-items: center; overflow-x: auto; }
.topbar nav a, .linkbtn { color: #fff; opacity: .85; padding: 6px 10px; border-radius: 8px; white-space: nowrap;
  background: none; border: 0; font: inherit; cursor: pointer; }
.topbar nav a.on, .topbar nav a:hover, .linkbtn:hover { opacity: 1; background: rgba(255,255,255,.14); }

.flashes { padding: 8px 16px 0; }
.flash { padding: 10px 12px; border-radius: var(--radius); margin-bottom: 8px; background: var(--info-bg); color: var(--info); }
.flash.ok { background: var(--ok-bg); color: var(--ok); } .flash.error { background: var(--danger-bg); color: var(--danger); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent-2); }
.btn.primary { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #0f141a; } }
.btn.danger { color: var(--danger); } .btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: progress; }
.chip { flex-shrink: 0; border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 999px; padding: 4px 11px;
  font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap; }
.chip.on, .chip:hover { background: var(--accent-soft); border-color: var(--accent-2); color: var(--accent-2); }

.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); } .badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.danger { background: var(--danger-bg); color: var(--danger); } .badge.info { background: var(--info-bg); color: var(--info); }
.badge.muted { background: var(--muted-bg); color: var(--muted); }
.badge.awaiting { background: var(--accent-soft); color: var(--accent-2); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
input, textarea { width: 100%; font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 9px 11px; }
input:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent-2); }
label { display: block; font-weight: 600; margin: 10px 0 6px; }

/* logowanie */
body.center main { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: min(360px, 100%); } .login .btn { width: 100%; margin-top: 14px; }

/* układ skrzynki */
body.app { overflow: hidden; }
body.app main { height: calc(100dvh - 52px); }
.layout { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; height: 100%; }
.list { border-right: 1px solid var(--line); background: var(--panel); overflow-y: auto; display: flex; flex-direction: column; }
.filters { flex-shrink: 0; display: flex; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--line); overflow-x: auto;
  position: sticky; top: 0; background: var(--panel); z-index: 1; }
.thread { flex-shrink: 0; display: block; padding: 11px 14px; border-bottom: 1px solid var(--line); color: var(--text); }
.thread:hover { background: var(--bg); } .thread.active { background: var(--accent-soft); }
.thread .row1 { display: flex; justify-content: space-between; gap: 8px; }
.thread .who { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread.unread .who { font-weight: 700; }
.thread time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.thread .snippet { color: var(--muted); font-size: 13px; margin-top: 2px; overflow: hidden; display: -webkit-box;
  -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.thread.unread .snippet { color: var(--text); }
.thread .badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.thread .badges:empty { display: none; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); margin-right: 7px; vertical-align: middle; }
.pager { display: flex; justify-content: space-between; gap: 8px; padding: 12px; margin-top: auto; }
.empty, .placeholder { color: var(--muted); padding: 24px; text-align: center; }
.placeholder { display: grid; place-items: center; height: 100%; }

.detail { overflow: hidden; display: flex; flex-direction: column; min-width: 0; }
.thread-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.thread-head .title { flex: 1; min-width: 0; } .thread-head h2 { margin: 0; overflow: hidden; text-overflow: ellipsis; }
.back { display: none; font-size: 22px; padding: 0 6px; color: var(--text); }
.thread-body { flex: 1; display: grid; grid-template-columns: 1fr minmax(280px, 340px); min-height: 0; }
.conversation { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: min(680px, 88%); } .msg.me { align-self: flex-end; } .msg.them { align-self: flex-start; }
.bubble { padding: 10px 13px; border-radius: 12px; border: 1px solid var(--line); background: var(--them); }
.msg.me .bubble { background: var(--me); border-color: transparent; }
.bubble .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble .subject { font-weight: 600; margin-bottom: 4px; }
.attachments { margin: 8px 0 0; padding: 0; list-style: none; font-size: 13px; color: var(--muted); }
.msg .meta { font-size: 12px; color: var(--muted); margin-top: 4px; padding: 0 4px; }
.msg.me .meta { text-align: right; }

.reply { border-top: 1px solid var(--line); background: var(--panel); padding: 10px 16px 12px; }
.tpl-chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; }
.reply textarea { resize: vertical; min-height: 84px; }
.reply-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }

.orders { border-left: 1px solid var(--line); overflow-y: auto; padding: 14px; background: var(--bg); }
.order { margin-bottom: 12px; padding: 12px; }
.order summary { display: flex; justify-content: space-between; gap: 8px; align-items: center; cursor: pointer; font-weight: 600; list-style: none; }
.order summary::-webkit-details-marker { display: none; }
.order dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 12px 0; font-size: 14px; }
.order dt { color: var(--muted); } .order dd { margin: 0; overflow-wrap: anywhere; }
.order .note { white-space: pre-wrap; background: var(--warn-bg); padding: 6px 8px; border-radius: 6px; }
.items { list-style: none; padding: 10px 0 0; margin: 0 0 10px; border-top: 1px solid var(--line); font-size: 14px; }
.items li { display: flex; justify-content: space-between; gap: 10px; padding: 3px 0; }
.items li span:last-child { white-space: nowrap; }
.selectable { user-select: all; }

/* strony */
.page { padding: 20px 16px 40px; max-width: 1100px; margin: 0 auto; }
.page.narrow { max-width: 720px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.tpl { margin-bottom: 12px; }
.tpl-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.tpl-head span { display: flex; gap: 6px; }
.tpl-body { white-space: pre-wrap; font: inherit; color: var(--muted); margin: 10px 0 0; overflow-wrap: anywhere; }
.form textarea { resize: vertical; }
.ph-list { display: flex; flex-wrap: wrap; gap: 6px; }
.row-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0 0 16px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; }
.scope { background: var(--muted-bg); padding: 1px 6px; border-radius: 5px; display: inline-block; margin: 0 2px 4px 0; }

/* tablet i telefon: rozmowa przewija się jako jedna strona, zamówienie nad wiadomościami */
@media (max-width: 1180px) {
  .detail { display: block; overflow-y: auto; }
  .thread-head { position: sticky; top: 0; z-index: 2; }
  .thread-body { display: flex; flex-direction: column; }
  .orders { order: -1; border-left: 0; border-bottom: 1px solid var(--line); overflow: visible; }
  .conversation { min-height: 0; }
  .messages { overflow: visible; }
  .reply { position: sticky; bottom: 0; z-index: 2; box-shadow: 0 -2px 8px rgba(16,24,40,.06); }
}
/* telefon: jedna kolumna */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .layout .detail { display: none; }
  .layout.has-detail .list { display: none; }
  .layout.has-detail .detail { display: block; }
  .back { display: block; }
  .thread-head { flex-wrap: wrap; padding: 8px 12px; }
  .thread-head form { width: 100%; } .thread-head form .btn { width: 100%; }
  .orders { max-height: none; padding: 10px; }
  .order:not([open]) { margin-bottom: 8px; }
  .messages { padding: 12px; } .msg { max-width: 92%; }
  .reply { padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); }
  .grid2 { grid-template-columns: 1fr; }
  .topbar { padding: 8px 10px; gap: 6px; } .acct, .brand-full { display: none; }
  .topbar nav a, .linkbtn { padding: 6px 7px; font-size: 14px; }
  .orders h3 { display: none; }
  body.app main { height: calc(100dvh - 48px); }
}
