:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #646b78;
  --line: #e2e5ea;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --accent-soft: #e8f0fe;
  --ok: #1a7f37;
  --ok-soft: #e6f4ea;
  --bad: #b42318;
  --bad-soft: #fdecea;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 5%);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1b1e24;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --line: #2c313a;
    --accent: #4c8dff;
    --accent-soft: #1d2a42;
    --ok: #4ac26b;
    --ok-soft: #16301f;
    --bad: #ff7b72;
    --bad-soft: #3a1d1d;
    --warn: #e3b341;
    --warn-soft: #332a12;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-width: 360px;           /* popup extension */
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}
main { flex: 1; width: 100%; max-width: 760px; margin: 0 auto; padding: 12px 12px 24px; }
h1 { font-size: 18px; margin: 4px 0 12px; }
h2 { font-size: 16px; margin: 20px 0 8px; }
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Thanh tìm kiếm ---------- */
.bar {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 8px; align-items: center;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { width: 36px; height: 36px; flex: none; border-radius: 9px; display: block; }
.search { position: relative; flex: 1; max-width: 736px; margin: 0 auto; }
.search-icon {
  position: absolute; left: 11px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%);
  fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
}
#q {
  width: 100%; height: 42px; padding: 0 12px 0 36px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg);
  font-size: 16px; /* iOS không tự phóng to */
}
#q:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--surface); }
.icon-btn {
  width: 42px; height: 42px; flex: none; display: grid; place-items: center;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
body[data-view="settings"] .icon-btn { border-color: var(--accent); }
body[data-view="settings"] .icon-btn svg { stroke: var(--accent); }

/* ---------- Nút ---------- */
.primary, .ghost {
  border-radius: var(--radius); cursor: pointer; padding: 10px 16px; font-weight: 600;
  border: 1px solid transparent; transition: filter .15s, background .15s;
}
.primary { background: var(--accent); color: var(--accent-text); }
.primary:hover { filter: brightness(1.08); }
.primary:active { filter: brightness(.95); }
.ghost { background: var(--surface); border-color: var(--line); }
.ghost:hover { border-color: var(--muted); }
.small { padding: 6px 10px; font-size: 13px; font-weight: 500; }
button:disabled { opacity: .6; cursor: default; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.linkish { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 12px; padding: 4px; }
.linkish:hover { color: var(--text); text-decoration: underline; }

/* ---------- Kết quả ---------- */
.empty { color: var(--muted); text-align: center; padding: 28px 8px; margin: 0; }
.empty.error, .error { color: var(--bad); }
#view-results.loading { opacity: .6; }
.results { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.result {
  width: 100%; display: grid; grid-template-columns: 48px 1fr auto; gap: 10px; align-items: center;
  padding: 8px; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.result:hover, .result.active { border-color: var(--accent); background: var(--accent-soft); }
.result img, .result .img-empty { width: 48px; height: 48px; border-radius: 6px; object-fit: cover; background: var(--bg); }
.r-main { min-width: 0; display: grid; gap: 2px; }
.r-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.r-meta { display: flex; flex-wrap: wrap; gap: 4px 10px; color: var(--muted); font-size: 12px; }
.sku { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12px; }
.badge {
  display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 600;
  background: var(--warn-soft); color: var(--warn);
}

/* ---------- Tồn kho (chỉ hiển thị) ---------- */
.stock { font-size: 12px; font-weight: 600; white-space: nowrap; padding: 2px 8px; border-radius: 999px; }
.stock.ok { color: var(--ok); background: var(--ok-soft); }
.stock.out { color: var(--bad); background: var(--bad-soft); }
.stock.none { color: var(--muted); background: var(--bg); font-weight: 500; }
@media (max-width: 420px) {
  .result { grid-template-columns: 48px 1fr; }
  .result .stock { grid-column: 2; justify-self: start; }
}

/* ---------- Chi tiết ---------- */
.back { margin-bottom: 10px; }
.p-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.p-head h1 { margin: 0 0 10px; }
.variants { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 6px; }
.variant {
  width: 100%; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; align-items: center;
  padding: 9px 10px; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.variant:hover { border-color: var(--muted); }
.variant.selected { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.v-title { font-weight: 600; }
.v-price { font-weight: 600; text-align: right; }
.v-sku { color: var(--muted); }
.variant .stock { justify-self: end; }

.info { display: grid; gap: 10px; margin-top: 4px; scroll-margin-top: 72px; }
.internal {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px;
  border-radius: var(--radius); padding: 8px 12px; font-size: 14px; white-space: normal;
  border: 1px dashed currentColor;
}
.internal-note { font-size: 12px; font-weight: 400; opacity: .85; }
.info-grid { display: grid; grid-template-columns: 1fr auto; gap: 12px; align-items: start; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; min-width: 0;
}
.c-name { margin: 0 0 8px; font-size: 17px; }
.c-desc { margin: 0 0 10px; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text); }
.c-price { margin: 0 0 12px; font-size: 18px; font-weight: 700; color: var(--accent); }
.c-img img, .c-img .img-empty { display: block; width: 100%; max-width: 420px; aspect-ratio: 1; object-fit: contain; border-radius: 8px; background: var(--bg); }
.img-empty { background: repeating-linear-gradient(45deg, var(--bg), var(--bg) 8px, var(--line) 8px, var(--line) 9px); }

.actions { position: sticky; top: 76px; display: grid; gap: 8px; width: 150px; }
.actions button { padding: 12px 10px; }
.actions .insert { background: var(--ok); }
.actions .info-img { background: #8250df; }

@media (max-width: 560px) {
  .info-grid { grid-template-columns: 1fr; }
  /* Điện thoại / popup extension: thanh nút ghim ở đáy, 2 cột */
  .actions {
    position: sticky; bottom: 0; top: auto; width: auto; order: 2;
    grid-template-columns: 1fr 1fr;
    background: var(--bg); padding: 8px 0 max(8px, env(safe-area-inset-bottom));
  }
  .actions .insert, .actions .info-img { grid-column: 1 / -1; }
}

/* Popup extension: Chrome giới hạn khoảng 800x600 */
html[data-platform="extension"] body { width: 420px; min-height: 520px; }

/* ---------- Cài đặt ---------- */
#settings-form { display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-weight: 600; font-size: 14px; }
label input {
  height: 42px; padding: 0 10px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); font-weight: 400; font-size: 16px; min-width: 0;
}
label input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.with-btn { display: flex; gap: 6px; }
.with-btn input { flex: 1; }
.row { display: flex; gap: 8px; }
.msg { margin: 0; min-height: 1.2em; font-size: 13px; }
.msg.ok { color: var(--ok); }
.msg.warn { color: var(--warn); }
.msg.error { color: var(--bad); }
.status-box {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 12px; font-size: 14px; display: grid; gap: 2px; margin-bottom: 10px;
}
.hint { color: var(--muted); font-size: 13px; }

/* ---------- Chân trang + thông báo ---------- */
#footer { text-align: center; padding: 6px 12px max(6px, env(safe-area-inset-bottom)); }
#footer .warn { color: var(--warn); }
#footer .ver { color: var(--muted); font-size: 11px; opacity: .7; margin-left: 6px; }
body[data-view="detail"] #footer { display: none; }
#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 999px;
  background: #16181d; color: #fff; font-weight: 600; font-size: 14px; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--bad); border-radius: var(--radius); }
