:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --accent-text: #3730a3;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --ok: #16a34a;
  --ok-bg: #f0fdf4;
  --warn: #d97706;
  --warn-bg: #fffbeb;
  --msg-in: #f1f5f9;
  --msg-out: #eef2ff;
}

[data-theme='dark'] {
  --bg: #0b0f19;
  --surface: #111827;
  --surface-alt: #1e293b;
  --text: #f8fafc;
  --muted: #94a3b8;
  --border: #1f2937;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-soft: rgba(99, 102, 241, 0.18);
  --accent-text: #c7d2fe;
  --danger: #f87171;
  --danger-bg: rgba(220, 38, 38, 0.16);
  --ok: #4ade80;
  --ok-bg: rgba(22, 163, 74, 0.16);
  --warn: #fbbf24;
  --warn-bg: rgba(217, 119, 6, 0.16);
  --msg-in: #1e293b;
  --msg-out: rgba(99, 102, 241, 0.22);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
}

.app-root { height: 100%; display: flex; flex-direction: column; }
.center-loading { margin: auto; color: var(--muted); font-weight: 600; }

/* Login screen */
.login-wrap {
  margin: auto;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.08);
}
.login-brand { display: flex; align-items: center; gap: 10px; font-size: 19px; font-weight: 800; }
.logo-badge {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: #fff; display: inline-flex; align-items: center; justify-content: center;
  font-weight: 900; font-size: 16px;
}
.muted { color: var(--muted); font-size: 13px; line-height: 1.45; margin: 0; }

/* Top bar */
.topbar {
  min-height: 54px;
  padding: 6px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 12px;
  flex-shrink: 0;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.nav-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 7px 12px;
  border-radius: 9px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.nav-btn.is-active { background: var(--accent-soft); color: var(--accent-text); }
.chip-btn {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  padding: 6px 10px;
  border-radius: 9px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.chip-btn.is-on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-text); }
.chip-btn.danger { color: var(--danger); border-color: rgba(220, 38, 38, 0.35); background: var(--danger-bg); }
.primary-btn {
  border: none;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.sec-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 13px;
  border-radius: 10px;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
}

/* Main 3-column desk */
.desk {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 330px 1fr 270px;
}
.col-list {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.tabs-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
}
.tab-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 6px 4px;
  border-radius: 8px;
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.tab-btn.is-active { background: var(--accent-soft); color: var(--accent-text); }
.tab-count { font-size: 11px; opacity: 0.85; }
.search-box { padding: 8px; border-bottom: 1px solid var(--border); }
.search-input, .text-input, .select-input {
  width: 100%;
  padding: 8px 11px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
}
.ticket-list { flex: 1; overflow-y: auto; }
.ticket-item {
  width: 100%;
  text-align: left;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  padding: 11px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ticket-item:hover { background: var(--surface-alt); }
.ticket-item.is-selected { background: var(--accent-soft); }
.ticket-item-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; font-size: 12px; }
.ticket-num { font-weight: 800; color: var(--accent-text); }
.ticket-time { color: var(--muted); font-size: 11px; }
.ticket-user { font-weight: 700; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket-preview { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ticket-badges { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 2px; }
.badge {
  font-size: 10.5px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-alt);
  color: var(--muted);
}
.badge.wait { background: var(--warn-bg); color: var(--warn); }
.badge.assigned { background: var(--accent-soft); color: var(--accent-text); }
.badge.closed { background: var(--surface-alt); color: var(--muted); }

/* Chat column */
.col-chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}
.chat-hdr {
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.chat-hdr-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.msg-row { display: flex; flex-direction: column; max-width: 78%; }
.msg-row.in { align-self: flex-start; }
.msg-row.out { align-self: flex-end; align-items: flex-end; }
.msg-row.sys { align-self: center; max-width: 90%; }
.msg-bubble {
  padding: 10px 13px;
  border-radius: 14px;
  background: var(--msg-in);
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.42;
}
.msg-row.out .msg-bubble { background: var(--msg-out); }
.msg-sys {
  font-size: 11.5px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
}
.msg-meta { font-size: 11px; color: var(--muted); margin-top: 3px; display: flex; gap: 6px; }
.msg-err { color: var(--danger); font-weight: 700; }
.msg-att { margin-bottom: 6px; display: flex; flex-direction: column; gap: 6px; }
.msg-att img {
  max-width: 280px;
  max-height: 260px;
  border-radius: 10px;
  cursor: zoom-in;
  display: block;
}
.msg-att video { max-width: 300px; max-height: 260px; border-radius: 10px; }
.msg-att audio { max-width: 280px; }

.composer {
  padding: 10px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer-textarea {
  flex: 1;
  min-height: 44px;
  max-height: 140px;
  resize: vertical;
  padding: 9px 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 13.5px;
}
.composer-banner {
  padding: 10px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.file-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-text);
  align-self: flex-start;
}

/* Right info column */
.col-info {
  border-left: 1px solid var(--border);
  background: var(--surface);
  padding: 14px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.info-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.info-title { font-size: 12px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.info-kv { display: flex; justify-content: space-between; gap: 8px; font-size: 12.5px; }
.info-kv span:first-child { color: var(--muted); }
.info-kv strong { text-align: right; word-break: break-all; }

/* Staff panel */
.staff-wrap {
  max-width: 860px;
  width: 100%;
  margin: 20px auto;
  padding: 0 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.staff-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.staff-form { display: grid; grid-template-columns: 160px 1fr 130px auto; gap: 8px; }
.staff-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

/* Lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  cursor: zoom-out;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 95vw; max-height: 92vh; border-radius: 10px; }

.toast {
  padding: 8px 12px;
  border-radius: 9px;
  font-size: 12.5px;
  font-weight: 600;
  background: var(--danger-bg);
  color: var(--danger);
}
.toast.ok { background: var(--ok-bg); color: var(--ok); }
.mobile-back { display: none; }

@media (max-width: 1080px) {
  .desk { grid-template-columns: 300px 1fr; }
  .col-info { display: none; }
  .desk.show-info .col-info { display: flex; position: fixed; right: 0; top: 54px; bottom: 0; width: 300px; z-index: 50; box-shadow: -8px 0 24px rgba(0, 0, 0, 0.2); }
}

@media (max-width: 760px) {
  .desk { grid-template-columns: 1fr; }
  .desk.has-ticket .col-list { display: none; }
  .desk:not(.has-ticket) .col-chat { display: none; }
  .mobile-back { display: inline-flex; }
  .staff-form { grid-template-columns: 1fr; }
}
