/* ==========================================================================
   Портал поддержки — оформление
   ========================================================================== */

:root {
  --bg: #f2f4f9;
  --bg-accent: #e8ecff;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --sidebar: #ffffff;
  --border: #e3e8f0;
  --border-strong: #cbd4e3;
  --text: #101828;
  --text-2: #475467;
  --muted: #7c879b;
  --primary: #4f46e5;
  --primary-2: #6366f1;
  --primary-soft: #eef2ff;
  --accent: #06b6d4;
  --ok: #12b76a;
  --warn: #f59e0b;
  --danger: #ef4444;
  --danger-soft: #fef2f2;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(16,24,40,.06);
  --shadow: 0 4px 12px rgba(16,24,40,.07), 0 12px 32px rgba(16,24,40,.05);
  --shadow-lg: 0 24px 60px rgba(16,24,40,.16);
  --sidebar-w: 260px;
}

[data-theme="dark"] {
  --bg: #0d1117;
  --bg-accent: #131a2b;
  --surface: #161b26;
  --surface-2: #1c2230;
  --sidebar: #11161f;
  --border: #262d3b;
  --border-strong: #38415a;
  --text: #e8ecf4;
  --text-2: #b3bdcf;
  --muted: #7d8899;
  --primary: #7c7cf9;
  --primary-2: #8b8bfb;
  --primary-soft: #1e2340;
  --ok: #2ecc8f;
  --warn: #f5a524;
  --danger: #f87171;
  --danger-soft: #2a1a1c;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.3);
  --shadow: 0 4px 14px rgba(0,0,0,.4);
  --shadow-lg: 0 24px 60px rgba(0,0,0,.55);
}

* { box-sizing: border-box; }

/* .tab и .field задают display, поэтому браузерное [hidden] перестаёт
   работать — возвращаем его приоритет. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body.no-scroll { overflow: hidden; }

h1, h2, h3 { margin: 0; font-weight: 640; letter-spacing: -.02em; }

/* ============================== КАРКАС ============================== */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  /* Колонка сайдбара красится на всю высоту страницы, а сам сайдбар
     закреплён на 100vh — иначе при прокрутке остаётся пустое место. */
  background: linear-gradient(90deg,
    var(--sidebar) 0 var(--sidebar-w), transparent var(--sidebar-w));
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px 14px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; align-items: center; gap: 11px; padding: 4px 8px 18px; }
.brand-mark {
  width: 38px; height: 38px; flex: none;
  border-radius: 11px; overflow: hidden;
  box-shadow: 0 6px 16px rgba(79,70,229,.35);
}
.brand-mark img { display: block; width: 100%; height: 100%; object-fit: cover; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 16px; letter-spacing: -.02em; }
.brand-text span { font-size: 12px; color: var(--muted); }

.sidebar-foot { margin-top: auto; display: grid; gap: 10px; }

.me {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--border);
}
.avatar {
  width: 34px; height: 34px; flex: none; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--primary-2), var(--accent));
  color: #fff; font-weight: 700; font-size: 14px;
}
.me-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.me-text b { font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-text i { font-style: normal; font-size: 11.5px; color: var(--muted); }

/* Отделы пользователя — прямо под именем в шапке */
.me-groups {
  margin-top: 3px; font-size: 11px; line-height: 1.3; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.me-groups.warn { color: var(--warn); }

.side-actions { display: flex; gap: 8px; }
.icon-btn {
  flex: 1;
  appearance: none; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-2);
  border-radius: 10px; padding: 8px; cursor: pointer; font-size: 14px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s, border-color .15s, transform .1s;
}
.icon-btn:hover { background: var(--primary-soft); color: var(--primary); border-color: var(--primary); }
.icon-btn:active { transform: scale(.96); }

/* Кнопки в шапке: одинаковые, компактные, всегда справа */
.top-actions { margin-left: auto; gap: 6px; flex: none; }
.top-actions .icon-btn { flex: none; width: 36px; height: 36px; padding: 0; border-radius: 10px; }

/* Единый набор иконок: тонкие штрихи, цвет и размер наследуются от текста */
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.ico {
  width: 18px; height: 18px; flex: none; display: block;
  fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.btn .ico { width: 16px; height: 16px; }

.content { min-width: 0; display: flex; flex-direction: column; }

/* Grid-элементы по умолчанию имеют min-width:auto, из-за чего широкая
   панель навигации распирает сетку за пределы экрана. */
.sidebar, .content { min-width: 0; }

/* Пока пользователь не вошёл, видна только форма входа:
   содержимое портала под ней не должно мелькать и быть доступным.
   Важно не скрывать .tabs внутри самой модалки входа. */
body.auth-locked main,
body.auth-locked .sidebar .tabs { display: none; }

.topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 78%, transparent);
  backdrop-filter: blur(10px);
  position: sticky; top: 0; z-index: 5;
}
.crumb { display: flex; align-items: center; gap: 10px; }
.topbar .sub { margin: 0; font-size: 12.5px; color: var(--muted); }

main { padding: 26px 28px 64px; max-width: 1120px; width: 100%; }

.panel { display: none; }
.panel.active { display: block; animation: rise .28s cubic-bezier(.2,.7,.3,1); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }


/* ============================== ВКЛАДКИ ============================== */

.tabs { display: flex; gap: 6px; flex-direction: column; }

.tab {
  appearance: none; border: 0; background: transparent;
  display: flex; align-items: center; gap: 11px;
  width: 100%; padding: 10px 12px;
  font: inherit; font-weight: 560; font-size: 14.5px;
  color: var(--text-2); text-align: left; cursor: pointer;
  border-radius: 11px; position: relative;
  transition: background .15s, color .15s, transform .1s;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab:active { transform: translateY(1px); }
.tab.active {
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  color: #fff;
  box-shadow: 0 6px 18px rgba(79,70,229,.28);
}
[data-theme="dark"] .tab.active { box-shadow: 0 6px 18px rgba(0,0,0,.4); }
.tab-ico { font-size: 15px; width: 20px; text-align: center; flex: none; }
.tab-label { flex: 1; }

.tab-badge {
  min-width: 20px; padding: 1px 7px; border-radius: 999px;
  background: var(--danger); color: #fff;
  font-size: 11px; font-weight: 700; line-height: 17px; text-align: center;
}
.tab.active .tab-badge { background: rgba(255,255,255,.28); }

.tabs.small { flex-direction: row; gap: 4px; }
.tabs.small .tab { width: auto; padding: 7px 13px; font-size: 13.5px; border-radius: 9px; }
.tabs.small .tab.active { background: var(--primary-soft); color: var(--primary); box-shadow: none; }

/* ============================== КАРТОЧКИ ============================== */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  margin-bottom: 18px;
}

.card-title {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin-bottom: 16px;
}
.card-title h2 { font-size: 16px; }
.chip {
  font-size: 11.5px; font-weight: 600; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 3px 9px; border-radius: 999px;
}

.toolbar { display: flex; gap: 9px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar.compact { margin-bottom: 0; }
.toolbar select, .toolbar input[type=search] { width: auto; min-width: 150px; }

.search { position: relative; flex: 1; min-width: 190px; }
.search-ico { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); font-size: 12px; opacity: .55; }
.search input[type=search] { width: 100%; padding-left: 32px; }

/* ============================== ПОЛЯ ============================== */

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
.field { margin-bottom: 15px; }
.grid .field { margin-bottom: 0; }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin-bottom: 7px; }
.req { color: var(--danger); }

input[type=text], input[type=password], input[type=email], input[type=search],
input[type=date], input[type=number], input[type=datetime-local], select, textarea {
  width: 100%; padding: 10px 12px;
  font: inherit; color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  transition: border-color .15s, box-shadow .15s, background .15s;
}
textarea { resize: vertical; min-height: 110px; }
select { cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%237c879b' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px;
}
input:focus, select:focus, textarea:focus {
  outline: none; background: var(--surface);
  border-color: var(--primary);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 16%, transparent);
}
input.invalid, textarea.invalid, select.invalid { border-color: var(--danger); }
input.invalid:focus, textarea.invalid:focus { box-shadow: 0 0 0 4px color-mix(in srgb, var(--danger) 16%, transparent); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .75; }

.hint { margin: 7px 0 0; font-size: 12.5px; color: var(--muted); }
.hint.error { color: var(--danger); }
.muted { color: var(--muted); font-size: 12.5px; }
.summary { font-size: 12.5px; color: var(--muted); }

.check { display: flex; align-items: center; gap: 10px; font-size: 14px; cursor: pointer; margin-top: 6px; color: var(--text-2); }
.check input { width: 17px; height: 17px; accent-color: var(--primary); flex: none; }

.switch { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--text-2); cursor: pointer; white-space: nowrap; }
.switch input { width: 16px; height: 16px; accent-color: var(--primary); }

/* ============================== КНОПКИ ============================== */

.btn {
  appearance: none; font: inherit; font-weight: 600; font-size: 14px;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 9px 16px; border-radius: var(--radius-sm);
  border: 1px solid transparent; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .1s, box-shadow .15s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary {
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 4px 14px rgba(79,70,229,.3);
}
.btn-primary:hover:not(:disabled) { box-shadow: 0 8px 22px rgba(79,70,229,.4); filter: brightness(1.05); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); border-color: var(--primary); color: var(--primary); }
.btn-lg { padding: 12px 24px; font-size: 15.5px; }
.btn.small { padding: 6px 11px; font-size: 13px; }

.actions { display: flex; align-items: center; gap: 11px; flex-wrap: wrap; }
.savehint { font-size: 12.5px; color: var(--muted); }


/* ============================== ВЛОЖЕНИЯ ============================== */

.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 26px 18px; text-align: center; cursor: pointer;
  border: 2px dashed var(--border-strong); border-radius: 14px;
  background: var(--surface-2);
  transition: border-color .18s, background .18s, transform .12s;
}
.dropzone:hover, .dropzone:focus-visible { border-color: var(--primary); background: var(--primary-soft); outline: none; }
.dropzone.drag { border-color: var(--primary); background: var(--primary-soft); transform: scale(1.01); }
.dz-icon { font-size: 24px; }
.dz-text { font-size: 14px; color: var(--text-2); }
.dz-hint { font-size: 12.5px; color: var(--muted); }

.filelist { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 7px; }
.filelist li {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 11px; font-size: 13.5px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.filelist .fname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filelist .fsize { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.filelist .rm {
  border: 0; background: none; cursor: pointer; color: var(--muted);
  font-size: 18px; line-height: 1; padding: 2px 6px; border-radius: 7px;
}
.filelist .rm:hover { color: var(--danger); background: var(--danger-soft); }
.filelist.inline { display: flex; flex-wrap: wrap; margin: 0; }
.filelist.inline li { padding: 5px 9px; font-size: 12.5px; }

/* ============================== СПИСОК ЗАЯВОК ============================== */

.list-note {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 0 0 12px;
  padding: 9px 12px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--surface-2, rgba(0, 0, 0, 0.03));
  color: var(--muted);
  font-size: 12.5px;
}
.list-note[hidden] { display: none; }

.ticket-list { display: grid; gap: 9px; }

/* Каскад появления строк списка */
.ticket-list > *:nth-child(1)  { animation-delay: .02s; }
.ticket-list > *:nth-child(2)  { animation-delay: .05s; }
.ticket-list > *:nth-child(3)  { animation-delay: .08s; }
.ticket-list > *:nth-child(4)  { animation-delay: .11s; }
.ticket-list > *:nth-child(5)  { animation-delay: .14s; }
.ticket-list > *:nth-child(6)  { animation-delay: .17s; }
.ticket-list > *:nth-child(7)  { animation-delay: .20s; }
.ticket-list > *:nth-child(8)  { animation-delay: .23s; }
.ticket-list > *:nth-child(n+9) { animation-delay: .26s; }

.ticket {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 15px; cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: 13px;
  transition: border-color .16s, transform .12s, box-shadow .16s, background .16s;
  animation: rise .3s backwards;
}
.ticket:hover {
  border-color: var(--border-strong); border-left-color: var(--primary);
  background: var(--surface-2);
  transform: translateX(3px);
  box-shadow: var(--shadow-sm);
}
.ticket:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.ticket.free { border-left-color: var(--warn); }

/* Цветовые метки по состоянию */
.ticket.state-new {
  border-left-color: var(--primary);
  border-left-width: 4px;
  background: color-mix(in srgb, var(--primary) 5%, var(--surface));
}
.ticket.state-open { border-left-color: var(--warn); }
.ticket.state-pending { border-left-color: #e67e22; }
.ticket.state-closed { border-left-color: var(--ok); opacity: .86; }
.ticket.state-closed:hover { opacity: 1; }

/* Легенда статусов над списком заявок */
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin: 2px 0 12px; }
.legend .lg {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; color: var(--text-2);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 10px;
}
.legend .lg::before {
  content: ''; width: 8px; height: 8px; border-radius: 50%;
  background: var(--border-strong);
}
.legend .lg.new::before { background: var(--primary); }
.legend .lg.open::before { background: var(--warn); }
.legend .lg.pending::before { background: #e67e22; }
.legend .lg.closed::before { background: var(--ok); }

.tnum {
  font-variant-numeric: tabular-nums; font-weight: 700; font-size: 13.5px;
  color: var(--primary); white-space: nowrap;
}
.tmain { flex: 1; min-width: 0; }
.ttitle { display: block; font-weight: 570; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tmeta { display: block; font-size: 12.5px; color: var(--muted); margin-top: 2px; }

.tcount {
  background: var(--danger); color: #fff;
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; white-space: nowrap;
}


.badge {
  font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-2);
  border: 1px solid var(--border); white-space: nowrap;
}
.badge.new {
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 2px 8px rgba(79,70,229,.3);
}
.badge.open {
  background: linear-gradient(135deg, #f59e0b, #fbbf24);
  color: #1a1a1a;
  border-color: transparent;
  box-shadow: 0 2px 8px rgba(245,158,11,.3);
}
.badge.pending {
  background: linear-gradient(135deg, #e67e22, #f39c12);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 2px 8px rgba(230,126,34,.3);
}
.badge.closed {
  background: linear-gradient(135deg, #12b76a, #2ecc8f);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 2px 8px rgba(18,183,106,.3);
}
.badge.free { background: var(--surface-2); color: var(--muted); border-style: dashed; }
.badge.fresh {
  background: linear-gradient(135deg, #12b76a, #2ecc8f);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 2px 8px rgba(18,183,106,.3);
}
[data-theme="dark"] .badge.new {
  background: linear-gradient(135deg, #7c7cf9, #8b8bfb);
  box-shadow: 0 2px 8px rgba(124,124,249,.4);
}
[data-theme="dark"] .badge.open,
[data-theme="dark"] .badge.pending { background: #3a2f16; color: #f0c674; }
[data-theme="dark"] .badge.closed { background: #16301f; color: #6ee7a8; }
[data-theme="dark"] .badge.fresh { background: #14321f; color: #86efac; }

.empty {
  color: var(--muted); font-size: 14px; text-align: center;
  padding: 34px 14px; margin: 0;
  border: 1px dashed var(--border); border-radius: 13px; background: var(--surface-2);
}
.spinner { color: var(--muted); text-align: center; padding: 22px; }

/* ============================== СЧЁТЧИКИ ============================== */

.counters { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; }
.counter {
  display: flex; flex-direction: column; gap: 3px;
  padding: 15px 16px; border-radius: 14px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  transition: transform .14s, box-shadow .16s;
}
.counter:hover { transform: translateY(-2px); box-shadow: var(--shadow-sm); }
.counter.new { border-left-color: var(--primary); }
.counter.open, .counter.pending { border-left-color: var(--warn); }
.counter.closed { border-left-color: var(--ok); }
.counter-num { font-size: 26px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.counter-label { font-size: 12.5px; color: var(--muted); }

/* Разбивка по отделам в панели сотрудника */
.groupstats { display: grid; gap: 6px; margin-top: 12px; }
.gstat {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 12px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13.5px;
}
.gstat-name { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gstat-num { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Подзаголовок внутри карточки: «Взял в работу», «Создал сам» */
.card > h3.sub {
  margin: 18px 0 8px; font-size: 12.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
}
.card > h3.sub:first-of-type { margin-top: 4px; }

/* ============================== СТАТУС / РОЛЬ ============================== */

.status {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 13px; color: var(--text-2);
  padding: 5px 12px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); flex: none; }
.status.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 35%, transparent); background: color-mix(in srgb, var(--ok) 10%, transparent); }
.status.ok .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 22%, transparent); }
.status.err { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); background: var(--danger-soft); }
.status.err .dot { background: var(--danger); }

.rolechip {
  font-size: 12px; font-weight: 650; padding: 5px 12px; border-radius: 999px;
  border: 1px solid transparent; white-space: nowrap;
}
.rolechip.agent { background: var(--primary-soft); color: var(--primary); }
.rolechip.customer { background: color-mix(in srgb, var(--ok) 12%, transparent); color: var(--ok); }


/* ============================== КАРТОЧКА ЗАЯВКИ ============================== */

.drawer-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(16,24,40,.5);
  display: flex; justify-content: flex-end;
}
.drawer-backdrop[hidden] { display: none; }
.drawer {
  width: min(780px, 100%); height: 100%;
  background: var(--surface);
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: slide-in .22s cubic-bezier(.2,.7,.3,1);
}
@keyframes slide-in { from { transform: translateX(28px); opacity: .5; } to { transform: none; opacity: 1; } }

.drawer-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 14px;
  padding: 18px 22px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.drawer-head-main { min-width: 0; }
.drawer-head .tnum {
  display: inline-block;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}
.drawer-head h2 { font-size: 21px; line-height: 1.25; margin: 6px 0 6px; overflow-wrap: anywhere; }
.drawer-head .sub { font-size: 12.5px; color: var(--muted); margin: 0; }
.drawer-head-actions { display: flex; align-items: center; gap: 10px; flex: none; }
.drawer-head-actions .icon-btn { flex: none; width: 34px; }

.drawer-body { flex: 1; overflow-y: auto; padding: 20px 22px; background: var(--bg); }

.msg {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: 14px; padding: 13px 15px; margin-bottom: 12px;
  animation: rise .26s backwards;
}
.msg.from-agent { border-left-color: var(--primary); }
.msg.from-customer { border-left-color: var(--accent); }
.msg.internal { background: color-mix(in srgb, var(--warn) 7%, var(--surface)); border-color: color-mix(in srgb, var(--warn) 30%, transparent); border-left-color: var(--warn); }
.msg-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12.5px; color: var(--muted); margin-bottom: 9px; }
.msg-head b { color: var(--text); font-size: 13.5px; }
.msg-meta { color: var(--muted); overflow-wrap: anywhere; }
.msg-head time { margin-left: auto; white-space: nowrap; }
.tag-internal { background: color-mix(in srgb, var(--warn) 18%, transparent); color: #8a5a12; font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
[data-theme="dark"] .tag-internal { color: #f0c674; }
.msg-body { font-size: 14px; line-height: 1.6; overflow-wrap: anywhere; }
.msg-body img { max-width: 100%; height: auto; border-radius: 8px; }
.msg-body table { border-collapse: collapse; }
.msg-body td, .msg-body th { border: 1px solid var(--border); padding: 5px 8px; }
.msg-files { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 11px; }
.filechip {
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 12.5px; text-decoration: none; cursor: pointer;
  color: var(--primary); background: var(--primary-soft);
  border: 1px solid transparent; border-radius: 9px; padding: 6px 11px;
  max-width: 100%;
  transition: background .15s, transform .12s, border-color .15s;
}
.filechip:hover:not(:disabled) { background: color-mix(in srgb, var(--primary) 18%, transparent); transform: translateY(-1px); }
.filechip:active:not(:disabled) { transform: translateY(0); }
.filechip:disabled { cursor: progress; opacity: .75; }

.drawer-foot { border-top: 1px solid var(--border); padding: 13px 22px 18px; background: var(--surface); }
.drawer-actions { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; margin-bottom: 11px; }
.drawer-actions[hidden] { display: none; }
.action-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.action-label {
  font-size: 11px; font-weight: 600; letter-spacing: .05em;
  text-transform: uppercase; color: var(--muted);
}
.apply-bar {
  display: flex; align-items: center; gap: 9px; flex-wrap: wrap;
  margin: 0 0 11px; padding: 9px 12px;
  border: 1px solid var(--primary);
  border-radius: 8px;
  background: var(--primary-soft, rgba(99, 91, 255, .08));
  font-size: 13px;
}
.apply-bar[hidden] { display: none; }
.apply-bar .apply-text { min-width: 0; }
.apply-bar .spacer { flex: 1; }
.inline-select {
  font: inherit; font-size: 13px; padding: 6px 10px; cursor: pointer;
  width: auto; min-width: 0; max-width: 100%;
  border: 1px solid var(--border-strong); border-radius: 9px;
  background-color: var(--surface); color: var(--text-2);
  transition: border-color .15s, color .15s;
}
.inline-select:hover { border-color: var(--primary); color: var(--primary); }

.drawer-note {
  margin: 0;
  padding: 10px 12px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--surface-2, rgba(0, 0, 0, 0.03));
  color: var(--muted, #667085);
  font-size: 13px;
  line-height: 1.45;
}
.drawer-note[hidden] { display: none; }

.reply-block { border-top: 1px dashed var(--border); padding-top: 11px; }
.reply-block textarea { min-height: 78px; }
.reply-tools { display: flex; align-items: center; gap: 8px; margin-top: 9px; flex-wrap: wrap; }
.reply-tools .spacer { flex: 1; }


/* ============================== МАРШРУТИЗАЦИЯ ============================== */

.route-group { max-width: 520px; }

.assign-toggle {
  display: flex; align-items: flex-start; gap: 12px; cursor: pointer;
  padding: 13px 15px; margin: 4px 0 14px; border-radius: 13px;
  border: 1.5px solid var(--border); background: var(--surface-2);
  transition: border-color .16s, background .16s, box-shadow .16s;
}
.assign-toggle:hover { border-color: var(--border-strong); }
.assign-toggle.disabled { opacity: .55; cursor: not-allowed; }
.assign-toggle input { position: absolute; opacity: 0; width: 0; height: 0; }

/* Свой переключатель: галочка появляется при включении */
.assign-box {
  width: 21px; height: 21px; flex: none; margin-top: 1px;
  border-radius: 6px; border: 1.5px solid var(--border-strong);
  background: var(--surface);
  position: relative;
  transition: background .15s, border-color .15s;
}
.assign-box::after {
  content: ''; position: absolute; inset: 0;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 14'%3E%3Cpath d='M2 7.5l3.2 3.2L12 3.8' stroke='%23fff' stroke-width='2' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E")
    center / 14px no-repeat;
  opacity: 0; transform: scale(.5); transition: opacity .15s, transform .15s;
}
.assign-toggle input:checked + .assign-box { background: var(--primary); border-color: var(--primary); }
.assign-toggle input:checked + .assign-box::after { opacity: 1; transform: none; }
.assign-toggle input:focus-visible + .assign-box { box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 20%, transparent); }

.assign-text { display: flex; flex-direction: column; gap: 3px; line-height: 1.4; }
.assign-text b { font-size: 14px; }
.assign-text i { font-style: normal; font-size: 12.5px; color: var(--muted); }

.assign-toggle:has(input:checked) {
  border-color: var(--primary); background: var(--primary-soft);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 13%, transparent);
}
.assign-toggle:has(input:checked) .assign-text b { color: var(--primary); }

.assign-field { max-width: 520px; animation: rise .2s; }

/* ============================== ПРАВА ДОСТУПА ============================= */

.rights-card { padding: 0; }
.rights-card > summary {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px; cursor: pointer; list-style: none;
  border-radius: 15px;
}
.rights-card > summary::-webkit-details-marker { display: none; }
.rights-card > summary::after {
  content: '▾'; margin-left: auto; color: var(--muted);
  transition: transform .18s;
}
.rights-card[open] > summary::after { transform: rotate(180deg); }
.rights-card > summary h2 { font-size: 15px; margin: 0; }
.rights-card[open] > summary { border-bottom: 1px solid var(--border); border-radius: 15px 15px 0 0; }

.rights-body { padding: 4px 18px 16px; }
.rights-list {
  display: grid; grid-template-columns: max-content 1fr;
  gap: 7px 16px; margin: 10px 0 12px; font-size: 13.5px;
}
.rights-list dt { color: var(--muted); }
.rights-list dd { margin: 0; overflow-wrap: anywhere; }
.rights-body .hint { margin: 0; }

.route-preview {
  margin: 0; padding: 11px 14px; font-size: 13.5px; line-height: 1.5;
  border-radius: 12px; color: var(--text-2);
  background: var(--surface-2); border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
}
.route-preview.warn { border-left-color: var(--warn); }

/* Полоса маршрута в карточке заявки */
.route-strip { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 11px 22px; background: var(--surface); border-bottom: 1px solid var(--border); }
.route-strip .rs-item { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); }
.route-strip .rs-ico { color: var(--muted); font-size: 12px; }
.route-strip .rs-sep { color: var(--border-strong); }
.route-strip .rs-free { color: var(--warn); font-weight: 600; }
.route-strip .rs-me { color: var(--primary); font-weight: 600; }

/* «Переписка / История» — обычные кнопки нормального размера,
   справа от них переключатель автообновления. */
.drawer-views {
  padding: 12px 22px 0;
  display: flex; align-items: center; gap: 6px;
}
.drawer-views .tab {
  width: auto; flex: none;
  padding: 9px 18px; min-height: 40px;
  font-size: 14px; font-weight: 600;
}
.drawer-auto { margin-left: auto; font-size: 12.5px; color: var(--muted); }
.drawer-auto input { width: 15px; height: 15px; }

/* ============================== ИСТОРИЯ ============================== */

#dwHistory { padding-top: 14px; }

.hrow {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px 14px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px;
  animation: rise .26s backwards;
}
.hicon {
  width: 30px; height: 30px; flex: none; border-radius: 9px;
  display: grid; place-items: center; font-size: 13px;
  background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border);
}
.hmain { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.htext { font-size: 14px; line-height: 1.45; }
.hwho { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.hwho b { color: var(--text-2); font-weight: 600; }
.hwho b.sys { color: var(--muted); font-weight: 500; }
.hwho time { margin-left: auto; white-space: nowrap; }

.hrow.h-create { border-left: 3px solid var(--primary); }
.hrow.h-create .hicon { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.hrow.h-group { border-left: 3px solid var(--accent); }
.hrow.h-group .hicon { color: var(--accent); }
.hrow.h-owner { border-left: 3px solid var(--ok); }
.hrow.h-owner .hicon { background: color-mix(in srgb, var(--ok) 12%, transparent); color: var(--ok); border-color: transparent; }
.hrow.h-state .hicon { color: var(--primary); }
.hrow.h-state.h-new .hicon, .hrow.h-state.h-open .hicon { color: var(--warn); }
.hrow.h-state.h-closed .hicon { color: var(--ok); }
.hrow.h-priority .hicon { color: var(--warn); }

.hnote { margin: 12px 0 0; font-size: 12.5px; color: var(--muted); text-align: center; }

/* ============================== МОДАЛКИ ============================== */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(16,24,40,.55);
  display: flex; align-items: center; justify-content: center; padding: 20px;
  animation: fade .18s;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); border-radius: 18px; padding: 24px;
  width: 100%; max-width: 450px;
  box-shadow: var(--shadow-lg);
  max-height: 90vh; overflow: auto;
  animation: pop .22s cubic-bezier(.2,.7,.3,1);
}
@keyframes pop { from { transform: scale(.96) translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal h3 { font-size: 18px; margin-bottom: 9px; }
.modal-text { font-size: 13.5px; color: var(--muted); margin: 0 0 16px; white-space: pre-line; }
.modal-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 20px; }

/* Узкая модалка подтверждения с цветным значком */
.modal-narrow { max-width: 420px; }
.modal-badge {
  width: 46px; height: 46px; border-radius: 50%;
  display: grid; place-items: center; margin-bottom: 14px;
  background: var(--primary-soft); color: var(--primary);
}
.modal-badge .ico { width: 24px; height: 24px; stroke-width: 2.2; }
.modal-badge.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.modal-badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.modal-badge.danger { background: var(--danger-soft); color: var(--danger); }
.btn-danger {
  color: #fff;
  background: linear-gradient(135deg, #dc2626, #ef4444);
  box-shadow: 0 4px 14px rgba(220, 38, 38, .3);
}
.btn-danger:hover:not(:disabled) { filter: brightness(1.05); box-shadow: 0 8px 22px rgba(220, 38, 38, .38); }
.modal-error {
  margin: 12px 0 0; padding: 10px 12px; font-size: 13.5px;
  background: var(--danger-soft); border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
  border-radius: 10px; color: var(--danger);
}

.tpl-list { display: grid; gap: 8px; }
.tpl {
  display: flex; flex-direction: column; gap: 4px; text-align: left;
  padding: 11px 13px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); cursor: pointer; font: inherit;
  transition: border-color .15s, background .15s, transform .12s;
}
.tpl:hover { background: var(--primary-soft); border-color: var(--primary); transform: translateY(-1px); }
.tpl b { font-size: 13.5px; }
.tpl span { font-size: 12.5px; color: var(--muted); }

/* ============================== УВЕДОМЛЕНИЯ ============================== */

.toasts { position: fixed; right: 18px; bottom: 18px; z-index: 90; display: grid; gap: 10px; max-width: min(420px, 92vw); }
.toast {
  padding: 13px 16px; border-radius: 13px; font-size: 14px;
  box-shadow: var(--shadow-lg);
  background: #1f2937; color: #fff;
  animation: toast-in .24s cubic-bezier(.2,.7,.3,1);
}
@keyframes toast-in { from { opacity: 0; transform: translateY(10px) scale(.97); } to { opacity: 1; transform: none; } }
.toast.ok { background: linear-gradient(135deg, #0f9d63, #12b76a); }
.toast.err { background: linear-gradient(135deg, #c62f2f, #ef4444); }
.toast.warn { background: linear-gradient(135deg, #b45309, #f59e0b); }
.toast .tlink { display: block; margin-top: 8px; color: #cfe0ff; font-size: 13px; }

/* ============================== АДАПТИВ ============================== */

@media (max-width: 900px) {
  /* minmax(0, 1fr) обязателен: обычный 1fr берёт min-content содержимого
     и выталкивает карточки за правый край экрана. */
  .app {
    grid-template-columns: minmax(0, 1fr);
    background: none;
    /* место под нижний бар, чтобы он не закрывал последнюю карточку */
    padding-bottom: calc(68px + env(safe-area-inset-bottom));
  }
  html, body { overflow-x: hidden; }
  .ticket { min-width: 0; }
  .ticket .tnum, .ticket .badge { flex: none; }
  .ticket .tmain { min-width: 0; }

  /* Навигация как в мобильных приложениях: полоса внизу экрана,
     крупная иконка + подпись под ней. */
  .sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    /* выше карточки заявки (60), но ниже модальных окон (70) */
    z-index: 65; height: auto;
    flex-direction: row; align-items: stretch; gap: 6px;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-bottom: 0;
    border-top: 1px solid var(--border);
    background: var(--surface);
    box-shadow: 0 -6px 20px rgba(16, 24, 40, .08);
    overflow: visible;
  }
  .brand { display: none; }
  .sidebar-foot { display: none; }
  /* Только вкладки в нижнем баре: остальные .tab (карточка, вход) не трогаем. */
  .sidebar .tabs { flex-direction: row; flex: 1; gap: 2px; }
  .sidebar .tab {
    flex: 1 1 0; width: auto; min-height: 52px;
    flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; padding: 5px 4px; border-radius: 12px;
    font-size: 11px; font-weight: 600; text-align: center;
  }
  .sidebar .tab-ico { width: auto; line-height: 1; }
  .sidebar .tab-ico .ico { width: 21px; height: 21px; }
  .sidebar .tab-label {
    display: block; flex: none; max-width: 100%;
    font-size: 11px; line-height: 1.15;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .sidebar .tab.active { background: var(--primary-soft); color: var(--primary); box-shadow: none; }
  .sidebar .tab-badge {
    position: absolute; top: 2px; right: 2px; margin: 0;
    min-width: 16px; padding: 0 4px; line-height: 15px; font-size: 10px;
    pointer-events: none;
  }
  main { padding: 18px 16px 24px; }
  .topbar {
    padding: 10px 16px; flex-wrap: nowrap; gap: 8px;
    background: var(--surface);
  }
  .topbar .crumb { min-width: 0; overflow: hidden; }
  .status { min-width: 0; }
  .status span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top-actions .icon-btn { width: 34px; height: 34px; }
  .card { padding: 16px; }
  .toolbar select, .toolbar input[type=search] { min-width: 0; flex: 1; }
  .drawer-head, .drawer-body, .drawer-foot { padding-left: 16px; padding-right: 16px; }
}

/* ============================== МОБИЛЬНЫЕ ЭКРАНЫ ============================== */

.attach-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 9px; }
.attach-actions .btn { min-height: 36px; }
.icon-btn.on { color: var(--primary); border-color: var(--primary); }

@media (max-width: 900px) {
  /* Нижний бар закреплён в первом блоке — здесь только содержимое панелей. */

  /* Тулбар списка: поиск на всю ширину, фильтры в одну-две строки */
  .toolbar { gap: 8px; }
  .toolbar .search { flex: 1 1 100%; }
  .toolbar select, .toolbar .btn { flex: 1 1 auto; min-height: 40px; }
  .list-note { flex-direction: column; align-items: flex-start; gap: 8px; }

  /* Карточка заявки — на весь экран */
  .drawer-backdrop { padding: 0; }
  .drawer {
    width: 100%; max-width: none;
    height: 100vh; height: 100dvh;
    max-height: 100vh; max-height: 100dvh;
    border-radius: 0; box-shadow: none;
    /* нижний бар остаётся видимым поверх открытой заявки */
    padding-bottom: calc(64px + env(safe-area-inset-bottom));
  }
  .drawer-head { flex: none; gap: 10px; }
  /* На телефоне карточка прокручивается целиком: вложенные области
     прокрутки (переписка + футер) прятали кнопки ответа за краем экрана. */
  .drawer { overflow-y: auto; overscroll-behavior: contain; }
  .drawer-head {
    position: sticky; top: 0; z-index: 3;
    background: var(--surface);
  }
  .drawer-body { flex: 0 0 auto; overflow: visible; min-height: 0; }
  .drawer-foot { flex: 0 0 auto; max-height: none; overflow: visible; padding-top: 10px; padding-bottom: 16px; }
  .reply-block textarea { min-height: 64px; }
  .drawer-views { padding-left: 16px; padding-right: 16px; }

  /* Действия агента и подтверждение — в колонку */
  .drawer-actions { flex-direction: column; align-items: stretch; gap: 8px; }
  .drawer-actions .action-field,
  .drawer-actions .inline-select,
  .drawer-actions .btn { width: 100%; }
  .apply-bar { flex-direction: column; align-items: stretch; }
  .apply-bar .btn { width: 100%; }

  /* Ответ: кнопки удобно нажимать пальцем */
  .reply-tools { gap: 6px; }
  .reply-tools .btn,
  .reply-tools .switch { min-height: 40px; justify-content: center; }
  .reply-tools .btn-primary { width: 100%; }

  .toasts {
    left: 12px; right: 12px; max-width: none;
    bottom: calc(76px + env(safe-area-inset-bottom));
  }
  .modal { padding: 18px; border-radius: 14px; }
  .modal-actions { flex-direction: column-reverse; }
  .modal-actions .btn { width: 100%; }

  /* iOS не увеличивает страницу при фокусе в поле */
  input, select, textarea { font-size: 16px; }
}

@media (max-width: 600px) {
  .brand-mark img { width: 32px; height: 32px; }
  /* В узкой шапке оставляем имя пользователя, роль дублирует профиль */
  .rolechip { display: none; }
  .card { padding: 14px; }
  .card-title h2 { font-size: 16px; }
  .drawer-head h2 { font-size: 18px; }
  .drawer-head .tnum { font-size: 10.5px; }
  .msg { padding: 10px 12px; }
  .msg-body { font-size: 14px; }
  .ticket { padding: 10px 12px; }
  .legend .lg { font-size: 11.5px; padding: 4px 8px; }
  .tabs .tab-ico { font-size: 17px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001s !important; transition-duration: .001s !important; }
}
