:root {
  --blue: #0076b3;
  --purple: #ac72c3;
  --canvas: #e6e8f0;
  --ink: #212528;
  --muted: #545659;
  --line: #d7d9e1;
  --surface: #fdfdff;
  --radius: 6px;
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
body { margin: 0; color: var(--ink); background: var(--canvas); }
button, input, select { font: inherit; letter-spacing: 0; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; overflow-wrap: anywhere; }
.error-text { color: var(--purple); }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 220px minmax(0, 1fr); }
.sidebar { position: sticky; top: 0; height: 100vh; padding: 24px 16px; background: var(--blue); color: white; }
.sidebar-brand { display: flex; align-items: center; gap: 12px; padding: 0 8px 24px; border-bottom: 1px solid rgba(255,255,255,.18); }
.brand-logo { width: 40px; height: 40px; }
.sidebar-brand strong, .sidebar-brand span { display: block; }
.sidebar-brand span { margin-top: 3px; color: rgba(255,255,255,.68); font-size: 12px; }
.side-nav { display: grid; gap: 6px; margin-top: 22px; }
.side-nav-item { width: 100%; padding: 11px 14px; border: 0; border-radius: var(--radius); color: rgba(255,255,255,.78); background: transparent; text-align: left; }
.side-nav-item:hover, .side-nav-item.active { color: var(--blue); background: var(--surface); }

.main { min-width: 0; padding: 0 28px 48px; }
.topbar { min-height: 92px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); }
.topbar h1 { margin: 3px 0 0; font-size: 24px; letter-spacing: 0; }
.eyebrow { color: var(--purple); font-size: 11px; font-weight: 700; }
.view-section { padding-top: 24px; }

.primary-button, .secondary-button, .danger-button, .table-button, .pagination button, .segmented button, .icon-button {
  min-height: 38px; padding: 0 15px; border: 1px solid transparent; border-radius: var(--radius); font-weight: 600;
}
.primary-button { color: white; background: var(--blue); }
.primary-button:hover { filter: brightness(.94); }
.secondary-button, .table-button, .pagination button { color: var(--blue); border-color: var(--blue); background: var(--surface); }
.secondary-button:hover, .table-button:hover, .pagination button:hover { color: white; background: var(--blue); }
.danger-button { color: var(--purple); border-color: var(--purple); background: var(--surface); }
.danger-button:hover { color: var(--ink); background: var(--purple); }
.icon-button { width: 38px; padding: 0; color: var(--purple); border-color: var(--purple); background: var(--surface); font-size: 22px; }
.icon-button:hover { color: var(--ink); background: var(--purple); }

.toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; padding: 14px 0; border-bottom: 1px solid var(--line); }
.toolbar input[type="search"] { width: min(420px, 48%); }
.toolbar-copy { display: grid; gap: 3px; margin-right: auto; }
.toolbar-copy span { color: var(--muted); font-size: 13px; }
input, select { min-height: 38px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 4px; color: var(--ink); background: var(--surface); outline: none; }
input:focus, select:focus { border-color: var(--blue); box-shadow: 0 0 0 2px rgba(0,118,179,.14); }

.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.stat-card { min-height: 140px; padding: 20px; border: 1px solid var(--line); border-top: 4px solid var(--blue); border-radius: var(--radius); background: var(--surface); }
.stat-card:nth-child(3n + 2) { border-top-color: var(--blue); }
.stat-card:nth-child(3n) { border-top-color: var(--purple); }
.stat-card span, .stat-card small { display: block; color: var(--muted); }
.stat-card strong { display: block; margin: 12px 0 8px; color: var(--blue); font-size: 28px; }
.stat-card small { line-height: 1.6; }
.quick-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-top: 24px; }
.quick-grid button { min-height: 98px; padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); text-align: left; }
.quick-grid button:hover { border-color: var(--purple); }
.quick-grid strong, .quick-grid span { display: block; }
.quick-grid span { margin-top: 8px; color: var(--muted); font-size: 13px; }

.panel { margin-bottom: 18px; padding: 20px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.panel h2 { margin: 0; font-size: 17px; }
.panel p { margin: 5px 0 0; color: var(--muted); font-size: 13px; }
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.table-wrap { max-width: 100%; overflow: auto; border: 1px solid var(--line); }
table { width: 100%; border-collapse: collapse; background: var(--surface); }
th, td { padding: 11px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; white-space: nowrap; }
th { position: sticky; top: 0; z-index: 1; color: var(--ink); background: var(--canvas); font-size: 12px; }
td { font-size: 13px; }
td strong, td small { display: block; }
td small { max-width: 230px; margin-top: 4px; color: var(--muted); white-space: normal; overflow-wrap: anywhere; }
.compact { width: 150px; min-height: 34px; }
.actions { display: flex; gap: 6px; }
.table-button { min-height: 32px; padding: 0 10px; font-size: 12px; }
.empty-state { padding: 28px; color: var(--muted); text-align: center; }
.pagination { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 16px; color: var(--muted); font-size: 13px; }
.pagination button { min-height: 32px; padding: 0 11px; }
.status { display: inline-flex; padding: 3px 7px; border-radius: 4px; color: var(--ink); background: var(--canvas); }
.status.paid, .status.success { color: white; background: var(--blue); }
.status.refunded, .status.failed { color: var(--ink); background: var(--purple); }

.product-list { display: grid; gap: 12px; }
.product-row { display: grid; grid-template-columns: 1.2fr 1.2fr .7fr .7fr .5fr .7fr auto; align-items: end; gap: 10px; padding: 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.product-row label, .group-inputs label { display: grid; gap: 6px; color: var(--muted); font-size: 12px; }
.row-actions { display: flex; gap: 7px; }

.segmented { display: inline-flex; margin-bottom: 18px; padding: 3px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.segmented button { color: var(--muted); background: transparent; }
.segmented button.active { color: var(--ink); background: var(--purple); }
.toggle { display: inline-flex; align-items: center; gap: 8px; }
.toggle input { min-height: auto; accent-color: var(--blue); }
.toggle.small { font-size: 13px; }
.ad-page-list { display: grid; gap: 14px; }
.ad-slot { display: grid; grid-template-columns: 120px minmax(220px, 1fr) 100px 78px 38px; gap: 8px; margin-top: 8px; }
.group-inputs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 16px; }
.string-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; margin-top: 16px; }
.string-toggle { position: relative; }
.string-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.string-toggle span { display: grid; place-items: center; min-height: 44px; border: 1px solid var(--line); border-radius: 4px; background: var(--surface); cursor: pointer; }
.string-toggle input:checked + span { color: white; border-color: var(--blue); background: var(--blue); }
.settings-panel { max-width: 520px; }
.settings-panel h2 { margin-top: 8px; font-size: 22px; }
.settings-panel .danger-button { margin-top: 22px; }

.toast { position: fixed; top: 18px; right: 18px; z-index: 30; max-width: 360px; padding: 11px 15px; border-radius: 4px; color: white; background: var(--blue); opacity: 0; transform: translateY(-8px); pointer-events: none; transition: .16s ease; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--purple); }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: var(--canvas); }
.login-shell { width: min(420px, 100%); min-width: 0; }
.login-panel { min-width: 0; padding: 34px; border: 1px solid var(--line); border-top: 5px solid var(--blue); border-radius: var(--radius); background: var(--surface); }
.login-brand { text-align: center; }
.login-brand .brand-logo { width: 52px; height: 52px; }
.login-brand h1 { margin: 15px 0 7px; font-size: 23px; }
.login-brand p { margin: 0; color: var(--muted); font-size: 13px; }
.login-form { display: grid; gap: 14px; margin-top: 28px; }
.login-form label { display: grid; gap: 7px; color: var(--muted); font-size: 13px; }
.login-form input { width: 100%; min-width: 0; }
.login-button { width: 100%; margin-top: 5px; }
.login-message { min-height: 22px; margin-top: 15px; color: var(--muted); text-align: center; font-size: 13px; }
.login-message.error { color: var(--purple); }

@media (max-width: 1100px) {
  .stats-grid, .quick-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .product-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .row-actions { grid-column: 1 / -1; }
}

@media (max-width: 760px) {
  .app-shell { display: block; }
  .sidebar { position: static; width: 100%; height: auto; padding: 14px; }
  .sidebar-brand { padding-bottom: 12px; }
  .side-nav { display: flex; gap: 6px; margin-top: 12px; overflow-x: auto; }
  .side-nav-item { width: auto; flex: 0 0 auto; }
  .main { padding: 0 14px 32px; }
  .topbar { min-height: 78px; }
  .toolbar { align-items: stretch; flex-wrap: wrap; }
  .toolbar input[type="search"] { width: 100%; }
  .stats-grid, .quick-grid, .group-inputs { grid-template-columns: 1fr; }
  .product-row { grid-template-columns: 1fr 1fr; }
  .ad-slot { grid-template-columns: 1fr 1fr; padding: 10px 0; border-top: 1px solid var(--line); }
  .ad-slot input[data-field="unitId"] { grid-column: 1 / -1; }
  .string-grid { grid-template-columns: repeat(3, 1fr); }
  .panel { padding: 14px; }
}
