:root {
  --bg: #0b0f1a;
  --bg-card: #12182b;
  --bg-hover: #1a2238;
  --border: #243049;
  --text: #e8edf7;
  --muted: #8b9ab8;
  --primary: #6366f1;
  --primary-hover: #818cf8;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --sidebar-w: 260px;
  --radius: 12px;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

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

body {
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden { display: none !important; }

/* Login */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at top, #1e1b4b 0%, var(--bg) 60%);
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
}

.brand { text-align: center; margin-bottom: 2rem; }
.brand-icon {
  width: 56px; height: 56px; border-radius: 14px;
  background: linear-gradient(135deg, var(--primary), #a855f7);
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1.2rem; margin-bottom: 1rem;
}
.brand-img {
  border-radius: 12px;
  object-fit: cover;
  flex-shrink: 0;
}
.brand-img-lg {
  margin-bottom: 1rem;
  border-radius: 14px;
}
.brand h1 { font-size: 1.5rem; margin-bottom: 0.25rem; }
.brand p { color: var(--muted); font-size: 0.9rem; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.35rem; margin-top: 1rem; }
input, select, textarea {
  width: 100%; padding: 0.65rem 0.85rem;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 0.95rem;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); border-color: transparent; }
textarea { min-height: 100px; resize: vertical; font-family: inherit; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.6rem 1.1rem; border-radius: 8px; border: none;
  font-size: 0.9rem; font-weight: 600; cursor: pointer; transition: 0.15s;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg-hover); color: var(--text); }
.btn-danger { background: var(--danger); color: white; }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.8rem; }
.btn-block { width: 100%; margin-top: 1.25rem; }

.error { color: var(--danger); font-size: 0.85rem; margin-top: 0.75rem; }

/* Dashboard layout */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w); background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: fixed;
  height: 100vh; overflow-y: auto;
}

.sidebar-header {
  padding: 1.25rem; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 0.75rem;
}
.sidebar-header .brand-icon { width: 40px; height: 40px; font-size: 0.9rem; margin: 0; }
.sidebar-header small { color: var(--muted); }
.sidebar-header h2 { font-size: 1rem; }

.nav { flex: 1; padding: 0.75rem; }
.nav-item {
  display: flex; align-items: center; gap: 0.65rem;
  padding: 0.65rem 0.85rem; border-radius: 8px;
  color: var(--muted); cursor: pointer; font-size: 0.9rem;
  border: none; background: none; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text); }
.nav-item.active { background: rgba(99,102,241,0.15); color: var(--primary-hover); }

.sidebar-footer { padding: 1rem; border-top: 1px solid var(--border); }

.main { margin-left: var(--sidebar-w); flex: 1; min-height: 100vh; }
.topbar {
  padding: 1rem 1.5rem; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  background: rgba(18,24,43,0.8); backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 1.25rem; }
.topbar-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}
.topbar-left h1 {
  font-size: 1.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  display: none;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 1.25rem;
  cursor: pointer;
  flex-shrink: 0;
}
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
  opacity: 0;
  transition: opacity 0.2s;
}
.sidebar-overlay.visible { display: block; opacity: 1; }
body.nav-open { overflow: hidden; }
.content { padding: 1.5rem; }

/* Cards & grid */
.grid { display: grid; gap: 1rem; }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

.card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem;
}
.card h3 { font-size: 0.85rem; color: var(--muted); font-weight: 500; margin-bottom: 0.5rem; }
.card .value { font-size: 1.75rem; font-weight: 700; }
.card .sub { font-size: 0.8rem; color: var(--muted); margin-top: 0.25rem; }

.status-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px;
}
.status-dot.ok { background: var(--success); }
.status-dot.warn { background: var(--warning); }
.status-dot.err { background: var(--danger); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { padding: 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; }
tr:hover td { background: var(--bg-hover); }

.badge {
  display: inline-block; padding: 0.2rem 0.55rem; border-radius: 999px;
  font-size: 0.75rem; font-weight: 600;
}
.badge-success { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-muted { background: rgba(139,154,184,0.15); color: var(--muted); }
.badge-primary { background: rgba(99,102,241,0.15); color: var(--primary-hover); }

.section { display: none; }
.section.active { display: block; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-group { margin-bottom: 1rem; }
.form-actions { display: flex; gap: 0.5rem; margin-top: 1rem; }

.workflow-canvas {
  background: var(--bg); border: 1px dashed var(--border);
  border-radius: var(--radius); padding: 2rem; min-height: 300px;
}
.workflow-node {
  display: inline-block; background: var(--bg-card);
  border: 2px solid var(--primary); border-radius: 10px;
  padding: 0.75rem 1.25rem; margin: 0.5rem; text-align: center;
  position: relative;
}
.workflow-arrow { color: var(--muted); font-size: 1.5rem; text-align: center; }

.chart-bar {
  display: flex; align-items: flex-end; gap: 4px; height: 120px; margin-top: 1rem;
}
.chart-bar .bar {
  flex: 1; background: var(--primary); border-radius: 4px 4px 0 0;
  min-height: 4px; opacity: 0.8;
}

.toast {
  position: fixed; bottom: 1.5rem; right: 1.5rem;
  background: var(--bg-card); border: 1px solid var(--border);
  padding: 0.85rem 1.25rem; border-radius: 8px; z-index: 100;
  animation: slideIn 0.3s ease;
}
@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } }

.post-list { display: flex; flex-direction: column; gap: 0.75rem; }
.post-item {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem;
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
}
.post-item h4 { font-size: 1rem; margin-bottom: 0.35rem; }
.post-item .meta { font-size: 0.85rem; color: var(--muted); }
.post-item .actions { display: flex; gap: 0.5rem; flex-shrink: 0; }
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 1rem;
}
.modal { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.5rem; width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto; }
.modal h3 { margin-bottom: 1rem; }
.keyword-tags { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.5rem 0; }
.keyword-tag { background: rgba(99,102,241,0.15); color: var(--primary-hover);
  padding: 0.2rem 0.55rem; border-radius: 6px; font-size: 0.8rem; }
.toggle-btn.on { background: rgba(34,197,94,0.2); color: var(--success); border-color: var(--success); }
.toggle-btn.off { background: rgba(139,154,184,0.1); color: var(--muted); }
.filter-bar { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.filter-btn.active { background: var(--primary); color: white; border-color: var(--primary); }

@media (max-width: 768px) {
  .icon-btn { display: inline-flex; align-items: center; justify-content: center; }

  .sidebar {
    transform: translateX(-100%);
    z-index: 50;
    transition: transform 0.25s ease;
    width: min(280px, 88vw);
    box-shadow: none;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.45);
  }

  .main { margin-left: 0; width: 100%; }
  .topbar { padding: 0.75rem 1rem; }
  .content { padding: 1rem; }
  .form-row { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }

  .post-item {
    flex-direction: column;
    align-items: stretch;
  }
  .post-item .actions {
    flex-wrap: wrap;
    width: 100%;
  }
  .post-item .actions .btn { flex: 1; min-width: calc(50% - 0.25rem); }

  .form-actions { flex-wrap: wrap; }
  .form-actions .btn { flex: 1; min-width: 120px; }

  .modal {
    max-width: none;
    margin: 0;
    max-height: 85dvh;
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .modal-backdrop { align-items: flex-end; padding: 0; }

  .btn, .nav-item { min-height: 44px; }
  input, select, textarea { font-size: 16px; }

  .toast {
    left: 1rem;
    right: 1rem;
    bottom: calc(1rem + env(safe-area-inset-bottom));
  }

  .table-wrap { margin: 0 -0.25rem; }
  th, td { padding: 0.6rem 0.5rem; font-size: 0.82rem; }

  .workflow-canvas { padding: 1rem; min-height: auto; }
  .workflow-node { display: block; margin: 0.35rem 0; }
}

@media (max-width: 400px) {
  .grid-4 { grid-template-columns: 1fr; }
  .login-card { padding: 1.75rem 1.25rem; margin: 0.5rem; }
}
