/* ── Variables ── */
:root {
  --bg: #f4f6f9;
  --sidebar-bg: #0f172a;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #2563eb;
  --sidebar-active-bg: rgba(37, 99, 235, 0.12);
  --sidebar-hover-bg: rgba(255, 255, 255, 0.05);
  --card: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --success: #16a34a;
  --success-light: #f0fdf4;
  --warning: #d97706;
  --sidebar-width: 240px;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

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

/* ── LOGIN SCREEN ── */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 1rem;
}
.login-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.07);
}
.login-brand {
  text-align: center;
  margin-bottom: 2rem;
}
.login-logo {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: var(--primary);
  color: #fff;
  font-size: 1.4rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1rem;
}
.login-brand h1 {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text);
}
.login-brand p {
  color: var(--muted);
  font-size: 0.875rem;
  margin-top: 0.2rem;
}
.login-body {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ── APP SHELL ── */
.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ── SIDEBAR ── */
.sidebar {
  width: var(--sidebar-width);
  min-height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.25rem 1rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,0.07);
  margin-bottom: 0.5rem;
}
.sidebar-logo {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.sidebar-name {
  color: #f1f5f9;
  font-weight: 600;
  font-size: 0.9rem;
}
.sidebar-role {
  color: #64748b;
  font-size: 0.75rem;
}
.sidebar-nav {
  flex: 1;
  padding: 0.5rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  border: none;
  background: transparent;
  color: var(--sidebar-text);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  width: 100%;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}
.nav-item:hover {
  background: var(--sidebar-hover-bg);
  color: #f1f5f9;
}
.nav-item.active {
  background: var(--sidebar-active-bg);
  color: #93c5fd;
  font-weight: 600;
}
.nav-icon {
  font-size: 1rem;
  opacity: 0.7;
  flex-shrink: 0;
}
.sidebar-footer {
  padding: 1rem;
  border-top: 1px solid rgba(255,255,255,0.07);
}
.sidebar-user {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.75rem;
}
.sidebar-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #334155;
  color: #94a3b8;
  font-size: 0.8rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.sidebar-user-email {
  color: #94a3b8;
  font-size: 0.78rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 130px;
}
.sidebar-user-role {
  color: #475569;
  font-size: 0.7rem;
}
.sidebar-user-info { overflow: hidden; }

/* ── MAIN CONTENT ── */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-height: 100vh;
  padding: 0;
  display: flex;
  flex-direction: column;
}
.main-topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
}
.page-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
}
.page-subtitle {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.1rem;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.top-status-bar {
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.3rem 0.7rem;
  border-radius: 8px;
  min-width: 1px;
}
.top-status-bar.ok { background: var(--success-light); color: var(--success); }
.top-status-bar.error { background: var(--danger-light); color: var(--danger); }

/* ── SECTIONS ── */
.section {
  padding: 1.5rem;
  flex: 1;
}

/* ── CARDS ── */
.section-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
}
.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.card-head h3 {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
}
.card-sub {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

/* ── ANALYTICS ── */
.analytics-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.metric {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.1rem 1.25rem;
}
.metric .k {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.metric .v {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text);
  margin-top: 0.25rem;
}
.quick-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0;
}
.qs-item {
  padding: 0.9rem 1.25rem;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.qs-item:nth-child(even) { border-right: none; }
.qs-item:nth-last-child(-n+2) { border-bottom: none; }
.qs-label { font-size: 0.84rem; color: var(--muted); }
.qs-val { font-size: 0.95rem; font-weight: 700; }

/* ── FORM FIELDS ── */
.field-label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--muted);
}
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0.55rem 0.7rem;
  font-size: 0.875rem;
  font-family: inherit;
  background: #fff;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
  outline: none;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
textarea { min-height: 64px; resize: vertical; line-height: 1.4; }
input[type="checkbox"] {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  cursor: pointer;
  accent-color: var(--primary);
}

/* ── BUTTONS ── */
.btn {
  border: none;
  border-radius: 9px;
  padding: 0.5rem 0.9rem;
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.secondary { background: #eff6ff; color: #1e40af; }
.btn.secondary:hover { background: #dbeafe; }
.btn.danger { background: var(--danger-light); color: #991b1b; }
.btn.danger:hover { background: #fecaca; }
.btn.small { padding: 0.35rem 0.65rem; font-size: 0.78rem; }
.btn.full { width: 100%; padding: 0.7rem; font-size: 0.9rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-group { display: flex; gap: 0.5rem; align-items: center; }
.head-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.filter-label { font-size: 0.82rem; color: var(--muted); white-space: nowrap; }
.filter-select { width: auto; min-width: 12rem; }

/* ── STATUS ── */
.status {
  font-size: 0.83rem;
  font-weight: 500;
  text-align: center;
  min-height: 1.2em;
}
.status.ok { color: var(--success); }
.status.error { color: var(--danger); }
.muted { color: var(--muted); font-size: 0.82rem; }

/* ── TABLE ── */
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
}
thead tr {
  background: #f8fafc;
  border-bottom: 2px solid var(--border);
}
th {
  padding: 0.65rem 0.85rem;
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  text-align: left;
  white-space: nowrap;
  position: sticky;
  top: 0;
  background: #f8fafc;
  z-index: 1;
}
td {
  padding: 0.5rem 0.85rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  font-size: 0.86rem;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #fafbfc; }

/* Column widths */
td.col-id { width: 48px; color: var(--muted); font-size: 0.78rem; font-variant-numeric: tabular-nums; }
td.col-check { width: 56px; text-align: center; }
td.col-num { width: 80px; }
td.col-act { width: 90px; }
td.col-wide input, td.col-wide textarea { min-width: 180px; }

/* Inline table inputs */
td input[type="text"],
td input:not([type="checkbox"]):not([type="number"]),
td textarea {
  border-radius: 7px;
  padding: 0.4rem 0.55rem;
  font-size: 0.83rem;
  border-color: transparent;
  background: #f8fafc;
}
td input:focus, td textarea:focus {
  background: #fff;
  border-color: var(--primary);
}
td input[type="number"] {
  border-radius: 7px;
  padding: 0.4rem 0.5rem;
  font-size: 0.83rem;
  background: #f8fafc;
  border-color: transparent;
  width: 70px;
}
td input[type="number"]:focus {
  background: #fff;
  border-color: var(--primary);
}
.row-actions { display: flex; gap: 0.3rem; }

/* ── IMAGE CELL ── */
.img-cell {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  min-width: 200px;
}
.img-preview-wrap {
  position: relative;
  width: 100%;
  height: 72px;
  border-radius: 8px;
  overflow: hidden;
  background: #f1f5f9;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
}
.img-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.img-preview.empty { display: none; }
.img-empty-state {
  font-size: 0.72rem;
  color: #94a3b8;
  text-align: center;
  padding: 0.5rem;
}
.img-empty-state.hidden { display: none; }
.img-actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}
.upload-progress {
  font-size: 0.75rem;
  color: var(--primary);
  font-weight: 500;
}
.img-fallback-note {
  font-size: 0.7rem;
  color: var(--warning);
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 5px;
  padding: 0.2rem 0.45rem;
}

/* ── RESPONSIVE ── */
@media (max-width: 1024px) {
  .analytics-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 768px) {
  .sidebar { width: 100%; min-height: auto; position: relative; }
  .main-content { margin-left: 0; }
  .analytics-grid { grid-template-columns: 1fr 1fr; }
  .quick-stats { grid-template-columns: 1fr; }
  .qs-item { border-right: none; }
  .qs-item:nth-last-child(-n+2) { border-bottom: 1px solid var(--border); }
  .qs-item:last-child { border-bottom: none; }
}

/* ── Image library picker modal ── */
.image-library-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.image-library-modal.hidden {
  display: none;
}
.image-library-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}
.image-library-dialog {
  position: relative;
  background: var(--card);
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  width: 100%;
  max-width: 720px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.image-library-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.image-library-head h3 {
  font-size: 1rem;
  font-weight: 600;
}
.image-library-close {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  border-radius: 8px;
}
.image-library-close:hover {
  background: #f1f5f9;
  color: var(--text);
}
.image-library-search-wrap {
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.image-library-search {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.image-library-search:focus {
  outline: none;
  border-color: var(--primary);
}
.image-library-grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 6.75rem;
  align-content: start;
}
.image-library-item {
  position: relative;
  aspect-ratio: 1;
  min-width: 0;
  min-height: 0;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
  background: #f1f5f9;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.image-library-item:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
}
.image-library-item img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.image-library-item .image-library-path {
  font-size: 0.65rem;
  padding: 0.25rem;
  word-break: break-all;
  text-align: center;
  color: var(--muted);
  line-height: 1.2;
}
.image-library-empty {
  grid-column: 1 / -1;
  text-align: center;
  padding: 2rem;
  color: var(--muted);
}
