:root {
  --ink: #0b1f33;
  --ink-soft: #3d5266;
  --muted: #6b7c8f;
  --paper: #f3f6fa;
  --panel: #ffffff;
  --line: #d5dde8;
  --teal: #0a7c78;
  --teal-deep: #065f5c;
  --teal-soft: #d7f1ef;
  --amber: #c47a14;
  --rose: #b42345;
  --ok: #1f7a4c;
  --shadow: 0 18px 50px rgba(11, 31, 51, 0.08);
  --radius: 18px;
  --font: "Manrope", system-ui, sans-serif;
  --display: "Fraunces", Georgia, serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--paper);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }
a { color: var(--teal); }
.mobile-only { display: none !important; }

.brand-mark {
  margin: 0;
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.35rem;
  letter-spacing: -0.02em;
  color: var(--ink);
}

.btn {
  border: 0;
  border-radius: 999px;
  padding: 0.7rem 1.15rem;
  font-weight: 600;
  transition: transform .15s ease, background .15s ease, opacity .15s ease;
  touch-action: manipulation;
}
.btn:hover { transform: translateY(-1px); }
.btn:disabled { opacity: .55; cursor: wait; transform: none; }
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:hover { background: var(--teal-deep); }
.btn-soft {
  background: var(--teal-soft);
  color: var(--teal-deep);
  padding: 0.45rem 0.9rem;
  border-radius: 12px;
}
.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid var(--line);
}

.form-error {
  color: var(--rose);
  margin: 0.4rem 0 0;
  font-size: 0.9rem;
}

.login-body {
  min-height: 100%;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(1rem, var(--safe-top)) max(1rem, var(--safe-right)) max(1rem, var(--safe-bottom)) max(1rem, var(--safe-left));
  position: relative;
  overflow-x: hidden;
  overflow-y: auto;
}
.login-atmosphere {
  position: absolute;
  inset: -20%;
  background:
    radial-gradient(circle at 18% 20%, rgba(10, 124, 120, 0.18), transparent 42%),
    radial-gradient(circle at 82% 10%, rgba(196, 122, 20, 0.12), transparent 36%),
    linear-gradient(155deg, #e7eef7 0%, #f7f9fc 45%, #dff3f1 100%);
  z-index: 0;
}
.login-atmosphere::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    -28deg,
    transparent 0 18px,
    rgba(11, 31, 51, 0.025) 18px 19px
  );
}
.login-shell {
  position: relative;
  z-index: 1;
  width: min(980px, 100%);
  display: grid;
  grid-template-columns: 1.2fr 0.9fr;
  gap: 1.5rem;
  align-items: stretch;
}
.login-brand {
  padding: 2.4rem 1.5rem 2.4rem 0.5rem;
  animation: rise 0.7s ease both;
}
.login-brand h1 {
  font-family: var(--display);
  font-size: clamp(2.2rem, 4vw, 3.4rem);
  line-height: 1.05;
  margin: 1rem 0 0.9rem;
  letter-spacing: -0.03em;
  max-width: 11ch;
}
.brand-lead {
  color: var(--ink-soft);
  font-size: 1.05rem;
  max-width: 34ch;
  line-height: 1.55;
}
.login-card {
  background: var(--panel);
  border: 1px solid rgba(213, 221, 232, 0.9);
  border-radius: 28px;
  box-shadow: var(--shadow);
  padding: 2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  animation: rise 0.7s ease 0.08s both;
}
.login-card h2 { margin: 0 0 1.2rem; font-size: 1.15rem; }
.login-card label { display: grid; gap: 0.35rem; margin-bottom: 0.9rem; }
.login-card label span { font-size: 0.82rem; color: var(--muted); font-weight: 600; }
.login-card input {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.85rem 0.95rem;
  background: #fbfcfe;
  outline: none;
  font-size: 16px;
}
.login-card input:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(10, 124, 120, 0.15);
}
.login-card .btn-primary { width: 100%; margin-top: 0.4rem; min-height: 48px; }

@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: none; }
}

.app-body {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
.app-grid {
  height: 100%;
  display: grid;
  grid-template-columns: 220px 340px minmax(0, 1fr) 260px;
  background:
    linear-gradient(180deg, rgba(215, 241, 239, 0.35), transparent 28%),
    var(--paper);
}

.sidebar {
  padding: 1.2rem 1rem;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background: rgba(255,255,255,0.55);
  backdrop-filter: blur(8px);
}
.sidebar-user { margin: 0.25rem 0 0; color: var(--muted); font-size: 0.85rem; }
.side-nav { display: grid; gap: 0.4rem; }
.nav-btn {
  text-align: left;
  border: 0;
  background: transparent;
  padding: 0.7rem 0.85rem;
  border-radius: 12px;
  color: var(--ink-soft);
  font-weight: 600;
  min-height: 44px;
  touch-action: manipulation;
}
.nav-btn.active, .nav-btn:hover {
  background: var(--teal-soft);
  color: var(--teal-deep);
}
.stat-strip { display: grid; gap: 0.55rem; margin-top: auto; }
.stat-strip div {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.7rem 0.8rem;
}
.stat-strip span {
  display: block;
  font-family: var(--display);
  font-size: 1.4rem;
  line-height: 1;
}
.stat-strip small { color: var(--muted); }
.logout-btn { width: 100%; min-height: 44px; }

.inbox-pane, .users-pane, .chat-pane, .user-pane {
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.inbox-pane, .users-pane { border-right: 1px solid var(--line); background: rgba(255,255,255,0.4); }
.pane-head {
  padding: 1rem;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 0.75rem;
}
.filters {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.35rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding-bottom: 2px;
}
.filters::-webkit-scrollbar { display: none; }
.chip {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 0.45rem 0.85rem;
  font-size: 0.82rem;
  color: var(--ink-soft);
  font-weight: 600;
  white-space: nowrap;
  flex: 0 0 auto;
  min-height: 36px;
  touch-action: manipulation;
}
.chip.active {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}
#search {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.75rem 0.85rem;
  background: #fff;
  outline: none;
  font-size: 16px;
}
#search:focus { border-color: var(--teal); }

.ticket-list {
  overflow: auto;
  padding: 0.5rem;
  -webkit-overflow-scrolling: touch;
}
.ticket-item {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 16px;
  padding: 0.95rem 0.9rem;
  display: grid;
  gap: 0.25rem;
  margin-bottom: 0.35rem;
  color: inherit;
  min-height: 64px;
  touch-action: manipulation;
}
.ticket-item:hover { background: rgba(255,255,255,0.8); }
.ticket-item.active {
  background: #fff;
  border-color: var(--line);
  box-shadow: 0 8px 24px rgba(11, 31, 51, 0.06);
}
.ticket-top {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  align-items: center;
}
.ticket-id { font-weight: 700; }
.badge {
  font-size: 0.72rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0.18rem 0.5rem;
}
.badge-open { background: #fff1d6; color: var(--amber); }
.badge-in_progress { background: var(--teal-soft); color: var(--teal-deep); }
.badge-closed { background: #e7f6ec; color: var(--ok); }
.ticket-subject {
  color: var(--ink-soft);
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ticket-sub { color: var(--muted); font-size: 0.78rem; }

.chat-pane { background: #f7fafc; position: relative; }
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--ink-soft);
  padding: 2rem;
  animation: rise .5s ease both;
}
.empty-state h2 {
  font-family: var(--display);
  margin: 0.6rem 0 0.4rem;
  color: var(--ink);
}
.chat-wrap { height: 100%; display: flex; flex-direction: column; min-height: 0; }
.chat-head {
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
  background: rgba(255,255,255,0.75);
  backdrop-filter: blur(8px);
}
.chat-head-main { min-width: 0; flex: 1; }
.chat-head h2 {
  margin: 0;
  font-family: var(--display);
  font-size: 1.35rem;
}
.chat-head p { margin: 0.3rem 0 0; color: var(--muted); font-size: 0.88rem; }
.chat-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.back-btn {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 1.2rem;
  flex: 0 0 auto;
  touch-action: manipulation;
}

.mobile-user-card {
  margin: 0;
  border-bottom: 1px solid var(--line);
  background: #fff;
}
.mobile-user-card summary {
  list-style: none;
  cursor: pointer;
  padding: 0.7rem 1rem;
  font-weight: 600;
  color: var(--teal-deep);
}
.mobile-user-card summary::-webkit-details-marker { display: none; }
.mobile-user-card dl {
  margin: 0;
  padding: 0 1rem 0.9rem;
  display: grid;
  gap: 0.55rem;
}
.mobile-user-card dt {
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 600;
}
.mobile-user-card dd { margin: 0.1rem 0 0; font-weight: 600; }

.messages {
  flex: 1;
  overflow: auto;
  padding: 1.1rem 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  -webkit-overflow-scrolling: touch;
}
.bubble {
  max-width: min(640px, 88%);
  padding: 0.8rem 0.95rem;
  border-radius: 16px;
  line-height: 1.45;
  animation: rise .25s ease both;
  white-space: pre-wrap;
  word-break: break-word;
}
.bubble.user {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--line);
}
.bubble.admin {
  align-self: flex-end;
  background: var(--teal);
  color: #fff;
}
.bubble.system {
  align-self: center;
  background: transparent;
  color: var(--muted);
  font-size: 0.82rem;
  padding: 0.2rem;
}
.bubble .meta {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.72rem;
  opacity: 0.75;
}
.attach-tag {
  display: inline-block;
  margin-top: 0.35rem;
  font-size: 0.8rem;
  opacity: 0.9;
}
.attach-list {
  display: grid;
  gap: 0.45rem;
  margin-top: 0.45rem;
}
.attach-preview {
  display: block;
  max-width: min(360px, 100%);
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid rgba(11, 31, 51, 0.08);
}
.attach-preview img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 420px;
  object-fit: contain;
  background: rgba(0,0,0,0.04);
}
.bubble.admin .attach-preview { border-color: rgba(255,255,255,0.25); }
.attach-file {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: inherit;
  font-size: 0.88rem;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.composer {
  border-top: 1px solid var(--line);
  padding: 0.9rem 1.1rem calc(1.1rem + var(--safe-bottom));
  background: #fff;
}
.composer-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 0.55rem;
  align-items: end;
}
.composer textarea {
  resize: none;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.75rem 0.85rem;
  outline: none;
  min-height: 48px;
  max-height: 140px;
  font-size: 16px;
}
.composer textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(10, 124, 120, 0.12);
}
.file-btn {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  display: grid;
  place-items: center;
  background: #f8fafc;
  touch-action: manipulation;
}
.file-name { margin: 0.45rem 0 0; color: var(--muted); font-size: 0.82rem; word-break: break-all; }
#send-btn { min-height: 48px; }

.user-pane {
  border-left: 1px solid var(--line);
  padding: 1.1rem;
  background: rgba(255,255,255,0.65);
}
.user-pane h3 {
  margin: 0 0 0.8rem;
  font-family: var(--display);
}
.user-pane dl { margin: 0; display: grid; gap: 0.7rem; }
.user-pane dt { font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.user-pane dd { margin: 0.15rem 0 0; font-weight: 600; }

.users-pane { grid-column: 2 / -1; }
.users-table-wrap { overflow: auto; padding: 0.5rem 1rem 1rem; -webkit-overflow-scrolling: touch; }
.users-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.users-table th, .users-table td {
  padding: 0.8rem 0.9rem;
  text-align: left;
  border-bottom: 1px solid var(--line);
  font-size: 0.92rem;
}
.users-table th { color: var(--muted); font-weight: 600; background: #f8fafc; }

@media (max-width: 1100px) {
  .app-grid { grid-template-columns: 200px 300px minmax(0, 1fr); }
  .user-pane { display: none !important; }
}

@media (max-width: 860px) {
  .mobile-only { display: initial !important; }
  .login-shell { grid-template-columns: 1fr; gap: 1rem; }
  .login-brand { padding: 0.25rem 0.25rem 0; }
  .login-brand h1 {
    font-size: clamp(1.9rem, 9vw, 2.6rem);
    max-width: none;
  }
  .brand-lead { font-size: 0.98rem; max-width: none; }
  .login-card { padding: 1.35rem; border-radius: 22px; }

  .app-grid {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .sidebar {
    flex-direction: row;
    align-items: center;
    gap: 0.55rem;
    padding: calc(0.65rem + var(--safe-top)) max(0.75rem, var(--safe-right)) 0.65rem max(0.75rem, var(--safe-left));
    border-right: 0;
    border-bottom: 1px solid var(--line);
    overflow: visible;
    z-index: 5;
  }
  .sidebar-brand { min-width: 0; flex: 1; }
  .sidebar-brand .brand-mark { font-size: 1.05rem; }
  .sidebar-user {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .side-nav { display: flex; gap: 0.3rem; flex: 0 0 auto; }
  .nav-btn {
    padding: 0.55rem 0.7rem;
    font-size: 0.85rem;
    white-space: nowrap;
  }
  .stat-strip,
  .logout-btn:not(.mobile-only) { display: none !important; }
  .logout-btn.mobile-only {
    display: inline-flex !important;
    width: auto;
    padding: 0.5rem 0.7rem;
    font-size: 0.82rem;
    flex: 0 0 auto;
  }

  .inbox-pane,
  .chat-pane,
  .users-pane {
    display: none;
    min-height: 0;
    height: 100%;
    border-right: 0;
  }
  .app-grid.show-list .inbox-pane { display: flex; }
  .app-grid.show-chat .chat-pane { display: flex; }
  .app-grid.show-users .users-pane {
    display: flex;
    grid-column: 1;
  }
  .app-grid.show-list .chat-pane,
  .app-grid.show-list .users-pane,
  .app-grid.show-chat .inbox-pane,
  .app-grid.show-chat .users-pane,
  .app-grid.show-users .inbox-pane,
  .app-grid.show-users .chat-pane { display: none; }

  .empty-state { display: none; }

  .chat-head {
    padding: 0.7rem 0.75rem;
    align-items: center;
    gap: 0.55rem;
  }
  .chat-head h2 { font-size: 1.1rem; }
  .chat-head p {
    font-size: 0.78rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .chat-actions {
    flex-wrap: nowrap;
    overflow-x: auto;
    max-width: 42%;
  }
  .chat-actions .btn-soft {
    padding: 0.4rem 0.65rem;
    font-size: 0.78rem;
    white-space: nowrap;
  }
  .back-btn { display: inline-grid !important; place-items: center; }

  .mobile-user-card[hidden] { display: none !important; }
  .mobile-user-card:not([hidden]) { display: block !important; }

  .messages { padding: 0.85rem 0.75rem; }
  .bubble { max-width: 92%; }
  .attach-preview { max-width: 100%; }
  .attach-preview img { max-height: 260px; }

  .composer { padding: 0.65rem 0.7rem calc(0.75rem + var(--safe-bottom)); }
  .composer-row {
    grid-template-columns: 1fr 48px;
    grid-template-areas:
      "text text"
      "file send";
  }
  .composer textarea { grid-area: text; min-height: 52px; }
  .file-btn { grid-area: file; }
  #send-btn {
    grid-area: send;
    width: 100%;
    border-radius: 14px;
  }

  .pane-head { padding: 0.75rem; }
  .ticket-list { padding: 0.35rem 0.45rem calc(0.75rem + var(--safe-bottom)); }

  .users-pane .pane-head h2 { font-size: 1.05rem; margin: 0; }
  .users-table-wrap { padding: 0.35rem 0.55rem calc(0.85rem + var(--safe-bottom)); }
  .users-table,
  .users-table thead,
  .users-table tbody,
  .users-table th,
  .users-table td,
  .users-table tr { display: block; }
  .users-table thead { display: none; }
  .users-table tr {
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #fff;
    margin-bottom: 0.55rem;
    padding: 0.7rem 0.8rem;
  }
  .users-table td {
    border: 0;
    padding: 0.2rem 0;
    font-size: 0.88rem;
  }
  .users-table td:nth-child(1)::before { content: "ID · "; color: var(--muted); font-weight: 600; }
  .users-table td:nth-child(2)::before { content: "Имя · "; color: var(--muted); font-weight: 600; }
  .users-table td:nth-child(3)::before { content: "Заявки · "; color: var(--muted); font-weight: 600; }
  .users-table td:nth-child(4)::before { content: "Открыто · "; color: var(--muted); font-weight: 600; }
  .users-table td:nth-child(5)::before { content: "Визит · "; color: var(--muted); font-weight: 600; }
  .users-table td:last-child { padding-top: 0.55rem; }
}

@media (max-width: 420px) {
  .sidebar-brand .brand-mark { font-size: 0.95rem; }
  .nav-btn { padding: 0.5rem 0.55rem; font-size: 0.8rem; }
  .chat-actions { max-width: 38%; }
}
