/* ==========================================================================
   Токены. Светлая тема по умолчанию, тёмная — по системе или вручную (data-theme)
   ========================================================================== */
:root {
  --bg: #F4F1EC;
  --panel: #FFFFFF;
  --panel2: #EEE9E1;
  --ink: #1E1A16;
  --mute: #6C645B;
  --line: #E0D9CE;
  --accent: #4F55C9;
  --accent-ink: #FFFFFF;
  --accent-soft: #E1E3F8;
  --warm: #C8553D;
  --warm-soft: #F8E0D8;
  --warn: #A86A00;
  --warn-soft: #FBEFD5;
  --neg: #B23A2C;
  --neg-soft: #F7DCD7;
  --shadow: 0 1px 0 rgba(30, 26, 22, .04), 0 10px 28px -16px rgba(30, 26, 22, .28);
  --radius: 14px;
  --radius-sm: 9px;
  --font: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-head: "Unbounded", "IBM Plex Sans", sans-serif;
  --nav-h: 0px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14120F;
    --panel: #1E1B17;
    --panel2: #29251F;
    --ink: #F1EBE2;
    --mute: #A39A8E;
    --line: #36302A;
    --accent: #9EA4F5;
    --accent-ink: #14163A;
    --accent-soft: #252848;
    --warm: #F08A6B;
    --warm-soft: #3D231C;
    --warn: #E8B85A;
    --warn-soft: #3A2E15;
    --neg: #F08A7B;
    --neg-soft: #3B1F1B;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14120F;
  --panel: #1E1B17;
  --panel2: #29251F;
  --ink: #F1EBE2;
  --mute: #A39A8E;
  --line: #36302A;
  --accent: #9EA4F5;
  --accent-ink: #14163A;
  --accent-soft: #252848;
  --warm: #F08A6B;
  --warm-soft: #3D231C;
  --warn: #E8B85A;
  --warn-soft: #3A2E15;
  --neg: #F08A7B;
  --neg-soft: #3B1F1B;
  --shadow: none;
  color-scheme: dark;
}

/* ==========================================================================
   База
   ========================================================================== */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
h1, h2, h3 { font-family: var(--font-head); font-weight: 600; line-height: 1.2; margin: 0; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; display: block; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.mute { color: var(--mute); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack.tight { gap: 6px; }
.center { text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ==========================================================================
   Каркас: шапка, навигация, контент
   ========================================================================== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.topbar-in {
  max-width: 1280px;
  margin: 0 auto;
  height: 56px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--ink); font: 600 16px/1 var(--font-head); }
.brand:hover { text-decoration: none; }
.brand-logo { font-size: 22px; }
.topnav { display: none; gap: 4px; }
.topnav a {
  color: var(--mute);
  padding: 8px 12px;
  border-radius: 999px;
  font-weight: 500;
  font-size: 14px;
}
.topnav a:hover { text-decoration: none; color: var(--ink); background: var(--panel2); }
.topnav a.active { color: var(--accent-ink); background: var(--accent); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.bottomnav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--panel);
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
}
.bottomnav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--mute);
  font-size: 11px;
  font-weight: 500;
}
.bottomnav a:hover { text-decoration: none; }
.bottomnav a .ico { font-size: 21px; line-height: 1; filter: grayscale(.9); opacity: .75; }
.bottomnav a.active { color: var(--accent); }
.bottomnav a.active .ico { filter: none; opacity: 1; }

main#view {
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h1 { flex: 1; min-width: 0; overflow-wrap: anywhere; }

@media (min-width: 900px) {
  body { padding-bottom: 0; }
  .bottomnav { display: none; }
  .topnav { display: flex; }
  main#view { padding: 24px 24px 48px; }
  h1 { font-size: 26px; }
}

/* ==========================================================================
   Компоненты
   ========================================================================== */
.card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 16px;
}
.card.flat { box-shadow: none; }
.card h2 { margin-bottom: 10px; }
.section + .section { margin-top: 16px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 10px;
  padding: 9px 14px;
  font-weight: 500;
  font-size: 14px;
  line-height: 1.2;
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { text-decoration: none; background: var(--panel2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--neg); }
.btn.primary.danger { background: var(--neg); border-color: var(--neg); color: var(--panel); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel2); }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.icon { padding: 7px; min-width: 36px; }
.btn.block { width: 100%; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--panel2);
  color: var(--mute);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}
.chip.wrap { white-space: normal; overflow-wrap: anywhere; max-width: 100%; } /* длинные псевдонимы USDA */
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.neg { background: var(--neg-soft); color: var(--neg); }
.chip.warm { background: var(--warm-soft); color: var(--warm); }

/* Переключатели-«таблетки» (фильтры, вкладки, сегменты) */
.pills { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.pills::-webkit-scrollbar { display: none; }
.pill {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.pill:hover { background: var(--panel2); }
.pill.on { background: var(--ink); border-color: var(--ink); color: var(--bg); }

.segmented {
  display: inline-flex;
  background: var(--panel2);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}
.segmented button {
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 500;
  color: var(--mute);
  cursor: pointer;
}
.segmented button.on { background: var(--panel); color: var(--ink); box-shadow: var(--shadow); }

/* Формы */
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--mute); }
.field > span { font-weight: 500; }
.input, .select, .textarea {
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  color: var(--ink);
  font-size: 15px;
}
.textarea { min-height: 96px; resize: vertical; line-height: 1.45; }
.input:focus, .select:focus, .textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.form-grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 640px) {
  .form-grid.two { grid-template-columns: 1fr 1fr; }
  .form-grid.four { grid-template-columns: repeat(4, 1fr); }
}
.search {
  position: relative;
  flex: 1;
  min-width: 180px;
}
.search .input { padding-left: 34px; }
.search::before {
  content: "🔍";
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  opacity: .6;
}

.alert { border-radius: var(--radius-sm); padding: 10px 12px; font-size: 14px; }
.alert.err { background: var(--neg-soft); color: var(--neg); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.ok { background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; color: var(--mute); padding: 40px 16px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }

.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Таблицы */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; color: var(--mute); font-weight: 500; font-size: 12px; padding: 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table td.r, .table th.r { text-align: right; }
.table tr.click { cursor: pointer; }
.table tr.click:hover td { background: var(--panel2); }

/* Модальное окно: снизу «шторкой» на телефоне, по центру на десктопе */
.modal-back {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fade .15s ease-out;
}
.modal {
  background: var(--panel);
  width: 100%;
  max-height: 92vh;
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom);
  animation: rise .2s ease-out;
}
.modal-head { display: flex; align-items: center; gap: 8px; padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }
.modal-head h2 { flex: 1; font-size: 16px; }
.modal-body { padding: 14px 16px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 16px 14px; border-top: 1px solid var(--line); }
@media (min-width: 640px) {
  .modal-back { align-items: center; padding: 24px; }
  .modal { max-width: 560px; border-radius: 18px; max-height: 86vh; }
  .modal.wide { max-width: 860px; }
}
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px); opacity: .6; } }

.toasts {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom));
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(92vw, 420px);
  pointer-events: none;
}
.toast {
  background: var(--ink);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
  animation: rise .2s ease-out;
}
.toast.err { background: var(--neg); color: #fff; }
@media (min-width: 900px) { .toasts { bottom: 24px; } }

/* Меню пользователя */
.menu { position: relative; }
.menu-pop {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 200px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18);
  padding: 6px;
  z-index: 50;
}
.menu-pop a, .menu-pop button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  border: 0;
  background: none;
  color: var(--ink);
  font-size: 14px;
  cursor: pointer;
}
.menu-pop a:hover, .menu-pop button:hover { background: var(--panel2); text-decoration: none; }
.menu-pop hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.menu-pop .who { padding: 8px 10px; font-size: 12px; color: var(--mute); }

/* Экран входа */
.auth-wrap { max-width: 380px; margin: 6vh auto 0; }
.auth-wrap .card { padding: 24px 20px; }
.auth-wrap h1 { font-size: 20px; margin-bottom: 4px; }

/* ==========================================================================
   Списки: панель инструментов и чипы фильтров
   ========================================================================== */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.filters { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.filters .search { flex: none; width: 100%; }

/* Чипы сортировки и фильтров (filter-chips.js): на телефоне — лента с прокруткой, шире — с переносом */
.fchips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.fchips::-webkit-scrollbar { display: none; }
@media (min-width: 640px) { .fchips { flex-wrap: wrap; overflow: visible; } }
.fchip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
  max-width: 260px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 999px;
  padding: 7px 12px 7px 14px;
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.fchip:hover { background: var(--panel2); }
.fchip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.fchip[aria-pressed] { padding-right: 14px; }
.fchip-text { overflow: hidden; text-overflow: ellipsis; }
.fchip-icon { font-size: 13px; opacity: .8; }
.fchip-caret { width: 12px; height: 12px; flex: none; opacity: .65; }
.fchip[aria-expanded="true"] .fchip-caret { transform: rotate(180deg); }
.fchip.on, .fchip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.fchip.reset { border-color: transparent; background: transparent; color: var(--mute); padding: 7px 10px; }
.fchip.reset:hover { color: var(--ink); }

/* Варианты: общий вид для выпадающего списка и шторки */
.fopt {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  text-align: left;
  padding: 9px 12px;
  border-radius: 8px;
  cursor: pointer;
}
.fopt:hover, .fopt:focus-visible { background: var(--panel2); outline: none; }
.fmark { flex: none; width: 18px; height: 18px; border: 1.5px solid var(--line); background: var(--panel); position: relative; }
.fmark.radio { border-radius: 50%; }
.fmark.box { border-radius: 5px; }
.fopt[aria-selected="true"] .fmark { border-color: var(--accent); }
.fopt[aria-selected="true"] .fmark.radio::after { content: ""; position: absolute; inset: 3px; border-radius: 50%; background: var(--accent); }
.fopt[aria-selected="true"] .fmark.box { background: var(--accent); }
.fopt[aria-selected="true"] .fmark.box::after {
  content: "";
  position: absolute;
  left: 5px;
  top: 1px;
  width: 5px;
  height: 10px;
  border: solid var(--accent-ink);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
.fmenu {
  position: absolute;
  z-index: 90;
  min-width: 220px;
  max-width: min(340px, calc(100vw - 16px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 32px -12px rgba(0, 0, 0, .35);
  padding: 4px;
  animation: fade .12s ease-out;
}
.fmenu-list { max-height: min(360px, 60vh); overflow-y: auto; }
.fmenu-foot { border-top: 1px solid var(--line); margin-top: 4px; padding: 6px 8px 4px; }
.fmenu-clear { border: 0; background: none; padding: 2px 4px; color: var(--accent); font: inherit; font-size: 13px; font-weight: 500; cursor: pointer; }
.fsheet { margin: -6px -8px; }
.fsheet .fopt { font-size: 16px; padding: 13px 8px; }
.fsheet .fmark { width: 22px; height: 22px; }
.fsheet .fopt[aria-selected="true"] .fmark.radio::after { inset: 4px; }
.fsheet .fopt[aria-selected="true"] .fmark.box::after { left: 6px; top: 2px; width: 6px; height: 12px; }

.chip.ok { background: var(--accent-soft); color: var(--accent); }
.chip.st-resolved { background: color-mix(in srgb, #2F7A4F 16%, transparent); color: #2F7A4F; }
.chip.st-in_progress { background: var(--warn-soft); color: var(--warn); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .chip.st-resolved { color: #7FD3A0; } }
:root[data-theme="dark"] .chip.st-resolved { color: #7FD3A0; }

/* ==========================================================================
   Обращения: список
   ========================================================================== */
.tlist { display: flex; flex-direction: column; gap: 8px; }
.trow { display: flex; flex-direction: column; gap: 6px; color: var(--ink); padding: 12px 14px; transition: border-color .12s; }
.trow:hover { text-decoration: none; border-color: var(--accent); }
.trow-top { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.trow-kind { font-size: 16px; line-height: 1; }
.trow-text { overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.trow-meta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.svc { background: var(--panel2); color: var(--ink); }

/* ==========================================================================
   Обращения: карточка
   ========================================================================== */
.back-link { display: inline-block; margin-bottom: 8px; color: var(--mute); }
.ticket-head h1 { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.ticket-layout { display: grid; gap: 16px; grid-template-columns: 1fr; }
.ticket-main, .ticket-side { min-width: 0; }
@media (min-width: 1000px) {
  .ticket-layout { grid-template-columns: minmax(0, 1fr) 360px; align-items: start; }
  .ticket-side { position: sticky; top: 72px; }
}
.message { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 16px; line-height: 1.55; }
.gallery { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.shot { padding: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--panel2); cursor: zoom-in; width: 140px; height: 104px; }
.shot img { width: 100%; height: 100%; object-fit: cover; }
.full-shot { width: auto; height: auto; max-width: 100%; max-height: 70vh; margin: 0 auto; border-radius: 8px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.kv { display: grid; grid-template-columns: minmax(96px, auto) 1fr; gap: 6px 12px; margin: 0; font-size: 14px; }
.kv dt { color: var(--mute); }
.kv dd { margin: 0; overflow-wrap: anywhere; min-width: 0; }
.ua { margin-top: 10px; }
.ua code { display: block; font-size: 12px; margin-top: 6px; overflow-wrap: anywhere; color: var(--mute); }
.refs { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 4px; }
.log { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.log li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px; padding: 6px 8px; border-radius: 8px; background: var(--panel2); }
.log li.bad .chip { background: var(--neg-soft); color: var(--neg); }
.log code { font-size: 12px; overflow-wrap: anywhere; }
.log code.msg { flex-basis: 100%; white-space: pre-wrap; }
.timeline { list-style: none; margin: 0 0 14px; padding: 0 0 0 14px; border-left: 2px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.ev { position: relative; }
.ev::before { content: ""; position: absolute; left: -20px; top: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--line); border: 2px solid var(--panel); }
.ev-status::before { background: var(--accent); }
.ev-comment::before { background: var(--warm); }
.ev-text { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 4px; padding: 8px 10px; background: var(--panel2); border-radius: 8px; font-size: 14px; }
