:root {
  --bg: #f5f7fb;
  --panel: #ffffff;
  --border: #e4e8f0;
  --text: #1a2233;
  --muted: #6b7488;
  --primary: #2563eb;
  --primary-h: #1d4ed8;
  --accent: #0ea5e9;
  --green: #16a34a;
  --green-bg: #e7f7ec;
  --red: #dc2626;
  --red-bg: #fdeaea;
  --neutral-bg: #eef0f4;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  color: var(--text);
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

.hidden { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  height: 56px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%; background: linear-gradient(135deg, var(--primary), var(--accent)); display: inline-block; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.global-status { color: var(--muted); font-size: 13px; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: calc(100vh - 56px); }

.sidebar {
  width: 260px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  flex-shrink: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sidebar-head h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }

.project-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.project-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
}
.project-item:hover { background: var(--bg); }
.project-item.active { background: #eef4ff; border-color: #cfe0ff; }
.project-item .p-name { font-weight: 600; }
.project-item .p-domain { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-item .p-meta { font-size: 11px; color: var(--muted); margin-top: 2px; }

.content { flex: 1; padding: 24px; max-width: 1200px; }

.empty-state { text-align: center; padding: 80px 20px; color: var(--muted); }
.empty-state h3 { font-size: 22px; color: var(--text); }

/* ---------- Project header ---------- */
.project-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.project-header h1 { margin: 0 0 4px; font-size: 22px; }
.project-domain { color: var(--primary); text-decoration: none; font-weight: 500; }
.project-domain:hover { text-decoration: underline; }
.project-meta { display: block; color: var(--muted); font-size: 13px; margin-top: 4px; }
.project-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--bg); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-h); border-color: var(--primary-h); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-accent:hover { filter: brightness(.95); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-danger { color: var(--red); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-icon { padding: 4px 8px; font-size: 15px; }

/* ---------- Table ---------- */
.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
.kw-table { width: 100%; border-collapse: collapse; min-width: 720px; }
.kw-table th, .kw-table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.kw-table thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: #fafbfd; position: sticky; top: 0; }
.kw-table th.sortable { cursor: pointer; user-select: none; }
.kw-table th.sortable:hover { color: var(--text); background: #eef2fb; }
.sort-arrow { display: inline-block; font-size: 11px; margin-left: 4px; color: var(--primary); font-weight: 700; }
.kw-table tbody tr:last-child td { border-bottom: none; }
.kw-table tbody tr:hover { background: #fafbfd; }
.kw-table .num { text-align: right; }
.kw-table .actions-col { text-align: right; white-space: nowrap; }

.kw-name { font-weight: 600; cursor: pointer; color: var(--primary); }
.kw-name:hover { text-decoration: underline; }
.kw-locale { font-size: 11px; color: var(--muted); }

.pos { font-size: 20px; font-weight: 700; }
.pos.big { font-size: 20px; }
.pos.notfound { color: var(--muted); font-weight: 600; font-size: 15px; }
.pos-prev { color: var(--muted); font-size: 14px; }

.badge { display: inline-block; padding: 3px 8px; border-radius: 999px; font-weight: 700; font-size: 13px; min-width: 34px; text-align: center; }
.delta-up { background: var(--green-bg); color: var(--green); }
.delta-down { background: var(--red-bg); color: var(--red); }
.delta-neutral { background: var(--neutral-bg); color: var(--muted); }

.kw-url { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; vertical-align: middle; color: var(--text); text-decoration: none; }
.kw-url:hover { color: var(--primary); text-decoration: underline; }

.date-cell { color: var(--muted); font-size: 12px; white-space: nowrap; }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Modals ---------- */
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 100; overflow-y: auto; }
.modal-box { background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); padding: 24px; width: 100%; max-width: 440px; }
.modal-lg { max-width: 560px; }
.modal-xl { max-width: 1000px; }
.modal-box h3 { margin: 0 0 12px; font-size: 18px; }
.modal-box h4 { margin: 0 0 8px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.modal-box label { display: block; margin: 12px 0 4px; font-size: 13px; font-weight: 600; }
.modal-box input[type="text"], .modal-box input[type="password"], .modal-box textarea, .modal-box select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; font-family: inherit;
}
.modal-box textarea { resize: vertical; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.error { color: var(--red); font-size: 13px; margin-top: 10px; }

.advanced { margin-top: 14px; }
.advanced summary { cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 600; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 14px; margin-top: 8px; }

/* ---------- Detail modal ---------- */
.detail-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.detail-head h3 { margin: 0; flex: 1; }
.detail-badges { display: flex; gap: 6px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; margin-top: 12px; }
@media (max-width: 800px) { .detail-grid { grid-template-columns: 1fr; } }

.chart-wrap { position: relative; height: 300px; background: #fafbfd; border: 1px solid var(--border); border-radius: var(--radius); padding: 8px; }
.chart-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 13px; }

.serp-list { list-style: none; margin: 0; padding: 0; max-height: 420px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.serp-item { padding: 12px 14px; border-bottom: 1px solid var(--border); }
.serp-item:last-child { border-bottom: none; }
.serp-item .s-top { display: flex; align-items: baseline; gap: 8px; }
.serp-item .s-pos { font-weight: 700; color: var(--muted); font-size: 12px; min-width: 24px; }
.serp-item .s-domain { color: var(--muted); font-size: 12px; }
.serp-item a.s-title { color: #1a0dab; text-decoration: none; font-size: 15px; display: block; }
.serp-item a.s-title:hover { text-decoration: underline; }
.serp-item .s-snippet { color: #4a5568; font-size: 13px; margin-top: 3px; }
.serp-item.ours { background: #eefaf1; border-left: 3px solid var(--green); }
.serp-item.ours .s-pos { color: var(--green); }
.ours-tag { background: var(--green-bg); color: var(--green); font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 4px; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px; font-size: 13px; z-index: 200; box-shadow: var(--shadow); }
.toast.error { background: var(--red); }
