*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #f5f5f4;
  --surface:  #ffffff;
  --border:   #e5e5e3;
  --text:     #1c1c1a;
  --muted:    #737370;
  --accent:   #6b48ff;
  --accent-h: #5538e0;
  --danger:   #e24b4a;
  --sidebar-w: 220px;
}

body { font-family: system-ui, -apple-system, sans-serif; font-size: 15px;
       background: var(--bg); color: var(--text); line-height: 1.6; }

/* ---- LAYOUT ---- */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w); background: var(--surface);
  border-right: 1px solid var(--border); display: flex;
  flex-direction: column; padding: 20px 0; position: fixed;
  top: 0; left: 0; height: 100vh;
}
.sidebar-logo { font-weight: 600; font-size: 18px; padding: 0 20px 20px; }

.sidebar-nav { flex: 1; }
.nav-item {
  display: block; padding: 10px 20px; color: var(--muted);
  text-decoration: none; border-radius: 0;
  transition: background .15s, color .15s;
}
.nav-item:hover, .nav-item.active {
  background: var(--bg); color: var(--text);
}
.nav-item.active { font-weight: 500; }

.sidebar-footer { padding: 20px; border-top: 1px solid var(--border); font-size: 13px; }
.tenant-name { display: block; color: var(--muted); margin-bottom: 6px; }
.logout-link { color: var(--danger); text-decoration: none; font-size: 13px; }

.main { margin-left: var(--sidebar-w); flex: 1; padding: 32px; }
.page-header h1 { font-size: 22px; font-weight: 500; margin-bottom: 24px; }

/* ---- CARDS ---- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 20px;
}
.card h3 { font-size: 16px; font-weight: 500; margin-bottom: 16px; }

/* ---- STATS ---- */
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 20px;
}
.stat-value { font-size: 32px; font-weight: 600; }
.stat-label { font-size: 13px; color: var(--muted); margin-top: 4px; }

/* ---- TABLE ---- */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; padding: 8px 12px; color: var(--muted);
            border-bottom: 1px solid var(--border); font-weight: 500; }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: var(--bg); }

/* ---- FORMS ---- */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 500;
               margin-bottom: 6px; color: var(--muted); }
input[type="email"], input[type="password"], input[type="text"] {
  width: 100%; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 15px; background: var(--surface);
  color: var(--text); transition: border-color .15s;
}
input:focus { outline: none; border-color: var(--accent); }

/* ---- BUTTONS ---- */
.btn {
  display: inline-block; padding: 10px 20px; border-radius: 8px;
  font-size: 15px; font-weight: 500; cursor: pointer;
  border: none; text-decoration: none; transition: background .15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-h); }
.btn-full { width: 100%; text-align: center; }

/* ---- ALERTS ---- */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* ---- LOGIN ---- */
.login-body { display: flex; align-items: center;
              justify-content: center; min-height: 100vh; }
.login-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 40px; width: 100%; max-width: 400px;
}
.login-box h2 { font-size: 22px; font-weight: 600; margin-bottom: 28px; text-align: center; }

/* ---- NAV SECTION ---- */
.nav-section {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted); padding: 16px 20px 6px;
}
.nav-sub { padding-left: 32px; font-size: 14px; }

/* ---- DANGER BUTTON SMALL ---- */
.btn-danger-sm {
  display: inline-block; padding: 5px 12px; border-radius: 6px;
  font-size: 13px; font-weight: 500; cursor: pointer;
  border: 1px solid #fecaca; background: #fef2f2;
  color: #991b1b; transition: background .15s;
}
.btn-danger-sm:hover { background: #fee2e2; }

/* ---- ALERT INFO ---- */
.alert-info    { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }

/* ========================================
   CHAT LAYOUT
   ======================================== */
.page-content:has(.chat-layout) { padding: 0; }

.chat-layout {
  display: flex;
  height: calc(100vh - 89px);
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

/* --- Список чатів --- */
.chat-list {
  width: 300px; min-width: 300px;
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.chat-list-header { padding: 12px; border-bottom: 1px solid var(--border); }
.chat-search {
  width: 100%; padding: 8px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; background: var(--bg);
}
.chat-list-items { flex: 1; overflow-y: auto; }
.chat-empty-list { padding: 24px; color: var(--muted); font-size: 14px; text-align: center; }

.chat-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; text-decoration: none; color: var(--text);
  border-bottom: 1px solid var(--border);
  transition: background .12s;
}
.chat-item:hover { background: var(--bg); }
.chat-item.active { background: #f0edff; }

.chat-avatar {
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 600; color: #fff;
  flex-shrink: 0;
}
.chat-item-info { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; align-items: center; }
.chat-item-name { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-time { font-size: 11px; color: var(--muted); flex-shrink: 0; margin-left: 8px; }
.chat-item-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

/* --- Вікно чату --- */
.chat-window { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-placeholder {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--muted); gap: 8px;
}
.chat-placeholder div { font-size: 48px; }
.chat-placeholder p { font-size: 15px; }

.chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.chat-header-name { font-size: 15px; font-weight: 500; }
.chat-header-sub  { font-size: 12px; color: var(--muted); }

.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 8px;
  background: var(--bg);
}
.chat-no-messages { text-align: center; color: var(--muted); font-size: 14px; margin: auto; }

.chat-msg { display: flex; flex-direction: column; max-width: 68%; }
.chat-msg-in  { align-self: flex-start; align-items: flex-start; }
.chat-msg-out { align-self: flex-end; align-items: flex-end; }

.chat-bubble {
  padding: 9px 14px; border-radius: 14px;
  font-size: 14px; line-height: 1.5; word-break: break-word;
}
.chat-msg-in  .chat-bubble { background: var(--accent); color: #fff; border-radius: 14px 14px 14px 2px; }
.chat-msg-out .chat-bubble { background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: 14px 14px 2px 14px; }
.chat-msg-time { font-size: 11px; color: var(--muted); margin-top: 3px; padding: 0 4px; }
.chat-msg-in .chat-msg-time { color: rgba(255,255,255,.7); }

.chat-form {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 12px 16px; border-top: 1px solid var(--border);
  background: var(--surface);
}
.chat-input {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 20px; font-size: 14px; resize: none;
  font-family: inherit; line-height: 1.4; max-height: 120px;
  transition: border-color .15s;
}
.chat-input:focus { outline: none; border-color: var(--accent); }
.chat-send-btn {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: #fff; border: none;
  font-size: 16px; cursor: pointer; flex-shrink: 0;
  transition: background .15s;
}
.chat-send-btn:hover { background: var(--accent-h); }

/* ---- CHAT MEDIA ---- */
.chat-media-photo {
  max-width: 260px; max-height: 260px; border-radius: 10px;
  display: block; cursor: zoom-in; object-fit: cover;
}
.chat-media-video {
  max-width: 280px; border-radius: 10px; display: block;
}
.chat-media-audio {
  width: 240px; height: 36px;
}
.chat-doc-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 12px; background: rgba(255,255,255,.15);
  border-radius: 8px; text-decoration: none;
  color: inherit; font-size: 14px;
  border: 1px solid rgba(255,255,255,.2);
}
.chat-msg-in .chat-doc-link {
  background: var(--bg); border-color: var(--border); color: var(--accent);
}

/* ---- LOCATION ---- */
.chat-location {
  display: block; text-decoration: none; color: inherit;
  border-radius: 10px; overflow: hidden;
  border: 1px solid rgba(255,255,255,.2);
}
.chat-msg-in .chat-location { border-color: var(--border); }

.chat-location-map {
  background: #e8f4e8; height: 80px;
  display: flex; align-items: center; justify-content: center;
  font-size: 36px;
}
.chat-location-coords { font-size: 12px; opacity: .7; padding: 6px 10px 2px; }
.chat-location-link   { font-size: 13px; font-weight: 500; padding: 2px 10px 8px; color: var(--accent); }
.chat-msg-in .chat-location-link { color: rgba(255,255,255,.9); }

/* ---- SHARED CONTACT ---- */
.chat-contact-card {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 4px;
}
.chat-contact-icon  { font-size: 28px; }
.chat-contact-name  { font-weight: 500; font-size: 14px; }
.chat-contact-phone { font-size: 13px; color: var(--accent); text-decoration: none; }
.chat-msg-in .chat-contact-phone { color: rgba(255,255,255,.85); }

/* ---- CONTACT PANEL ---- */
.chat-layout { position: relative; }

.contact-panel {
  width: 0; min-width: 0; overflow: hidden;
  border-left: 0 solid var(--border);
  transition: width .25s ease, min-width .25s ease, border-left-width .25s ease;
  background: var(--surface); display: flex; flex-direction: column;
}
.contact-panel.open {
  width: 280px; min-width: 280px;
  border-left-width: 1px;
}
.contact-panel-inner { width: 280px; padding: 20px; overflow-y: auto; flex: 1; }

.contact-panel-avatar {
  width: 64px; height: 64px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; font-weight: 700; color: #fff;
  margin: 0 auto 12px; position: relative;
}
.contact-panel-avatar img {
  width: 100%; height: 100%; border-radius: 50%;
  object-fit: cover; position: absolute; top: 0; left: 0;
}
.contact-panel-name {
  font-size: 16px; font-weight: 600; text-align: center; margin-bottom: 4px;
}
.contact-panel-username {
  font-size: 13px; color: var(--muted); text-align: center; margin-bottom: 20px;
}
.contact-panel-section {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted); margin: 16px 0 8px;
}
.contact-panel-row {
  display: flex; justify-content: space-between; align-items: flex-start;
  font-size: 13px; padding: 5px 0; border-bottom: 1px solid var(--border);
}
.contact-panel-row:last-child { border-bottom: none; }
.contact-panel-label { color: var(--muted); flex-shrink: 0; margin-right: 8px; }
.contact-panel-value { text-align: right; word-break: break-all; }

.btn-icon {
  width: 32px; height: 32px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; display: flex; align-items: center;
  justify-content: center; font-size: 16px;
  transition: background .15s;
}
.btn-icon:hover { background: var(--bg); }
.btn-icon.active { background: #f0edff; border-color: var(--accent); }

.badge-premium {
  display: inline-block; font-size: 11px; padding: 2px 6px;
  background: linear-gradient(135deg, #f59e0b, #ef4444);
  color: #fff; border-radius: 4px; font-weight: 600;
}

/* ---- AVATAR FIX ---- */
.chat-avatar, .contact-panel-avatar {
  position: relative; display: flex;
  align-items: center; justify-content: center;
  border-radius: 50%; flex-shrink: 0;
}
.chat-avatar img, .contact-panel-avatar img {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  border-radius: 50%; object-fit: cover;
}
.avatar-initials { position: relative; z-index: 1; }

/* ---- EDITABLE FIELD ---- */
.editable-field { margin-bottom: 4px; }
.editable-field label {
  display: block; font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); margin-bottom: 4px;
}
.editable-field input {
  width: 100%; padding: 7px 10px; border: 1px solid var(--border);
  border-radius: 7px; font-size: 13px; background: var(--bg);
  color: var(--text); transition: border-color .15s;
}
.editable-field input:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.btn-save-fields {
  width: 100%; margin-top: 12px; padding: 8px;
  background: var(--accent); color: #fff; border: none;
  border-radius: 8px; font-size: 14px; font-weight: 500;
  cursor: pointer; transition: background .15s;
}
.btn-save-fields:hover { background: var(--accent-h); }
.save-status { font-size: 12px; text-align: center; margin-top: 6px; color: var(--muted); min-height: 16px; }

/* ---- REMOVE PAGE HEADER ---- */
.page-header { display: none; }
.page-content { padding: 0; }
.main { padding: 24px 32px; }

/* ========================================
   COLLAPSIBLE SIDEBAR
   ======================================== */
:root { --sidebar-collapsed-w: 60px; }

.sidebar-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px 16px; gap: 8px;
}
.sidebar-logo-icon { display: none; font-weight: 700; font-size: 18px; }

.sidebar-toggle {
  width: 28px; height: 28px; border-radius: 6px; flex-shrink: 0;
  border: 1px solid var(--border); background: var(--bg);
  cursor: pointer; font-size: 13px; display: flex;
  align-items: center; justify-content: center;
  color: var(--muted); transition: background .15s;
}
.sidebar-toggle:hover { background: var(--border); }

.nav-icon  { font-size: 16px; flex-shrink: 0; min-width: 20px; text-align: center; }
.nav-label { transition: opacity .2s; white-space: nowrap; overflow: hidden; }
.nav-divider { height: 1px; background: var(--border); margin: 6px 12px; }

/* Collapsed state */
.sidebar.collapsed {
  width: var(--sidebar-collapsed-w);
  min-width: var(--sidebar-collapsed-w);
}
.sidebar.collapsed .sidebar-logo    { display: none; }
.sidebar.collapsed .sidebar-logo-icon { display: block; }
.sidebar.collapsed .nav-label       { display: none; }
.sidebar.collapsed .nav-section     { display: none; }
.sidebar.collapsed .nav-item        { padding: 10px 0; justify-content: center; }
.sidebar.collapsed .nav-sub         { padding-left: 0; }
.sidebar.collapsed .tenant-name     { display: none; }
.sidebar.collapsed .logout-link     { justify-content: center; }

.sidebar-collapsed .main { margin-left: var(--sidebar-collapsed-w); }

.nav-item { display: flex; align-items: center; gap: 10px; }
.sidebar-footer { display: flex; flex-direction: column; gap: 4px; }
.logout-link    { display: flex; align-items: center; gap: 8px; }

/* Sidebar transition */
.sidebar {
  transition: width .2s ease, min-width .2s ease;
}
.main { transition: margin-left .2s ease; }

/* ========================================
   MOBILE
   ======================================== */
.mobile-menu-btn {
  display: none; position: fixed; top: 12px; left: 12px; z-index: 200;
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border);
  font-size: 18px; cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
.mobile-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.4); z-index: 99;
}

@media (max-width: 768px) {
  .mobile-menu-btn  { display: flex; align-items: center; justify-content: center; }
  .mobile-overlay   { display: block; }
  .sidebar-toggle   { display: none; }

  .sidebar {
    position: fixed; left: -240px; top: 0; height: 100vh;
    z-index: 100; width: var(--sidebar-w) !important;
    min-width: var(--sidebar-w) !important;
    box-shadow: 4px 0 20px rgba(0,0,0,.12);
    transition: left .25s ease;
  }
  .sidebar.mobile-open { left: 0; }
  .sidebar .nav-label  { display: block !important; }
  .sidebar .nav-item   { padding: 10px 20px !important; justify-content: flex-start !important; }
  .sidebar .nav-section{ display: block !important; }

  .main {
    margin-left: 0 !important;
    padding: 60px 16px 16px;
  }

  /* Chat mobile */
  .chat-layout { flex-direction: column; height: auto; min-height: calc(100vh - 76px); }
  .chat-list   { width: 100% !important; max-height: 240px; border-right: none; border-bottom: 1px solid var(--border); }
  .chat-window { flex: 1; }
  .contact-panel.open { width: 100% !important; min-width: 100% !important; border-left: none; border-top: 1px solid var(--border); }

  /* Stats grid */
  .stats-grid { grid-template-columns: 1fr; }

  /* Tables scroll */
  .card { overflow-x: auto; }
}

@media (max-width: 480px) {
  .main { padding: 56px 12px 12px; }
  .chat-list { max-height: 200px; }
}

/* ========================================
   SIDEBAR TOGGLE — Claude style
   ======================================== */
.sidebar-top {
  display: flex; align-items: center;
  justify-content: space-between;
  padding: 0 12px 16px; gap: 8px;
}
.sidebar.collapsed .sidebar-top {
  justify-content: center;
  padding: 0 0 16px;
}

.sidebar-toggle {
  width: 32px; height: 32px; border-radius: 8px; flex-shrink: 0;
  border: 1px solid var(--border); background: transparent;
  cursor: pointer; display: flex; align-items: center;
  justify-content: center; color: var(--muted);
  transition: background .15s, color .15s;
}
.sidebar-toggle:hover { background: var(--bg); color: var(--text); }
.sidebar-toggle svg   { display: block; }

/* Mobile: collapsed by default, no overlay needed */
@media (max-width: 768px) {
  .sidebar {
    width: var(--sidebar-collapsed-w) !important;
    min-width: var(--sidebar-collapsed-w) !important;
  }
  .sidebar-collapsed .main,
  .main { margin-left: var(--sidebar-collapsed-w); }

  .sidebar:not(.collapsed) {
    width: var(--sidebar-w) !important;
    min-width: var(--sidebar-w) !important;
    position: fixed; z-index: 100; height: 100vh;
    box-shadow: 4px 0 20px rgba(0,0,0,.12);
  }

  .main { padding: 24px 16px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .chat-layout { flex-direction: column; height: auto; }
  .chat-list   { width: 100% !important; max-height: 220px; border-right: none; border-bottom: 1px solid var(--border); }
  .contact-panel.open { width: 100% !important; min-width: 100% !important; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .main { padding: 16px 12px; }
}

/* ========================================
   KANBAN BOARD
   ======================================== */
.kanban-board {
  display: flex; gap: 14px; overflow-x: auto;
  padding-bottom: 16px; align-items: flex-start;
  min-height: calc(100vh - 140px);
}
.kanban-col {
  min-width: 240px; max-width: 240px;
  background: var(--bg); border-radius: 10px;
  display: flex; flex-direction: column; gap: 0;
  border: 1px solid var(--border);
}
.kanban-col-header {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.kanban-dot    { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.kanban-col-name  { font-size: 13px; font-weight: 600; flex: 1; }
.kanban-col-count {
  font-size: 12px; color: var(--muted);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 1px 7px;
}

.kanban-cards { padding: 10px; display: flex; flex-direction: column; gap: 8px; flex: 1; }

.kanban-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px; cursor: grab;
  transition: box-shadow .15s, transform .15s;
}
.kanban-card:active { cursor: grabbing; }
.kanban-card:hover  { box-shadow: 0 2px 8px rgba(0,0,0,.08); transform: translateY(-1px); }
.kanban-card-name    { font-size: 13px; font-weight: 500; margin-bottom: 4px; }
.kanban-card-contact { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kanban-card-amount  { font-size: 12px; color: var(--accent); font-weight: 600; margin-top: 4px; }

.kanban-add-btn {
  width: 100%; padding: 8px; border: 1px dashed var(--border);
  border-radius: 8px; background: transparent; cursor: pointer;
  font-size: 13px; color: var(--muted); text-align: center;
  transition: background .15s, color .15s;
}
.kanban-add-btn:hover { background: var(--surface); color: var(--text); }

@media (max-width: 768px) {
  .kanban-board { flex-direction: column; }
  .kanban-col   { min-width: 100%; max-width: 100%; }
}