:root {
  --bg:      #f9fafb;
  --surface: #ffffff;
  --fg:      #111827;
  --muted:   #6b7280;
  --border:  #e5e7eb;
  --accent:  #2563eb;
  --accent-h:#1d4ed8;
  --danger:  #dc2626;
  --danger-h:#b91c1c;
  --success: #16a34a;
  --error:   #dc2626;
  --radius:  6px;
  --shadow:  0 1px 3px rgba(0,0,0,.08);
  --font:    -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  font-size: 14px;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Header ── */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.site-logo {
  font-size: 22px;
  font-weight: 700;
  color: var(--fg);
  text-decoration: none;
  letter-spacing: -1px;
}
.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.header-nav {
  display: flex;
  gap: 2px;
}

/* ── Main ── */
.site-main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px;
}

/* ── Card ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 14px;
  color: var(--fg);
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--fg);
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--bg); text-decoration: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-h); border-color: var(--accent-h); }

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.btn-danger:hover { background: var(--danger-h); border-color: var(--danger-h); }

.btn-ghost {
  border-color: transparent;
  background: transparent;
}
.btn-ghost:hover { background: var(--bg); }

.btn-sm { padding: 3px 8px; font-size: 12px; }
.btn-icon { padding: 3px 7px; font-size: 14px; line-height: 1; }

.btn-copy {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  padding: 0 2px;
  vertical-align: middle;
}
.btn-copy:hover { color: var(--accent); }

/* ── Flash messages ── */
.flash {
  padding: 10px 14px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-size: 13px;
  font-weight: 500;
}
.flash-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.flash-error   { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

/* ── Login page ── */
.login-wrap {
  max-width: 360px;
  margin: 80px auto 0;
  text-align: center;
}
.login-title {
  font-size: 42px;
  font-weight: 700;
  letter-spacing: -2px;
  margin-bottom: 24px;
}
.login-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.login-form input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
}
.login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(37,99,235,.2); }
.login-form button {
  padding: 9px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.login-form button:hover { background: var(--accent-h); }

/* ── Category badge ── */
.cat-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 99px;
  background: #eff6ff;
  color: #1d4ed8;
  border: 1px solid #bfdbfe;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}

/* ── Dashboard ── */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
@media (max-width: 700px) {
  .dashboard-grid { grid-template-columns: 1fr; }
}
.period-card { margin-bottom: 0; }
.period-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.period-label {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 0;
}
.period-totals {
  display: flex;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}
.period-clicks {
  font-weight: 600;
  color: var(--fg);
}
.period-table { font-size: 12px; }

/* ── Add form ── */
.add-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
}
.add-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
}
.add-form input {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
  min-width: 180px;
}
.add-form input[name="url"]     { min-width: 300px; }
.add-form input[name="keyword"] { min-width: 120px; }
.add-form input:focus { border-color: var(--accent); }
.add-form button { align-self: flex-end; }
.add-form-select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--bg);
  outline: none;
  align-self: flex-end;
  height: 32px;
}
.add-form-select:focus { border-color: var(--accent); }

/* ── List header ── */
.list-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.stats-summary {
  display: flex;
  gap: 16px;
  font-size: 13px;
  color: var(--muted);
}
.search-form {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}
.search-form input, .search-form select {
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  background: var(--bg);
  outline: none;
}
.search-form input:focus, .search-form select:focus { border-color: var(--accent); }

/* ── Links table ── */
.links-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  table-layout: fixed;
}
.links-table th {
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.links-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.links-table tr:last-child td { border-bottom: none; }
.links-table tr:hover td { background: var(--bg); }

.cell-keyword   { font-weight: 600; white-space: nowrap; width: 110px; }
.cell-url       { width: 28%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-title     { color: var(--muted); width: 18%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-category  { white-space: nowrap; width: 90px; }
.cell-date      { white-space: nowrap; color: var(--muted); font-size: 12px; width: 110px; }
.cell-actions   { white-space: nowrap; width: 90px; }
.cell-actions > * { margin-right: 4px; }
.num                { text-align: right; }
.links-table th.num,
.stats-table th.num { text-align: right; }

/* ── Pagination ── */
.pagination {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  font-size: 13px;
  color: var(--muted);
}

/* ── Empty state ── */
.empty { color: var(--muted); font-size: 13px; padding: 16px 0; }

/* ── Edit form ── */
.add-form label span { margin-bottom: 2px; }
.form-actions { display: flex; gap: 8px; margin-top: 4px; }

/* ── Stats ── */
.stat-grid {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 8px 12px;
  font-size: 13px;
  margin-bottom: 4px;
}
.stat-grid dt { color: var(--muted); font-weight: 500; align-self: center; }
.stat-grid dd { word-break: break-all; }

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.stats-table th {
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}
.stats-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
}
.stats-table tr:last-child td { border-bottom: none; }

/* ── Stats day expand button ── */
.btn-day-expand {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  font-size: 13px;
  color: var(--fg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-align: left;
}
.btn-day-expand:hover { color: var(--accent); }

.expand-icon {
  font-size: 10px;
  color: var(--muted);
  transition: transform .15s;
  display: inline-block;
  width: 10px;
}

/* ── Detail row (hidden by default, shown when open) ── */
.detail-row { display: none; }
.detail-row--open { display: table-row; }

.detail-cell {
  padding: 0 10px 12px !important;
  background: var(--bg);
}

.detail-loading {
  font-size: 12px;
  color: var(--muted);
  padding: 8px 0;
  display: block;
}

/* ── Detail inner table ── */
.detail-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  margin-top: 8px;
}
.detail-table th {
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.detail-table td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.detail-table tr:last-child td { border-bottom: none; }

.detail-time { white-space: nowrap; color: var(--muted); }
.detail-ip   { white-space: nowrap; font-family: monospace; }
.detail-ua   { max-width: 400px; word-break: break-all; color: var(--muted); }
.detail-ref  { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
