:root {
  --bg: #f4f3f0;
  --panel: #ffffff;
  --panel-2: #faf9f7;
  --text: #1f2328;
  --muted: #6b6f76;
  --border: #e3e1dc;
  --accent: #d97706;
  --accent-ink: #ffffff;
  --accent-soft: #fdf1e1;
  --ok: #15803d;
  --ok-soft: #e7f5ec;
  --warn: #b45309;
  --danger: #b91c1c;
  --code: #f1efeb;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 10px;
  /* Фото товаров обычно на белом фоне — подложка белая и в тёмной теме. */
  --photo-bg: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --panel: #1d2024;
    --panel-2: #23272c;
    --text: #e6e7e9;
    --muted: #9aa0a8;
    --border: #30353b;
    --accent: #f59e0b;
    --accent-ink: #1a1305;
    --accent-soft: #3a2a10;
    --ok: #4ade80;
    --ok-soft: #173322;
    --warn: #fbbf24;
    --danger: #f87171;
    --code: #262a30;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
button, input, textarea, select { font: inherit; color: inherit; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }

/* ---------- Вход ---------- */
.login { display: grid; place-items: center; height: 100%; padding: 16px; }
.login-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
  padding: 28px; width: min(360px, 100%); display: grid; gap: 12px;
}
.login-card h1 { margin: 0; }
.login-card p { margin: 0 0 8px; color: var(--muted); }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); }
.error-text { color: var(--danger); font-size: 14px; min-height: 1em; }

/* ---------- Каркас ---------- */
.app { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--accent); white-space: nowrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; }
.tabs button { border: 0; background: transparent; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); white-space: nowrap; }
.tabs button.active { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.balance { font-size: 13px; color: var(--muted); white-space: nowrap; }

/* ---------- Настройки ---------- */
.modal {
  border: 1px solid var(--border); border-radius: 14px; background: var(--panel); color: var(--text);
  box-shadow: var(--shadow); padding: 24px; width: min(580px, calc(100vw - 32px));
}
.modal::backdrop { background: rgba(0, 0, 0, 0.4); }
.modal h2 { margin: 0; }
.modal h3 { margin: 16px 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.providers { display: grid; gap: 10px; }
.provider { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; }
.provider.active { border-color: var(--accent); background: var(--accent-soft); }
.provider .s { font-size: 13px; color: var(--muted); }
.provider-title { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: baseline; cursor: pointer; }
.provider-title input { margin: 0; align-self: center; }
.key-row { display: flex; gap: 8px; }
.key-row input {
  flex: 1; min-width: 0; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--panel); font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 14px;
}
.models { display: grid; gap: 6px; }
.model { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--border); border-radius: var(--radius); padding: 9px 12px; cursor: pointer; }
.model.active { border-color: var(--accent); background: var(--accent-soft); }
.model input { margin: 4px 0 0; }
.model .price { font-size: 13px; color: var(--text); white-space: nowrap; }
.model .s { display: block; font-size: 13px; color: var(--muted); }
.effort-field { margin-top: 12px; max-width: 320px; }
.settings-status { min-height: 1.5em; margin: 14px 0 4px; font-size: 14px; }
.settings-status.ok { color: var(--ok); }
.settings-status.warn { color: var(--warn); }
.settings-status.error { color: var(--danger); }
.view { flex: 1; min-height: 0; display: flex; }

/* ---------- Чат ---------- */
.sidebar {
  width: 270px; flex-shrink: 0; border-right: 1px solid var(--border); background: var(--panel-2);
  padding: 12px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto;
}
.conv-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.conv-list li {
  padding: 8px 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 8px; align-items: center;
}
.conv-list li:hover { background: var(--panel); }
.conv-list li.active { background: var(--accent-soft); }
.conv-list .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conv-list .del { opacity: 0; border: 0; background: none; cursor: pointer; color: var(--muted); }
.conv-list li:hover .del { opacity: 1; }

.chat-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 10px 20px; border-bottom: 1px solid var(--border); }
.chat-title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-cost { font-size: 13px; color: var(--muted); }
.only-mobile { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.empty { margin: auto; width: 100%; max-width: 560px; text-align: center; color: var(--muted); }
.composer-row > * { min-width: 0; }
.empty h2 { color: var(--text); }
.examples { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 12px; }
.chip { border: 1px solid var(--border); background: var(--panel); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: 14px; }
.chip:hover { border-color: var(--accent); }

.msg { max-width: 920px; width: 100%; }
.msg.user { align-self: flex-end; max-width: 720px; }
.msg.user .bubble { background: var(--accent-soft); border-radius: 14px 14px 4px 14px; padding: 10px 14px; white-space: pre-wrap; }
.msg.user img { max-width: 220px; max-height: 220px; border-radius: 8px; display: block; margin-bottom: 6px; }
.msg.user .doc { font-size: 13px; color: var(--muted); }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 14px 18px; box-shadow: var(--shadow); }
.msg .meta { font-size: 12px; color: var(--muted); margin-top: 4px; }

.activity { margin-bottom: 8px; font-size: 13px; color: var(--muted); }
.activity summary { cursor: pointer; user-select: none; }
.activity ol { margin: 6px 0 0; padding-left: 4px; list-style: none; max-height: 260px; overflow-y: auto; }
.activity li { overflow-wrap: anywhere; }
.thinking-live { font-size: 13px; color: var(--muted); font-style: italic; white-space: pre-wrap; max-height: 5.5em; overflow: hidden; margin-bottom: 8px; }
.notice { font-size: 14px; color: var(--warn); margin-top: 8px; }
.notice.error { color: var(--danger); }
.dots::after { content: "…"; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: 0.2; } }

.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; display: block; overflow-x: auto; margin: 10px 0; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; vertical-align: top; }
.md th { background: var(--panel-2); }
.md code { background: var(--code); padding: 1px 5px; border-radius: 4px; font-size: 0.92em; }
.md pre { background: var(--code); padding: 12px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md h1, .md h2, .md h3 { line-height: 1.25; }
.md h2 { font-size: 1.2em; }
.md h3 { font-size: 1.05em; }

.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 9px 12px;
  background: var(--panel-2); max-height: 200px; min-height: 40px;
}
.attach { font-size: 18px; padding: 6px 10px; }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; }
.attachments:not(:empty) { margin-bottom: 8px; }
.att { position: relative; border: 1px solid var(--border); border-radius: 8px; padding: 4px; font-size: 12px; display: flex; align-items: center; gap: 6px; background: var(--panel-2); }
.att img { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; }
.att button { border: 0; background: none; cursor: pointer; color: var(--muted); }

/* ---------- Списки и карточки ---------- */
.list-pane { width: 360px; flex-shrink: 0; border-right: 1px solid var(--border); padding: 12px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; background: var(--panel-2); }
.detail-pane { flex: 1; min-width: 0; overflow-y: auto; padding: 20px; }
.search { padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
.cards { display: grid; gap: 8px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; cursor: pointer; }
.card:hover, .card.active { border-color: var(--accent); }
.card .h { font-weight: 600; }
.card .s { font-size: 13px; color: var(--muted); }
.card-thumb { float: right; width: 44px; height: 44px; object-fit: contain; background: var(--photo-bg); border-radius: 6px; margin: 0 0 4px 8px; }
.placeholder { color: var(--muted); text-align: center; margin-top: 15vh; }

/* ---------- Документация ---------- */
.docs { margin: 12px 0 4px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); }
.docs-h { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); margin-bottom: 4px; }
.docs ul, .doc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.docs .s, .doc-list .s { font-size: 12px; color: var(--muted); }

/* ---------- Фото ---------- */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin: 12px 0 4px; }
.gallery figure { margin: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--panel); display: flex; flex-direction: column; }
.gallery img { display: block; width: 100%; aspect-ratio: 1; object-fit: contain; background: var(--photo-bg); }
.gallery figcaption { padding: 6px 8px; font-size: 12px; line-height: 1.35; color: var(--muted); display: flex; gap: 4px; justify-content: space-between; align-items: flex-start; overflow-wrap: anywhere; }
.thumb { display: block; width: 48px; height: 48px; object-fit: contain; background: var(--photo-bg); border: 1px solid var(--border); border-radius: 6px; }

.detail h2 { margin: 0 0 4px; }
.detail .sub { color: var(--muted); margin-bottom: 16px; }
.detail section { margin-bottom: 22px; }
.detail h3 { font-size: 15px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 8px; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.field label { display: block; font-size: 12px; color: var(--muted); }
.field input, .field textarea, .field select { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
.field textarea { min-height: 70px; resize: vertical; }
.letter-body { min-height: 320px !important; font-family: ui-monospace, Consolas, monospace; font-size: 14px; }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { border-bottom: 1px solid var(--border); padding: 7px 8px; text-align: left; vertical-align: top; }
.tbl th { font-size: 12px; color: var(--muted); font-weight: 600; }
.tbl.kv th { width: 35%; }
.detail h4 { margin: 12px 0 4px; font-size: 14px; }
.tbl-wrap { overflow-x: auto; }
.tag { display: inline-block; font-size: 12px; border-radius: 999px; padding: 1px 8px; background: var(--panel-2); border: 1px solid var(--border); white-space: nowrap; }
.tag.wear { background: var(--accent-soft); border-color: transparent; }
.tag.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.tag.warn { background: var(--accent-soft); color: var(--warn); border-color: transparent; }
.kv-mini { display: grid; grid-template-columns: minmax(80px, auto) 1fr; gap: 2px 10px; margin: 4px 0 0; font-size: 12px; line-height: 1.35; }
.kv-mini dt { color: var(--muted); }
.kv-mini dd { margin: 0; }
.specs summary { cursor: pointer; font-size: 12px; color: var(--accent); margin-top: 4px; }
.specs[open] summary { margin-bottom: 2px; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.x { border: 0; background: none; color: var(--muted); cursor: pointer; }
.x:hover { color: var(--danger); }

/* ---------- Телефон ---------- */
@media (max-width: 760px) {
  .topbar { padding: 8px 12px; gap: 10px; flex-wrap: wrap; }
  .tabs { order: 3; flex-basis: 100%; }
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; z-index: 10; top: 0; bottom: 0; left: 0; width: 82vw; transform: translateX(-100%); transition: transform 0.2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .messages { padding: 12px; }
  .chat-head { padding: 8px 12px; }
  .view:not(.chat-view) { flex-direction: column; overflow-y: auto; }
  .list-pane { width: auto; border-right: 0; border-bottom: 1px solid var(--border); max-height: 45vh; }
  .detail-pane { padding: 16px; overflow: visible; }
  .msg.user img { max-width: 160px; }
  .modal { padding: 16px; }
  .model .price { white-space: normal; display: block; }
  .key-row { flex-wrap: wrap; }
}

/* ---------- Роли: скрытие недоступных действий ---------- */
/* Элементы с классом op-* показываются только при соответствующем классе на <body> */
body:not(.cap-search) .op-search { display: none !important; }
body:not(.cap-edit)   .op-edit   { display: none !important; }
body:not(.cap-admin)  .op-admin  { display: none !important; }
.op-view-only { display: none; }
body:not(.cap-search) .op-view-only { display: block; }

.whoami { font-size: 13px; color: var(--muted); margin: 0 2px 0 6px; white-space: nowrap; }
.view-note {
  margin: 12px; padding: 10px 14px; font-size: 14px;
  background: var(--accent-soft); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--warn);
}

/* Окно «Пользователи» */
#users.modal { width: min(760px, calc(100vw - 32px)); }
.user-add { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.user-add input, .user-add select { padding: 7px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); color: var(--text); }
.user-add input { flex: 1 1 140px; }
#userTable td.act { white-space: nowrap; }
#userTable select.urole { padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel); color: var(--text); }
