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

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-400: #9ca3af;
  --gray-600: #4b5563;
  --gray-800: #1f2937;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
}

body { font-family: system-ui, -apple-system, sans-serif; background: var(--gray-50); color: var(--gray-800); min-height: 100vh; }

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

/* Layout */
.page { display: none; }
.page.active { display: block; }

.container { max-width: 1100px; margin: 0 auto; padding: 0 16px; }

/* Navbar */
nav { background: #fff; border-bottom: 1px solid var(--gray-200); position: sticky; top: 0; z-index: 200; box-shadow: 0 2px 6px rgba(0,0,0,.06); }
.nav-inner { display: flex; align-items: center; justify-content: space-between; height: 56px; }
.nav-logo { font-weight: 700; font-size: 1.1rem; color: var(--primary); }
.nav-links { display: flex; gap: 8px; align-items: center; }
.nav-links button { background: none; border: none; cursor: pointer; padding: 6px 12px; border-radius: var(--radius); color: var(--gray-600); font-size: .9rem; }
.nav-links button:hover { background: var(--gray-100); }
#btn-logout { color: var(--danger); }
.nav-cart-btn { display: flex; align-items: center; gap: 6px; background: none; border: 1.5px solid var(--primary) !important; color: var(--primary) !important; font-weight: 600; padding: 6px 14px !important; border-radius: var(--radius) !important; cursor: pointer; font-size: .9rem; }
.nav-cart-btn:hover { background: var(--primary) !important; color: #fff !important; }
.nav-cart-btn .badge { background: var(--primary); color: #fff; border-radius: 10px; padding: 0 6px; font-size: .75rem; font-weight: 700; line-height: 1.5; display: inline-block; }

/* Cards */
.card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px; }

/* Auth */
.auth-wrap { min-height: calc(100vh - 57px); display: flex; align-items: center; justify-content: center; }
.auth-box { width: 100%; max-width: 400px; }
.auth-box h1 { font-size: 1.4rem; margin-bottom: 24px; }
.auth-step { display: none; }
.auth-step.active { display: block; }

/* Forms */
.form-group { margin-bottom: 16px; }
label { display: block; font-size: .85rem; font-weight: 500; color: var(--gray-600); margin-bottom: 4px; }
input[type="text"], input[type="email"], input[type="tel"], input[type="password"], input[type="number"], textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--gray-200); border-radius: var(--radius);
  font-size: 1rem; outline: none; transition: border .15s;
}
input:focus, textarea:focus { border-color: var(--primary); }
textarea { resize: vertical; min-height: 80px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 20px;
  border-radius: var(--radius); border: none; cursor: pointer; font-size: .95rem; font-weight: 500;
  transition: background .15s; width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline { background: #fff; color: var(--primary); border: 1px solid var(--primary); }
.btn-outline:hover { background: var(--gray-50); }
.btn-danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .6; cursor: not-allowed; }

.error-msg { color: var(--danger); font-size: .85rem; margin-top: 8px; min-height: 18px; }
.success-msg { color: var(--success); font-size: .85rem; margin-top: 8px; }

/* Products */
.toolbar { display: flex; gap: 12px; margin-bottom: 20px; align-items: center; flex-wrap: wrap; }
.search-input { flex: 1; min-width: 200px; }
.badge { background: var(--primary); color: #fff; border-radius: 12px; padding: 2px 8px; font-size: .75rem; font-weight: 600; }

/* Brand sidebar */
.brand-filter-section { padding-bottom: 12px; }
.brand-item { padding: 7px 12px; font-size: .85rem; color: var(--gray-700); cursor: pointer; border-radius: 6px; transition: background .15s; }
.brand-item:hover { background: var(--gray-100); }
.brand-item.active { background: var(--primary); color: #fff; font-weight: 600; }

.products-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.product-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px;
  display: flex; flex-direction: column; gap: 8px; }
.product-brand { font-size: .75rem; font-weight: 600; color: var(--primary); text-transform: uppercase; letter-spacing: .04em; }
.product-name { font-weight: 600; font-size: .95rem; cursor: default; }
.product-name.has-desc { cursor: help; text-decoration-line: underline; text-decoration-style: dotted; text-underline-offset: 3px; color: inherit; }
.product-code { color: var(--gray-400); font-size: .8rem; }
.product-desc { font-size: .82rem; color: var(--gray-600); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.product-price { font-size: 1.1rem; font-weight: 700; color: var(--primary); margin-top: auto; }
.product-unit { color: var(--gray-400); font-size: .8rem; }
.qty-row { display: flex; align-items: center; gap: 8px; margin-top: auto; }
.qty-row input { width: 60px; text-align: center; }
.btn-add { padding: 8px 14px; width: auto; font-size: .85rem; }

/* Cart */
.cart-panel { position: fixed; right: 0; top: 56px; bottom: 0; width: 360px; background: #fff;
  border-left: 1px solid var(--gray-200); transform: translateX(100%); transition: transform .25s;
  display: flex; flex-direction: column; z-index: 50; }
.cart-panel.open { transform: translateX(0); }
.cart-header { padding: 16px; border-bottom: 1px solid var(--gray-200); display: flex; justify-content: space-between; align-items: center; }
.cart-header h2 { font-size: 1rem; }
.cart-body { flex: 1; overflow-y: auto; padding: 12px; }
.cart-footer { padding: 16px; border-top: 1px solid var(--gray-200); }
.cart-item { display: flex; gap: 8px; align-items: flex-start; padding: 10px 0; border-bottom: 1px solid var(--gray-100); }
.cart-item-info { flex: 1; min-width: 0; }
.cart-item-name { font-size: .9rem; font-weight: 500; word-break: break-word; }
.cart-item-price { font-size: .85rem; color: var(--gray-600); }
.cart-item-controls { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.qty-btn { width: 28px; height: 28px; border: 1px solid var(--gray-200); background: var(--gray-50);
  border-radius: 4px; cursor: pointer; font-size: 1rem; line-height: 1; display: flex; align-items: center; justify-content: center; }
.qty-btn:hover { background: var(--gray-100); }
.qty-input { width: 48px; height: 28px; text-align: center; padding: 0 4px; font-size: .9rem;
  border: 1px solid var(--gray-200); border-radius: 4px; }
.cart-item-remove { background: none; border: none; color: var(--gray-400); cursor: pointer; font-size: 1.2rem;
  line-height: 1; padding: 0 2px; margin-left: 2px; }
.cart-item-remove:hover { color: var(--danger); }
.cart-total { font-size: 1rem; font-weight: 700; margin-bottom: 12px; }
.cart-empty { color: var(--gray-400); text-align: center; padding: 40px 0; }
.cart-toggle { position: relative; }
.cart-toggle .badge { position: absolute; top: -4px; right: -4px; }
.btn-sm { padding: 5px 12px; font-size: .8rem; width: auto; }

/* Toast notification */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--gray-800); color: #fff; padding: 10px 20px; border-radius: var(--radius);
  font-size: .9rem; opacity: 0; transition: opacity .2s, transform .2s; z-index: 300; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error { background: var(--danger); }

/* Orders history */
.orders-list { display: flex; flex-direction: column; gap: 16px; }
.order-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.order-meta { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.order-num { font-weight: 700; }
.status-badge { padding: 2px 10px; border-radius: 12px; font-size: .78rem; font-weight: 600; text-transform: uppercase; }
.status-pending { background: #fef3c7; color: #92400e; }
.status-sent_to_1c { background: #d1fae5; color: #065f46; }
.status-error { background: #fee2e2; color: #991b1b; }
.order-items-table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.order-items-table th { text-align: left; color: var(--gray-400); font-weight: 500; border-bottom: 1px solid var(--gray-100); padding: 4px 0; }
.order-items-table td { padding: 4px 0; }

/* Modals */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 200; align-items: center; justify-content: center; }
.modal-overlay.open { display: flex; }
.modal { background: #fff; border-radius: var(--radius); padding: 28px; width: 100%; max-width: 440px; margin: 16px;
  max-height: calc(100vh - 32px); overflow-y: auto; }
.modal h2 { margin-bottom: 16px; font-size: 1.1rem; }

/* Product images */
.product-img-wrap { position: relative; width: 100%; height: 160px; border-radius: 6px; background: var(--gray-100); margin-bottom: 8px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.product-img { width: 100%; height: 100%; object-fit: contain; }
.product-img-zoom { position: absolute; top: 6px; right: 6px; width: 28px; height: 28px;
  background: rgba(0,0,0,.45); border-radius: 50%; display: flex; align-items: center;
  justify-content: center; font-size: 14px; color: #fff; cursor: zoom-in;
  opacity: 0; transition: opacity .15s; z-index: 2; pointer-events: auto; }
.product-img-wrap:hover .product-img-zoom { opacity: 1; }
.product-img-placeholder { font-size: 2.5rem; color: var(--gray-400); user-select: none; }

/* Profile */
.profile-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }
.card-title { font-size: 1rem; font-weight: 600; margin-bottom: 16px; color: var(--gray-800); }
.profile-info { display: flex; flex-direction: column; gap: 12px; }
.profile-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--gray-100); font-size: .9rem; }
.profile-label { color: var(--gray-600); }
.discount-badge { background: #fef3c7; color: #92400e; padding: 2px 8px; border-radius: 12px; font-weight: 600; font-size: .8rem; }
.bonus-badge { background: #dcfce7; color: #166534; padding: 2px 10px; border-radius: 12px; font-weight: 600; font-size: .85rem; }
.price-original { text-decoration: line-through; color: var(--gray-400); font-size: .9rem; font-weight: 400; margin-right: 4px; }
.price-discounted { color: var(--primary); font-weight: 700; }
.order-items-table th, .order-items-table td { padding: 4px 6px; }
.nav-profile-btn { background: none; border: none; cursor: pointer; padding: 6px 12px; border-radius: var(--radius); color: var(--gray-600); font-size: .9rem; }
.nav-profile-btn:hover { background: var(--gray-100); }

/* Admin */
.admin-tabs { display: flex; gap: 4px; margin-bottom: 24px; border-bottom: 2px solid var(--gray-200); }
.admin-tab { background: none; border: none; cursor: pointer; padding: 10px 20px; font-size: .9rem;
  color: var(--gray-600); border-bottom: 2px solid transparent; margin-bottom: -2px; }
.admin-tab:hover { color: var(--gray-800); }
.admin-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.admin-tab-content { display: none; }
.admin-tab-content.active { display: block; }

.admin-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.admin-product-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px;
  display: flex; flex-direction: column; gap: 8px; }
.admin-card-img { width: 100%; height: 140px; object-fit: cover; border-radius: 4px; background: var(--gray-100);
  display: flex; align-items: center; justify-content: center; overflow: hidden; }
.admin-card-img img { width: 100%; height: 100%; object-fit: cover; }
.admin-card-img .no-img { font-size: 2rem; color: var(--gray-400); }
.admin-card-name { font-size: .82rem; font-weight: 500; line-height: 1.3; }
.admin-card-code { font-size: .75rem; color: var(--gray-400); }
.admin-card-actions { display: flex; gap: 6px; margin-top: auto; }
.admin-card-actions button { flex: 1; padding: 5px 8px; font-size: .78rem; border-radius: 4px;
  border: 1px solid var(--gray-200); background: #fff; cursor: pointer; }
.admin-card-actions button:hover { background: var(--gray-50); }
.admin-card-actions .btn-pick { border-color: var(--primary); color: var(--primary); }
.admin-card-actions .btn-del { border-color: var(--danger); color: var(--danger); }

/* Image import */
.import-file-label { display:block; border:2px dashed var(--gray-300); border-radius: var(--radius);
  padding: 32px 16px; text-align:center; cursor:pointer; transition: border-color .15s, background .15s; }
.import-file-label:hover { border-color: var(--primary); background: var(--gray-50); }
.import-drop-inner { display:flex; flex-direction:column; align-items:center; gap:8px;
  font-size: .9rem; color: var(--gray-500); pointer-events:none; }
.import-drop-icon { font-size: 2rem; }
.import-code { background: var(--gray-100); border-radius:6px; padding:10px 14px;
  font-size: .82rem; line-height:1.6; overflow-x:auto; }

/* Product popover */
.product-popover {
  position: fixed;
  z-index: 1200;
  background: #fff;
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,.18), 0 2px 8px rgba(0,0,0,.10);
  padding: 16px;
  width: 300px;
  pointer-events: none;
  opacity: 0;
  transition: opacity .15s;
}
.product-popover.visible { opacity: 1; pointer-events: auto; }
.product-popover-img-wrap { width: 100%; height: 150px; overflow: hidden; border-radius: 6px;
  background: var(--gray-100); margin-bottom: 10px; display: flex; align-items: center; justify-content: center; }
.product-popover-img-wrap img { width: 100%; height: 100%; object-fit: cover; }
.product-popover-img-wrap:empty { display: none; }
.product-popover-brand { font-size: .72rem; font-weight: 700; color: var(--primary); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 2px; }
.product-popover-brand:empty { display: none; }
.product-popover-name { font-weight: 700; font-size: .97rem; line-height: 1.35; margin-bottom: 4px; }
.product-popover-code { font-size: .78rem; color: var(--gray-400); margin-bottom: 6px; }
.product-popover-code:empty { display: none; }
.product-popover-desc { font-size: .84rem; color: var(--gray-700); line-height: 1.5; margin-bottom: 8px; max-height: 180px; overflow-y: auto; }
.product-popover-desc:empty { display: none; }
.product-popover-price { font-size: 1.1rem; font-weight: 700; color: var(--primary); }

.admin-cat-row { display: flex; align-items: center; padding: 5px 8px; border-radius: 6px; }
.admin-cat-row:hover { background: var(--gray-50); }
.admin-cat-label { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: .9rem; color: var(--gray-700); user-select: none; }
.admin-cat-label input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--primary); cursor: pointer; }
.admin-cat-name { line-height: 1.3; }

.img-upload-btn { display:inline-flex; align-items:center; gap:6px; padding:7px 14px;
  border:1px solid var(--gray-300); border-radius:6px; font-size:.85rem; font-weight:500;
  cursor:pointer; white-space:nowrap; background:#fff; transition:background .15s,border-color .15s; }
.img-upload-btn:hover { background:var(--gray-50); border-color:var(--primary); }

.img-candidates-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.img-candidate { position: relative; cursor: pointer; border-radius: 6px; overflow: hidden;
  border: 2px solid transparent; aspect-ratio: 1; background: var(--gray-100);
  display: flex; align-items: center; justify-content: center; }
.img-candidate:hover { border-color: var(--primary); }
.img-candidate img { width: 100%; height: 100%; object-fit: contain; display: block; }
.img-candidate--err { font-size: 1.5rem; color: var(--gray-400); }
.img-candidate-err { font-size: .75rem; color: var(--gray-400); text-align: center; }

/* Magnifier — fixed layer, positioned via JS */
.img-zoom-btn { position: fixed; width: 26px; height: 26px;
  background: rgba(0,0,0,.55); border-radius: 50%; display: none; align-items: center;
  justify-content: center; font-size: 13px; color: #fff; cursor: zoom-in;
  z-index: 1400; pointer-events: auto; transform: translate(-50%,-50%); }
.img-zoom-btn.visible { display: flex; }

/* Zoomed preview on magnifier area hover */
.img-zoom-preview { position: fixed; z-index: 1500; pointer-events: none; opacity: 0;
  transition: opacity .15s; border-radius: 8px; box-shadow: 0 8px 32px rgba(0,0,0,.35);
  background: #fff; overflow: hidden; }
.img-zoom-preview.visible { opacity: 1; }
.img-zoom-preview img { display: block; max-width: 400px; max-height: 400px;
  width: auto; height: auto; object-fit: contain; }

/* Catalog with sidebar */
.catalog-layout { display: flex; gap: 0; min-height: calc(100vh - 57px); }
.cat-sidebar { width: 240px; flex-shrink: 0; background: #fff; border-right: 1px solid var(--gray-200);
  overflow-y: auto; padding: 0; position: sticky; top: 56px; max-height: calc(100vh - 56px); }
.cat-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 10px;
  border-bottom: 1px solid var(--gray-100); font-size: .9rem; }
.cat-reset-btn { background: none; border: none; cursor: pointer; color: var(--primary); font-size: .8rem;
  padding: 0; text-decoration: underline; }
.catalog-main { flex: 1; min-width: 0; padding: 20px 20px 40px; }

/* Category tree */
.cat-item { display: block; width: 100%; text-align: left; background: none; border: none;
  cursor: pointer; padding: 7px 14px; font-size: .88rem; color: var(--gray-800);
  border-left: 3px solid transparent; transition: background .1s; }
.cat-item:hover { background: var(--gray-50); }
.cat-item.active { background: #eff6ff; border-left-color: var(--primary); color: var(--primary); font-weight: 600; }
.cat-children { padding-left: 10px; }
.cat-toggle { margin-right: 4px; font-size: .75rem; display: inline-block; width: 12px; }

/* Pagination */
.page-btn { padding: 6px 12px; border: 1px solid var(--gray-200); border-radius: var(--radius);
  background: #fff; cursor: pointer; font-size: .85rem; }
.page-btn:hover { background: var(--gray-50); }
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Misc */
.spinner { display: inline-block; width: 18px; height: 18px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; border-radius: 50%; animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.page-header { margin: 24px 0 20px; display: flex; align-items: center; gap: 12px; }
.page-header h1 { font-size: 1.3rem; }

/* ── Responsive ──────────────────────────────────────────────────────────── */

@media (max-width: 900px) {
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  .admin-grid    { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
  .img-candidates-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 768px) {
  /* Navbar */
  .nav-links { gap: 2px; }
  .nav-links button { padding: 6px 8px; font-size: .8rem; }

  /* Sidebar hidden by default on mobile, shown by toggle */
  .catalog-layout { flex-direction: column; }
  .cat-sidebar {
    width: 100%; border-right: none; border-bottom: 1px solid var(--gray-200);
    position: static; max-height: 0; overflow: hidden;
    transition: max-height .3s ease;
  }
  .cat-sidebar.open { max-height: 400px; overflow-y: auto; }
  .sidebar-toggle { display: flex !important; }
  .catalog-main { padding: 12px; }

  /* Cart full width */
  .cart-panel { width: 100%; top: 56px; }

  /* Admin tabs scroll */
  .admin-tabs { overflow-x: auto; flex-wrap: nowrap; padding-bottom: 2px; }
  .admin-tab   { white-space: nowrap; flex-shrink: 0; }

  /* Modal full screen */
  .modal { margin: 0; border-radius: 0; max-width: 100%; max-height: 100vh;
    width: 100%; min-height: 100vh; padding: 20px 16px; }
  .modal-overlay { align-items: flex-end; }

  /* Image grid 3 cols in modal */
  .img-candidates-grid { grid-template-columns: repeat(3, 1fr); }

  /* Profile */
  .profile-grid { grid-template-columns: 1fr; }

  /* Page header */
  .page-header { margin: 16px 0 12px; }
  .page-header h1 { font-size: 1.1rem; }

  /* Card padding */
  .card { padding: 16px; }

  /* Popover disabled on touch */
  .product-popover { display: none !important; }
}

@media (max-width: 480px) {
  /* Single column catalog */
  .products-grid { grid-template-columns: 1fr; gap: 12px; }

  /* Nav compact on very small screens */
  .nav-logo { font-size: .95rem; }
  #btn-nav-profile .nav-profile-name { display: none; }

  /* Auth box */
  .auth-wrap { align-items: flex-start; padding-top: 24px; }
  .auth-box { max-width: 100%; box-shadow: none; border-radius: 0; }

  /* Toolbar stacks */
  .toolbar { gap: 8px; }
  .search-input { min-width: 0; }

  /* Admin grid 2 cols */
  .admin-grid { grid-template-columns: 1fr 1fr; }

  /* Image modal 2 cols */
  .img-candidates-grid { grid-template-columns: repeat(2, 1fr); }

  /* Product card: compact image */
  .product-img-wrap { height: 130px; }

  /* Qty row wraps */
  .qty-row { flex-wrap: wrap; }
  .btn-add { flex: 1; min-width: 120px; }

  /* Orders */
  .order-meta { gap: 6px; }
}
