/**
 * Modal styles (DR-009)
 * Три типа: slide-panel, modal (centered), fullscreen modal
 */

/* --- Общие --- */

.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
}

.modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

/* --- Centered modal (формы, подтверждения) --- */

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  background: var(--bg-primary);
  border-radius: 12px;
  box-shadow: 0 8px 32px var(--shadow);
  z-index: 101;
  width: 90%;
  max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s, transform 0.2s;
}

.modal.active {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, -50%) scale(1);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  color: var(--text-secondary);
  min-height: 32px;
  min-width: 32px;
}

.modal-close:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Form elements */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg-primary);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s;
}

.form-group input:focus,
.form-group select:focus {
  border-color: var(--accent);
}

/* Кнопки */
.btn-primary {
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  padding: 10px 20px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s;
}

.btn-secondary:hover {
  background: var(--border);
}

/* --- Slide panel (настройки, инфо стрима) --- */

.slide-panel {
  position: fixed;
  top: 0;
  right: -100%;
  width: 100%;
  max-width: 360px;
  height: 100%;
  background: var(--bg-primary);
  box-shadow: -4px 0 16px var(--shadow);
  z-index: 101;
  display: flex;
  flex-direction: column;
  transition: right 0.25s ease;
}

.slide-panel.active {
  right: 0;
}

.slide-panel-header {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-shrink: 0;
}

.slide-panel-header h2 {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.slide-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

/* --- Fullscreen modal (редактор шаблонов, клиенты) --- */

.fullscreen-modal,
.fs-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg-primary);
  z-index: 101;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  opacity: 0;
  visibility: hidden;
  transition: transform 0.3s ease, opacity 0.2s, visibility 0.2s;
}

.fullscreen-modal.active,
.fs-modal.active {
  transform: translateY(0);
  opacity: 1;
  visibility: visible;
}

/* --- Fullscreen modal header --- */

.fs-modal-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-shrink: 0;
  background: var(--bg-primary);
}

.fs-modal-header h2 {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.fs-modal-back {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: var(--text-secondary);
  min-height: 36px;
  min-width: 36px;
}
.fs-modal-back:hover { background: var(--bg-secondary); color: var(--text-primary); }

.fs-modal-action {
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
  border-radius: 6px;
  min-height: 36px;
}
.fs-modal-action:hover { background: var(--accent-light); }

.fs-modal-save {
  color: #fff;
  background: var(--accent);
}
.fs-modal-save:hover { background: var(--accent-hover); }
.fs-modal-save:disabled { opacity: 0.5; }

.fs-modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.fs-loading,
.fs-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* --- Template cards (5.1) --- */

.tpl-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.tpl-card:hover {
  border-color: var(--accent);
  box-shadow: 0 2px 8px var(--shadow);
}

.tpl-card-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent-light);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.tpl-card-icon svg { width: 20px; height: 20px; }

.tpl-card-info {
  flex: 1;
  min-width: 0;
}

.tpl-card-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tpl-card-desc {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.tpl-card-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.tpl-card-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s;
}
.tpl-card:hover .tpl-card-actions {
  opacity: 1;
}

.tpl-action-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.tpl-action-btn:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}
.tpl-action-btn svg {
  width: 16px;
  height: 16px;
}
.tpl-delete-btn:hover {
  background: rgba(222, 53, 11, 0.1);
  color: #DE350B;
}

.tpl-card-arrow {
  color: var(--text-secondary);
  flex-shrink: 0;
}
.tpl-card-arrow svg { width: 16px; height: 16px; }

/* --- Editor (5.2) --- */

.editor-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  gap: 0;
  flex-shrink: 0;
  overflow-x: auto;
  background: var(--bg-primary);
}

.editor-tab {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  min-height: 40px;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
}
.editor-tab:hover { color: var(--text-primary); }
.editor-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.editor-body {
  padding: 0;
}

.editor-pane {
  display: none;
  height: 100%;
}
.editor-pane.active {
  display: flex;
  flex-direction: column;
}

.editor-textarea {
  width: 100%;
  flex: 1;
  min-height: 300px;
  padding: 16px;
  border: none;
  outline: none;
  resize: none;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.6;
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.editor-json {
  font-size: 12px;
}

/* --- Settings form (visual editor) --- */
.settings-form {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
}
.sf-group {
  margin-bottom: 14px;
}
.sf-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.sf-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: var(--bg-primary);
  color: var(--text-primary);
  box-sizing: border-box;
}
.sf-input:focus {
  border-color: var(--accent);
  outline: none;
}
.sf-hint {
  display: block;
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 3px;
}
.sf-row {
  display: flex;
  gap: 12px;
}
.sf-row .sf-group { flex: 1; }
.sf-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-primary);
  min-height: 38px;
  align-items: center;
}
.sf-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--accent-light, #E6F0FF);
  color: var(--accent, #0066FF);
  font-size: 12px;
  font-weight: 500;
}
.sf-tag-x {
  cursor: pointer;
  font-size: 14px;
  opacity: 0.6;
}
.sf-tag-x:hover { opacity: 1; }
.sf-tag-input {
  border: none !important;
  padding: 4px !important;
  flex: 1;
  min-width: 120px;
  font-size: 13px;
}
.sf-tag-input:focus { box-shadow: none !important; }

/* Tools grid — свичи инструментов */
.sf-tools-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-primary);
}
.sf-tool-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  font-size: 13px;
  color: var(--text-secondary);
  transition: background 0.15s;
}
.sf-tool-item:hover { background: var(--bg-hover, rgba(0,0,0,0.04)); }
.sf-tool-item.active { color: var(--text-primary); }
.sf-tool-switch {
  position: relative;
  width: 32px;
  height: 18px;
  border-radius: 9px;
  background: var(--border);
  transition: background 0.2s;
  flex-shrink: 0;
}
.sf-tool-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: white;
  transition: transform 0.2s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}
.sf-tool-item.active .sf-tool-switch {
  background: var(--accent, #0066FF);
}
.sf-tool-item.active .sf-tool-switch::after {
  transform: translateX(14px);
}

.sf-advanced {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.sf-advanced summary {
  padding: 8px 12px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
}
.sf-advanced .editor-textarea {
  min-height: 150px;
  border-top: 1px solid var(--border);
}

/* --- Test chat (5.3) --- */

.test-chat {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.test-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.test-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
}

.test-user {
  align-self: flex-end;
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 4px;
}

.test-agent {
  align-self: flex-start;
  background: var(--bubble-agent);
  color: var(--bubble-agent-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.test-input-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-primary);
}

.test-input-bar input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: var(--bg-tertiary);
  color: var(--text-primary);
  outline: none;
}
.test-input-bar input:focus { border-color: var(--accent); }

.test-input-bar button {
  padding: 8px 16px;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  min-width: 44px;
}
.test-input-bar button:hover { background: var(--accent-hover); }

/* --- Client cards (5.4) --- */

.client-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.client-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent-light);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  flex-shrink: 0;
}

.client-info {
  flex: 1;
  min-width: 0;
}

.client-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.client-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.4;
}
.badge-primary { background: var(--accent-light); color: var(--accent); }
.badge-info { background: #E3F2FD; color: #1565C0; }
[data-theme="dark"] .badge-info { background: #1B3A5C; color: #4C9AFF; }
.badge-danger { background: rgba(222,53,11,0.1); color: var(--danger); }

/* --- Billing (5.5) --- */

.billing-cards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.billing-stat {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  text-align: center;
}

.billing-stat-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
}

.billing-stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.billing-client-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.billing-client-pos {
  color: var(--text-secondary);
  width: 24px;
  flex-shrink: 0;
}

.billing-client-name {
  flex: 1;
  color: var(--text-primary);
  font-weight: 500;
}

.billing-client-cost {
  color: var(--danger);
  font-weight: 600;
  margin-right: 12px;
}

.billing-client-balance {
  color: var(--text-secondary);
  font-size: 13px;
}

@media (min-width: 768px) {
  .billing-cards {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* --- Billing Tabs & Tables (6.4) --- */

.billing-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.billing-tab {
  flex-shrink: 0;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.2s, border-color 0.2s;
}

.billing-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.billing-tab:hover {
  color: var(--text-primary);
}

.billing-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -16px;
  padding: 0 16px;
}

.billing-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  white-space: nowrap;
}

.billing-table th {
  text-align: left;
  padding: 8px 10px;
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-tertiary);
  position: sticky;
  top: 0;
}

.billing-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}

.billing-table tbody tr:hover {
  background: var(--bg-tertiary);
}

@media (max-width: 480px) {
  .billing-tab {
    padding: 8px 10px;
    font-size: 12px;
  }
  .billing-table {
    font-size: 12px;
  }
  .billing-table th,
  .billing-table td {
    padding: 6px 6px;
  }
}

/* --- Balance Badge (6.3) --- */

.balance-badge {
  background: none;
  border: none;
  border-radius: 8px;
  padding: 2px 8px;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
  line-height: 1.4;
  letter-spacing: -0.01em;
}

.balance-badge:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.balance-badge.balance-ok {
  color: var(--text-secondary);
}

.balance-badge.balance-low {
  color: var(--warning, #FF8B00);
}

.balance-badge.balance-zero {
  color: var(--danger, #DE350B);
}

/* --- Balance Transactions (6.3) --- */

.bal-tx-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.bal-tx {
  display: grid;
  grid-template-columns: 24px 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.bal-tx:last-child {
  border-bottom: none;
}

.bal-tx-icon {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  flex-shrink: 0;
}

.bal-tx-topup .bal-tx-icon {
  background: rgba(0, 135, 90, 0.1);
  color: var(--success, #00875A);
}

.bal-tx-usage .bal-tx-icon {
  background: rgba(222, 53, 11, 0.1);
  color: var(--danger, #DE350B);
}

.bal-tx-init .bal-tx-icon {
  background: var(--accent-light);
  color: var(--accent);
  font-size: 8px;
}

.bal-tx-desc {
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bal-tx-amount {
  font-weight: 600;
  white-space: nowrap;
}

.bal-tx-plus { color: var(--success, #00875A); }
.bal-tx-minus { color: var(--danger, #DE350B); }

.bal-tx-date {
  color: var(--text-secondary);
  font-size: 11px;
  white-space: nowrap;
}

@media (max-width: 480px) {
  .bal-tx {
    grid-template-columns: 24px 1fr auto;
    gap: 6px;
  }
  .bal-tx-date {
    grid-column: 2 / -1;
    font-size: 11px;
  }
}
