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

:root {
  --bg: #f7f5ef;
  --bg-soft: #fbfaf6;
  --card: #ffffff;
  --border: #e6e1d2;
  --border-strong: #d2cbb6;
  --text: #1c2a25;
  --muted: #7a7a72;
  --primary: #235A49;
  --primary-dark: #1a4439;
  --primary-light: #2f7560;
  --primary-soft: #e8f0ec;
  --accent: #c8954b;
  --accent-soft: #fbf3e2;
  --danger: #b13a2d;
  --danger-soft: #fbe7e3;
  --warning: #c47b1a;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Login gate ---------- */
[hidden] { display: none !important; }

.login-gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 60%, var(--primary-light) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  background: #fff;
  width: 100%;
  max-width: 380px;
  padding: 36px 32px 28px;
  border-radius: 16px;
  box-shadow: 0 30px 80px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: left;
}
.login-logo {
  height: 52px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-logo img { height: 100%; width: auto; object-fit: contain; }
.login-tag {
  font-size: 11px;
  letter-spacing: 0.22em;
  font-weight: 700;
  color: var(--primary);
  text-align: center;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.login-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  text-align: center;
  letter-spacing: -0.01em;
}
.login-sub {
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  margin-bottom: 12px;
}
.login-card label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.login-card input { padding: 11px 14px; font-size: 14px; }
.login-card .primary {
  margin-top: 10px;
  padding: 12px;
  font-size: 14px;
  font-weight: 600;
}
.login-err {
  color: var(--danger);
  font-size: 13px;
  background: var(--danger-soft);
  padding: 8px 12px;
  border-radius: 6px;
  display: none;
}
.login-err.show { display: block; }
.login-foot {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  margin-top: 6px;
}

/* User strip in sidebar */
.user-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: rgba(255,255,255,0.7);
}
.user-email {
  flex: 1;
  font-family: ui-monospace, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.logout-btn {
  background: rgba(255,255,255,0.08);
  color: rgba(255,255,255,0.7);
  border: 1px solid rgba(255,255,255,0.15);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  font-weight: 600;
  transition: all .15s;
}
.logout-btn:hover { background: rgba(255,255,255,0.15); color: #fff; }

/* ---------- App shell ---------- */
.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: linear-gradient(180deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: #fff;
  padding: 22px 18px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar-brand {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}
.sidebar-brand .logo {
  height: 52px;
  background: #fff;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  padding: 8px 14px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  align-self: flex-start;
}
.sidebar-brand .logo img { height: 100%; width: auto; object-fit: contain; display: block; max-width: 100%; }
.brand-tag {
  font-size: 10px;
  letter-spacing: 0.22em;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.65);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.brand-name {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.3;
  color: #fff;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  color: rgba(255, 255, 255, 0.78);
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  transition: all .15s;
}
.nav-link svg { width: 16px; height: 16px; flex-shrink: 0; }
.nav-link span:nth-child(2) { flex: 1; }
.nav-link:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.nav-link.active {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  box-shadow: inset 2px 0 0 #fff;
}
.nav-count {
  font-size: 11px;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 1px 8px;
  border-radius: 10px;
  min-width: 22px;
  text-align: center;
}
.nav-link.active .nav-count { background: rgba(255, 255, 255, 0.25); }

.sidebar-footer {
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  padding-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.footer-meta {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.5);
  font-family: ui-monospace, monospace;
  letter-spacing: 0.05em;
}

/* ---------- Main content ---------- */
.content {
  padding: 28px 32px 60px;
  max-width: 1100px;
  width: 100%;
}
.page-head {
  margin-bottom: 22px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
}
.page-head h1 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}
.page-head p {
  font-size: 13px;
  color: var(--muted);
  margin-top: 4px;
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .content { padding: 20px; }
}

.status-pill {
  font-size: 11px;
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.18);
  font-weight: 500;
  text-align: center;
}
.status-pill.ok {
  background: rgba(126, 211, 33, 0.15);
  color: #c8f5b0;
  border-color: rgba(200, 245, 176, 0.3);
}
.status-pill.err {
  background: rgba(255, 100, 90, 0.18);
  color: #ffc7c2;
  border-color: rgba(255, 199, 194, 0.3);
}

.content > .card { margin-bottom: 18px; }
.content > .card:last-child { margin-bottom: 0; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px 26px;
  box-shadow: 0 1px 2px rgba(28, 42, 37, 0.04), 0 4px 12px rgba(28, 42, 37, 0.03);
  transition: box-shadow .2s;
}
.card:hover { box-shadow: 0 1px 2px rgba(28, 42, 37, 0.05), 0 6px 18px rgba(28, 42, 37, 0.05); }

.card h2 {
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  letter-spacing: -0.005em;
}

.settings-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0;
  background: transparent;
  border: none;
  text-align: left;
  cursor: pointer;
  color: var(--text);
}
.settings-toggle:hover { background: transparent; }
.settings-toggle:hover .gear-icon { color: var(--primary); transform: rotate(45deg); }

.gear-icon {
  width: 38px; height: 38px;
  border-radius: 8px;
  background: var(--primary-soft);
  color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: transform .35s cubic-bezier(.4,0,.2,1), background .15s, color .15s;
}
.gear-icon svg { width: 20px; height: 20px; }

.settings-title { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.settings-title strong { font-size: 14px; font-weight: 600; }
.settings-title .muted-small { color: var(--muted); padding: 0 !important; }

.chevron {
  font-size: 14px;
  color: var(--muted);
  transition: transform .25s;
}
.card[id="settingsCard"]:not(.collapsed) .chevron { transform: rotate(90deg); color: var(--primary); }
.card[id="settingsCard"]:not(.collapsed) .gear-icon { background: var(--primary); color: #fff; }

#settingsBody {
  max-height: 0;
  overflow: hidden;
  transition: max-height .35s ease, margin-top .25s ease, opacity .25s ease;
  opacity: 0;
}
.card[id="settingsCard"]:not(.collapsed) #settingsBody {
  max-height: 1200px;
  margin-top: 18px;
  opacity: 1;
}
.count {
  font-size: 12px;
  font-weight: 500;
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 10px;
  color: var(--muted);
}

.muted { color: var(--muted); font-size: 13px; margin-top: 4px; margin-bottom: 14px; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin: 14px 0;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

input, textarea {
  font: inherit;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fdfcf9;
  color: var(--text);
  outline: none;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
input:focus, textarea:focus {
  border-color: var(--primary);
  background: white;
  box-shadow: 0 0 0 3px rgba(35, 90, 73, 0.12);
}

button {
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: white;
  color: var(--text);
  cursor: pointer;
  transition: all .12s;
  white-space: nowrap;
}
button:hover { background: #f7f4ec; }
button.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  box-shadow: 0 1px 2px rgba(35, 90, 73, 0.15);
}
button.primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
  box-shadow: 0 2px 6px rgba(35, 90, 73, 0.22);
  transform: translateY(-1px);
}
button.primary:active { transform: translateY(0); }
button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 8px;
}
button.ghost:hover { background: var(--bg); }
button.danger-outline { color: var(--danger); border-color: #f5c5c0; background: white; }
button.danger-outline:hover { background: var(--danger-soft); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.msg {
  font-size: 13px;
  padding: 8px 12px;
  border-radius: 6px;
  display: none;
  flex: 1;
  margin-right: 12px;
}
.msg.show { display: block; }
.msg.ok { background: var(--primary-soft); color: var(--primary); }
.msg.err { background: var(--danger-soft); color: var(--danger); }

.row-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.actions { display: flex; gap: 8px; }
.action-bar {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.table-wrap { overflow-x: auto; margin: 12px 0; border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fdfcf9; }
th {
  font-weight: 600;
  color: var(--muted);
  background: #faf8f3;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
td input {
  width: 100%;
  padding: 5px 7px;
  font-size: 13px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
}
td input:hover { border-color: var(--border); background: white; }
td input:focus { border-color: var(--primary); background: white; }
td.row-num { color: var(--muted); font-size: 12px; text-align: center; }

.env-locked {
  background: linear-gradient(180deg, var(--bg-soft), var(--bg));
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  font-size: 13px;
}
.env-locked.warn-box { background: var(--accent-soft); border-color: #f5d9a7; }
.env-locked-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.env-help { color: var(--muted); font-size: 12px; font-weight: 400; }
.env-table { display: grid; gap: 6px; margin-top: 6px; }
.env-row {
  display: grid;
  grid-template-columns: 130px 1fr;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  align-items: baseline;
}
.env-row:last-child { border-bottom: none; }
.env-row span { color: var(--muted); font-size: 12px; }
.env-row strong { font-weight: 500; word-break: break-word; }
.env-row.warn strong { color: var(--warning); }
.env-row.ok strong { color: var(--primary); }
.env-pre {
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  margin-top: 8px;
  overflow-x: auto;
}

.variant-finder { margin-top: 16px; padding-top: 14px; border-top: 1px dashed var(--border-strong); }
.variant-finder label { font-weight: 600; font-size: 13px; }
.variant-finder .row { display: flex; gap: 8px; margin-top: 6px; }
.variant-finder .row input { flex: 1; background: white; }
#variantResults { margin-top: 12px; display: flex; flex-direction: column; gap: 10px; }
.product-card {
  background: white;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 10px 12px;
}
.product-title { font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.product-title a { color: var(--text); text-decoration: none; }
.product-title a:hover { color: var(--primary); text-decoration: underline; }
.variant-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 0;
  font-size: 12px;
  border-top: 1px solid var(--border);
}
.variant-row:first-of-type { border-top: none; }
.variant-row code {
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: ui-monospace, monospace;
  font-size: 11px;
}
.variant-row span { flex: 1; color: var(--muted); }
.copy-btn { padding: 4px 10px; font-size: 11px; }
.muted-small { font-size: 12px; color: var(--muted); padding: 8px 0; }
.muted-small.err { color: var(--danger); }

.variant-input {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px !important;
}
.variant-input::placeholder { font-family: -apple-system, "Segoe UI", sans-serif; font-style: italic; }

.history-table { font-size: 13px; }
.history-table tbody tr { transition: background .12s; }
.history-table .variant-pill {
  background: var(--bg);
  color: var(--muted);
  padding: 3px 7px;
  border-radius: 4px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px;
  border: 1px solid var(--border);
}
.done-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--primary-soft);
  color: var(--primary);
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid #d2e3da;
  letter-spacing: 0.02em;
}

.import-block {
  margin: 14px 0 0;
  padding: 14px 16px;
  background: var(--bg);
  border: 1px dashed var(--border-strong);
  border-radius: 8px;
}
.import-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text);
}
.import-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.import-row input { flex: 1; background: white; }
.import-row button { white-space: nowrap; }

.paste-area { margin: 12px 0 0; font-size: 13px; }
.paste-area summary {
  cursor: pointer;
  padding: 8px 0;
  color: var(--muted);
  user-select: none;
}
.paste-area summary:hover { color: var(--text); }
.paste-area textarea {
  width: 100%;
  margin-top: 8px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  resize: vertical;
}
.paste-area button { margin-top: 8px; }

.status-cell { font-size: 12px; }
.status-cell.pending { color: var(--muted); }
.status-cell.checking { color: var(--warning); }
.status-cell.existing { color: var(--primary); }
.status-cell.new { color: var(--accent); }
.status-cell.success { color: var(--primary); font-weight: 600; }
.status-cell.error { color: var(--danger); }
.status-cell a { color: inherit; text-decoration: underline; }

.row-actions { text-align: center; }
.row-actions button {
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
  border-color: transparent;
}
.row-actions button:hover { color: var(--danger); border-color: #f5c5c0; background: var(--danger-soft); }

.log {
  background: #14201c;
  color: #cfd8d3;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  border-radius: 8px;
  padding: 14px 16px;
  max-height: 320px;
  min-height: 80px;
  overflow-y: auto;
  white-space: pre-wrap;
  line-height: 1.65;
  margin-top: 10px;
}
.log .ts { color: #6b6760; }
.log .ok { color: #8fbf6b; }
.log .err { color: #e58a82; }
.log .info { color: #d4ad6a; }
.log:empty::after {
  content: "Activity log empty. Run preview or place orders to see activity here.";
  color: #6b6760;
  font-style: italic;
}

@media (max-width: 720px) {
  .form-grid { grid-template-columns: 1fr; }
  main { padding: 0 12px; }
  header { padding: 12px 16px; }
  .row-between { align-items: stretch; }
  .actions { flex-wrap: wrap; }
}
