:root {
  --canvas: #f4f6f8;
  --surface: #ffffff;
  --surface-muted: #f9fafb;
  --ink: #17212b;
  --muted: #667789;
  --line: #dfe5eb;
  --blue: #1769d8;
  --blue-soft: #eaf2ff;
  --green: #178151;
  --green-soft: #e8f7ef;
  --orange: #c45d00;
  --orange-soft: #fff2e4;
  --red: #bd3333;
  --red-soft: #fff0f0;
  --radius: 8px;
  color: var(--ink);
  background: var(--canvas);
  font-family: Inter, "Noto Sans SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; background: var(--canvas); }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
a { color: inherit; }
h1, h2, h3, p { margin-top: 0; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.app-shell, .config-shell { width: min(1160px, calc(100% - 32px)); margin: 0 auto; padding: 30px 0 56px; }
.topbar, .config-topbar { display: flex; align-items: center; justify-content: space-between; gap: 20px; }
.brand-block { display: flex; align-items: center; gap: 11px; }
.brand-mark { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 8px; background: var(--blue); }
.brand-mark::before { width: 14px; height: 14px; border: 2px solid #fff; border-radius: 50%; content: ""; }
.eyebrow { margin: 0; color: var(--muted); font-size: 11px; font-weight: 700; letter-spacing: .08em; }
h1 { margin: 3px 0 0; font-size: 22px; line-height: 1.2; letter-spacing: 0; }
h2 { margin: 4px 0 0; font-size: 18px; line-height: 1.3; letter-spacing: 0; }
h3 { letter-spacing: 0; }

.top-actions, .connection-state { display: flex; align-items: center; gap: 10px; }
.connection-state { color: var(--muted); font-size: 13px; }
.connection-dot { width: 8px; height: 8px; border-radius: 50%; background: #99a6b2; }
.connection-dot.live { background: var(--green); box-shadow: 0 0 0 3px var(--green-soft); }
.connection-dot.busy { background: #df8b26; box-shadow: 0 0 0 3px var(--orange-soft); }
.icon-button { width: 40px; height: 40px; display: inline-grid; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: var(--radius); color: var(--ink); background: var(--surface); transition: background-color .18s ease, border-color .18s ease; }
.icon-button:hover { border-color: var(--blue); background: var(--blue-soft); }
.icon-button:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible { outline: 3px solid rgba(23, 105, 216, .28); outline-offset: 2px; }
.refresh-glyph { font-size: 23px; line-height: 1; }
.icon-button.loading .refresh-glyph { animation: spin .8s linear infinite; }

.control-bar { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-top: 26px; padding: 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.profile-tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.profile-tab { min-height: 36px; padding: 0 14px; border: 1px solid transparent; border-radius: 7px; color: var(--muted); background: transparent; font-size: 13px; font-weight: 700; transition: background-color .18s ease, color .18s ease; }
.profile-tab:hover { color: var(--ink); background: var(--surface-muted); }
.profile-tab.active { color: #0e4eaa; background: var(--blue-soft); border-color: #cfe0fb; }
.interval-control { display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: 13px; white-space: nowrap; }
.interval-value { color: var(--ink); font-size: 13px; font-weight: 700; }
select, input, textarea { width: 100%; border: 1px solid var(--line); border-radius: 7px; color: var(--ink); background: var(--surface); }
select, input { min-height: 40px; padding: 0 10px; }
textarea { min-height: 84px; padding: 10px; line-height: 1.55; resize: vertical; }
.interval-control select { width: 104px; min-height: 36px; }

.summary-strip { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)) minmax(170px, 1.35fr) minmax(170px, 1.35fr); margin-top: 14px; overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.summary-item { min-height: 68px; display: flex; flex-direction: column; justify-content: center; gap: 5px; padding: 12px 16px; border-right: 1px solid var(--line); }
.summary-item:last-child { border-right: 0; }
.summary-item span { color: var(--muted); font-size: 12px; }
.summary-item strong { overflow: hidden; color: var(--ink); font-size: 15px; font-weight: 700; line-height: 1.3; text-overflow: ellipsis; white-space: nowrap; }
.summary-item:not(.summary-wide) strong { color: var(--blue); font-size: 20px; }

.empty-state { margin-top: 18px; padding: 42px 20px; border: 1px dashed #cbd5df; border-radius: var(--radius); color: var(--muted); text-align: center; }
.empty-state h2 { margin-bottom: 0; color: var(--ink); font-size: 17px; }
.results-section { margin-top: 24px; }
.results-heading { display: flex; align-items: end; justify-content: space-between; gap: 16px; margin-bottom: 10px; }
.query-note { color: var(--muted); font-size: 13px; text-align: right; }
.result-panel { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.result-columns, .result-row { display: grid; grid-template-columns: 58px minmax(0, 1fr) 112px 96px 150px; align-items: center; gap: 14px; }
.result-columns { min-height: 38px; padding: 0 18px; border-bottom: 1px solid var(--line); color: var(--muted); background: var(--surface-muted); font-size: 11px; font-weight: 700; letter-spacing: .06em; }
.results-list { display: grid; }
.result-row { min-height: 92px; padding: 15px 18px; border-bottom: 1px solid var(--line); }
.result-row:last-child { border-bottom: 0; }
.result-row:hover { background: #fbfdff; }
.rank-column { color: #8a99a8; font-size: 13px; font-weight: 700; }
.product-cell { min-width: 0; }
.product-heading { display: flex; align-items: center; gap: 8px; min-width: 0; }
.product-heading h3 { overflow: hidden; margin: 0; color: var(--ink); font-size: 14px; line-height: 1.45; text-overflow: ellipsis; white-space: nowrap; }
.source-badge { flex: 0 0 auto; padding: 3px 6px; border-radius: 5px; color: #82511d; background: var(--orange-soft); font-size: 11px; font-weight: 700; }
.source-badge.store { color: #0b5bb3; background: var(--blue-soft); }
.merchant, .shop { display: block; overflow: hidden; margin-top: 5px; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.price { color: var(--orange); font-size: 16px; font-weight: 750; white-space: nowrap; }
.stock { color: var(--green); font-size: 13px; font-weight: 700; white-space: nowrap; }
.source-cell { min-width: 0; }
.product-link { display: inline-block; margin-top: 4px; color: var(--blue); font-size: 12px; font-weight: 700; text-decoration: none; }
.product-link:hover { text-decoration: underline; }
.downstream-details { margin-top: 8px; }
.downstream-details summary { color: var(--blue); cursor: pointer; font-size: 12px; font-weight: 700; }
.downstream-list { display: grid; gap: 6px; margin-top: 8px; padding: 9px; border-radius: 7px; background: var(--surface-muted); }
.downstream-row { display: flex; flex-wrap: wrap; gap: 7px; color: var(--muted); font-size: 12px; line-height: 1.45; }
.downstream-row .markup { color: var(--orange); font-weight: 700; }
.downstream-row a { color: var(--blue); text-decoration: none; }
.query-errors { margin-top: 12px; padding: 11px 13px; border: 1px solid #f0c4c4; border-radius: var(--radius); color: var(--red); background: var(--red-soft); font-size: 13px; line-height: 1.5; }

.config-page { background: var(--canvas); }
.config-topbar { margin-bottom: 20px; }
.back-link { min-height: 40px; display: inline-flex; align-items: center; padding: 0 12px; border: 1px solid var(--line); border-radius: var(--radius); color: var(--ink); background: var(--surface); font-size: 13px; font-weight: 700; text-decoration: none; }
.back-link:hover { border-color: var(--blue); background: var(--blue-soft); }
.config-panel { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.config-section { padding: 22px; border-bottom: 1px solid var(--line); }
.config-section:last-of-type { border-bottom: 0; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.inline-create { display: flex; align-items: center; gap: 8px; }
.inline-create input { width: 154px; }
.form-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.form-field { display: grid; gap: 7px; color: var(--muted); font-size: 13px; }
.form-field-wide { grid-column: span 2; }
.compact-field { width: min(230px, 100%); }
.profile-editor { display: flex; align-items: end; gap: 10px; margin-bottom: 16px; }
.profile-editor .form-field { flex: 1; }
.checkbox-field { grid-column: span 2; min-height: 40px; display: flex; align-items: center; gap: 9px; color: var(--ink); font-size: 13px; font-weight: 700; }
.checkbox-field input { width: 18px; height: 18px; accent-color: var(--blue); }
.shop-headings, .shop-row { display: grid; grid-template-columns: minmax(160px, .8fr) minmax(240px, 1.2fr) 40px; gap: 10px; align-items: center; }
.shop-headings { margin-bottom: 8px; color: var(--muted); font-size: 12px; font-weight: 700; }
.shop-list { display: grid; gap: 9px; }
.shop-row .icon-button { color: var(--red); }
.primary-button, .secondary-button, .danger-button { min-height: 40px; border-radius: 7px; padding: 0 13px; font-size: 13px; font-weight: 700; }
.primary-button { border: 1px solid var(--blue); color: #fff; background: var(--blue); }
.primary-button:hover { background: #105cbc; }
.secondary-button { border: 1px solid var(--line); color: var(--ink); background: var(--surface); }
.secondary-button:hover { border-color: var(--blue); background: var(--blue-soft); }
.danger-button { border: 1px solid #efc7c7; color: var(--red); background: var(--surface); }
.danger-button:disabled { cursor: not-allowed; opacity: .45; }
.config-footer { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 22px; background: var(--surface-muted); }
.config-status { color: var(--muted); font-size: 13px; }
.config-status.success { color: var(--green); }
.config-status.error { color: var(--red); }

@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 820px) {
  .summary-strip { grid-template-columns: 1fr 1fr; }
  .summary-item:nth-child(2) { border-right: 0; }
  .summary-item:nth-child(-n + 2) { border-bottom: 1px solid var(--line); }
  .result-columns { display: none; }
  .result-row { grid-template-columns: 36px minmax(0, 1fr) auto; gap: 8px 12px; min-height: 0; }
  .rank-column { grid-row: span 2; padding-top: 3px; }
  .product-cell { grid-column: 2; }
  .price { grid-column: 3; grid-row: 1; }
  .stock { grid-column: 3; grid-row: 2; }
  .source-cell { grid-column: 2 / span 2; display: flex; align-items: center; gap: 10px; }
  .source-cell .shop, .source-cell .product-link { margin-top: 0; }
  .source-cell .shop { max-width: 70%; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-field-wide { grid-column: span 2; }
}

@media (max-width: 620px) {
  .app-shell, .config-shell { width: min(100% - 24px, 1160px); padding-top: 20px; }
  .topbar, .config-topbar, .control-bar { align-items: flex-start; flex-direction: column; }
  .top-actions { width: 100%; justify-content: space-between; }
  .control-bar { padding: 10px; }
  .interval-control { width: 100%; justify-content: space-between; }
  .summary-strip { margin-top: 10px; }
  .summary-item { min-height: 62px; padding: 10px 12px; }
  .summary-item strong { font-size: 13px; }
  .results-heading { align-items: flex-start; flex-direction: column; }
  .query-note { text-align: left; }
  .result-row { padding: 14px 12px; }
  .product-heading h3 { white-space: normal; }
  .config-section { padding: 18px 14px; }
  .section-heading { align-items: flex-start; flex-direction: column; }
  .inline-create { width: 100%; }
  .inline-create input { flex: 1; width: auto; }
  .profile-editor { align-items: stretch; flex-direction: column; }
  .form-grid { grid-template-columns: 1fr; }
  .form-field-wide, .checkbox-field { grid-column: auto; }
  .shop-headings { display: none; }
  .shop-row { grid-template-columns: 1fr 40px; }
  .shop-row .shop-token { grid-column: 1; }
  .shop-row .remove-shop { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
  .config-footer { align-items: stretch; flex-direction: column; padding: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
