:root {
  --bg: var(--tg-theme-bg-color, #0f1419);
  --text: var(--tg-theme-text-color, #e7e9ea);
  --hint: var(--tg-theme-hint-color, #8b98a5);
  --accent: var(--tg-theme-button-color, #1d9bf0);
  --accent-text: var(--tg-theme-button-text-color, #fff);
  --secondary: var(--tg-theme-secondary-bg-color, #1a1f26);
  --border: rgba(255, 255, 255, 0.08);
  --mine: #1a3a2a;
  --theirs: #1e2732;
  --danger: #f4212e;
  --radius: 12px;
  --font: "DM Sans", system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

.boot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 1rem;
  color: var(--hint);
}

.boot__spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.app {
  display: flex;
  flex-direction: column;
  height: var(--tg-viewport-stable-height, 100dvh);
  max-height: var(--tg-viewport-stable-height, 100dvh);
}

.app.detail-mode .topbar,
.app.detail-mode #statsBar,
.app.detail-mode #viewOrders > .toolbar {
  display: none;
}

.app.detail-mode #viewOrders {
  flex: 1;
  min-height: 0;
}

.app.detail-mode .layout {
  flex: 1;
  min-height: 0;
}

.topbar {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--secondary);
}

.topbar__brand { display: flex; align-items: center; gap: 12px; }

.logo {
  width: 40px;
  height: 40px;
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
}

.topbar__title { font-weight: 700; font-size: 16px; }
.topbar__sub { font-size: 12px; color: var(--hint); margin-top: 2px; }

.stats {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.stat {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
}

.stat b { display: block; font-size: 16px; color: var(--accent); }

#viewOrders {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  align-items: center;
}

.btn-create {
  padding: 10px 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.toolbar input[type="search"],
.toolbar select {
  flex: 1;
  min-width: 140px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--secondary);
  color: var(--text);
  font: inherit;
}

.chk {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--hint);
  white-space: nowrap;
}

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.panel {
  min-height: 0;
  overflow: hidden;
}

.panel--list {
  width: 100%;
  max-width: 380px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.panel--chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.panel--empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--hint);
}

.ticket-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.ticket-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.ticket-item:hover,
.ticket-item.active {
  background: var(--secondary);
}

.ticket-item__row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.ticket-item__num {
  font-weight: 600;
  font-size: 13px;
  color: var(--accent);
}

.ticket-item__status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 99px;
  background: var(--bg);
  white-space: nowrap;
}

.ticket-item__subj {
  margin-top: 4px;
  font-size: 14px;
  line-height: 1.3;
}

.ticket-item__meta {
  margin-top: 4px;
  font-size: 12px;
  color: var(--hint);
}

.loader, .list-end {
  text-align: center;
  padding: 12px;
  font-size: 13px;
  color: var(--hint);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--secondary);
}

.btn-icon {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
}

.chat-header__info { flex: 1; min-width: 0; }
.chat-header__title { font-weight: 600; font-size: 15px; }
.chat-header__meta { font-size: 12px; color: var(--hint); margin-top: 2px; }

.status-select {
  max-width: 160px;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 92%;
  padding: 12px 14px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
}

.msg--mine {
  align-self: flex-end;
  background: var(--mine);
  border-bottom-right-radius: 4px;
}

.msg--theirs {
  align-self: flex-start;
  background: var(--theirs);
  border-bottom-left-radius: 4px;
}

.msg--system {
  align-self: center;
  background: transparent;
  color: var(--hint);
  font-size: 12px;
  max-width: 100%;
  text-align: center;
}

.msg__head {
  font-size: 11px;
  color: var(--hint);
  margin-bottom: 4px;
}

.msg__text {
  white-space: pre-wrap;
}

.msg__media-block {
  margin-bottom: 6px;
}

.msg__media-btn {
  display: block;
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  max-width: 100%;
}

.msg__media {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 8px;
  object-fit: contain;
}

.msg__media--photo {
  pointer-events: none;
}

.msg__media--video {
  width: 100%;
  background: #000;
  pointer-events: auto;
}

.msg__media-actions {
  display: flex;
  gap: 10px;
  margin-top: 6px;
}

.msg__media-dl {
  font: inherit;
  font-size: 13px;
  color: var(--accent, #2a9df4);
  text-decoration: none;
  padding: 4px 0;
  border: none;
  background: none;
  cursor: pointer;
}

.msg__media-dl:active {
  opacity: 0.7;
}

.msg__media-pending {
  font-size: 13px;
  color: var(--hint);
  font-style: italic;
}

.media-lightbox {
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: rgba(0, 0, 0, 0.94);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top, 16px) 12px env(safe-area-inset-bottom, 16px);
}

.media-lightbox.hidden {
  display: none;
}

body.lightbox-open {
  overflow: hidden;
}

.media-lightbox__img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
}

.media-lightbox__close {
  position: absolute;
  top: max(12px, env(safe-area-inset-top, 0px));
  right: 12px;
  z-index: 10001;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.btn-attach {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.attach-preview {
  font-size: 13px;
  color: var(--hint);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.attach-preview button {
  border: none;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
  font-size: 13px;
}

.msg.internal { opacity: 0.75; border: 1px dashed var(--hint); }

.tab-panel--chat .messages {
  flex: 1;
  min-height: 0;
  padding: 16px;
}

.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  color: var(--hint);
  font-size: 14px;
}

.chat-error {
  color: var(--danger);
  font-size: 13px;
  margin-bottom: 8px;
}

.btn-send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.reply-form {
  flex-shrink: 0;
  padding: 12px 14px calc(14px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  background: var(--secondary);
}

.reply-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
}

.reply-form textarea {
  flex: 1;
  min-width: 140px;
  width: auto;
  min-height: 88px;
  max-height: 160px;
  resize: vertical;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  line-height: 1.45;
}

.btn-send {
  min-height: 44px;
  padding: 0 20px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

@media (min-width: 520px) {
  .btn-send {
    min-width: 120px;
  }
}

.internal-chk { margin-bottom: 8px; }

@media (max-width: 768px) {
  .panel--list { max-width: none; width: 100%; }
  .layout.detail-open .panel--list.hidden { display: none; }
  .layout.detail-open .panel--empty { display: none; }
  #backBtn { display: block; }
}

@media (min-width: 769px) {
  #backBtn { display: none; }
  .layout.detail-open .panel--empty { display: none; }
}

:root { --card: var(--secondary); }

.nav {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.nav__btn {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.nav__btn--active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.order-list { flex: 1; overflow-y: auto; }

.order-card {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  border-left: 3px solid transparent;
  background: var(--bg);
}

.order-card:hover, .order-card.active {
  background: var(--secondary);
}

.order-card.active { border-left-color: var(--accent); }

.order-card__top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.order-card__top-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.order-card__unread {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 99px;
  background: #f4212e;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

.order-card--unread .order-card__num {
  padding-right: 2px;
}

.order-card__num { font-weight: 700; color: var(--accent); font-size: 13px; }
.order-card__badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--secondary); }
.order-card__product { margin-top: 6px; font-weight: 600; font-size: 15px; line-height: 1.3; }
.order-card__route { margin-top: 4px; font-size: 12px; color: var(--hint); }
.order-card__meta {
  margin-top: 8px;
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--hint);
}

.order-card__price { color: #00ba7c; font-weight: 700; }
.order-card__car { margin-top: 4px; font-size: 12px; color: var(--hint); }

.panel--detail {
  flex: 1;
  display: none;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}

.layout.detail-open .panel--detail {
  display: flex;
}

.app.detail-mode .panel--detail {
  position: fixed;
  inset: 0;
  z-index: 200;
  width: 100%;
  height: var(--tg-viewport-stable-height, 100dvh);
  max-height: var(--tg-viewport-stable-height, 100dvh);
  padding-top: env(safe-area-inset-top, 0px);
}

.detail-body {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.detail-body > .tab-panel {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.detail-body > .tab-panel.hidden {
  display: none !important;
}

.detail-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--secondary);
}

.detail-header__title { flex: 1; font-weight: 700; font-size: 16px; }

.detail-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.detail-tab {
  flex: 1;
  padding: 10px;
  border: none;
  background: none;
  color: var(--hint);
  font: inherit;
  cursor: pointer;
}

.detail-tab--active {
  color: var(--accent);
  border-bottom: 2px solid var(--accent);
  font-weight: 600;
}

.tab-panel--card .order-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.order-form {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.order-form__section {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  font-weight: 700;
  margin-top: 8px;
}

.ofield { display: flex; flex-direction: column; gap: 4px; }
.ofield span { font-size: 12px; color: var(--hint); }
.ofield input, .ofield textarea {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--secondary);
  color: var(--text);
  font: inherit;
}

.ofield textarea { min-height: 64px; resize: vertical; }
.ofield input[readonly], .ofield textarea[readonly] { opacity: 0.85; }

.order-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px;
  margin-top: 8px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(29,155,240,0.15), rgba(0,186,124,0.1));
  border: 1px solid var(--border);
}

.order-total strong { font-size: 20px; color: #00ba7c; }
.order-form__hint { font-size: 11px; color: var(--hint); }

.btn-primary {
  margin: 0 12px 12px;
  padding: 12px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.analytics-view {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.analytics h2 { margin-bottom: 16px; font-size: 20px; }
.analytics h3 { margin: 20px 0 12px; font-size: 15px; color: var(--hint); }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

@media (min-width: 600px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
}

.kpi {
  padding: 14px;
  border-radius: var(--radius);
  background: var(--secondary);
  border: 1px solid var(--border);
}

.kpi--accent { border-color: var(--accent); }
.kpi--green { border-color: #00ba7c; }
.kpi__val { font-size: 22px; font-weight: 700; }
.kpi__lbl { font-size: 11px; color: var(--hint); margin-top: 4px; }

.bar-chart { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 1fr 2fr auto; gap: 8px; align-items: center; font-size: 12px; }
.bar-row__track { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; }
.bar-row__fill { height: 100%; background: var(--accent); border-radius: 4px; }
.bar-row__val { font-weight: 600; white-space: nowrap; }

.status-pills { display: flex; flex-wrap: wrap; gap: 8px; }
.pill {
  padding: 6px 12px;
  border-radius: 99px;
  background: var(--secondary);
  border: 1px solid var(--border);
  font-size: 12px;
}

.layout.detail-open .panel--list { max-width: 380px; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.modal__box {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--secondary);
  border-radius: 16px 16px 0 0;
  padding: 16px;
  border-top: 1px solid var(--border);
}

.modal__box h3 { margin-bottom: 12px; font-size: 18px; }

.modal__actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.modal__actions button {
  flex: 1;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.modal__actions .btn-primary { border: none; background: var(--accent); color: var(--accent-text); }

.create-form { display: flex; flex-direction: column; gap: 10px; }

.detail-status {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.tab-panel--log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
}

.audit-list { display: flex; flex-direction: column; gap: 10px; }

.audit-row {
  padding: 12px;
  border-radius: var(--radius);
  background: var(--secondary);
  border: 1px solid var(--border);
  font-size: 13px;
}

.audit-row__head {
  display: flex;
  justify-content: space-between;
  color: var(--hint);
  font-size: 11px;
  margin-bottom: 6px;
}

.audit-row__action { font-weight: 600; }
.audit-row__diff { margin-top: 6px; color: var(--hint); }
.audit-empty { text-align: center; color: var(--hint); padding: 24px; }

@media (max-width: 768px) {
  .layout.detail-open .panel--list.hidden { display: none; }
}

@media (min-width: 769px) {
  .app.detail-mode .panel--detail {
    position: fixed;
    left: auto;
    right: 0;
    width: min(520px, 100%);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 32px rgba(0, 0, 0, 0.35);
  }
}
