:root {
  --canvas: #f3f4f1;
  --surface: #ffffff;
  --surface-soft: #f8f9f7;
  --sidebar: #181c1b;
  --sidebar-soft: #242a28;
  --ink: #1b2422;
  --muted: #69736f;
  --line: #dfe4e1;
  --line-strong: #cbd2ce;
  --blue: #2866dc;
  --blue-dark: #1f52b4;
  --teal: #16856f;
  --coral: #d4584b;
  --amber: #c78622;
  --shadow: 0 8px 28px rgba(24, 28, 27, 0.07);
  --sidebar-width: 248px;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--canvas); }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--canvas);
  color: var(--ink);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.55;
  letter-spacing: 0;
}
body, button, input, select, textarea { letter-spacing: 0; }
a { color: var(--blue); text-decoration: none; }
a:hover { color: var(--blue-dark); }
button, input, select, textarea { font: inherit; }
button, select { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.58; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible {
  outline: 3px solid rgba(40, 102, 220, 0.22);
  outline-offset: 2px;
}
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 6px; font-size: 28px; line-height: 1.25; font-weight: 760; }
h2 { margin-bottom: 4px; font-size: 18px; line-height: 1.35; }
h3 { margin-bottom: 8px; font-size: 16px; }
small { font-size: 12px; }
.page-header > div, .header-row > div, .page-header h1, .header-row h2 { min-width: 0; overflow-wrap: anywhere; }

.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;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 30;
  display: flex;
  width: var(--sidebar-width);
  min-height: 100vh;
  flex-direction: column;
  padding: 26px 18px 20px;
  overflow-y: auto;
  color: #f7faf8;
  background: var(--sidebar);
}
.brand, .mobile-brand {
  display: flex;
  align-items: center;
  gap: 11px;
  color: #ffffff;
}
.brand:hover, .mobile-brand:hover { color: #ffffff; }
.brand { padding: 0 8px 28px; }
.brand-mark {
  display: grid;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  place-items: center;
  border-radius: 6px;
  color: #ffffff;
  background: var(--blue);
  font-weight: 800;
}
.brand strong, .brand small { display: block; }
.brand strong { font-size: 17px; }
.brand small { margin-top: 1px; color: #98a39e; }
.primary-nav { display: grid; gap: 5px; }
.nav-link {
  display: grid;
  grid-template-columns: 32px 1fr;
  align-items: center;
  min-height: 46px;
  padding: 0 12px;
  border-radius: 6px;
  color: #bec8c3;
  font-weight: 650;
}
.nav-link:hover { color: #ffffff; background: var(--sidebar-soft); }
.nav-link.active { color: #ffffff; background: #2b3230; box-shadow: inset 3px 0 0 var(--blue); }
.nav-index { color: #7e8a85; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; }
.nav-link.active .nav-index { color: #88adf6; }
.side-note {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: auto;
  padding: 16px 10px 0;
  border-top: 1px solid #303734;
}
.side-note strong, .side-note small { display: block; }
.side-note strong { color: #e8eeeb; font-size: 12px; }
.side-note small { color: #8f9b96; }
.status-dot, .connection-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--teal); }
.connection-dot.offline { background: var(--coral); }
.logout-form { margin: 12px 0 0; }
.logout-button { width: 100%; min-height: 36px; color: #bec8c3; border-color: #3a4440; background: transparent; font-size: 12px; }
.logout-button:hover { color: #ffffff; border-color: #626f69; background: var(--sidebar-soft); }

.login-page { display: grid; place-items: center; padding: 20px; }
.login-shell { width: min(100%, 380px); }
.login-page .login-panel { padding: 28px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); box-shadow: var(--shadow); }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 28px; font-size: 17px; }
.login-form { display: grid; gap: 16px; }
.login-form button { width: 100%; margin-top: 2px; }

.mobile-bar { display: none; }
.nav-overlay { display: none; }
.main { min-height: 100vh; margin-left: var(--sidebar-width); }
.main-inner { width: min(100%, 1600px); margin: 0 auto; padding: 32px 36px 56px; }
.page-header, .header-row {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 22px;
}
.page-header > div > p:last-child, .header-row > div > p:last-child { margin-bottom: 0; color: var(--muted); }
.eyebrow { margin-bottom: 5px; color: var(--muted); font-size: 11px; font-weight: 760; text-transform: uppercase; }
.header-actions, .source-links, .actions-row { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }

button, .button-link {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  padding: 9px 15px;
  border: 1px solid var(--blue);
  border-radius: 6px;
  color: #ffffff;
  background: var(--blue);
  font-weight: 700;
  line-height: 1.2;
}
button:hover, .button-link:hover { color: #ffffff; border-color: var(--blue-dark); background: var(--blue-dark); }
.secondary-btn, .button-link.secondary, .quiet-btn {
  color: var(--ink);
  border-color: var(--line-strong);
  background: var(--surface);
}
.secondary-btn:hover, .button-link.secondary:hover, .quiet-btn:hover { color: var(--ink); border-color: #aab5b0; background: #eef1ef; }
.danger-btn { color: #9f3029; border-color: #e7c1bd; background: #fff7f6; }
.danger-btn:hover { color: #ffffff; border-color: var(--coral); background: var(--coral); }
.small-button { min-height: 32px; padding: 6px 10px; font-size: 12px; }
.text-link, .external-link { font-weight: 700; }
.external-link { white-space: nowrap; font-size: 12px; }
.secondary-link { color: var(--teal); }
.icon-button { width: 40px; padding: 0; font-size: 20px; }

input, select, textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 11px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  color: var(--ink);
  background: var(--surface);
}
input::placeholder, textarea::placeholder { color: #96a09c; }
label { color: #4e5a55; font-size: 12px; font-weight: 650; }
label input, label select, label textarea { margin-top: 5px; color: var(--ink); font-size: 14px; font-weight: 400; }

.metric-grid, .cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.metric-grid.three-columns { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.metric-card, .card {
  position: relative;
  min-height: 122px;
  padding: 19px 20px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}
.metric-card::before, .card::before { position: absolute; inset: 0 auto 0 0; width: 3px; content: ""; background: var(--blue); }
.metric-card.accent-amber::before { background: var(--amber); }
.metric-card.accent-teal::before { background: var(--teal); }
.metric-card span, .card span { display: block; color: var(--muted); font-size: 12px; font-weight: 650; }
.metric-card strong, .card strong { display: block; margin: 6px 0 1px; font-size: 30px; line-height: 1.2; }
.metric-card small, .card small { color: #8b9591; }

.panel {
  margin-bottom: 18px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}
.panel-title { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 15px; }
.panel-title h2, .panel-title h3, .panel-title p { margin-bottom: 0; }
.dashboard-grid { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(300px, 0.75fr); gap: 18px; }
.scan-console, .login-panel { min-height: 270px; }
.scan-rules { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border-block: 1px solid var(--line); }
.scan-rules div { min-width: 0; padding: 17px 14px; border-right: 1px solid var(--line); }
.scan-rules div:first-child { padding-left: 0; }
.scan-rules div:last-child { border-right: 0; }
.scan-rules strong, .scan-rules span { display: block; }
.scan-rules strong { font-size: 20px; }
.scan-rules span { color: var(--muted); font-size: 12px; }
.scan-status-box { padding-top: 17px; }
.scan-status-box p { margin-bottom: 2px; font-weight: 700; }
.scan-status-box small { color: var(--muted); }
.scan-form { display: grid; grid-template-columns: 150px auto; gap: 9px; }
.state-badge {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  color: #55605b;
  background: #edf0ee;
  font-size: 11px;
  font-weight: 760;
  white-space: nowrap;
}
.state-badge.running, .state-badge.enabled { color: #0c6d5a; background: #e2f3ee; }
.state-badge.paused { color: #855c1a; background: #f8edd9; }
.login-panel pre {
  min-height: 72px;
  margin: 8px 0 14px;
  padding: 13px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: #39433f;
  background: var(--surface-soft);
  white-space: pre-wrap;
  word-break: break-word;
}
.panel-note { margin: 16px 0 0; color: var(--muted); font-size: 12px; }
.login-actions { display: flex; align-items: center; margin: 0 0 14px; }
.login-actions button { min-width: 126px; }
.qr-wrap { display: grid; justify-items: start; gap: 10px; }
.qr-wrap p { margin: 0; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.qr-wrap .login-error { color: #a33a34; }
.qr-img { width: 210px; max-width: 100%; padding: 8px; border: 1px solid var(--line); background: #ffffff; }

.command-band, .filter-band {
  margin-bottom: 18px;
  padding: 16px 18px;
  border-block: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.58);
}
.keyword-create-form { display: grid; grid-template-columns: minmax(260px, 2fr) minmax(160px, 1fr) 100px auto; align-items: end; gap: 12px; }
.field label, .field input { display: block; }
.field label { margin-bottom: 5px; }
.filter-form { display: grid; grid-template-columns: 150px 170px minmax(220px, 1fr) auto; align-items: end; gap: 12px; }

.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 13px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { color: var(--muted); background: var(--surface-soft); font-size: 11px; font-weight: 760; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #fafbf9; }
.data-panel { padding: 0; overflow: hidden; }
.data-panel > .panel-title { padding: 18px 20px 0; }
.data-panel table { min-width: 900px; }
.count-label { color: var(--muted); font-size: 12px; }
.lead-title { display: block; min-width: 190px; color: var(--ink); font-weight: 720; }
.lead-title:hover { color: var(--blue); }
.cell-subtitle, .reason-text { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; font-weight: 400; }
.reason-text { max-width: 420px; }
.score-cell { font-variant-numeric: tabular-nums; font-weight: 760; }
.score-label { text-align: center; }
.empty-cell { padding: 36px 18px; color: var(--muted); text-align: center; }
.badge { display: inline-grid; width: 30px; height: 30px; place-items: center; border-radius: 50%; font-size: 12px; font-weight: 800; }
.badge.danger { color: #a6372e; background: #fae5e2; }
.badge.warning { color: #865b15; background: #f7ead3; }
.badge.info { color: #0d6d5a; background: #dff1ec; }
.status-text { color: #4c5753; white-space: nowrap; }
.source-links { align-items: flex-start; flex-direction: column; gap: 3px; }
.source-open-form { margin: 0; }
.source-action { text-decoration: none; }
.source-links:not(.prominent) .source-action {
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  color: var(--blue);
  background: transparent;
  font-size: 12px;
  white-space: nowrap;
}
.source-links:not(.prominent) .source-action:hover { color: var(--blue-dark); background: transparent; }
.source-links:not(.prominent) .source-action.secondary { color: var(--teal); }
.source-links:not(.prominent) .source-action.danger { color: #9f3029; }
.source-links:not(.prominent) .source-action.danger:hover { color: #7d211c; background: #fff7f6; }
.source-links.prominent { align-items: center; flex-direction: row; }
.source-links.prominent .source-action.secondary {
  color: var(--ink);
  border-color: var(--line-strong);
  background: var(--surface);
}
.source-links.prominent .source-action.secondary:hover { border-color: #aab5b0; background: #eef1ef; }

.grade-counts { display: flex; gap: 5px; flex-wrap: wrap; }
.grade-counts span { padding: 2px 6px; border-radius: 4px; font-size: 11px; font-weight: 760; }
.grade-s { color: #a6372e; background: #fae5e2; }
.grade-a { color: #865b15; background: #f7ead3; }
.grade-b { color: #0d6d5a; background: #dff1ec; }
.keyword-name-cell { min-width: 250px; }
.keyword-suggestions { display: flex; gap: 5px; margin-top: 8px; flex-wrap: wrap; }
.suggestion-chip { min-height: 27px; padding: 4px 7px; color: var(--blue); border-color: #c7d6f5; background: #f4f7fd; font-size: 11px; }
.suggestion-chip:hover { color: #ffffff; }
.row-actions { display: flex; min-width: 275px; gap: 6px; align-items: flex-start; flex-wrap: wrap; }
.edit-popover { position: relative; }
.edit-popover > summary { min-height: 32px; padding: 6px 10px; border: 1px solid var(--line-strong); border-radius: 6px; list-style: none; cursor: pointer; font-size: 12px; font-weight: 700; }
.edit-popover > summary::-webkit-details-marker { display: none; }
.edit-keyword-form {
  position: absolute;
  z-index: 10;
  top: 38px;
  right: 0;
  display: grid;
  width: 300px;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 28px; }
.detail-list { margin: 0; }
.detail-list > div { display: grid; grid-template-columns: 100px 1fr; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.detail-list dt { color: var(--muted); }
.detail-list dd { min-width: 0; margin: 0; overflow-wrap: anywhere; }
.operation-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px; }
.stack-form { display: grid; gap: 8px; padding-right: 22px; border-right: 1px solid var(--line); }
.stack-form:last-child { padding-right: 0; border-right: 0; }
.compact-actions { margin-top: 16px; }
.raw-detail summary, .run-history summary, .history-panel summary { cursor: pointer; color: var(--ink); font-weight: 760; }
.raw-detail pre { max-height: 380px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.historical-panel { border-color: #ead5d2; }
.run-list { display: grid; gap: 8px; margin: 14px 0 0; padding: 0; list-style: none; }
.run-list li { display: grid; grid-template-columns: 165px 1fr; gap: 12px; padding-top: 8px; border-top: 1px solid var(--line); color: var(--muted); }
.history-panel > summary span { margin-left: 6px; color: var(--muted); font-weight: 400; }
.history-panel .table-wrap { margin-top: 14px; }
.flash-stack { display: grid; gap: 8px; margin-bottom: 16px; }
.flash { padding: 11px 13px; border: 1px solid var(--line); border-radius: 6px; background: #ffffff; }
.flash.success { color: #0d6d5a; border-color: #b9ded4; background: #ecf7f4; }
.flash.warning { color: #855c1a; border-color: #ead2a8; background: #fcf5e8; }

@media (max-width: 1180px) {
  .metric-grid, .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dashboard-grid { grid-template-columns: 1fr; }
  .keyword-create-form { grid-template-columns: minmax(220px, 2fr) minmax(150px, 1fr) 90px; }
  .keyword-create-form > button { grid-column: 1 / -1; justify-self: end; }
}

@media (max-width: 900px) {
  .mobile-bar {
    position: sticky;
    top: 0;
    z-index: 25;
    display: flex;
    height: 60px;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    border-bottom: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.96);
  }
  .mobile-brand { color: var(--ink); font-weight: 760; }
  .mobile-brand:hover { color: var(--ink); }
  .mobile-brand .brand-mark { width: 30px; height: 30px; flex-basis: 30px; }
  .sidebar { transform: translateX(-105%); transition: transform 180ms ease; }
  .nav-open .sidebar { transform: translateX(0); }
  .nav-overlay { position: fixed; inset: 0; z-index: 29; border: 0; border-radius: 0; background: rgba(11, 15, 14, 0.44); }
  .nav-overlay:hover { background: rgba(11, 15, 14, 0.44); }
  .nav-open .nav-overlay { display: block; }
  .main { margin-left: 0; }
  .main-inner { padding: 24px 20px 44px; }
  .page-header, .header-row { align-items: flex-start; flex-direction: column; }
  .scan-form { width: 100%; grid-template-columns: minmax(0, 1fr) auto; }
  .filter-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filter-keyword { grid-column: 1 / -1; }
  .filter-form > button { justify-self: start; }
  .detail-grid, .operation-grid { grid-template-columns: 1fr; }
  .stack-form { padding: 0 0 18px; border-right: 0; border-bottom: 1px solid var(--line); }
  .stack-form:last-child { padding-bottom: 0; border-bottom: 0; }
}

@media (max-width: 640px) {
  h1 { font-size: 24px; }
  .main-inner { padding: 20px 14px 36px; }
  .metric-grid, .cards, .metric-grid.three-columns { grid-template-columns: 1fr 1fr; gap: 9px; }
  .metric-card, .card { min-height: 104px; padding: 15px; }
  .metric-card strong, .card strong { font-size: 25px; }
  .scan-rules { grid-template-columns: 1fr; }
  .scan-rules div, .scan-rules div:first-child { padding: 11px 0; border-right: 0; border-bottom: 1px solid var(--line); }
  .scan-rules div:last-child { border-bottom: 0; }
  .command-band, .filter-band { margin-inline: -14px; padding-inline: 14px; }
  .keyword-create-form, .filter-form { grid-template-columns: 1fr; }
  .filter-keyword, .keyword-create-form > button { grid-column: auto; }
  .keyword-create-form > button, .filter-form > button { width: 100%; justify-self: stretch; }
  .panel { padding: 15px; }
  .data-panel { padding: 0; }
  .data-panel > .panel-title { padding: 15px 15px 0; }
  .empty-cell { position: sticky; left: 0; min-width: calc(100vw - 58px); text-align: center; }
  .lead-table, .history-table { min-width: 0 !important; }
  .lead-table thead, .history-table thead { display: none; }
  .lead-table tbody, .history-table tbody { display: grid; }
  .lead-table tr, .history-table tr {
    display: grid;
    grid-template-columns: 1fr;
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
  }
  .lead-table td, .history-table td {
    display: grid;
    grid-template-columns: 76px minmax(0, 1fr);
    gap: 8px;
    padding: 6px 0;
    border: 0;
    overflow-wrap: anywhere;
  }
  .lead-table td::before, .history-table td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 11px;
    font-weight: 700;
  }
  .lead-table .source-links, .history-table .source-links { flex-direction: row; }
  .lead-table .score-label { text-align: left; }
  .edit-keyword-form { position: fixed; inset: auto 14px 14px; width: auto; max-height: 70vh; overflow-y: auto; }
  .detail-list > div { grid-template-columns: 1fr; gap: 3px; }
  .header-actions { width: 100%; align-items: stretch; flex-direction: column; }
  .header-actions .button-link { width: 100%; }
  .header-actions .source-links, .header-actions .source-open-form, .header-actions .source-action { width: 100%; }
  .header-actions .source-links { align-items: stretch; flex-direction: column; }
  .run-list li { grid-template-columns: 1fr; gap: 2px; }
}
