:root {
    --bg: #faf8f5;
    --surface: #ffffff;
    --border: #e7e2d9;
    --text: #1c1917;
    --text-muted: #8c8579;
    --accent: #4338ca;
    --accent-soft: #edebfb;
    --accent-contrast: #ffffff;
    --danger: #b3261e;
    --danger-soft: #fbeae8;
    --success: #1f7a4d;
    --radius: 7px;
    --font-sans: "IBM Plex Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
    --font-mono: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: var(--font-sans);
    font-size: 14.5px;
    line-height: 1.55;
    color: var(--text);
    background-color: var(--bg);
    background-image: radial-gradient(circle at 1px 1px, rgba(28, 25, 23, 0.045) 1px, transparent 0);
    background-size: 22px 22px;
}

/* ---------- Topbar ---------- */

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
    padding: 0 2rem;
    height: 60px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    display: flex;
    align-items: center;
    gap: .5rem;
    font-family: var(--font-mono);
    font-weight: 600;
    font-size: 1rem;
    letter-spacing: .04em;
    color: var(--text);
    text-decoration: none;
    white-space: nowrap;
}

.brand::before {
    content: "";
    display: inline-block;
    width: 9px;
    height: 9px;
    border-radius: 2px;
    background: var(--accent);
}

.brand-accent {
    color: var(--text-muted);
    font-weight: 400;
}

.topbar nav {
    display: flex;
    align-items: center;
    gap: 1.75rem;
}

.topbar nav a {
    position: relative;
    color: var(--text-muted);
    text-decoration: none;
    font-weight: 500;
    font-size: .9rem;
    padding: .3rem 0;
    transition: color .15s ease;
}

.topbar nav a:hover {
    color: var(--text);
}

.topbar nav a.active {
    color: var(--text);
}

.topbar nav a.active::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: -10px;
    height: 2px;
    background: var(--accent);
}

.topbar nav a.nav-logout {
    margin-left: .5rem;
    padding: .4rem .9rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
}

.topbar nav a.nav-logout:hover {
    border-color: var(--text-muted);
    color: var(--text);
}

/* ---------- Layout ---------- */

main {
    max-width: 1000px;
    margin: 0 auto;
    padding: 2.5rem 2rem 4rem;
}

h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -.01em;
    margin: 0 0 1.5rem;
}

h2 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 2.5rem 0 .85rem;
    padding-bottom: .5rem;
    border-bottom: 1px solid var(--border);
}

h1, h2 {
    color: var(--text);
}

a {
    color: var(--accent);
}

p.lede {
    color: var(--text-muted);
    margin-top: -.75rem;
    margin-bottom: 1.5rem;
}

/* ---------- Stat cards ---------- */

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 1px;
    margin-bottom: .5rem;
    background: var(--border);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.card {
    background: var(--surface);
    padding: 1.1rem 1.3rem;
}

.card-value {
    font-family: var(--font-mono);
    font-size: 1.75rem;
    font-weight: 600;
    letter-spacing: -.01em;
    line-height: 1.2;
}

.card-label {
    color: var(--text-muted);
    font-size: .72rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: .08em;
    margin-top: .35rem;
}

.card-sub {
    color: var(--text-muted);
    font-size: .75rem;
    margin-top: .2rem;
}

.kv-sub {
    color: var(--text-muted);
    font-size: .82rem;
    margin-left: .5rem;
}

/* ---------- Tables ---------- */

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 1.5rem;
    font-size: .9rem;
}

table.kv th {
    width: 240px;
}

th, td {
    padding: .65rem .9rem;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: middle;
}

thead th {
    font-size: .72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--text-muted);
    background: var(--bg);
}

table.kv th {
    font-weight: 500;
    color: var(--text-muted);
    background: transparent;
}

tbody tr:last-child td {
    border-bottom: none;
}

tbody tr:hover {
    background: var(--bg);
}

table.kv tbody tr:hover {
    background: transparent;
}

td .mono, th .mono, .mono {
    font-family: var(--font-mono);
}

td a {
    font-family: var(--font-mono);
    font-weight: 500;
    text-decoration: none;
}

td a:hover {
    text-decoration: underline;
}

/* ---------- Badges ---------- */

.badge {
    display: inline-block;
    padding: .15rem .55rem;
    border-radius: 999px;
    font-size: .72rem;
    font-weight: 600;
    letter-spacing: .03em;
    margin-left: .5rem;
    text-transform: uppercase;
}

.badge-alert {
    background: var(--danger-soft);
    color: var(--danger);
}

.badge-pro {
    background: var(--accent-soft);
    color: var(--accent);
}

.badge-free {
    background: var(--bg);
    color: var(--text-muted);
}

.badge-danger {
    background: var(--danger-soft);
    color: var(--danger);
    border: 1px solid var(--border);
}

/* ---------- Chart ---------- */

.bar-chart {
    margin-bottom: 1.5rem;
}

.bar-row {
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-bottom: .3rem;
}

.bar-date {
    font-family: var(--font-mono);
    font-size: .78rem;
    color: var(--text-muted);
    min-width: 42px;
    text-align: right;
}

.bar-track {
    flex: 1;
    height: 20px;
    background: var(--border);
    border-radius: 3px;
    overflow: hidden;
}

.bar-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    min-width: 2px;
    transition: width .3s ease;
}

.bar-value {
    font-family: var(--font-mono);
    font-size: .8rem;
    font-weight: 600;
    min-width: 32px;
}

.h2-sub {
    font-size: .8rem;
    font-weight: 400;
    color: var(--text-muted);
}

.chart {
    display: flex;
    align-items: flex-end;
    gap: 5px;
    height: 130px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1rem .9rem;
    margin-bottom: 1.5rem;
}

.chart-bar {
    flex: 1;
    background: var(--accent);
    opacity: .85;
    border-radius: 2px 2px 0 0;
    min-height: 3px;
    transition: opacity .15s ease;
}

.chart-bar:hover {
    opacity: 1;
}

.chart-empty {
    color: var(--text-muted);
}

/* ---------- Sparklines ---------- */

.sparklines {
    display: flex;
    flex-direction: column;
    gap: .9rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    margin-bottom: 1.5rem;
}

.sparkline-row {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.sparkline-label {
    width: 3.5rem;
    flex-shrink: 0;
    color: var(--text-muted);
    font-size: .8rem;
}

.sparkline {
    flex: 1;
    height: 48px;
    display: block;
}

.sparkline polyline {
    fill: none;
    stroke: var(--accent);
    stroke-width: 2;
    vector-effect: non-scaling-stroke;
}

/* ---------- Forms & inputs ---------- */

input[type="search"],
input[type="text"],
input[type="number"],
select {
    font-family: var(--font-sans);
    font-size: .9rem;
    padding: .55rem .75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    outline: none;
    transition: border-color .15s ease, box-shadow .15s ease;
}

input[type="search"]:focus,
input[type="text"]:focus,
input[type="number"]:focus,
select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

input[type="search"] {
    width: 100%;
    max-width: 420px;
    margin-bottom: 1.5rem;
}

input[type="number"] {
    width: 100%;
    max-width: 160px;
}

label {
    display: block;
    font-size: .8rem;
    font-weight: 500;
    color: var(--text-muted);
    margin-bottom: .35rem;
}

label.checkbox {
    display: flex;
    align-items: center;
    gap: .5rem;
    flex-direction: row;
    font-weight: 500;
    color: var(--text);
    cursor: pointer;
}

label.checkbox input {
    width: auto;
    margin: 0;
}

button,
.btn {
    font-family: var(--font-sans);
    font-size: .85rem;
    font-weight: 600;
    padding: .55rem 1.1rem;
    border-radius: var(--radius);
    border: 1px solid var(--accent);
    background: var(--accent);
    color: var(--accent-contrast);
    cursor: pointer;
    transition: opacity .15s ease;
}

button:hover,
.btn:hover {
    opacity: .88;
}

button.secondary {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

button.secondary:hover {
    border-color: var(--text-muted);
    opacity: 1;
}

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

.btn-danger:hover {
    opacity: .88;
}

/* ---------- Action / plan forms ---------- */

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: .75rem;
    margin-bottom: 1.5rem;
}

.actions form {
    margin: 0;
}

.inline-form {
    display: flex;
    align-items: center;
    gap: .6rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: .5rem .75rem;
}

.inline-form label {
    margin: 0;
    font-weight: 500;
    color: var(--text);
}

.inline-form input[type="number"] {
    width: 70px;
    max-width: none;
}

.inline-form span {
    color: var(--text-muted);
    font-size: .85rem;
}

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: .9rem;
    margin-bottom: 1.25rem;
}

.filter-bar label {
    margin: 0 0 .35rem;
}

.filter-bar select {
    min-width: 220px;
}

.pagination {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 1rem;
    color: var(--text-muted);
    font-size: .85rem;
}

.plan-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 1.1rem;
    align-items: end;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1.4rem;
    margin-bottom: 1.5rem;
}

.plan-form button {
    grid-column: -2 / -1;
    justify-self: start;
}

/* ---------- Broadcast / form helpers ---------- */

textarea {
    font-family: var(--font-sans);
    font-size: .9rem;
    padding: .55rem .75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--text);
    outline: none;
    width: 100%;
    max-width: 560px;
    resize: vertical;
    transition: border-color .15s ease, box-shadow .15s ease;
}

textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.form-group {
    margin-bottom: 1rem;
}

.form-group select {
    max-width: 280px;
    width: 100%;
}

.alert {
    padding: .75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1.5rem;
    font-weight: 500;
    font-size: .9rem;
}

.alert-ok {
    background: #e8f5e9;
    color: var(--success);
    border: 1px solid #c8e6c9;
}

.alert-warn {
    background: var(--danger-soft);
    color: var(--danger);
    border: 1px solid #f5c6c2;
}

.admin-add-form {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    flex-wrap: wrap;
    margin-bottom: 2rem;
}

.admin-add-form input[type="number"],
.admin-add-form input[type="text"] {
    max-width: 220px;
}

.msg-form {
    max-width: 560px;
    margin-bottom: 2rem;
}

/* ---------- Funnel ---------- */

.funnel {
    margin-bottom: 2rem;
}

.funnel-step {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: .5rem;
}

.funnel-bar-wrap {
    flex: 1;
    position: relative;
    height: 32px;
    background: var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.funnel-bar {
    height: 100%;
    background: var(--accent);
    border-radius: var(--radius);
    min-width: 2px;
    transition: width .3s ease;
}

.funnel-label {
    position: absolute;
    top: 50%;
    left: .75rem;
    transform: translateY(-50%);
    font-size: .8rem;
    font-weight: 600;
    color: var(--accent-contrast);
    white-space: nowrap;
    mix-blend-mode: difference;
}

.funnel-value {
    font-size: .85rem;
    font-weight: 600;
    white-space: nowrap;
    min-width: 90px;
    text-align: right;
}

.funnel-pct {
    color: var(--text-muted);
    font-weight: 400;
}

/* ---------- Logs ---------- */

.log-controls {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    margin-bottom: 1rem;
}

.log-pre {
    font-family: var(--font-mono);
    font-size: .78rem;
    line-height: 1.45;
    background: #1c1917;
    color: #e7e2d9;
    padding: 1rem 1.25rem;
    border-radius: var(--radius);
    overflow-x: auto;
    max-height: 70vh;
    overflow-y: auto;
    white-space: pre-wrap;
    word-break: break-all;
}

/* ---------- Login ---------- */

.login-shell {
    min-height: calc(100vh - 60px);
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-box {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2.5rem 2.25rem;
    text-align: center;
    margin: -4rem auto 0;
}

.login-box h1 {
    font-size: 1.15rem;
    margin-bottom: .5rem;
}

.login-box p {
    color: var(--text-muted);
    font-size: .9rem;
    margin: 0 0 1.5rem;
}

.login-box .widget-wrap {
    display: flex;
    justify-content: center;
}
