/* ─── Algorithma AI Web UI — Dark Theme ─── */
:root {
  --bg-body: #0d1117;
  --bg-card: #161b22;
  --bg-sidebar: #0d1117;
  --bg-input: #0d1117;
  --border: #30363d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #58a6ff;
  --accent-hover: #79c0ff;
  --success: #3fb950;
  --warning: #d29922;
  --danger: #f85149;
  --font-ui: system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Cascadia Code", monospace;
  --radius: 8px;
  --sidebar-w: 220px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-ui);
  background: var(--bg-body);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

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

/* ─── Sidebar ─── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 20px 0;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  display: flex;
  flex-direction: column;
  z-index: 100;
}

.sidebar-logo {
  padding: 0 20px 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}

.sidebar-logo h1 {
  font-size: 18px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-logo span.emoji { font-size: 24px; }
.sidebar-logo .version { font-size: 11px; color: var(--text-muted); font-weight: 400; }

.nav-links { list-style: none; flex: 1; }

.nav-links li a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.15s;
  border-left: 3px solid transparent;
}

.nav-links li a:hover {
  color: var(--text);
  background: rgba(88, 166, 255, 0.05);
}

.nav-links li a.active {
  color: var(--accent);
  background: rgba(88, 166, 255, 0.08);
  border-left-color: var(--accent);
}

.nav-links li a .icon { font-size: 18px; width: 22px; text-align: center; }

/* ─── Main content ─── */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 28px 32px;
  max-width: 1200px;
}

.page-header {
  margin-bottom: 24px;
}

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

.page-header p {
  color: var(--text-muted);
  font-size: 13px;
  margin-top: 4px;
}

/* ─── Cards ─── */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.grid-full {
  grid-template-columns: 1fr;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

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

.card-header h3 {
  font-size: 14px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
}

/* ─── Status badges ─── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
}

.badge-success { background: rgba(63, 185, 80, 0.15); color: var(--success); }
.badge-warning { background: rgba(210, 153, 34, 0.15); color: var(--warning); }
.badge-danger  { background: rgba(248, 81, 73, 0.15); color: var(--danger); }
.badge-info    { background: rgba(88, 166, 255, 0.15); color: var(--accent); }
.badge-muted   { background: rgba(139, 148, 158, 0.15); color: var(--text-muted); }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.status-dot.green  { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.yellow { background: var(--warning); box-shadow: 0 0 6px var(--warning); }
.status-dot.red    { background: var(--danger);  box-shadow: 0 0 6px var(--danger); }
.status-dot.gray   { background: var(--text-muted); }

/* ─── Tables ─── */
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.table th {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: rgba(88, 166, 255, 0.03); }

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover { border-color: var(--accent); color: var(--accent); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: #fff; }

.btn-danger {
  border-color: var(--danger);
  color: var(--danger);
}
.btn-danger:hover { background: var(--danger); color: #fff; }

.btn-sm { padding: 5px 10px; font-size: 12px; }

.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }

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

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

.form-group .help {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
}

input[type="text"],
input[type="password"],
input[type="number"],
select,
textarea {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  font-family: var(--font-ui);
  transition: border-color 0.15s;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}

textarea { font-family: var(--font-mono); resize: vertical; min-height: 80px; }

/* ─── Alerts ─── */
.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 16px;
  border: 1px solid;
}

.alert-info    { background: rgba(88, 166, 255, 0.08); border-color: rgba(88, 166, 255, 0.2); color: var(--accent); }
.alert-success { background: rgba(63, 185, 80, 0.08); border-color: rgba(63, 185, 80, 0.2); color: var(--success); }
.alert-warning { background: rgba(210, 153, 34, 0.08); border-color: rgba(210, 153, 34, 0.2); color: var(--warning); }
.alert-danger  { background: rgba(248, 81, 73, 0.08); border-color: rgba(248, 81, 73, 0.2); color: var(--danger); }

/* ─── Log viewer ─── */
.log-container {
  background: #010409;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  max-height: 600px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.6;
}

.log-line {
  display: flex;
  gap: 10px;
  padding: 2px 4px;
  border-radius: 3px;
}

.log-line:hover { background: rgba(255, 255, 255, 0.03); }

.log-time { color: var(--text-muted); white-space: nowrap; min-width: 80px; }
.log-level { font-weight: 700; min-width: 50px; text-transform: uppercase; }
.log-level.debug { color: var(--text-muted); }
.log-level.info  { color: var(--accent); }
.log-level.warn  { color: var(--warning); }
.log-level.error { color: var(--danger); }
.log-msg { color: var(--text); word-break: break-all; }
.log-src { color: #bc8cff; min-width: 80px; }

/* ─── Controls bar ─── */
.controls {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  flex-wrap: wrap;
}

.controls input[type="text"] { max-width: 250px; }

.controls label {
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

/* ─── Loading spinner ─── */
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  display: inline-block;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px;
  color: var(--text-muted);
  font-size: 13px;
}

/* ─── Toast ─── */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  animation: fadeIn 0.2s ease;
  max-width: 400px;
  border: 1px solid;
}

.toast-success { background: rgba(63, 185, 80, 0.12); border-color: rgba(63, 185, 80, 0.3); color: var(--success); }
.toast-error   { background: rgba(248, 81, 73, 0.12); border-color: rgba(248, 81, 73, 0.3); color: var(--danger); }
.toast-info    { background: rgba(88, 166, 255, 0.12); border-color: rgba(88, 166, 255, 0.3); color: var(--accent); }

@keyframes fadeIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: none; } }

/* ─── Modal ─── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 700px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.modal h3 { margin-bottom: 16px; }

.modal pre {
  background: #010409;
  padding: 12px;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  color: var(--text);
}

/* ─── Stat number ─── */
.stat { text-align: center; padding: 8px; }
.stat-value { font-size: 28px; font-weight: 700; color: var(--accent); }
.stat-label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* User menu in sidebar */
.sidebar-user { margin-top: auto; padding: 12px 20px; border-top: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.user-info { flex: 1; min-width: 0; }
.user-email { font-size: 12px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.btn-logout { font-size: 11px; padding: 4px 10px; background: transparent; color: var(--danger); border: 1px solid var(--danger); border-radius: 4px; cursor: pointer; flex-shrink: 0; }
.btn-logout:hover { background: rgba(248,81,73,0.1); }

/* ─── Mobile: bottom nav ─── */
@media (max-width: 768px) {
  body { flex-direction: column; padding-bottom: calc(96px + env(safe-area-inset-bottom, 0px)); }

  .sidebar {
    position: fixed;
    top: auto; bottom: 0; left: 0; right: 0;
    width: 100%;
    height: calc(60px + env(safe-area-inset-bottom, 0px));
    border-right: none;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: row;
    padding: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    z-index: 200;
    background: var(--bg-sidebar);
  }

  .sidebar-logo { display: none; }

  .nav-links {
    display: flex;
    flex-direction: row;
    width: 100%;
    align-items: center;
    height: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 0;
  }

  .nav-links::-webkit-scrollbar { display: none; }

  .nav-links li { flex: 0 0 auto; min-width: 64px; }

  .nav-links li a {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 8px 6px;
    font-size: 9px;
    gap: 2px;
    border-left: none;
    border-top: 3px solid transparent;
    text-align: center;
    height: 100%;
    white-space: nowrap;
  }

  .nav-links li a.active {
    border-left-color: transparent;
    border-top-color: var(--accent);
  }

  .nav-links li a .icon { font-size: 20px; width: auto; }

  .main {
    margin-left: 0;
    padding: 16px;
    width: 100%;
    max-width: 100%;
  }

  .page-header h2 { font-size: 18px; }
  .page-header p { font-size: 12px; }

  /* Stats grid: 2 columns on mobile */
  .grid { grid-template-columns: 1fr !important; }
  .grid-2col-mobile { grid-template-columns: 1fr 1fr !important; }

  .stat-value { font-size: 22px; }

  /* Cards tighter padding */
  .card { padding: 14px; }
  .card-header h3 { font-size: 12px; }

  /* Buttons bigger tap targets */
  .btn { padding: 10px 16px; font-size: 14px; min-height: 44px; }
  .btn-sm { padding: 8px 12px; font-size: 13px; min-height: 40px; }
  .btn-group { flex-direction: column; }
  .btn-group .btn { width: 100%; justify-content: center; }

  /* Forms bigger inputs */
  input[type="text"],
  input[type="password"],
  input[type="number"],
  select,
  textarea {
    padding: 12px;
    font-size: 16px; /* prevents iOS zoom on focus */
  }

  .form-group label { font-size: 14px; }

  /* Tables scroll horizontally */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table { min-width: 400px; }
  .table th, .table td { padding: 8px; font-size: 12px; }

  /* Logs */
  .log-container { padding: 8px; font-size: 11px; max-height: 400px; }
  .log-line { gap: 6px; flex-wrap: wrap; }
  .log-time { min-width: auto; font-size: 10px; }
  .log-level { min-width: 40px; font-size: 10px; }
  .log-src { display: none; }
  .log-msg { width: 100%; font-size: 11px; }

  /* Controls bar stack */
  .controls {
    gap: 8px;
    flex-wrap: wrap;
  }
  .controls input[type="text"] { max-width: 100%; flex: 1; min-width: 150px; }
  .controls label { font-size: 13px; }

  /* Modal full width */
  .modal {
    width: 95%;
    max-height: 85vh;
    padding: 16px;
  }
  .modal pre { font-size: 11px; }

  /* Toast */
  .toast-container { top: 10px; right: 10px; left: 10px; }
  .toast { max-width: 100%; }

  /* Page header flex wrap */
  .flex-between {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  /* Sidebar user menu mobile — pull out of bottom nav into a fixed strip above it */
  .sidebar-user {
    position: fixed;
    bottom: calc(60px + env(safe-area-inset-bottom, 0px));
    left: 0; right: 0;
    padding: 6px 16px;
    border-top: 1px solid var(--border);
    border-left: none;
    background: var(--bg-sidebar);
    z-index: 201;
    justify-content: space-between;
  }
  .user-email { font-size: 11px; max-width: 60vw; }
  .btn-logout { min-height: 32px; padding: 4px 12px; }

  /* Config manual grid to stack */
  .config-manual-grid {
    grid-template-columns: 1fr !important;
  }
}

/* Tablet tweaks */
@media (min-width: 769px) and (max-width: 1024px) {
  .sidebar { width: 180px; }
  .main { margin-left: 180px; padding: 20px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
}

/* Touch-friendly checkboxes */
@media (pointer: coarse) {
  input[type="checkbox"] {
    width: 20px;
    height: 20px;
    margin-right: 4px;
  }
  .controls label { padding: 4px 0; min-height: 36px; }
}

/* ─── Utility ─── */
.mono { font-family: var(--font-mono); }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-16 { margin-bottom: 16px; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-8 { gap: 8px; }
.hidden { display: none !important; }
