:root {
    --ivory: #f4f0e7;
    --paper: #fffdf8;
    --paper-deep: #eee8dc;
    --ink: #171917;
    --muted: #666960;
    --line: #d7d1c4;
    --line-dark: #b9b2a5;
    --cobalt: #1748e5;
    --cobalt-deep: #0d32ad;
    --cobalt-soft: #e7edff;
    --emerald: #087b58;
    --emerald-soft: #e2f3eb;
    --amber: #a25b00;
    --amber-soft: #fff0ce;
    --red: #ad292e;
    --red-soft: #fbe7e7;
    --shadow: 0 16px 34px rgba(36, 32, 23, .07);
    --radius: 8px;
}

*, *::before, *::after { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    min-height: 100vh;
    font-family: Tahoma, "Segoe UI", sans-serif;
    background: var(--ivory);
    color: var(--ink);
    font-size: 14px;
    line-height: 1.65;
}

button, input, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: inherit; }

:focus-visible {
    outline: 3px solid rgba(23, 72, 229, .3);
    outline-offset: 3px;
}

.ltr, .numeric, .date-value, .url-value {
    direction: ltr;
    unicode-bidi: isolate;
    font-variant-numeric: tabular-nums;
}

.skip-link {
    position: fixed;
    z-index: 100;
    top: 12px;
    right: 12px;
    padding: 8px 14px;
    background: var(--ink);
    color: white;
    transform: translateY(-160%);
}

.skip-link:focus { transform: translateY(0); }

.command-header {
    color: white;
    background:
        linear-gradient(90deg, rgba(255,255,255,.055) 1px, transparent 1px) 0 0 / 34px 34px,
        var(--ink);
    border-bottom: 4px solid var(--cobalt);
}

.command-header__inner {
    width: min(1480px, calc(100% - 48px));
    min-height: 92px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 28px;
}

.brand-lockup, .header-actions, .filter-actions, .legend,
.panel-heading, .section-heading, .pagination-bar, .pagination-controls,
.row-actions, .dialog-heading, .dialog-actions, .link-totals {
    display: flex;
    align-items: center;
}

.brand-lockup { gap: 14px; }
.brand-lockup h1 { margin: 0; font-size: 21px; letter-spacing: -.02em; }

.brand-mark {
    direction: ltr;
    width: 52px;
    height: 52px;
    display: grid;
    place-items: center;
    background: var(--cobalt);
    border: 1px solid #7191ff;
    font-weight: 900;
    letter-spacing: -.08em;
}

.eyebrow, .section-kicker {
    margin: 0 0 2px;
    color: #aeb4ab;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: .12em;
    text-transform: uppercase;
}

.section-kicker { color: var(--cobalt); }

.header-actions { gap: 10px; }

.live-status {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: 8px;
    color: #d9ddd7;
    font-size: 12px;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #63daa6;
    box-shadow: 0 0 0 4px rgba(99, 218, 166, .13);
}

.live-status.is-loading .status-dot {
    background: #f5b84c;
    animation: pulse 1.2s ease-in-out infinite;
}

.live-status.is-error .status-dot { background: #f27070; }

@keyframes pulse { 50% { opacity: .35; } }

.dashboard-shell {
    width: min(1480px, calc(100% - 48px));
    margin: 34px auto 70px;
}

.filter-bar, .trend-panel, .events-panel, .links-section {
    background: var(--paper);
    border: 1px solid var(--line);
    box-shadow: var(--shadow);
}

.filter-bar {
    padding: 20px 22px;
    display: grid;
    grid-template-columns: 180px minmax(0, 1fr);
    gap: 28px;
    align-items: end;
    border-top: 3px solid var(--ink);
}

h2, h3, p { margin-top: 0; }
.filter-bar h2, .section-heading h2, .panel-heading h2 { margin: 0; font-size: 18px; }

.filters {
    min-width: 0;
    display: grid;
    grid-template-columns: auto 145px 145px minmax(170px, 1fr) auto;
    gap: 12px;
    align-items: end;
}

.preset-group { display: flex; border: 1px solid var(--line-dark); background: white; }
.preset-button {
    min-height: 42px;
    padding: 0 13px;
    border: 0;
    border-left: 1px solid var(--line);
    color: var(--muted);
    background: transparent;
    cursor: pointer;
}
.preset-button:last-child { border-left: 0; }
.preset-button:hover { background: var(--paper-deep); }
.preset-button.is-active { background: var(--ink); color: white; }

.field { display: grid; gap: 5px; color: var(--muted); font-size: 11px; font-weight: 700; }
.field input, .field select, .event-search input {
    width: 100%;
    min-height: 42px;
    padding: 8px 11px;
    color: var(--ink);
    background: #fff;
    border: 1px solid var(--line-dark);
    border-radius: 3px;
}
.field input:hover, .field select:hover, .event-search input:hover { border-color: #7e7b73; }
.field input:focus, .field select:focus, .event-search input:focus { border-color: var(--cobalt); outline: 2px solid var(--cobalt-soft); }

.filter-actions { gap: 8px; }

.button {
    min-height: 44px;
    padding: 8px 15px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    border: 1px solid transparent;
    border-radius: 3px;
    font-weight: 700;
    font-size: 13px;
    text-decoration: none;
    cursor: pointer;
    transition: background .16s ease, border-color .16s ease, color .16s ease, transform .16s ease;
}
.button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.button--primary { background: var(--cobalt); color: white; }
.button--primary:hover { background: var(--cobalt-deep); }
.button--outline { color: var(--cobalt); background: white; border-color: var(--cobalt); }
.button--outline:hover { background: var(--cobalt-soft); }
.button--quiet { color: inherit; background: transparent; border-color: currentColor; }
.command-header .button--quiet { color: #e4e7e2; border-color: #555952; }
.button--danger-ghost { color: #ffc4c6; background: transparent; border-color: #7b4142; }
.button--ink { color: white; background: var(--ink); }

.kpi-section, .breakdowns { margin-top: 36px; }
.section-heading, .panel-heading { justify-content: space-between; gap: 24px; margin-bottom: 16px; }
.section-note { max-width: 620px; margin: 0; color: var(--muted); font-size: 13px; }

.kpi-strip {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    border: 1px solid var(--line);
    border-top: 3px solid var(--ink);
    background: var(--paper);
    box-shadow: var(--shadow);
}

.kpi {
    min-width: 0;
    min-height: 145px;
    padding: 18px 16px 15px;
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--line);
    position: relative;
}
.kpi:last-child { border-left: 0; }
.kpi::after { content: ""; position: absolute; right: 16px; left: 16px; bottom: 0; height: 3px; background: var(--line-dark); }
.kpi--primary::after { background: var(--cobalt); }
.kpi--healthy::after { background: var(--emerald); }
.kpi--bot::after { background: var(--red); }
.kpi--legacy { background: var(--amber-soft); }
.kpi--legacy::after { background: var(--amber); }
.kpi__label { min-height: 39px; color: var(--muted); font-size: 12px; font-weight: 700; }
.kpi__value { margin-top: 5px; font-size: clamp(26px, 2.2vw, 38px); line-height: 1.1; letter-spacing: -.04em; }
.kpi__context { margin-top: auto; color: #85877f; font-size: 10px; }
.info-button {
    width: 24px;
    height: 24px;
    padding: 0;
    display: inline-grid;
    place-items: center;
    border: 1px solid var(--line-dark);
    border-radius: 50%;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 11px;
}
.info-button[aria-expanded="true"] { color: white; background: var(--cobalt); border-color: var(--cobalt); }
.kpi-help {
    z-index: 3;
    position: absolute;
    top: 49px;
    right: 14px;
    left: 14px;
    padding: 9px 10px;
    color: white;
    background: var(--ink);
    border-right: 3px solid var(--cobalt);
    box-shadow: 0 8px 20px rgba(0,0,0,.18);
    font-size: 11px;
    line-height: 1.7;
}
.kpi-help[hidden] { display: none; }

.trend-panel, .events-panel, .links-section {
    margin-top: 36px;
    padding: 24px;
}
.trend-panel { border-top: 3px solid var(--cobalt); }
.panel-heading { margin-bottom: 20px; }
.legend { gap: 18px; color: var(--muted); font-size: 11px; }
.legend > span { display: flex; align-items: center; gap: 7px; }
.legend-line { width: 24px; height: 3px; background: var(--cobalt); }
.legend-line--visitors { background: var(--emerald); }
#trendChart { display: block; width: 100%; height: 280px; }
.trend-footer { display: flex; justify-content: space-between; gap: 20px; padding-top: 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px; }
.text-button { min-height: 44px; padding: 7px 4px; border: 0; background: none; color: var(--cobalt); font-weight: 700; cursor: pointer; }
.trend-table { margin-top: 16px; max-height: 260px; }

.loading-state, .empty-state {
    padding: 22px;
    color: var(--muted);
    background: repeating-linear-gradient(-45deg, #faf7f0, #faf7f0 8px, #f5f1e9 8px, #f5f1e9 16px);
    border: 1px solid var(--line);
    text-align: center;
}
.empty-state { background: var(--paper-deep); }
.error-state { color: var(--red); background: var(--red-soft); border-color: #e8b3b5; }

.breakdown-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    border-top: 3px solid var(--ink);
    border-right: 1px solid var(--line);
}
.rank-panel {
    min-width: 0;
    padding: 20px;
    background: var(--paper);
    border-left: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
}
.rank-panel h3 { margin-bottom: 15px; font-size: 14px; }
.rank-panel ol { margin: 0; padding: 0; list-style: none; }
.rank-row {
    min-height: 43px;
    display: grid;
    grid-template-columns: 24px minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    border-top: 1px solid #e8e3da;
}
.rank-index { color: #999991; font-size: 10px; }
.rank-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 700; font-size: 12px; }
.rank-values { text-align: left; color: var(--muted); font-size: 10px; }
.rank-values strong { display: block; color: var(--ink); font-size: 13px; }
.rank-empty { padding: 24px 0; color: var(--muted); font-size: 11px; }

.events-panel { border-top: 3px solid var(--ink); }
.events-heading { align-items: end; }
.event-search { width: min(480px, 100%); }
.event-search label { display: block; margin-bottom: 5px; color: var(--muted); font-size: 11px; font-weight: 700; }
.search-box { display: grid; grid-template-columns: 1fr auto; }
.search-box input { border-left: 0; }
.privacy-note { margin-bottom: 14px; padding: 9px 12px; color: #6f4c12; background: var(--amber-soft); border-right: 3px solid var(--amber); font-size: 11px; }
.table-scroll { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
    padding: 10px 11px;
    color: var(--muted);
    background: var(--paper-deep);
    border-bottom: 1px solid var(--line-dark);
    font-size: 10px;
    font-weight: 700;
    text-align: right;
    white-space: nowrap;
}
td { padding: 11px; border-bottom: 1px solid #e7e2d8; vertical-align: middle; font-size: 11px; }
tbody tr:hover { background: #f8f5ee; }
.events-table { min-width: 1120px; }
.events-table td { max-width: 180px; }
.primary-cell { display: block; font-weight: 700; }
.secondary-cell { display: block; color: var(--muted); font-size: 9px; }
.truncate { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { display: inline-block; padding: 2px 7px; border: 1px solid var(--line); border-radius: 12px; font-size: 9px; font-weight: 700; }
.badge--human { color: var(--emerald); background: var(--emerald-soft); border-color: #a8d9c5; }
.badge--bot { color: var(--red); background: var(--red-soft); border-color: #eab5b7; }
.pagination-bar { justify-content: space-between; gap: 20px; margin-top: 16px; color: var(--muted); font-size: 11px; }
.pagination-controls { gap: 12px; }

.links-section { border-top: 3px solid var(--cobalt); scroll-margin-top: 24px; }
.link-totals { gap: 8px; }
.link-totals span { padding: 5px 9px; background: var(--paper-deep); border: 1px solid var(--line); font-size: 10px; }
.create-form {
    margin-bottom: 18px;
    padding: 18px;
    display: grid;
    grid-template-columns: 240px minmax(280px, 1fr) 180px auto;
    gap: 14px;
    align-items: end;
    background: #eeebe3;
    border: 1px solid var(--line);
}
.create-form__heading h3 { margin: 0; font-size: 14px; }
.create-form__heading p { margin: 3px 0 0; color: var(--muted); font-size: 10px; }
.links-table { min-width: 1020px; }
.url-value { display: block; max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--cobalt); text-decoration: none; }
.code-copy { padding: 3px 7px; border: 0; background: var(--cobalt-soft); color: var(--cobalt); cursor: pointer; font-weight: 700; }
.row-actions { gap: 5px; white-space: nowrap; }
.mini-button { min-height: 34px; padding: 6px 9px; border: 1px solid var(--line-dark); background: white; color: var(--ink); border-radius: 2px; cursor: pointer; font-size: 11px; }
.mini-button:hover { border-color: var(--cobalt); color: var(--cobalt); }
.mini-button--danger:hover { border-color: var(--red); color: var(--red); }
.empty-cell { padding: 40px; color: var(--muted); text-align: center; }
.action-message { margin: -6px 0 16px; padding: 10px 12px; color: var(--emerald); background: var(--emerald-soft); border-right: 3px solid var(--emerald); font-size: 11px; }
.action-message.is-error { color: var(--red); background: var(--red-soft); border-color: var(--red); }

.edit-dialog {
    width: min(520px, calc(100% - 30px));
    padding: 0;
    color: var(--ink);
    background: var(--paper);
    border: 1px solid var(--line-dark);
    border-top: 4px solid var(--cobalt);
    box-shadow: 0 28px 80px rgba(0,0,0,.25);
}
.edit-dialog::backdrop { background: rgba(16, 18, 17, .62); }
.edit-dialog form { padding: 24px; }
.dialog-heading { justify-content: space-between; }
.dialog-heading h2 { margin: 0; font-size: 18px; }
.dialog-close { border: 0; background: none; font-size: 28px; cursor: pointer; }
.edit-dialog p { color: var(--muted); font-size: 11px; }
.edit-dialog .field { margin-top: 18px; }
.dialog-actions { justify-content: flex-end; gap: 8px; margin-top: 20px; }
.toast {
    position: fixed;
    z-index: 50;
    left: 24px;
    bottom: 24px;
    max-width: 360px;
    padding: 12px 16px;
    color: white;
    background: var(--ink);
    border-right: 4px solid var(--emerald);
    box-shadow: 0 12px 30px rgba(0,0,0,.22);
    font-size: 11px;
}
.toast.is-error { border-color: #ff686d; }

/* Keep the existing login page usable. */
body:not(.admin-page) input[type="text"],
body:not(.admin-page) input[type="password"] {
    font-family: inherit;
}

@media (max-width: 1180px) {
    .filters { grid-template-columns: auto repeat(2, 135px) minmax(150px, 1fr); }
    .filter-actions { grid-column: 1 / -1; justify-content: flex-end; }
    .kpi-strip { grid-template-columns: repeat(4, 1fr); }
    .kpi:nth-child(4) { border-left: 0; }
    .kpi:nth-child(-n+4) { border-bottom: 1px solid var(--line); }
    .create-form { grid-template-columns: 1fr 1fr; }
    .create-form__heading { grid-column: 1 / -1; }
}

@media (max-width: 820px) {
    .command-header__inner, .dashboard-shell { width: min(100% - 28px, 1480px); }
    .command-header__inner { padding: 16px 0; align-items: flex-start; }
    .header-actions { flex-wrap: wrap; justify-content: flex-end; }
    .live-status { width: 100%; justify-content: flex-end; }
    .filter-bar { grid-template-columns: 1fr; }
    .filters { grid-template-columns: 1fr 1fr; }
    .preset-group, .field--link, .filter-actions { grid-column: 1 / -1; }
    .preset-button { flex: 1; }
    .kpi-strip { grid-template-columns: repeat(2, 1fr); }
    .kpi:nth-child(n) { border-bottom: 1px solid var(--line); border-left: 1px solid var(--line); }
    .kpi:nth-child(2n) { border-left: 0; }
    .breakdown-grid { grid-template-columns: repeat(2, 1fr); }
    .panel-heading, .section-heading { align-items: flex-start; flex-direction: column; }
    .events-heading { align-items: stretch; }
    .event-search { width: 100%; }
}

@media (max-width: 560px) {
    body { font-size: 13px; }
    .command-header__inner { flex-direction: column; }
    .header-actions { width: 100%; justify-content: flex-start; }
    .live-status { width: auto; margin-left: auto; justify-content: flex-start; }
    .brand-mark { width: 46px; height: 46px; }
    .dashboard-shell { margin-top: 20px; }
    .filter-bar, .trend-panel, .events-panel, .links-section { padding: 17px; }
    .filters { grid-template-columns: 1fr; }
    .preset-group, .field--link, .filter-actions { grid-column: auto; }
    .filter-actions .button { flex: 1; }
    .kpi-strip { grid-template-columns: 1fr; }
    .kpi:nth-child(n) { min-height: 118px; border-left: 0; }
    .kpi__label { min-height: auto; }
    .breakdown-grid { grid-template-columns: 1fr; }
    .trend-footer, .pagination-bar { align-items: flex-start; flex-direction: column; }
    #trendChart { height: 220px; }
    .create-form { grid-template-columns: 1fr; }
    .create-form__heading { grid-column: auto; }
    .create-form .button { width: 100%; }
    .mini-button { min-height: 44px; padding-inline: 12px; }
    .toast { right: 14px; left: 14px; bottom: 14px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
