:root {
  --primary: #1a56db;
  --primary-dark: #1e429f;
  --success: #0e9f6e;
  --warning: #c27803;
  --danger: #e02424;
  --sidebar-w: 240px;
  --bg: #f3f4f6;
  --card: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;
}

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

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

/* ── Layout ─────────────────────────────────── */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: #1e2939;
  color: #d1d5db;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}

.sidebar-logo {
  padding: 20px 16px 16px;
  border-bottom: 1px solid #374151;
  display: flex; align-items: center; gap: 10px;
}

.sidebar-logo .logo-icon {
  width: 36px; height: 36px;
  background: var(--primary);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; color: #fff;
}

.sidebar-logo span { font-weight: 700; font-size: 15px; color: #f9fafb; }
.sidebar-logo small { display: block; font-size: 11px; color: #9ca3af; }

.sidebar-nav { flex: 1; padding: 12px 0; overflow-y: auto; }

.nav-section { padding: 8px 16px 4px; font-size: 10px; text-transform: uppercase;
  letter-spacing: .08em; color: #6b7280; font-weight: 600; }

.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 16px; color: #d1d5db; text-decoration: none;
  border-radius: 0; transition: background .15s;
  font-size: 13.5px;
}

.nav-link:hover { background: #374151; color: #f9fafb; }
.nav-link.active { background: var(--primary); color: #fff; }
.nav-link .icon { width: 18px; text-align: center; }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid #374151;
  font-size: 12px;
}

.user-badge { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.user-badge .avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px;
}

.main { margin-left: var(--sidebar-w); flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 50;
}

.topbar h1 { font-size: 17px; font-weight: 600; }

.content { padding: 24px; flex: 1; }

/* ── Cards & Tables ─────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

.card-title {
  font-size: 15px; font-weight: 600;
  margin-bottom: 16px;
  display: flex; align-items: center; justify-content: space-between;
}

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th { background: #f9fafb; text-align: left; padding: 10px 12px; font-weight: 600;
  border-bottom: 2px solid var(--border); white-space: nowrap; }
td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f9fafb; }

/* ── Badges / Status ────────────────────────── */
.badge {
  display: inline-flex; align-items: center;
  padding: 2px 8px; border-radius: 12px;
  font-size: 11.5px; font-weight: 600; white-space: nowrap;
}

.badge-green { background: #d1fae5; color: #065f46; }
.badge-blue { background: #dbeafe; color: #1e40af; }
.badge-yellow { background: #fef3c7; color: #92400e; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-gray { background: #f3f4f6; color: #374151; }
.badge-purple { background: #ede9fe; color: #5b21b6; }

/* ── Buttons ────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: 7px;
  font-size: 13px; font-weight: 500; cursor: pointer;
  border: none; text-decoration: none; transition: opacity .15s;
  white-space: nowrap;
}
.btn:hover { opacity: .88; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-warning { background: #fbbf24; color: #78350f; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-light { background: #f3f4f6; color: var(--text); border: 1px solid var(--border); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px; }

/* ── Forms ──────────────────────────────────── */
.form-group { margin-bottom: 14px; }
label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 5px; color: #374151; }
input, select, textarea {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 7px;
  font-size: 13.5px; font-family: inherit;
  background: #fff; color: var(--text);
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26,86,219,.12);
}
textarea { resize: vertical; min-height: 70px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }

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

.modal {
  background: var(--card);
  border-radius: 12px;
  width: 100%; max-width: 560px;
  max-height: 90vh; overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal-lg { max-width: 740px; }

.modal-header {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h2 { font-size: 16px; font-weight: 600; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-muted); line-height: 1; }

.modal-body { padding: 20px; }

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}

/* ── Stats grid ─────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 20px; }

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px; padding: 18px;
  display: flex; flex-direction: column; gap: 4px;
}
.stat-card .value { font-size: 28px; font-weight: 700; color: var(--primary); }
.stat-card .label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }

/* ── Alert ──────────────────────────────────── */
.alert {
  padding: 10px 14px; border-radius: 8px;
  font-size: 13px; margin-bottom: 14px;
  display: flex; align-items: center; gap: 8px;
}
.alert-error { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
.alert-success { background: #d1fae5; color: #065f46; border: 1px solid #6ee7b7; }
.alert-info { background: #dbeafe; color: #1e40af; border: 1px solid #93c5fd; }
.alert.hidden { display: none; }

/* ── Scan page ──────────────────────────────── */
.scan-container {
  max-width: 480px; margin: 0 auto; text-align: center;
}

#scan-video-wrap {
  position: relative; border-radius: 12px; overflow: hidden;
  background: #000; margin-bottom: 16px;
}

#scan-video { width: 100%; display: block; max-height: 320px; object-fit: cover; }

.scan-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}

.scan-frame {
  width: 200px; height: 200px;
  border: 3px solid rgba(255,255,255,.7);
  border-radius: 12px;
}

.scan-result-card {
  background: var(--card);
  border: 2px solid var(--success);
  border-radius: 10px; padding: 18px;
  text-align: left; margin-top: 12px;
}

/* ── Article tree ───────────────────────────── */
.article-tree .parent-row td:first-child { font-weight: 600; }
.article-tree .child-row td:first-child { padding-left: 32px; }
.article-tree .child-row td:first-child::before {
  content: '↳ '; color: var(--text-muted);
}

/* ── QR popup ───────────────────────────────── */
.qr-preview { text-align: center; }
.qr-preview img { max-width: 280px; border: 1px solid var(--border); border-radius: 8px; padding: 12px; }
.qr-preview p { margin-top: 10px; font-weight: 600; font-size: 14px; }

/* ── Mobile responsive ──────────────────────────────────────────────────────── */

.menu-toggle {
  display: none;
  background: none; border: none;
  font-size: 22px; cursor: pointer;
  padding: 4px 8px; border-radius: 6px;
  color: var(--text); line-height: 1;
}

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

@media (max-width: 768px) {
  .menu-toggle { display: flex; align-items: center; }

  .sidebar {
    transform: translateX(-100%);
    transition: transform .25s ease;
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-overlay { display: block; }

  .main { margin-left: 0; }

  .content { padding: 14px; }

  .topbar { padding: 0 14px; }

  .form-row,
  .form-row-3 { grid-template-columns: 1fr; }

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

  /* Dashboard two-col → one-col */
  .content > div[style*="grid-template-columns:1fr 1fr"],
  .content > div[style*="grid-template-columns: 1fr 1fr"] {
    display: block;
  }
  .content > div[style*="grid-template-columns:1fr 1fr"] > .card,
  .content > div[style*="grid-template-columns: 1fr 1fr"] > .card {
    margin-bottom: 16px;
  }

  /* Scan page two-col → one-col */
  .content > div[style*="grid-template-columns:1fr 1fr;gap:20px"],
  .content > div[style*="grid-template-columns: 1fr 1fr; gap: 20px"] {
    display: block;
  }

  .modal { max-width: 100% !important; margin: 0; border-radius: 12px 12px 0 0; }
  .modal-backdrop { align-items: flex-end; padding: 0; }

  .btn { font-size: 13px; }
  td, th { padding: 8px 8px; }

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

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .topbar h1 { font-size: 15px; }
}
.text-muted { color: var(--text-muted); }
.text-right { text-align: right; }
.flex { display: flex; align-items: center; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.mt-1 { margin-top: 6px; }
.mt-2 { margin-top: 12px; }
.mb-0 { margin-bottom: 0; }
.w-full { width: 100%; }
.hidden { display: none !important; }

/* ── Login page ─────────────────────────────── */
body.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: #1e2939;
}
.login-box {
  background: var(--card); border-radius: 14px;
  padding: 36px 32px; width: 360px; max-width: 95vw;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.login-logo {
  text-align: center; margin-bottom: 28px;
}
.login-logo .icon {
  width: 52px; height: 52px; background: var(--primary);
  border-radius: 12px; display: inline-flex;
  align-items: center; justify-content: center;
  font-size: 26px; color: #fff; margin-bottom: 10px;
}
.login-logo h1 { font-size: 20px; font-weight: 700; }
.login-logo p { font-size: 13px; color: var(--text-muted); }

/* Logo image in sidebar */
.sidebar-logo img { height: 36px; width: auto; }
.sidebar-logo .logo-icon { background: transparent; padding: 0; }

/* Project items drag-and-drop */
.project-item-row { cursor: default; transition: background .1s; }
.project-item-row.dragging { opacity: .4; }
.project-item-row.drag-over { border-top: 2px solid var(--primary); }
.drag-handle { cursor: grab; color: var(--text-muted); padding: 0 6px; font-size: 16px; line-height:1; }
.drag-handle:active { cursor: grabbing; }
.header-row td { background: #f0f4ff; font-weight: 700; color: #1e40af; font-size: 13px; border-top: 2px solid #bfdbfe; }

/* Scan overlay popup */
.scan-popup {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: #1e2939; color: #fff; border-radius: 12px;
  padding: 14px 22px; min-width: 280px; max-width: 90vw;
  box-shadow: 0 8px 32px rgba(0,0,0,.35);
  z-index: 9000; text-align: center;
  animation: slideUp .25s ease;
}
.scan-popup.success { border-top: 3px solid #0e9f6e; }
.scan-popup.error { border-top: 3px solid #e02424; }
.scan-popup-title { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.scan-popup-sub { font-size: 12px; color: #9ca3af; }
@keyframes slideUp { from { bottom: 60px; opacity: 0; } to { bottom: 80px; opacity: 1; } }

/* badge-orange */
.badge-orange { background: #fff3e0; color: #e65100; }

/* Project parent/child rows */
.project-parent-row { background: #f0f5ff; }
.project-parent-row td { font-size: 13.5px; }
.project-child-row { background: #fafafa; }
.project-child-row td { font-size: 13px; color: #374151; }
