/* ── Reset & Base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:       #6366f1;
  --primary-dark:  #4f46e5;
  --primary-light: #e0e7ff;
  --success:       #22c55e;
  --danger:        #ef4444;
  --warning:       #f59e0b;
  --info:          #3b82f6;

  --bg:            #f8fafc;
  --surface:       #ffffff;
  --border:        #e2e8f0;
  --text:          #0f172a;
  --text-muted:    #64748b;
  --text-sm:       0.8125rem;

  --sidebar-w:     260px;
  --topbar-h:      60px;
  --radius:        10px;
  --radius-lg:     16px;
  --shadow:        0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --shadow-md:     0 4px 16px rgba(0,0,0,.1);
  --transition:    0.18s ease;
}

html { font-size: 16px; }
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

/* ── Auth Pages ──────────────────────────────────────────────────────────── */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center;
             background: linear-gradient(135deg, #6366f1 0%, #818cf8 50%, #c7d2fe 100%); }
.auth-container { width: 100%; max-width: 440px; padding: 1.5rem; }
.auth-card { background: var(--surface); border-radius: var(--radius-lg); padding: 2.5rem;
             box-shadow: var(--shadow-md); }
.auth-logo { text-align: center; margin-bottom: 2rem; }
.auth-logo h1 { font-size: 1.5rem; font-weight: 700; color: var(--text); margin-top: .5rem; }
.auth-logo p  { color: var(--text-muted); margin-top: .25rem; }
.logo-icon-lg { font-size: 3rem; display: block; }
.auth-form .form-group { margin-bottom: 1.25rem; }
.auth-footer-note { text-align: center; margin-top: 1.5rem; font-size: var(--text-sm);
                    color: var(--text-muted); }

/* ── Layout ──────────────────────────────────────────────────────────────── */
body.has-sidebar { display: flex; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: fixed;
  left: 0; top: 0;
  z-index: 200;
  transition: transform var(--transition);
}

.sidebar-header {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-logo { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1rem; }
.logo-icon { font-size: 1.4rem; }
.sidebar-close { display: none; background: none; border: none; font-size: 1.1rem; cursor: pointer; }

.sidebar-nav { flex: 1; padding: 1rem .75rem; overflow-y: auto; }
.nav-section { margin-bottom: 1.25rem; }
.nav-label { display: block; font-size: .7rem; font-weight: 600; text-transform: uppercase;
             letter-spacing: .08em; color: var(--text-muted); padding: 0 .5rem .5rem; }
.nav-link {
  display: flex; align-items: center; gap: .65rem;
  padding: .55rem .75rem;
  border-radius: 8px;
  color: var(--text-muted);
  font-size: .9rem;
  font-weight: 500;
  transition: all var(--transition);
  margin-bottom: 2px;
}
.nav-link:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.nav-link.active { background: var(--primary-light); color: var(--primary); }
.nav-icon { font-size: 1rem; width: 1.25rem; text-align: center; }

.sidebar-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: .75rem;
}
.user-info { display: flex; align-items: center; gap: .65rem; flex: 1; min-width: 0; }
.user-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .85rem; flex-shrink: 0;
}
.user-name { font-weight: 600; font-size: .875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: .75rem; color: var(--text-muted); }
.btn-logout {
  background: none; border: 1px solid var(--border); border-radius: 8px;
  width: 34px; height: 34px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text-muted); font-size: 1.1rem;
  transition: all var(--transition); flex-shrink: 0;
}
.btn-logout:hover { background: var(--danger); border-color: var(--danger); color: #fff; text-decoration: none; }

.sidebar-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.35);
  z-index: 199;
}

/* ── Layout Wrapper / Topbar ─────────────────────────────────────────────── */
.layout-wrapper {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-width: 0;
}
.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 1.5rem;
  gap: 1rem;
  position: sticky; top: 0; z-index: 100;
}
.menu-toggle { display: none; background: none; border: none; font-size: 1.25rem; cursor: pointer; }
.topbar-title { font-weight: 600; font-size: 1rem; flex: 1; }
.topbar-right { margin-left: auto; }
.main-content { flex: 1; padding: 1.75rem; max-width: 1280px; width: 100%; }

/* ── Badges / Pills ──────────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center;
  padding: .2rem .65rem; border-radius: 99px;
  font-size: .73rem; font-weight: 600; white-space: nowrap;
}
.badge-admin   { background: #ede9fe; color: #7c3aed; }
.badge-user    { background: #dbeafe; color: #1d4ed8; }
.badge-scrape  { background: #fef9c3; color: #92400e; }
.badge-crawl   { background: #d1fae5; color: #065f46; }
.badge-search  { background: #e0e7ff; color: #3730a3; }
.badge-role    { }
.badge-admin.badge-role { }
.badge-user.badge-role  { }
.status-pending   { background: #fef3c7; color: #b45309; }
.status-running   { background: #dbeafe; color: #1e40af; }
.status-completed { background: #d1fae5; color: #065f46; }
.status-failed    { background: #fee2e2; color: #991b1b; }
.status-active    { background: #d1fae5; color: #065f46; }
.status-suspended { background: #fee2e2; color: #991b1b; }

/* ── Alerts ──────────────────────────────────────────────────────────────── */
.alert {
  padding: .85rem 1rem;
  border-radius: var(--radius);
  display: flex; align-items: flex-start; gap: .75rem;
  margin-bottom: 1rem;
  font-size: .9rem;
  position: relative;
}
.alert-success { background: #f0fdf4; color: #15803d; border: 1px solid #bbf7d0; }
.alert-error   { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.alert-info    { background: #eff6ff; color: #1d4ed8; border: 1px solid #bfdbfe; }
.alert-close { margin-left: auto; background: none; border: none; cursor: pointer;
               font-size: 1rem; opacity: .6; flex-shrink: 0; }
.alert-close:hover { opacity: 1; }

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem;
}
.card-header h3 { font-size: 1rem; font-weight: 600; margin: 0; }
.card-body { padding: 1.25rem; }
.card-body.p-0 { padding: 0; }
.card-actions { display: flex; gap: .5rem; }
.mt-2 { margin-top: 1.25rem; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem 1.1rem;
  border-radius: 8px;
  font-size: .875rem; font-weight: 500;
  cursor: pointer; border: none;
  transition: all var(--transition);
  white-space: nowrap;
  text-decoration: none !important;
}
.btn:disabled { opacity: .55; pointer-events: none; }
.btn-primary  { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline  { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-danger   { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-block    { width: 100%; }
.btn-sm       { padding: .35rem .75rem; font-size: .8rem; }
.btn-xs       { padding: .2rem .55rem; font-size: .75rem; border-radius: 6px; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 1.1rem; }
.form-group label {
  display: block; margin-bottom: .4rem;
  font-size: .875rem; font-weight: 500; color: var(--text);
}
.form-control {
  width: 100%; padding: .6rem .875rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .9rem; color: var(--text);
  background: var(--surface);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
}
.form-control:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.15);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
             padding: 1.75rem; box-shadow: var(--shadow); max-width: 720px; }
.admin-form .form-actions { display: flex; gap: .75rem; margin-top: 1.5rem; }
.input-with-toggle { position: relative; }
.input-with-toggle .form-control { padding-right: 2.75rem; }
.toggle-pw {
  position: absolute; right: .75rem; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 1rem; opacity: .6;
}
.toggle-pw:hover { opacity: 1; }
.checkbox-group { display: flex; gap: 1.25rem; flex-wrap: wrap; }
.checkbox-group label { display: flex; align-items: center; gap: .4rem; font-weight: 400; margin: 0; }

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table { width: 100%; border-collapse: collapse; font-size: .875rem; }
.table th {
  padding: .7rem 1rem;
  text-align: left; font-weight: 600; font-size: .78rem; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-muted);
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.table td { padding: .75rem 1rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: #fafbff; }
.td-url { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-cell { display: flex; align-items: center; gap: .65rem; }
.avatar-sm {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--primary-light); color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .78rem; flex-shrink: 0;
}
.action-btns { display: flex; gap: .4rem; }
.fw-medium { font-weight: 500; }
.text-sm { font-size: var(--text-sm); }
.text-muted { color: var(--text-muted); }

/* ── Stats Grid ──────────────────────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1.5rem; }
.stats-grid-4 { grid-template-columns: repeat(4, 1fr); }
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  display: flex; align-items: center; gap: 1rem;
  box-shadow: var(--shadow);
}
.stat-icon { font-size: 1.75rem; }
.stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1; }
.stat-label { font-size: .8rem; color: var(--text-muted); margin-top: .2rem; }
.stat-blue   { border-left: 4px solid var(--info); }
.stat-green  { border-left: 4px solid var(--success); }
.stat-purple { border-left: 4px solid var(--primary); }
.stat-orange { border-left: 4px solid var(--warning); }

/* ── Page Header ─────────────────────────────────────────────────────────── */
.page-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  margin-bottom: 1.5rem; gap: 1rem; flex-wrap: wrap;
}
.page-header h2 { font-size: 1.4rem; font-weight: 700; }
.page-header p  { color: var(--text-muted); margin-top: .2rem; font-size: .9rem; }
.header-actions { display: flex; gap: .75rem; flex-shrink: 0; }

/* ── Quick Actions ───────────────────────────────────────────────────────── */
.quick-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1.5rem; }
.action-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.25rem;
  display: flex; align-items: center; gap: 1rem;
  transition: all var(--transition);
  text-decoration: none !important;
  color: var(--text);
  box-shadow: var(--shadow);
}
.action-card:hover { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.action-card strong { display: block; font-weight: 600; }
.action-card p { font-size: .82rem; color: var(--text-muted); margin: .15rem 0 0; }
.action-icon { font-size: 1.75rem; flex-shrink: 0; }

/* ── Tool Layout ─────────────────────────────────────────────────────────── */
.tool-layout { display: grid; grid-template-columns: 380px 1fr; gap: 1.25rem; align-items: start; }
.tool-panel { }
.result-panel { }

/* ── Tabs ────────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; padding-bottom: 0; }
.tab {
  padding: .5rem 1rem; background: none; border: none;
  font-size: .875rem; font-weight: 500; color: var(--text-muted);
  cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px;
  transition: all var(--transition);
}
.tab:hover  { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ── Code / Markdown Areas ───────────────────────────────────────────────── */
.code-area {
  width: 100%; min-height: 300px; max-height: 520px;
  padding: 1rem; overflow: auto;
  background: #f8fafc; border: 1px solid var(--border);
  border-radius: var(--radius); font-family: 'Fira Mono', 'Courier New', monospace;
  font-size: .82rem; line-height: 1.6; resize: vertical; white-space: pre-wrap;
}
.markdown-preview {
  min-height: 200px; max-height: 520px; overflow-y: auto;
  font-size: .9rem; line-height: 1.75;
  padding: .5rem 0;
  white-space: pre-wrap;
  color: var(--text);
}
.meta-bar { font-size: .8rem; color: var(--text-muted); margin-bottom: .75rem; }

/* ── Progress Bar ────────────────────────────────────────────────────────── */
.progress-wrap { margin-bottom: 1rem; }
.progress-bar-bg {
  height: 8px; background: var(--bg);
  border-radius: 99px; overflow: hidden;
  border: 1px solid var(--border); margin-bottom: .4rem;
}
.progress-bar-fill { height: 100%; background: var(--primary); border-radius: 99px; transition: width .4s ease; }
.badge-row { margin-bottom: 1rem; }
.pages-list { max-height: 280px; overflow-y: auto; font-size: .82rem; }
.pages-list a { display: block; padding: .3rem 0; border-bottom: 1px solid var(--border);
                color: var(--primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Search Results ──────────────────────────────────────────────────────── */
.results-header { font-size: .9rem; color: var(--text-muted); margin-bottom: 1rem; font-weight: 500; }
.result-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 1.25rem; margin-bottom: 1rem;
  box-shadow: var(--shadow);
}
.result-card-title { font-weight: 600; font-size: 1rem; color: var(--primary); margin-bottom: .3rem; }
.result-card-url   { font-size: .78rem; color: var(--text-muted); margin-bottom: .6rem; }
.result-card-body  { font-size: .875rem; line-height: 1.65; color: var(--text-muted); }

/* ── History Layout ──────────────────────────────────────────────────────── */
.history-layout { display: grid; grid-template-columns: 320px 1fr; gap: 1.25rem; align-items: start; }
.history-list { display: flex; flex-direction: column; gap: .5rem; }
.job-row {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: .85rem 1rem;
  cursor: pointer; transition: all var(--transition);
  text-decoration: none !important; color: var(--text); display: block;
}
.job-row:hover { border-color: var(--primary); background: #fafbff; }
.job-row.active { border-color: var(--primary); background: var(--primary-light); }
.job-row-top { display: flex; gap: .4rem; margin-bottom: .35rem; }
.job-row-url { font-size: .84rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.job-row-meta { font-size: .76rem; color: var(--text-muted); margin-top: .2rem; }
.history-results { }
.results-count { font-size: .85rem; color: var(--text-muted); font-weight: 500; margin-bottom: 1rem; }
.result-item {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem;
  margin-bottom: .75rem; box-shadow: var(--shadow);
}
.result-url { font-size: .8rem; color: var(--primary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-title { font-weight: 600; margin: .35rem 0 .5rem; }
.result-details summary { cursor: pointer; font-size: .84rem; color: var(--text-muted); }

/* ── Two-col grid ────────────────────────────────────────────────────────── */
.two-col-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; }

/* ── Empty States ────────────────────────────────────────────────────────── */
.empty-state {
  text-align: center; padding: 3rem 1rem;
  color: var(--text-muted); display: flex; flex-direction: column; align-items: center; gap: .75rem;
}
.empty-state span { font-size: 2.5rem; }
.empty-panel {
  text-align: center; padding: 4rem 1rem;
  color: var(--text-muted); display: flex; flex-direction: column; align-items: center; gap: .75rem;
}
.empty-panel span { font-size: 3rem; }

/* ── Search form ─────────────────────────────────────────────────────────── */
.search-form { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.search-input { max-width: 320px; }

/* ── Admin form ──────────────────────────────────────────────────────────── */
/* ── Spacing utilities ───────────────────────────────────────────────────── */
.mt-1 { margin-top: .75rem; }
.mt-2 { margin-top: 1.25rem; }
.mt-3 { margin-top: 2rem; }
.mb-1 { margin-bottom: .75rem; }
.mb-2 { margin-bottom: 1.25rem; }
.mb-3 { margin-bottom: 2rem; }

/* ── Misc helpers ────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 1100px) {
  .stats-grid-4 { grid-template-columns: repeat(2, 1fr); }
  .two-col-grid { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .tool-layout { grid-template-columns: 1fr; }
  .history-layout { grid-template-columns: 1fr; }
  .quick-actions { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    box-shadow: var(--shadow-md);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: block; }
  .sidebar-overlay.open { display: block; }
  .layout-wrapper { margin-left: 0; }
  .menu-toggle { display: block; }
  .form-row { grid-template-columns: 1fr; }
  .main-content { padding: 1rem; }
  .stats-grid { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .auth-card { padding: 1.75rem 1.25rem; }
  .page-header { flex-direction: column; }
}