/* ========================================
   King's Grill Bot Manager — Style System
   Mobile-first · Blue Gradient · Premium
   ======================================== */

:root {
  --bg-start: #dbeafe;
  --bg-end: #f8fafc;
  --surface: #ffffff;
  --surface-hover: #f1f5f9;
  --surface-active: #e2e8f0;
  --border: #e2e8f0;
  --border-focus: #3b82f6;
  --text: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --primary: #2563eb;
  --primary-light: #dbeafe;
  --primary-hover: #1d4ed8;
  --success: #059669;
  --success-light: #d1fae5;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
  --transition: 0.2s ease;
  --sidebar-w: 240px;
  --topbar-h: 60px;
  --mobile-nav-h: 64px;
}

/* Reset */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  font-family: 'Inter', -apple-system, sans-serif;
  background: linear-gradient(165deg, var(--bg-start) 0%, var(--bg-end) 50%);
  background-attachment: fixed;
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, select, textarea { font-family: inherit; }

/* ============ SIDEBAR (Desktop) ============ */
.sidebar {
  display: none;
  position: fixed; top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 100;
  flex-direction: column;
}
.sidebar-header {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 16px; border-bottom: 1px solid var(--border);
}
.logo { font-size: 2rem; }
.brand-name { font-size: 1rem; font-weight: 700; color: var(--text); }
.brand-sub { font-size: 0.75rem; color: var(--text-muted); font-weight: 500; }
.sidebar-nav { flex: 1; padding: 12px 8px; overflow-y: auto; }
.sidebar-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 10px 12px; border-radius: var(--radius-sm);
  font-size: 0.875rem; font-weight: 500; color: var(--text-secondary);
  transition: all var(--transition);
}
.sidebar-item:hover { background: var(--surface-hover); color: var(--text); }
.sidebar-item.active {
  background: var(--primary-light); color: var(--primary); font-weight: 600;
}
.sidebar-item span:first-child { font-size: 1.1rem; }
.sidebar-footer { padding: 12px 16px; border-top: 1px solid var(--border); }
.connection-badge {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.8rem; color: var(--text-muted);
}
.conn-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--danger); flex-shrink: 0;
}
.conn-dot.online { background: var(--success); }

/* ============ MOBILE NAV ============ */
.mobile-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--mobile-nav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-around;
  z-index: 200;
  padding-bottom: env(safe-area-inset-bottom, 0);
  box-shadow: 0 -2px 12px rgba(0,0,0,0.06);
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 12px; border-radius: var(--radius-sm);
  transition: all var(--transition); color: var(--text-muted);
  min-width: 56px;
}
.nav-item.active { color: var(--primary); }
.nav-icon { font-size: 1.25rem; }
.nav-label { font-size: 0.65rem; font-weight: 600; }

/* ============ MAIN ============ */
.main {
  padding: 0 16px 80px;
  min-height: 100vh;
}
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 0; position: sticky; top: 0; z-index: 50;
  background: linear-gradient(165deg, var(--bg-start) 0%, var(--bg-end) 50%);
}
.page-title { font-size: 1.25rem; font-weight: 700; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary-light); display: flex; align-items: center;
  justify-content: center; font-size: 1rem;
}

/* ============ CARDS ============ */
.card {
  background: var(--surface); border-radius: var(--radius-lg);
  border: 1px solid var(--border); box-shadow: var(--shadow-sm);
  margin-bottom: 16px; overflow: hidden;
  transition: box-shadow var(--transition);
}
.card:hover { box-shadow: var(--shadow); }
.card-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.card-header h3 { font-size: 0.95rem; font-weight: 600; }
.card-body { padding: 20px; }

/* ============ STATS ============ */
.stats-grid {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 12px; margin-bottom: 16px;
}
.stat-card {
  background: var(--surface); border-radius: var(--radius);
  padding: 16px; border: 1px solid var(--border);
  box-shadow: var(--shadow-sm); transition: transform var(--transition);
}
.stat-card:hover { transform: translateY(-2px); }
.stat-card .icon { font-size: 1.5rem; margin-bottom: 8px; }
.stat-card .value { font-size: 1.75rem; font-weight: 800; color: var(--text); }
.stat-card .label { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; font-weight: 500; }
.stat-card .badge {
  display: inline-block; padding: 2px 8px; border-radius: 99px;
  font-size: 0.7rem; font-weight: 600; margin-top: 6px;
}
.badge-green { background: var(--success-light); color: var(--success); }
.badge-red { background: var(--danger-light); color: var(--danger); }
.badge-yellow { background: var(--warning-light); color: var(--warning); }

/* ============ PANELS GRID ============ */
.panels-grid {
  display: grid; grid-template-columns: 1fr; gap: 16px;
}

/* ============ TABS ============ */
.tab-content { display: none; animation: fadeIn 0.3s ease; }
.tab-content.active { display: block; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============ FORMS ============ */
.form-group { margin-bottom: 16px; }
.form-group label {
  display: block; font-size: 0.8rem; font-weight: 600;
  color: var(--text-secondary); margin-bottom: 6px;
}
.input {
  width: 100%; padding: 10px 14px; border-radius: var(--radius-sm);
  border: 1.5px solid var(--border); font-size: 0.875rem;
  background: var(--surface); color: var(--text);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
}
.input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}
.textarea { resize: vertical; min-height: 80px; }
.input-group { display: flex; gap: 8px; }
.input-group .input { flex: 1; min-width: 0; }
.help-text { font-size: 0.75rem; color: var(--text-muted); margin-top: 4px; display: block; }
.tab-toolbar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; margin-bottom: 16px; flex-wrap: wrap;
}
.search-box {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); padding: 0 12px; flex: 1; max-width: 320px;
}
.search-box input {
  border: none; outline: none; padding: 10px 0;
  font-size: 0.875rem; background: none; width: 100%; color: var(--text);
}
.search-icon { font-size: 0.9rem; }

/* ============ BUTTONS ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; padding: 10px 18px; border-radius: var(--radius-sm);
  font-size: 0.85rem; font-weight: 600;
  transition: all var(--transition); white-space: nowrap;
}
.btn-primary {
  background: var(--primary); color: white; box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--primary-hover); box-shadow: var(--shadow); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-ghost { color: var(--text-secondary); padding: 8px; }
.btn-ghost:hover { background: var(--surface-hover); }
.btn-sm { padding: 6px 12px; font-size: 0.8rem; }
.btn-lg { width: 100%; padding: 14px; font-size: 1rem; margin-top: 8px; }
.btn-outline {
  border: 1.5px solid var(--border); color: var(--text-secondary);
  background: var(--surface);
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }

/* ============ TABLE ============ */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table {
  width: 100%; border-collapse: collapse; font-size: 0.82rem;
  min-width: 500px;
}
th, td {
  padding: 10px 12px; text-align: left;
  border-bottom: 1px solid var(--border);
}
th {
  font-weight: 600; color: var(--text-muted); font-size: 0.75rem;
  text-transform: uppercase; letter-spacing: 0.5px;
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
td { color: var(--text-secondary); word-break: break-word; }
tr:hover td { background: var(--surface-hover); }
.toggle {
  width: 42px; height: 24px; border-radius: 12px;
  background: #cbd5e1; cursor: pointer;
  position: relative; transition: background var(--transition);
}
.toggle.on { background: var(--success); }
.toggle::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 18px; height: 18px; border-radius: 50%;
  background: white; box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  transition: transform var(--transition);
}
.toggle.on::after { transform: translateX(18px); }
.actions { display: flex; gap: 4px; }
.action-btn {
  padding: 4px 8px; border-radius: 6px; font-size: 0.8rem;
  transition: background var(--transition);
}
.action-btn:hover { background: var(--surface-active); }
.truncate-cell { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ============ KB GRID ============ */
.kb-grid {
  display: grid; grid-template-columns: 1fr; gap: 12px;
}
.kb-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  transition: box-shadow var(--transition);
}
.kb-card:hover { box-shadow: var(--shadow); }
.kb-keyword {
  font-weight: 700; font-size: 0.95rem; color: var(--primary);
  margin-bottom: 6px;
}
.kb-content {
  font-size: 0.82rem; color: var(--text-secondary);
  max-height: 60px; overflow: hidden; line-height: 1.5;
  word-break: break-word;
}
.kb-footer {
  display: flex; justify-content: space-between; align-items: center;
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border);
}

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px); z-index: 500;
  display: none; justify-content: center; align-items: flex-end;
  animation: fadeIn 0.2s ease;
}
.modal-overlay.active { display: flex; }
.modal {
  background: var(--surface); width: 100%;
  max-width: 500px; max-height: 90vh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  overflow-y: auto; animation: slideUp 0.3s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.modal-header h3 { font-size: 1rem; font-weight: 700; }
.modal-close {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem; color: var(--text-muted);
  transition: background var(--transition);
}
.modal-close:hover { background: var(--surface-hover); }
.modal-body { padding: 20px; }
.modal-footer {
  padding: 12px 20px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}

/* ============ HEALTH CHECK ============ */
.health-results { margin-top: 16px; }
.health-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); margin-bottom: 8px;
}
.health-ok { border-left: 3px solid var(--success); }
.health-fail { border-left: 3px solid var(--danger); }
.health-label { font-size: 0.85rem; font-weight: 500; }
.health-status { font-size: 0.85rem; font-weight: 600; }

/* ============ CHAT PREVIEW ============ */
.chat-preview {
  margin-top: 12px; min-height: 100px;
  background: #f1f5f9; border-radius: var(--radius);
  padding: 16px; max-height: 300px; overflow-y: auto;
}
.chat-msg {
  padding: 10px 14px; border-radius: 12px;
  margin-bottom: 8px; max-width: 85%; font-size: 0.85rem;
  line-height: 1.5; word-break: break-word;
}
.chat-user {
  background: var(--primary); color: white;
  margin-left: auto; border-bottom-right-radius: 4px;
}
.chat-bot {
  background: white; color: var(--text);
  border: 1px solid var(--border); border-bottom-left-radius: 4px;
}

/* ============ CODE BLOCK ============ */
.code-block {
  margin-top: 12px; padding: 14px;
  background: #1e293b; color: #e2e8f0;
  border-radius: var(--radius-sm); font-family: 'Courier New', monospace;
  font-size: 0.78rem; overflow-x: auto; white-space: pre-wrap;
  word-break: break-all; max-height: 300px; overflow-y: auto;
}

/* ============ TOAST ============ */
.toast-container {
  position: fixed; top: 16px; right: 16px;
  z-index: 600; display: flex; flex-direction: column; gap: 8px;
}
.toast {
  padding: 12px 20px; border-radius: var(--radius-sm);
  font-size: 0.85rem; font-weight: 500; color: white;
  box-shadow: var(--shadow-lg); animation: slideIn 0.3s ease;
  max-width: 320px;
}
@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-info { background: var(--primary); }

/* ============ SETUP OVERLAY ============ */
.setup-overlay {
  position: fixed; inset: 0; z-index: 700;
  background: linear-gradient(165deg, var(--bg-start) 0%, var(--bg-end) 100%);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.setup-overlay.hidden { display: none; }
.setup-card {
  background: var(--surface); border-radius: var(--radius-lg);
  padding: 32px 24px; max-width: 420px; width: 100%;
  box-shadow: var(--shadow-lg); text-align: center;
}
.setup-icon { font-size: 3rem; margin-bottom: 12px; }
.setup-card h2 { font-size: 1.4rem; margin-bottom: 6px; }
.setup-card p { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 24px; }
.setup-card .form-group { text-align: left; }
.setup-error { color: var(--danger); font-size: 0.82rem; margin-top: 12px; }

/* ============ SKELETONS ============ */
.skeleton { position: relative; overflow: hidden; }
.skeleton-line {
  height: 40px; background: var(--surface-hover);
  border-radius: 6px;
}
.skeleton::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
  animation: shimmer 1.5s infinite;
}
@keyframes shimmer { 0% { transform: translateX(-100%); } 100% { transform: translateX(100%); } }

/* ============ EMPTY ============ */
.empty-state {
  text-align: center; padding: 32px 16px;
  color: var(--text-muted); font-size: 0.9rem;
}

/* ============ RESPONSIVE ============ */

/* Tablet */
@media (min-width: 640px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
  .panels-grid { grid-template-columns: 1fr 1fr; }
  .kb-grid { grid-template-columns: 1fr 1fr; }
  .modal { border-radius: var(--radius-lg); align-self: center; }
}

/* Desktop */
@media (min-width: 1024px) {
  .mobile-nav { display: none; }
  .sidebar { display: flex; }
  .main {
    margin-left: var(--sidebar-w);
    padding: 0 32px 32px;
    max-width: calc(100vw - var(--sidebar-w));
  }
  .topbar {
    padding: 20px 0;
    background: linear-gradient(165deg, var(--bg-start) 0%, var(--bg-end) 50%);
  }
  .page-title { font-size: 1.5rem; }
  .stats-grid { grid-template-columns: repeat(4, 1fr); gap: 16px; }
  .panels-grid { gap: 20px; }
  .kb-grid { grid-template-columns: repeat(3, 1fr); }
}
