/* ===========================================
   CRM Messages - Style
   Based on shared CRM design system
   =========================================== */

:root {
  --font: 'Rubik', sans-serif;
  --bg: #f0f2f5;
  --bg-card: #ffffff;
  --bg-sidebar: #ffffff;
  --bg-sidebar-hover: #f3f4f6;
  --bg-sidebar-active: #eff6ff;

  --text-primary: #1f2937;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --text-inverse: #ffffff;
  --text-sidebar: #6b7280;
  --text-sidebar-active: #3b82f6;

  --accent: #3b82f6;
  --accent-light: #dbeafe;
  --accent-hover: #2563eb;

  --success: #10b981;
  --success-light: #d1fae5;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --danger: #ef4444;
  --danger-light: #fee2e2;

  --border: #e5e7eb;
  --border-light: #f3f4f6;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.1);

  --sidebar-width: 220px;
  --sidebar-collapsed: 60px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  direction: rtl;
  min-height: 100vh;
}

/* ---- Auth ---- */
.auth-view {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}

.auth-card {
  text-align: center;
  background: var(--bg-card);
  padding: 48px 40px;
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  max-width: 400px;
  width: 90%;
}

.auth-logo {
  margin-bottom: 16px;
}

.auth-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 4px;
}

.auth-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 32px;
}

.btn-google {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 12px 28px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  font-family: var(--font);
  cursor: pointer;
  transition: box-shadow 0.2s;
}
.btn-google:hover { box-shadow: var(--shadow-md); }

/* ---- App Shell ---- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ---- Sidebar ---- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  transition: width 0.2s;
  overflow: hidden;
}

.sidebar.collapsed {
  width: var(--sidebar-collapsed);
}
.sidebar.collapsed .sidebar-title,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .user-name,
.sidebar.collapsed #user-role,
.sidebar.collapsed .user-avatar {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border-light);
}

.sidebar-logo-icon {
  color: var(--accent);
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
}

.sidebar-nav {
  flex: 1;
  padding: 8px;
  overflow-y: auto;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--text-sidebar);
  text-decoration: none;
  font-size: 14px;
  transition: background 0.15s;
  cursor: pointer;
}
.nav-item:hover { background: var(--bg-sidebar-hover); }
.nav-item.active {
  background: var(--bg-sidebar-active);
  color: var(--text-sidebar-active);
  font-weight: 500;
}

.nav-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.nav-divider {
  height: 1px;
  background: var(--border-light);
  margin: 8px 12px;
}

.nav-external { opacity: 0.7; }
.nav-external:hover { opacity: 1; }

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid var(--border-light);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
}

.user-name {
  font-size: 13px;
  font-weight: 500;
}

.btn-sign-out {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  border-radius: 4px;
}
.btn-sign-out:hover { color: var(--danger); }

.sidebar-toggle {
  position: fixed;
  top: 50%;
  right: var(--sidebar-width);
  transform: translateY(-50%);
  z-index: 101;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: right 0.2s;
  box-shadow: var(--shadow-sm);
}
.sidebar.collapsed ~ .sidebar-toggle {
  right: var(--sidebar-collapsed);
}

/* ---- Main Content ---- */
.main-content {
  flex: 1;
  margin-right: var(--sidebar-width);
  padding: 24px 32px;
  transition: margin-right 0.2s;
  max-width: 1200px;
}

.sidebar.collapsed ~ .sidebar-toggle + .main-content,
.sidebar.collapsed + .sidebar-toggle + .main-content {
  margin-right: var(--sidebar-collapsed);
}

/* ---- Messages View ---- */
.messages-view {
  width: 100%;
}

.messages-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.messages-header h2 {
  font-size: 22px;
  font-weight: 600;
}

/* ---- Table ---- */
.messages-table-wrap {
  background: var(--bg-card);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.messages-table {
  width: 100%;
  border-collapse: collapse;
}

.messages-table thead {
  background: var(--border-light);
}

.messages-table th {
  padding: 12px 14px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-align: right;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.messages-table td {
  padding: 14px 14px;
  border-top: 1px solid var(--border-light);
  font-size: 14px;
  vertical-align: middle;
}

.messages-table tbody tr {
  transition: background 0.15s;
}
.messages-table tbody tr:hover {
  background: #fafbfc;
}

/* Column widths */
.col-drag { width: 36px; text-align: center; }
.col-title { min-width: 180px; cursor: pointer; }
.col-title:hover { color: var(--accent); }
.col-trigger { min-width: 140px; }
.col-channel { min-width: 80px; }
.col-language { min-width: 70px; }
.col-source { min-width: 100px; }
.col-telegram { min-width: 110px; }
.col-actions { width: 80px; text-align: center; white-space: nowrap; }

.col-trigger code {
  background: var(--border-light);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  direction: ltr;
  display: inline-block;
}

/* ---- Drag & Drop ---- */
.drag-handle {
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
.drag-handle:active { cursor: grabbing; }

.messages-table tbody tr.dragging {
  opacity: 0.4;
  background: var(--accent-light);
}

.messages-table tbody tr.drag-over {
  border-top: 3px solid var(--accent);
}

/* ---- Inline Edit ---- */
.row-editing {
  background: var(--accent-light) !important;
}

.inline-edit-row {
  background: #f8f9fb;
}

.inline-edit-cell {
  padding: 0 14px 14px !important;
  border-top: none !important;
}

.inline-edit-form {
  background: #fff;
  padding: 18px 20px;
  border-radius: 10px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.inline-edit-footer {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-light);
}

/* ---- Badges ---- */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.badge-green { background: #d1fae5; color: #065f46; }
.badge-yellow { background: #fef3c7; color: #92400e; }
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-purple { background: #ede9fe; color: #5b21b6; }
.badge-orange { background: #ffedd5; color: #9a3412; }
.badge-pink { background: #fce7f3; color: #9d174d; }
.badge-teal { background: #ccfbf1; color: #115e59; }
.badge-gray { background: #f3f4f6; color: #4b5563; }
.badge-telegram { background: #dbeafe; color: #1e40af; }

/* ---- Trigger Groups ---- */
.group-header-row {
  background: #f8fafc;
  cursor: pointer;
  border-right: 3px solid var(--accent);
}
.group-header-row:hover {
  background: #eef2f7;
}
.group-header-row.group-open {
  background: var(--accent-light);
}

.group-child {
  background: #fff8ed;
  border-right: 3px solid #f59e0b;
}
.group-child:hover {
  background: #fef3c7 !important;
}
.group-child td:first-child {
  padding-right: 24px;
}

.group-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 600;
}

.badge-mini {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
  margin-left: 3px;
}

/* Custom badge dropdown */
.badge-dropdown {
  position: relative;
  display: inline-block;
}

.badge-dropdown-btn {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s;
  user-select: none;
}
.badge-dropdown-btn:hover {
  opacity: 0.8;
}

.badge-dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 50;
  background: var(--bg-card);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  padding: 4px;
  min-width: 120px;
}
.badge-dropdown-menu.open {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.badge-dropdown-item {
  padding: 5px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  text-align: center;
  transition: opacity 0.15s;
}
.badge-dropdown-item:hover {
  opacity: 0.75;
}
.badge-dropdown-item.active {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ---- Telegram Groups Panel ---- */
.groups-panel {
  background: var(--bg-card);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 16px;
}

.group-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-light);
}
.group-row:last-of-type {
  border-bottom: none;
}

.color-picker-row {
  display: flex;
  gap: 5px;
  align-items: center;
  margin-right: auto;
}

.color-dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.15s, border-color 0.15s;
}
.color-dot:hover {
  transform: scale(1.2);
}
.color-dot.active {
  border-color: currentColor;
  transform: scale(1.2);
  box-shadow: 0 0 0 2px rgba(0,0,0,0.15);
}

.group-add-form {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.group-add-form .form-input {
  flex: 1;
  padding: 8px 12px;
  font-size: 13px;
}
.group-add-form .btn {
  white-space: nowrap;
  padding: 8px 16px;
  font-size: 13px;
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 18px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: var(--text-inverse);
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--border-light);
  color: var(--text-primary);
}
.btn-secondary:hover { background: var(--border); }

.btn-add {
  font-size: 13px;
  padding: 8px 16px;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  transition: color 0.15s;
}
.btn-icon:hover { color: var(--accent); }
.btn-icon-danger:hover { color: var(--danger); }

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-card {
  background: var(--bg-card);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border-light);
}

.modal-header h3 {
  font-size: 18px;
  font-weight: 600;
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  justify-content: flex-start;
  gap: 10px;
  padding: 16px 24px;
  border-top: 1px solid var(--border-light);
}

/* ---- Form ---- */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: var(--font);
  color: var(--text-primary);
  background: var(--bg-card);
  transition: border-color 0.15s;
}
.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

.form-textarea {
  resize: vertical;
  min-height: 120px;
  line-height: 1.6;
}

.form-row {
  display: flex;
  gap: 12px;
}
.form-row .form-group { flex: 1; }

select.form-input {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='%236b7280'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 12px center;
  padding-left: 32px;
}

/* ---- Spinner ---- */
.spinner {
  display: flex;
  justify-content: center;
  padding: 60px;
}

.spinner-ring {
  width: 36px;
  height: 36px;
  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); } }

/* ---- Empty State ---- */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
  font-size: 15px;
}

/* ---- Toast ---- */
.crm-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  font-family: var(--font);
  z-index: 2000;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  box-shadow: var(--shadow-md);
}
.crm-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.crm-toast-success { background: var(--success); color: #fff; }
.crm-toast-danger { background: var(--danger); color: #fff; }
.crm-toast-warning { background: var(--warning); color: #fff; }

/* ---- Mobile ---- */
@media (max-width: 768px) {
  .sidebar {
    width: var(--sidebar-collapsed);
  }
  .sidebar .sidebar-title,
  .sidebar .nav-label,
  .sidebar .user-name,
  .sidebar #user-role,
  .sidebar .user-avatar {
    display: none;
  }

  .sidebar-toggle { display: none; }

  .main-content {
    margin-right: var(--sidebar-collapsed);
    padding: 16px;
  }

  .messages-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .messages-header h2 { font-size: 18px; }

  .messages-table-wrap { overflow-x: auto; }

  .messages-table {
    min-width: 600px;
  }

  .form-row {
    flex-direction: column;
    gap: 0;
  }

  .modal-card {
    max-width: 100%;
    border-radius: 12px;
  }
}
