:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --border: #e7e5e0;
  --border-strong: #d6d3cd;
  --text: #1a1a1a;
  --muted: #8a8781;
  --accent: #1a1a1a;
  --accent-text: #ffffff;
  --blue: #3056d3;
  --green: #1a7f4b;
  --green-bg: #e7f4ec;
  --amber: #9a6700;
  --amber-bg: #fff3d6;
  --red: #c53030;
  --red-bg: #fdecec;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 20, 18, .05);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.error { color: var(--red); margin-top: 8px; font-size: 13px; }
.m-only { display: none; }

/* ─── Логин ─── */
#login-view {
  display: flex; align-items: center; justify-content: center; height: 100vh;
  padding: 20px;
}
.login-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow);
  padding: 40px 36px; width: 360px; max-width: 100%;
  display: flex; flex-direction: column; gap: 12px;
}
.login-logo {
  font-size: 26px; font-weight: 700; letter-spacing: -0.5px; text-align: center;
}
.login-sub {
  text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 16px;
}

/* ─── Инпуты и кнопки ─── */
input, textarea, select {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius); color: var(--text);
  padding: 9px 12px; font-size: 14px; font-family: inherit; outline: none;
  transition: border-color .12s;
}
input:focus, textarea:focus, select:focus { border-color: var(--text); }
textarea { resize: vertical; min-height: 60px; }
::placeholder { color: #b3b0aa; }

.btn {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius); color: var(--text);
  padding: 8px 15px; font-size: 14px; font-weight: 500; font-family: inherit;
  cursor: pointer; white-space: nowrap; transition: all .12s;
}
.btn:hover { border-color: var(--text); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { opacity: .85; }
.btn-danger { color: var(--red); }
.btn-danger:hover { border-color: var(--red); background: var(--red-bg); }
.btn-sm { padding: 5px 11px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-icon { padding: 5px 10px; font-size: 16px; line-height: 1; }
.btn:disabled { opacity: .45; cursor: default; }
.btn-more { margin: 10px; }

/* ─── Каркас ─── */
#app-view { display: flex; height: 100vh; height: 100dvh; }

#sidebar {
  width: 168px; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 18px 12px; gap: 2px; flex-shrink: 0;
}
.logo {
  font-weight: 700; font-size: 19px; letter-spacing: -0.4px;
  padding: 2px 10px 18px; cursor: pointer; user-select: none;
}
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-btn {
  background: none; border: none; color: var(--muted); text-align: left;
  padding: 8px 10px; border-radius: 8px; font-size: 14px; font-weight: 500;
  font-family: inherit; cursor: pointer; width: 100%; transition: all .12s;
}
.nav-btn:hover { background: var(--bg); color: var(--text); }
.nav-btn.active { background: #eeede9; color: var(--text); }
.me-name { padding: 8px 10px; font-size: 12px; color: var(--muted); word-break: break-word; }
/* Десктоп: обёртка меню прозрачна для флекса, дети — обычные пункты сайдбара */
#nav-more { display: contents; }

.page { flex: 1; overflow-y: auto; min-width: 0; }
.page-pad { padding: 24px 28px; }
.page h2 {
  margin-bottom: 18px; font-size: 19px; font-weight: 600; letter-spacing: -0.3px;
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
}
.section-title { font-size: 14px; font-weight: 600; margin-bottom: 10px; }

/* ─── Тикеты: 3 колонки ─── */
#page-tickets { display: flex; overflow: hidden; }

#ticket-list-col {
  width: 320px; border-right: 1px solid var(--border); background: var(--surface);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.list-header {
  padding: 14px 12px 10px; border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 10px;
}
.tabs { display: flex; gap: 4px; }
.tab {
  background: none; border: none; border-radius: 8px; color: var(--muted);
  padding: 6px 12px; font-size: 13px; font-weight: 500; font-family: inherit;
  cursor: pointer; transition: all .12s;
}
.tab:hover { color: var(--text); }
.tab.active { background: #eeede9; color: var(--text); }
#ticket-search { width: 100%; font-size: 13px; }

#ticket-list { flex: 1; overflow-y: auto; }
.ticket-item {
  padding: 11px 14px; border-bottom: 1px solid var(--border); cursor: pointer;
  transition: background .1s;
}
.ticket-item:hover { background: var(--bg); }
.ticket-item.active { background: #eeede9; }
.ticket-item .t-row1 { display: flex; gap: 7px; align-items: center; margin-bottom: 3px; min-width: 0; }
.ticket-item .t-id { color: var(--muted); font-size: 12px; flex-shrink: 0; }
.ticket-item .t-user {
  font-weight: 600; font-size: 13px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.ticket-item .t-preview {
  color: var(--muted); font-size: 12.5px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.ticket-item .t-meta {
  display: flex; gap: 6px; margin-top: 4px; font-size: 11.5px; color: var(--muted);
}

.badge {
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 20px;
  white-space: nowrap; background: var(--bg); color: var(--muted); flex-shrink: 0;
}
.badge.open { background: var(--green-bg); color: var(--green); }
.badge.in_progress { background: var(--amber-bg); color: var(--amber); }
.badge.closed { background: #ececea; color: var(--muted); }
.badge.dept2 { background: #e8ecfb; color: var(--blue); }
.badge.pending { background: var(--amber-bg); color: var(--amber); }

/* ─── Чат ─── */
#ticket-chat-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.placeholder {
  display: flex; align-items: center; justify-content: center;
  height: 100%; color: var(--muted);
}
#chat-wrap { display: flex; flex-direction: column; height: 100%; min-height: 0; }
#chat-header {
  display: flex; align-items: center; gap: 8px; padding: 12px 16px;
  border-bottom: 1px solid var(--border); background: var(--surface);
  flex-wrap: wrap; row-gap: 8px;
}
#chat-title-wrap {
  display: flex; align-items: center; gap: 8px; min-width: 0; flex-shrink: 1;
}
#chat-title {
  font-weight: 600; font-size: 14.5px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}

#ticket-actions {
  display: flex; align-items: center; gap: 8px; padding: 8px 16px;
  border-bottom: 1px solid var(--border); background: var(--bg);
  overflow-x: auto;
}

#chat-messages {
  flex: 1; overflow-y: auto; padding: 18px 18px 8px;
  display: flex; flex-direction: column; gap: 10px;
}
.msg-photo {
  display: block; max-width: 100%; max-height: 320px;
  border-radius: 10px; margin-bottom: 4px; cursor: zoom-in;
}
.msg-video { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; }
.msg-audio { display: block; max-width: 240px; }
.msg-file { color: inherit; font-weight: 500; }
.msg {
  max-width: 68%; padding: 10px 14px; border-radius: 14px;
  white-space: pre-wrap; word-break: break-word; font-size: 14px;
  box-shadow: var(--shadow);
}
.msg .m-meta { font-size: 11px; color: var(--muted); margin-top: 5px; }
.msg.user {
  background: var(--surface); border: 1px solid var(--border);
  align-self: flex-start; border-bottom-left-radius: 4px;
}
.msg.support {
  background: var(--accent); color: var(--accent-text);
  align-self: flex-end; border-bottom-right-radius: 4px;
}
.msg.support .m-meta { color: rgba(255, 255, 255, .55); }
.msg.ai {
  background: #eef1fb; border: 1px solid #dde3f7;
  align-self: flex-end; border-bottom-right-radius: 4px;
}
.msg.ai .m-meta { color: #7787c4; }
.msg.ai .ai-label { font-size: 11px; font-weight: 600; color: var(--blue); margin-bottom: 4px; }
.msg.system {
  background: none; border: 1px dashed var(--border-strong); box-shadow: none;
  align-self: center; color: var(--muted); font-size: 12.5px; max-width: 85%;
}

#quick-replies-row {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 18px 0;
}
.qr-btn {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 20px; color: var(--text); font-family: inherit;
  padding: 5px 13px; font-size: 12.5px; font-weight: 500; cursor: pointer;
  transition: all .12s;
}
.qr-btn:hover { border-color: var(--text); }
.qr-btn.qr-propose { color: var(--muted); border-style: dashed; }

/* Поле ввода в стиле мессенджера: одна капсула, круглая кнопка отправки */
#chat-input-row { padding: 10px 18px 14px; }
#input-shell {
  display: flex; align-items: flex-end; gap: 4px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 24px; padding: 5px 5px 5px 6px;
  transition: border-color .12s;
}
#input-shell:focus-within { border-color: var(--text); }
#reply-input {
  flex: 1; border: none; background: none; padding: 9px 4px;
  min-height: 38px; max-height: 160px; resize: none; line-height: 1.4;
}
#reply-input:focus { border: none; }
.in-btn {
  width: 38px; height: 38px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: 50%;
  color: var(--muted); cursor: pointer; transition: all .12s;
}
.in-btn svg { width: 20px; height: 20px; }
.in-btn:hover { background: var(--bg); color: var(--text); }
.in-btn:disabled { opacity: .4; cursor: default; }
.in-send { background: var(--accent); color: var(--accent-text); }
.in-send:hover { background: var(--accent); color: var(--accent-text); opacity: .85; }

/* Круглая иконка-кнопка (Инфо в хедере) */
.btn-round {
  width: 34px; height: 34px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 50%; color: var(--text); cursor: pointer;
}
.btn-round svg { width: 18px; height: 18px; }

/* ─── Карточка юзера ─── */
#user-card-col {
  width: 296px; border-left: 1px solid var(--border); background: var(--bg);
  overflow-y: auto; flex-shrink: 0;
}
#card-mobile-head {
  align-items: center; gap: 10px; padding: 12px 14px; font-weight: 600;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
#user-card { padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.uc-section {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow);
}
.uc-section > summary {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; cursor: pointer; list-style: none; user-select: none;
}
.uc-section > summary::-webkit-details-marker { display: none; }
.uc-section h4 {
  font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .5px;
}
.uc-chev { color: var(--muted); font-size: 15px; transition: transform .15s; }
.uc-section[open] > summary .uc-chev { transform: rotate(90deg); }
.uc-body { padding: 0 14px 12px; }
.uc-line { display: flex; justify-content: space-between; gap: 8px; padding: 2.5px 0; font-size: 13px; }
.uc-line .v { font-weight: 600; text-align: right; word-break: break-all; }
.uc-mono { font-family: ui-monospace, monospace; font-size: 11px; font-weight: 500; }
.uc-actions { display: flex; flex-direction: column; gap: 6px; }
.uc-actions .btn { text-align: left; }

/* Устройства: вложенные раскрывашки */
.uc-dev-det { border-bottom: 1px solid var(--border); }
.uc-dev-det:last-child { border-bottom: none; }
.uc-dev-det > summary {
  display: flex; gap: 7px; align-items: center; font-size: 13px;
  padding: 6px 0; cursor: pointer; list-style: none;
}
.uc-dev-det > summary::-webkit-details-marker { display: none; }
.uc-dev-det > summary .uc-chev { font-size: 13px; }
.uc-dev-det[open] > summary .uc-chev { transform: rotate(90deg); }
.uc-dev-name { font-weight: 500; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.uc-dev-body { padding: 2px 0 8px 18px; }
.uc-dev-body .uc-copy {
  margin-top: 6px; width: 100%; max-width: 100%;
  overflow: hidden; text-overflow: ellipsis; font-size: 12.5px;
}

/* Платежи: выровненная сетка */
.uc-pay-grid {
  display: grid; grid-template-columns: auto 1fr auto;
  column-gap: 10px; row-gap: 3px; font-size: 12.5px; align-items: baseline;
}
.uc-pay-h {
  font-size: 10px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .4px;
}
.uc-pay-grid .p-amount { font-weight: 600; }
.uc-pay-grid .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ok { color: var(--green); }
.bad { color: var(--red); }
.warn { color: var(--amber); }

/* ─── Панели / таблицы ─── */
.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 16px;
}
.panel h3 { font-size: 14px; font-weight: 600; margin-bottom: 12px; }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.form-col { display: flex; flex-direction: column; gap: 8px; }

.table-scroll { overflow-x: auto; }
table {
  width: 100%; border-collapse: separate; border-spacing: 0;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; overflow: hidden;
}
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
tr:last-child td { border-bottom: none; }
th {
  color: var(--muted); font-weight: 600; font-size: 11px;
  text-transform: uppercase; letter-spacing: .4px; background: var(--bg);
}

.agent-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow);
  padding: 15px 16px; margin-bottom: 12px;
}
.agent-card .a-head { display: flex; gap: 9px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.agent-card .a-head b { font-size: 15px; }
.perm-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px; margin: 12px 0;
}
.perm-item {
  display: flex; gap: 9px; align-items: center; font-size: 13px; cursor: pointer;
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px;
  background: var(--bg);
}
.perm-item span { display: flex; flex-direction: column; line-height: 1.3; flex: 1; }
.perm-item small { color: var(--muted); font-size: 11.5px; font-weight: 400; }
input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; flex-shrink: 0; }

/* Крупная форма создания агента */
.panel-create { max-width: 560px; padding: 22px 24px; }
.panel-create h3 { font-size: 17px; margin-bottom: 18px; }
.fg { margin-bottom: 16px; }
.fg label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 3px; }
.fg .hint { color: var(--muted); font-size: 12.5px; margin-bottom: 7px; }
.fg input, .fg select { width: 100%; font-size: 15px; padding: 11px 13px; }
.fg-row { display: flex; gap: 8px; }
.fg-row input { flex: 1; }
.btn-lg { padding: 12px 22px; font-size: 15px; width: 100%; }
.na-success {
  margin-top: 14px; background: var(--green-bg); border: 1px solid #cde8d8;
  border-radius: 10px; padding: 14px; font-size: 13.5px; line-height: 1.5;
}
.na-creds {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; margin: 10px 0;
  font-family: ui-monospace, monospace; font-size: 13px;
}

/* ─── Статистика: карточки и графики ─── */
.stat-cards {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px; margin-bottom: 16px;
}
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow); padding: 13px 15px;
}
.sc-num { font-size: 24px; font-weight: 700; letter-spacing: -0.5px; line-height: 1.2; }
.sc-label { font-size: 12px; color: var(--muted); margin-top: 2px; }
.sc-sub { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.verdict {
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 20px;
  vertical-align: 4px;
}
.v-low { background: #e8ecfb; color: var(--blue); }
.v-norm { background: var(--green-bg); color: var(--green); }
.v-high { background: var(--red-bg); color: var(--red); }

.chart {
  display: flex; align-items: flex-end; gap: 2px; height: 150px;
  padding-top: 6px;
}
.chart .bar-col {
  flex: 1; min-width: 2px; height: 100%;
  display: flex; align-items: flex-end; cursor: default;
}
.chart .bar-col:hover { background: rgba(0, 0, 0, .04); border-radius: 3px; }
.chart .bar {
  flex: 1; min-width: 2px; background: #cfccc5; border-radius: 3px 3px 0 0;
  transition: background .12s; width: 100%;
}
.chart .bar-col:hover .bar { background: var(--text); }

#chart-tip {
  position: fixed; z-index: 90; pointer-events: none;
  background: var(--accent); color: var(--accent-text);
  border-radius: 8px; padding: 6px 11px; font-size: 12.5px; font-weight: 500;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .2); white-space: nowrap;
}
.chart-hours { gap: 5px; }
.bar-pair {
  flex: 1; display: flex; align-items: flex-end; gap: 1px; height: 100%;
}
.bar-pair .bar { border-radius: 2px 2px 0 0; }
.bar-pair .bar-avg { background: #d9d6d0; }
.bar-pair .bar-today { background: var(--accent); }
.bar-pair:hover .bar-avg { background: #bcb8b1; }
.chart-x {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--muted); margin-top: 6px;
}
.chart-legend { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 10px; }
.chart-legend .lg {
  display: inline-block; width: 10px; height: 10px; border-radius: 3px;
  margin: 0 4px 0 10px; vertical-align: -1px;
}
.lg-avg { background: #d9d6d0; }
.lg-today { background: var(--accent); }

/* ─── Тост ─── */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: var(--accent-text);
  border-radius: 10px; padding: 10px 20px; z-index: 100; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .18); max-width: 90vw;
}
#toast.err { background: var(--red); }

/* ─── Планшет ─── */
@media (max-width: 1150px) {
  #user-card-col { width: 260px; }
  #ticket-list-col { width: 280px; }
  #sidebar { width: 140px; }
}

/* ─── Мобилка ─── */
@media (max-width: 900px) {
  body { overflow: auto; }
  .m-only { display: inline-flex; }

  #app-view { flex-direction: column; height: 100dvh; }

  #sidebar {
    width: 100%; flex-direction: row; align-items: center;
    padding: 6px 12px; gap: 4px; border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .logo { padding: 0 8px 0 0; font-size: 16px; }
  .nav-group { flex-direction: row; gap: 2px; }
  .nav-btn { width: auto; padding: 7px 10px; font-size: 13px; flex-shrink: 0; }
  #btn-burger { font-size: 17px; padding: 5px 12px; color: var(--text); }

  /* Остальные пункты меню — в выпадашке под бургером */
  #nav-more { display: none; }
  #sidebar.menu-open #nav-more {
    display: flex; flex-direction: column; gap: 2px;
    position: fixed; top: 46px; right: 8px; z-index: 60;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 12px; box-shadow: 0 8px 28px rgba(0, 0, 0, .14);
    padding: 8px; min-width: 190px;
  }
  #sidebar.menu-open #admin-nav { flex-direction: column; }
  #sidebar.menu-open #nav-more .nav-btn {
    width: 100%; text-align: left; font-size: 14px; padding: 10px 12px;
  }
  #sidebar.menu-open #nav-more .me-name {
    display: block; border-top: 1px solid var(--border); margin-top: 4px; padding-top: 10px;
  }
  .me-name { display: none; }

  /* Тикеты: одна колонка, переключение видов через классы на #page-tickets */
  #page-tickets { position: relative; }
  #ticket-list-col { width: 100%; border-right: none; }
  #ticket-chat-col { display: none; }
  #user-card-col { display: none; }

  #page-tickets.show-chat #ticket-list-col { display: none; }
  #page-tickets.show-chat #ticket-chat-col { display: flex; width: 100%; }

  #page-tickets.show-card #ticket-list-col,
  #page-tickets.show-card #ticket-chat-col { display: none; }
  #page-tickets.show-card #user-card-col {
    display: block; width: 100%; border-left: none;
  }
  #card-mobile-head { display: flex; }

  /* Хедер чата: одна строка, заголовок обрезается, «Инфо» — круглая иконка */
  #chat-header { padding: 8px 12px; flex-wrap: nowrap; }
  #chat-title-wrap { overflow: hidden; }
  #chat-title { flex-shrink: 1; }
  #ticket-actions { padding: 8px 12px; }
  #chat-messages { padding: 14px 12px 8px; }
  .msg { max-width: 84%; }
  #quick-replies-row { padding: 8px 12px 0; }

  #chat-input-row { padding: 8px 10px calc(12px + env(safe-area-inset-bottom)); }
  /* 16px шрифт — iOS не зумит страницу при фокусе */
  #reply-input { font-size: 16px; min-height: 40px; }
  .in-btn { width: 42px; height: 42px; }
  .in-btn svg { width: 22px; height: 22px; }

  .page-pad { padding: 16px 14px; }
  .form-row input, .form-row select { flex: 1 1 130px; min-width: 0; }
}

@media (min-width: 901px) {
  #card-mobile-head { display: none; }
}
