:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --border: #dde3ea;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --danger: #dc2626;
  --danger-contrast: #ffffff;
  --input-bg: #ffffff;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.layout { display: flex; min-height: 100vh; }

.menu-toggle, .menu-backdrop { display: none; }

.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sidebar h1 { font-size: 18px; margin: 0 0 12px; }

.nav-btn {
  text-align: left;
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 10px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.nav-btn:hover { background: #eef1f6; color: var(--text); }
.nav-btn.active { background: var(--accent); color: var(--accent-contrast); font-weight: 600; }
.sidebar-settings { display: flex; flex-direction: column; gap: 2px; }
.nav-sub-btn { background: transparent; border: 0; color: var(--muted); cursor: pointer; font-size: 12px; padding: 7px 12px 7px 24px; text-align: left; }
.nav-sub-btn:hover, .nav-sub-btn.active { color: var(--accent); font-weight: 600; }

.content { flex: 1; padding: 24px 32px; overflow-x: auto; }
.login-overlay { align-items: center; background: rgba(31, 41, 55, 0.72); display: flex; inset: 0; justify-content: center; position: fixed; z-index: 100; }
.login-overlay.hidden { display: none; }
.login-panel { background: var(--panel); border-radius: 8px; display: grid; gap: 14px; max-width: 380px; padding: 24px; width: calc(100% - 32px); }
.login-panel h2 { margin: 0; }
.error-text { color: var(--danger); font-size: 13px; min-height: 18px; }
.stock-check-list { display: flex; flex-direction: column; gap: 8px; }
.stock-check-row { align-items: center; border-top: 1px solid var(--border); display: grid; gap: 10px; grid-template-columns: minmax(240px, 1fr) 110px auto; padding-top: 8px; }
.stock-check-progress { margin-bottom: 12px; }

.search-bar { display: flex; gap: 8px; margin-bottom: 20px; max-width: 520px; }
.search-bar input { flex: 1; }

.tab.hidden { display: none; }

.tab-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.tab-header h2 { margin: 0; }
.pagination { align-items: center; display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.pagination-page { min-width: 78px; text-align: center; }
.pagination-size-label { align-items: center; color: var(--muted); display: flex; font-size: 13px; gap: 6px; }
.pagination-size { padding: 5px 8px; }
.items-layout { align-items: start; display: grid; gap: 18px; grid-template-columns: minmax(560px, 1fr) 230px; }
.item-list-content { min-width: 0; }
.item-category-filter { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 12px; position: sticky; top: 16px; }
.item-category-filter-header { align-items: center; display: flex; justify-content: space-between; margin-bottom: 10px; }
.item-category-filter-header h3 { font-size: 14px; margin: 0; }
.item-category-options { display: flex; flex-direction: column; gap: 7px; }
.item-category-option { overflow-wrap: anywhere; }
.item-stock-metrics { border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; margin-top: 14px; padding-top: 14px; }
.item-stock-metrics h3 { font-size: 14px; margin: 0 0 2px; }
.item-stock-metric { display: flex; font-size: 13px; justify-content: space-between; gap: 10px; }
.item-stock-metric span { color: var(--muted); }
.item-stock-metric.resale-value strong { color: #15803d; }
.item-list-image { width: 84px; }
.item-list-details { min-width: 250px; }
.item-list-model { font-size: 15px; font-weight: 700; text-align: left; white-space: normal; }
.item-list-meta { color: var(--muted); font-size: 12px; line-height: 1.5; margin-top: 2px; }
.item-list-stock, .item-list-price { min-width: 94px; }
.item-list-stock strong, .item-list-price strong { display: block; font-size: 16px; }
.item-list-stock span, .item-list-price span { color: var(--muted); display: block; font-size: 12px; line-height: 1.5; }
.item-list-reserved { color: #92400e !important; }
.sortable-heading { background: transparent; color: var(--muted); font: inherit; padding: 0; }
.sortable-heading:hover { color: var(--accent); opacity: 1; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; font-weight: 600; margin-bottom: 6px; }
.dashboard-trend { margin-bottom: 18px; width: 100%; }
.dashboard-trend .chart-wrap { width: 100%; }
.interactive-chart { position: relative; }
.chart-tooltip { background: #1f2937; border-radius: 4px; color: #ffffff; font-size: 12px; max-width: 210px; padding: 6px 8px; pointer-events: none; position: absolute; z-index: 2; }
.chart-tooltip.hidden { display: none; }
.chart-point-active { r: 5; stroke-width: 3; }
.insight-positive { color: #15803d; }
.item-insight-chart { border-bottom: 1px solid var(--border); margin-bottom: 8px; padding-bottom: 8px; text-align: center; }
.item-insight-chart .muted { font-size: 12px; }

h2 { margin-top: 0; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

label.field-label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); flex: 1; min-width: 160px; }
label.field-label.full-span { grid-column: 1 / -1; }
label.field-label.billing-field { margin: 10px 0; }

input, textarea, select {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}

textarea { width: 100%; min-height: 50px; font-family: monospace; resize: vertical; }

.checkbox { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.checkbox-list { display: flex; flex-wrap: wrap; gap: 6px 16px; border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; }

button {
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  padding: 8px 14px;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

button.secondary { background: #eef1f6; color: var(--text); border: 1px solid var(--border); }
button.reverse { background: #fff7ed; color: #9a3412; border: 1px solid #fed7aa; }
button.danger { background: var(--danger); color: var(--danger-contrast); }
button.small { padding: 5px 10px; font-size: 12px; }
button:hover { opacity: 0.9; }

.icon-btn {
  background: transparent;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  padding: 4px 8px;
}

table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: 8px; overflow: hidden; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 14px; }
th { color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
td.col-actions, th.col-actions { text-align: right; white-space: nowrap; }
td.col-actions button { margin-left: 6px; }
.muted { color: var(--muted); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; background: #eef1f6; border: 1px solid var(--border); }
.badge.status-early { background: #f3f4f6; border-color: #d1d5db; color: #374151; }
.badge.status-progress { background: #fef3c7; border-color: #fde68a; color: #92400e; }
.badge.status-complete { background: #dcfce7; border-color: #bbf7d0; color: #166534; }

.trackers-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; margin: 0 0 16px; }
.tracker-panel { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px; }
.tracker-title { color: var(--muted); font-size: 12px; font-weight: 700; letter-spacing: 0.04em; margin-bottom: 10px; text-transform: uppercase; }
.tracker { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.tracker-step { align-items: center; color: var(--muted); display: flex; font-size: 13px; gap: 8px; min-width: 0; }
.tracker-dot { align-items: center; background: #eef1f6; border: 1px solid var(--border); border-radius: 999px; display: inline-flex; flex: 0 0 24px; height: 24px; justify-content: center; font-size: 12px; }
.tracker-step.done { color: var(--text); }
.tracker-step.done .tracker-dot { background: var(--primary); border-color: var(--primary); color: #fff; }
.tracker-step.active { font-weight: 700; }
.action-row { flex-wrap: wrap; margin-top: 12px; }

.toast {
  position: fixed;
  top: 16px;
  right: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 14px;
  z-index: 30;
}

.toast.error { border-color: var(--danger); color: var(--danger); }
.toast.hidden { display: none; }

/* ---- Modal ---- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 20;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  max-width: 640px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 { margin: 0; font-size: 16px; }

.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; max-height: 70vh; overflow-y: auto; }

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* ---- Dynamic custom fields / line builders ---- */

.field-grid { display: flex; flex-direction: column; gap: 12px; }

.subsection {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.subsection-title { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }

.builder-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
}

.builder-row select, .builder-row input { flex: 1; min-width: 100px; }
.builder-row .remove-row { background: var(--danger); color: var(--danger-contrast); padding: 6px 10px; }

/* ---- Searchable item picker ---- */

.item-search-wrapper { position: relative; flex: 2; min-width: 220px; }
.item-search-input { width: 100%; }

.item-search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  max-height: 220px;
  overflow-y: auto;
  z-index: 15;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.12);
}

.item-search-results.hidden { display: none; }

.item-search-result { padding: 8px 10px; cursor: pointer; font-size: 14px; }
.item-search-result:hover { background: var(--bg); }
.item-search-empty { padding: 8px 10px; color: var(--muted); font-size: 13px; }

.empty-state { color: var(--muted); font-size: 13px; padding: 8px 0; }

/* ---- Images ---- */

.thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); }
.thumb-placeholder { width: 40px; height: 40px; border-radius: 6px; border: 1px dashed var(--border); display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 10px; }

.image-picker { display: flex; flex-direction: column; gap: 10px; }
.image-preview-grid { display: flex; flex-wrap: wrap; gap: 10px; }
.image-preview {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.image-preview img { width: 100%; height: 100%; object-fit: cover; display: block; }
.image-preview .remove-image {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(15, 23, 42, 0.65);
  color: #ffffff;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 12px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
}

.search-results { display: flex; flex-direction: column; gap: 20px; }
.search-results h4 { margin: 0 0 8px; color: var(--muted); font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; }

/* ---- Detail pages ---- */

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
  font-weight: 600;
  text-align: left;
  font-size: inherit;
}
.link-btn:hover { text-decoration: underline; }

.breadcrumb { display: flex; align-items: center; gap: 6px; margin-bottom: 16px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.breadcrumb .link-btn { color: var(--muted); font-weight: 400; }
.breadcrumb .current { color: var(--text); font-weight: 600; }

.detail-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 12px; flex-wrap: wrap; }
.detail-header h2 { margin: 0; }

.detail-grid { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(260px, 0.65fr); gap: 24px; align-items: start; }
@media (max-width: 900px) { .detail-grid { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
}
.panel h3 { margin: 0 0 12px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }

.dashboard-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.dashboard-number { font-size: 28px; font-weight: 800; }
.chart-wrap { width: 100%; overflow: hidden; }

.stat-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.stat-row:last-child { border-bottom: none; }
.stat-row .stat-label { color: var(--muted); }

.line-table-wrap { overflow-x: auto; }
.order-lines-table th, .order-lines-table td { vertical-align: top; }
.order-lines-table th:nth-child(2), .order-lines-table td:nth-child(2),
.order-lines-table th:nth-child(3), .order-lines-table td:nth-child(3),
.order-lines-table th:nth-child(4), .order-lines-table td:nth-child(4) { text-align: right; }
.line-title { font-weight: 700; }
.shipping-line td { background: #f8fafc; }
.shipment-chip { background: #f8fafc; border: 1px solid var(--border); border-radius: 7px; display: inline-flex; flex-direction: column; min-width: 80px; padding: 6px 10px; text-align: center; }
.shipment-chip small { color: var(--muted); font-size: 11px; font-weight: 700; margin-top: 2px; }
.shipment-chip.partial { background: #fff7ed; border-color: #fed7aa; }
.shipment-chip.shipped { background: #ecfdf3; border-color: #bbf7d0; }
.shipment-chip.shipped small { color: #16a34a; }
.order-total-box { border-top: 2px solid var(--border); margin-left: auto; margin-top: 8px; max-width: 280px; padding-top: 8px; }
.order-total-box > div { display: flex; justify-content: space-between; padding: 6px 0; }
.order-total-box .grand-total { font-size: 18px; }

.item-list-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.item-list-row:last-child { border-bottom: none; }
.item-list-row .link-btn { flex: 1; }

/* ---- Packages ---- */

.package-list { display: flex; flex-direction: column; gap: 12px; margin-bottom: 16px; }

.package-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  background: var(--bg);
}

.package-card-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.package-card-title { font-weight: 600; font-size: 15px; }

.package-contents { margin: 0; padding-left: 18px; font-size: 14px; }
.package-contents li { padding: 2px 0; }
.tracking-form { display: grid; gap: 10px; grid-template-columns: repeat(4, minmax(0, 1fr)); margin-top: 12px; }
.tracking-form .action-row { grid-column: 1 / -1; margin-top: 0; }

.settings-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.settings-grid { display: grid; grid-template-columns: minmax(260px, 0.8fr) minmax(520px, 1.6fr); gap: 24px; align-items: start; }
.settings-row { align-items: center; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: space-between; padding-top: 8px; }
.settings-row-edit { align-items: start; display: grid; grid-template-columns: minmax(0, 1fr); justify-content: stretch; }
.settings-row-edit .rate-tier-editor { grid-column: 1 / -1; }
.rate-tier-editor { display: flex; flex-direction: column; gap: 6px; }
.rate-tier-list { display: flex; flex-direction: column; gap: 6px; }
.rate-tier-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 6px; }
.rate-tier-row input { min-width: 0; }

@media (max-width: 860px) {
  .menu-toggle {
    display: block;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 35;
  }
  .menu-backdrop {
    background: rgba(15, 23, 42, 0.35);
    inset: 0;
    position: fixed;
    z-index: 39;
  }
  .menu-backdrop.visible { display: block; }
  .sidebar {
    display: flex;
    height: 100vh;
    left: 0;
    position: fixed;
    top: 0;
    transform: translateX(-100%);
    transition: transform 180ms ease;
    z-index: 40;
  }
  .sidebar.menu-open { transform: translateX(0); }
  .content { padding: 64px 16px 16px; }
  .stock-check-row { grid-template-columns: 1fr; }
  .items-layout { grid-template-columns: 1fr; }
  .item-category-filter { position: static; }
  .trackers-grid { grid-template-columns: 1fr; }
  .tracking-form { grid-template-columns: 1fr; }
  .dashboard-stats { grid-template-columns: 1fr 1fr; }
  .settings-grid { grid-template-columns: 1fr; }
  .settings-row-edit { grid-template-columns: 1fr; }
}

