:root {
  --paper: #f4f5f2; --card: #ffffff; --ink: #15171a; --muted: #62686e; --line: #dde0da; --hover: #eceee9;
  --thread: #2c4fa3; --thread-ink: #fff; --thread-soft: #e6ecf8;
  --ok: #1d7444; --ok-bg: #e3f2e8; --warn: #8a5a00; --warn-bg: #fbf0d5; --err: #b42318; --err-bg: #fbe9e7;
  --note: #fff6d6; --note-ink: #5c4a00;
  --tg: #229ed9; --max: #6a4cf5; --email: #c2571a;
  --r: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --paper: #16181b; --card: #1f2226; --ink: #eceff1; --muted: #9aa2a8; --line: #30353a; --hover: #282c31;
    --thread: #86a2f2; --thread-ink: #0e1430; --thread-soft: #232c45;
    --ok: #6fd39a; --ok-bg: #173323; --warn: #f0c060; --warn-bg: #3a2f12; --err: #ff8a7a; --err-bg: #3a1b17;
    --note: #3a3313; --note-ink: #f2dd8c; --tg: #4cb6ea; --max: #9d87ff; --email: #f08c4f; color-scheme: dark; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; width: 100%; overflow-x: hidden; }
:root { padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); }
body { background: var(--paper); color: var(--ink); font: 15px/1.45 "Golos Text", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; overflow: hidden; }
#app { height: 100%; display: flex; flex-direction: column; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--thread); }
:focus-visible { outline: 2px solid var(--thread); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* кнопки и поля */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 38px; padding: 0 14px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); cursor: pointer; font-weight: 500; white-space: nowrap; text-decoration: none; color: var(--ink); }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--thread); border-color: var(--thread); color: var(--thread-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--err); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.icon { width: 38px; padding: 0; }
.btn.sm { min-height: 30px; padding: 0 10px; font-size: 13px; border-radius: 8px; }
input[type=text], input[type=email], input[type=tel], input[type=number], input[type=search], input[type=password], textarea, select {
  width: 100%; min-height: 38px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--thread); }
textarea { resize: vertical; }
label.f { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.f > input, label.f > textarea { color: var(--ink); font-size: 15px; }

/* верхняя панель */
.top { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: var(--card); border-bottom: 1px solid var(--line); flex: none; }
.brand { font-weight: 700; letter-spacing: -.2px; margin-right: 6px; white-space: nowrap; }
.brand span { color: var(--muted); font-weight: 500; }
.tabs { display: flex; gap: 2px; }
.tabs button { white-space: nowrap; border: 0; background: transparent; padding: 7px 12px; border-radius: 9px; cursor: pointer; font-weight: 500; color: var(--muted); position: relative; }
.tabs button[aria-current=page] { color: var(--ink); background: var(--hover); }
.badge { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--thread); color: var(--thread-ink); font-size: 12px; font-weight: 600; line-height: 1; }
.conn { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.ok { background: var(--ok); } .dot.err { background: var(--err); }

/* три колонки */
.work { flex: 1; min-height: 0; display: grid; grid-template-columns: 340px minmax(0, 1fr) 320px; }
.col { min-height: 0; min-width: 0; display: flex; flex-direction: column; background: var(--card); }
.list { border-right: 1px solid var(--line); }
.side { border-left: 1px solid var(--line); overflow-y: auto; }
.chat { background: var(--paper); }

/* список диалогов */
.lhead { padding: 10px 12px 6px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.lhead .row { display: flex; gap: 6px; }
.chips { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; padding-bottom: 4px; }
.chips::-webkit-scrollbar { display: none; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 16px; padding: 3px 10px; font-size: 13px; cursor: pointer; white-space: nowrap; color: var(--muted); }
.chip[aria-pressed=true] { background: var(--ink); border-color: var(--ink); color: var(--card); }
.rows { overflow-y: auto; flex: 1; }
.crow { display: grid; grid-template-columns: 42px minmax(0, 1fr) auto; gap: 2px 10px; align-items: center; width: 100%; padding: 10px 12px; border: 0; border-bottom: 1px solid var(--line);
  background: transparent; text-align: left; cursor: pointer; position: relative; }
.crow:hover { background: var(--hover); }
.crow[aria-current=true] { background: var(--thread-soft); }
.crow[aria-current=true]::before { content: ""; position: absolute; left: 3px; top: 8px; bottom: 8px; border-left: 2px dashed var(--thread); }
.crow .av { grid-row: span 2; }
.crow .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crow .tm { font-size: 12px; color: var(--muted); }
.crow .pv { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crow.unread .nm, .crow.unread .pv { color: var(--ink); }
.crow .meta { display: flex; gap: 4px; align-items: center; justify-content: flex-end; }
.av { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 600; font-size: 15px; background: var(--hover); color: var(--ink); position: relative; flex: none; }
.av .chm { position: absolute; right: -2px; bottom: -2px; }
.chm { width: 16px; height: 16px; border-radius: 50%; display: inline-grid; place-items: center; font-size: 9px; font-weight: 700; color: #fff; border: 2px solid var(--card); line-height: 1; }
.chm.tg { background: var(--tg); } .chm.max { background: var(--max); } .chm.email { background: var(--email); }
.st { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--hover); color: var(--muted); white-space: nowrap; }
.st.awaiting_payment, .st.expired { background: var(--warn-bg); color: var(--warn); }
.st.paid, .st.in_work, .st.shipped { background: var(--thread-soft); color: var(--thread); }
.st.received { background: var(--ok-bg); color: var(--ok); }
.st.cancelled, .st.refunded { background: var(--err-bg); color: var(--err); }
.empty { padding: 28px 18px; text-align: center; color: var(--muted); }

/* чат */
.chead { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--card); border-bottom: 1px solid var(--line); min-height: 58px; }
.chead .who { min-width: 0; flex: 1; }
.chead .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.feed { flex: 1; overflow-y: auto; padding: 14px 16px 8px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 12px; color: var(--muted); padding: 2px 10px; margin: 8px 0 2px; }
.msg { max-width: min(560px, 82%); padding: 8px 11px 6px; border-radius: 14px; background: var(--card); border: 1px solid var(--line); align-self: flex-start; overflow-wrap: anywhere; }
.msg.out { align-self: flex-end; background: var(--thread-soft); border-color: transparent; }
.msg.out.auto { border: 1px dashed var(--thread); }
.msg .tx { white-space: pre-wrap; }
.msg .sub { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.msg .ft { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg.err { border-color: var(--err); }
.msg .er { color: var(--err); font-size: 12px; margin-top: 4px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.sys { align-self: center; max-width: 88%; text-align: center; font-size: 12.5px; color: var(--muted); padding: 2px 8px; }
.note { align-self: center; max-width: min(560px, 90%); background: var(--note); color: var(--note-ink); border-radius: 10px; padding: 7px 11px; font-size: 14px; white-space: pre-wrap; }
.note b { display: block; font-size: 11px; font-weight: 600; opacity: .8; }
.files { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0 4px; }
.files img { max-width: 240px; max-height: 240px; border-radius: 9px; display: block; cursor: zoom-in; background: var(--hover); }
.file { display: inline-flex; gap: 6px; align-items: center; padding: 6px 10px; border-radius: 9px; background: var(--hover); font-size: 13px; text-decoration: none; color: var(--ink); max-width: 260px; }
.file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
audio, video { max-width: 260px; display: block; }

/* поле ввода */
.composer { background: var(--card); border-top: 1px solid var(--line); padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px) * 0); display: grid; gap: 6px; }
.composer .bar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.composer .inp { display: flex; gap: 6px; align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 40vh; resize: none; border-radius: 12px; padding: 10px 12px; }
.composer.note-mode textarea { background: var(--note); color: var(--note-ink); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 4px 10px; font-size: 13px; cursor: pointer; color: var(--muted); }
.seg button[aria-pressed=true] { background: var(--ink); color: var(--card); }
.att { display: flex; gap: 6px; flex-wrap: wrap; }
.att .file button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 0 0 4px; font-size: 15px; line-height: 1; }
.tpls { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: thin; }
.tpls button { flex: none; }

/* карточка клиента */
.sec { padding: 14px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.sec h3 { margin: 0; font-size: 14px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.chlist { display: grid; gap: 4px; font-size: 13px; }
.chlist div { display: flex; gap: 6px; align-items: center; }
.ocard { border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; display: grid; gap: 3px; cursor: pointer; background: var(--card); text-align: left; width: 100%; }
.ocard:hover { background: var(--hover); }
.ocard .l1 { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }

/* окно заказа */
.modal { position: fixed; inset: 0; background: rgba(10, 12, 14, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.sheet { background: var(--card); border-radius: 16px; width: min(640px, 100%); max-height: calc(100% - 20px); overflow-y: auto; box-shadow: 0 20px 60px rgba(0, 0, 0, .25); }
.sheet header { position: sticky; top: 0; background: var(--card); display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); z-index: 1; }
.sheet header h2 { margin: 0; font-size: 18px; flex: 1; }
.sheet .body { padding: 14px 16px; display: grid; gap: 12px; }
.sheet footer { position: sticky; bottom: 0; background: var(--card); border-top: 1px solid var(--line); padding: 12px 16px; display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.items { display: grid; gap: 8px; }
.item { display: grid; grid-template-columns: minmax(0, 1fr) 96px 64px auto auto; gap: 6px; align-items: center; }
.item label.mk { display: flex; gap: 4px; align-items: center; font-size: 12px; color: var(--muted); white-space: nowrap; }
.total { display: flex; justify-content: space-between; font-size: 17px; font-weight: 600; padding-top: 4px; }
.box { border-radius: 10px; padding: 10px 12px; background: var(--hover); font-size: 14px; display: grid; gap: 6px; }
.box.warn { background: var(--warn-bg); color: var(--warn); }
.box.err { background: var(--err-bg); color: var(--err); }
.link { display: flex; gap: 6px; }
.link input { font-size: 13px; }
.hist { font-size: 13px; color: var(--muted); display: grid; gap: 2px; }
details summary { cursor: pointer; font-size: 13px; color: var(--muted); }

/* заказы и настройки */
.page { flex: 1; overflow-y: auto; }
.page .inner { max-width: 960px; margin: 0 auto; padding: 16px; display: grid; gap: 14px; }
.otable { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 12px; overflow: hidden; }
.otable th, .otable td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; }
.otable th { font-weight: 500; color: var(--muted); font-size: 13px; }
.otable tr.click { cursor: pointer; }
.otable tr.click:hover td { background: var(--hover); }
.otable td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px; display: grid; gap: 10px; }
.card h2 { margin: 0; font-size: 17px; }
.chstat { display: grid; grid-template-columns: auto 1fr; gap: 6px 10px; align-items: center; }
.tpl { display: grid; grid-template-columns: 180px 1fr auto; gap: 6px; align-items: start; }
.code { font-size: 22px; font-weight: 700; letter-spacing: 2px; font-variant-numeric: tabular-nums; }
.switch { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--thread); }

/* вход */
.login { margin: auto; width: min(340px, 92%); background: var(--card); border-radius: 16px; padding: 26px 22px; display: grid; gap: 12px; text-align: center; border: 1px solid var(--line); }
.login h1 { margin: 0; font-size: 22px; }
.login input { text-align: center; font-size: 22px; letter-spacing: 6px; min-height: 52px; }
.login .btn { min-height: 48px; }
.error { background: var(--err-bg); color: var(--err); padding: 8px 12px; border-radius: 10px; font-size: 14px; }

#toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); background: var(--ink); color: var(--card); padding: 9px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 80; max-width: 90vw; }
#toast.on { opacity: 1; }
.viewer { position: fixed; inset: 0; background: rgba(0, 0, 0, .85); display: grid; place-items: center; z-index: 90; cursor: zoom-out; }
.viewer img { max-width: 96vw; max-height: 92vh; }
.only-m { display: none; }

/* планшет: карточка клиента выезжает поверх */
@media (max-width: 1180px) {
  .work { grid-template-columns: 320px minmax(0, 1fr); }
  .side { position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 100%); z-index: 40; box-shadow: -10px 0 40px rgba(0, 0, 0, .2); transform: translateX(105%); transition: transform .2s; padding-top: env(safe-area-inset-top, 0px); }
  .side.open { transform: none; }
  .only-t { display: inline-flex !important; }
}
.only-t { display: none; }
/* телефон: одна колонка */
@media (max-width: 760px) {
  .work { grid-template-columns: 100%; }
  .work.in-chat .list { display: none; }
  .work:not(.in-chat) .chat { display: none; }
  .only-m { display: inline-flex; }
  .top { padding: 6px 10px; gap: 6px; }
  .brand, .conn .lbl, .conn .st { display: none; }
  .tabs { flex: 1; }
  .tabs button { flex: 1; }
  .chead { gap: 6px; padding: 6px 8px; }
  .chead .av { width: 36px; height: 36px; font-size: 13px; }
  .tabs button { padding: 7px 9px; }
  .msg { max-width: 88%; }
  .item { grid-template-columns: minmax(0, 1fr) 84px 54px auto; }
  .item label.mk { grid-column: 1 / -1; }
  .tpl { grid-template-columns: 1fr auto; }
  .tpl textarea { grid-column: 1 / -1; }
  .otable .hide-m { display: none; }
  .modal { padding: 0; align-items: end; }
  .sheet { border-radius: 16px 16px 0 0; max-height: 94%; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
