:root {
  --bg: #0f1117;
  --surface: #161b27;
  --surface-elevated: #1e2535;
  --border: #2a3a52;
  --accent: #3b82f6;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
}

a { color: var(--accent); text-decoration: none; }

.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 220px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  flex-shrink: 0;
}

.sidebar .brand { font-weight: 600; font-size: 18px; padding: 0 10px 20px; display: flex; align-items: center; justify-content: space-between; }

.sidebar-close { display: none; background: none; border: none; color: var(--text-secondary); font-size: 20px; cursor: pointer; padding: 4px 8px; }

.sidebar nav a {
  display: block;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--text-secondary);
  font-weight: 500;
  margin-bottom: 4px;
}

.sidebar nav a:hover, .sidebar nav a.active {
  background: var(--surface-elevated);
  color: var(--text-primary);
}

.main { flex: 1; min-width: 0; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.menu-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 16px;
  cursor: pointer;
}

.sidebar-overlay { display: none; }

.content { padding: 24px; }

.breadcrumb { color: var(--text-muted); margin-bottom: 16px; font-size: 13px; overflow-wrap: anywhere; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-wrap table { min-width: 640px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
}

.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }

@media (max-width: 768px) {
  .grid-4, .grid-2 { grid-template-columns: 1fr; }

  .menu-toggle { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 1001;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,.4);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; }
  .sidebar nav a { min-height: 44px; display: flex; align-items: center; }

  .sidebar-overlay.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 1000;
  }

  .content { padding: 16px; }
  .topbar { padding: 12px 16px; }
  .card { padding: 14px; }
  .stat-value { font-size: 22px; }

  .kanban-col { min-width: 180px; }
  .bubble { max-width: 85%; }
  .bubble .time { display: block; }

  .tabs { overflow-x: auto; white-space: nowrap; }

  /* Touch targets */
  .btn, button, input[type="submit"] { min-height: 44px; }
  input, select { min-height: 44px; }
  .toggle-switch { min-height: 44px; }
  .attention-row { flex-wrap: wrap; }
  .attention-row .btn { width: 100%; text-align: center; }

  /* Chat reply box pinned to the bottom of the viewport */
  .chat-page { padding-bottom: 84px; }
  .reply-form-fixed {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 10px 12px;
    z-index: 500;
  }
}

@media (max-width: 600px) {
  /* Stacked-card tables instead of horizontal scroll */
  .table-wrap.stack-mobile table,
  .table-wrap.stack-mobile thead,
  .table-wrap.stack-mobile tbody,
  .table-wrap.stack-mobile th,
  .table-wrap.stack-mobile td,
  .table-wrap.stack-mobile tr { display: block; }
  .table-wrap.stack-mobile table { min-width: 0; }
  .table-wrap.stack-mobile thead { display: none; }
  .table-wrap.stack-mobile tr {
    background: var(--surface-elevated);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px 12px;
    margin-bottom: 10px;
  }
  .table-wrap.stack-mobile td {
    border-bottom: none;
    padding: 6px 0;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    text-align: right;
  }
  .table-wrap.stack-mobile td::before {
    content: attr(data-label);
    color: var(--text-muted);
    font-size: 12px;
    text-transform: uppercase;
    text-align: left;
  }
}

@media (max-width: 480px) {
  body { font-size: 13px; }
  .btn { padding: 10px 14px; font-size: 13px; }
  .toast-container { left: 12px; right: 12px; top: 12px; }
  .toast { width: auto; }
}

.stat-label { color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 28px; font-weight: 600; margin-top: 6px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-size: 12px; text-transform: uppercase; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
}
.badge.new { color: var(--accent); border-color: var(--accent); }
.badge.booked, .badge.converted, .badge.confirmed { color: var(--success); border-color: var(--success); }
.badge.lost, .badge.cancelled { color: var(--error); border-color: var(--error); }
.badge.replied, .badge.qualified, .badge.pending { color: var(--warning); border-color: var(--warning); }

.btn {
  display: inline-block;
  padding: 8px 16px;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  border: none;
  cursor: pointer;
  font-weight: 500;
  font-size: 14px;
}
.btn.secondary { background: var(--surface-elevated); color: var(--text-primary); border: 1px solid var(--border); }
.btn.danger { background: var(--error); }

input, select, textarea {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 9px 12px;
  font-family: inherit;
  font-size: 14px;
  width: 100%;
}

label { display: block; margin-bottom: 6px; color: var(--text-secondary); font-size: 13px; }
.field { margin-bottom: 16px; }

.kanban { display: flex; gap: 12px; overflow-x: auto; }
.kanban-col { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; min-width: 220px; flex: 1; padding: 10px; }
.kanban-col h4 { margin: 4px 8px 12px; font-size: 13px; color: var(--text-secondary); display: flex; justify-content: space-between; }
.kanban-card { background: var(--surface-elevated); border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin-bottom: 8px; cursor: grab; }
.kanban-card .name { font-weight: 500; }
.kanban-card .meta { color: var(--text-muted); font-size: 12px; margin-top: 4px; }

.chat { display: flex; flex-direction: column; gap: 10px; padding: 16px; max-height: 60vh; overflow-y: auto; }
.bubble { max-width: 60%; padding: 10px 14px; border-radius: 14px; font-size: 14px; position: relative; }
.bubble.inbound { background: var(--surface-elevated); align-self: flex-start; border: 1px solid var(--border); }
.bubble.outbound { background: var(--accent); align-self: flex-end; color: white; }
.bubble .ai-flag { font-size: 11px; margin-left: 6px; }
.bubble .time { display: none; font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.bubble:hover .time { display: block; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tabs a { padding: 10px 16px; color: var(--text-secondary); border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--text-primary); border-color: var(--accent); }

.toast-container { position: fixed; top: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 999; }
.toast { padding: 12px 16px; border-radius: 8px; background: var(--surface-elevated); border: 1px solid var(--border); box-shadow: 0 4px 12px rgba(0,0,0,.4); }
.toast.success { border-color: var(--success); }
.toast.error { border-color: var(--error); }

.skeleton { background: linear-gradient(90deg, var(--surface) 25%, var(--surface-elevated) 50%, var(--surface) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; border-radius: 8px; height: 16px; }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.banner-warning { background: var(--warning); color: #1a1a1a; padding: 10px 24px; font-weight: 500; }

.attention-card {
  border-color: var(--warning);
  box-shadow: 0 0 0 1px var(--warning);
}
.attention-card .stat-value { color: var(--warning); }

.attention-list { display: flex; flex-direction: column; gap: 10px; }
.attention-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.attention-row .name { font-weight: 500; }
.attention-row .reason { color: var(--text-secondary); font-size: 13px; margin-top: 2px; }
.attention-row .meta { color: var(--text-muted); font-size: 12px; margin-top: 4px; }
.attention-row .btn { flex-shrink: 0; }

.all-clear { color: var(--text-secondary); padding: 12px 0; margin: 0; }

.collapsible-summary {
  cursor: pointer;
  font-weight: 600;
  list-style: none;
  padding: 2px 0;
}
.collapsible-summary::-webkit-details-marker { display: none; }
.collapsible-summary::before { content: "▸ "; color: var(--text-muted); }
details[open] .collapsible-summary::before { content: "▾ "; }

.toggle-switch {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}
.toggle-switch .track {
  width: 44px;
  height: 24px;
  border-radius: 999px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  position: relative;
  transition: background 0.15s ease;
}
.toggle-switch .track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text-secondary);
  transition: transform 0.15s ease, background 0.15s ease;
}
.toggle-switch.on .track { background: var(--success); border-color: var(--success); }
.toggle-switch.on .track::after { transform: translateX(20px); background: white; }
.toggle-switch.off .track::after { background: var(--text-muted); }

.manual-mode-banner {
  background: var(--surface-elevated);
  border: 1px solid var(--warning);
  color: var(--warning);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 12px;
}

.test-mode-banner {
  background: var(--warning);
  color: #1a1a1a;
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 600;
  text-align: center;
  margin-bottom: 16px;
}

.simulated-booking-card {
  background: var(--surface-elevated);
  border: 1px solid var(--success);
  border-radius: 10px;
  padding: 14px;
  margin: 8px 0;
  font-size: 14px;
}

/* ── Stat delta ── */
.stat-delta { font-size: 12px; margin-top: 4px; }
.stat-delta.up { color: var(--success); }
.stat-delta.down { color: var(--error); }
.stat-delta.neutral { color: var(--text-muted); }

/* ── Integration tiles ── */
.integration-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.integration-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.integration-tile:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.integration-tile-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.integration-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  color: white;
  flex-shrink: 0;
}

.integration-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-muted);
}
.integration-status .status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}
.integration-status.connected { color: var(--success); }
.integration-status.connected .status-dot { background: var(--success); }

.integration-name { font-weight: 600; font-size: 14px; }
.integration-desc { color: var(--text-muted); font-size: 12px; line-height: 1.4; flex: 1; }
.integration-action { margin-top: 4px; text-align: center; pointer-events: none; }

/* ── Modals ── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 2000;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-overlay.open { display: flex; }

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  width: 100%;
  max-width: 540px;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 8px;
  min-height: auto;
  border-radius: 6px;
}
.modal-close:hover { background: var(--surface-elevated); color: var(--text-primary); }

.admin-only-label {
  display: inline-block;
  background: var(--warning);
  color: #1a1a1a;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: 8px;
}

/* Review rate colours on analytics */
.review-rate-good { color: var(--success); }
.review-rate-low { color: var(--warning); }

@media (max-width: 600px) {
  .integration-grid { grid-template-columns: 1fr; }
  .modal-box { padding: 16px; }
}
