:root {
    --bg: #f2f4f8;
    --card: #ffffff;
    --text: #1a1d23;
    --muted: #6b7280;
    --border: #e2e6ec;
    --accent: #4f6df5;
    --accent-hover: #3d5ae0;
    --accent-soft: rgba(79, 109, 245, 0.12);
    --danger: #d9534f;
    --danger-soft: rgba(217, 83, 79, 0.12);
    --success-soft: #e3f6e8;
    --success-text: #1e7b3a;
    --error-soft: #fdecec;
    --error-text: #b23330;
    --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
}

[data-theme="dark"] {
    --bg: #0f1116;
    --card: #171a21;
    --text: #e8eaf0;
    --muted: #8b93a4;
    --border: #262b36;
    --accent: #6c8bff;
    --accent-hover: #86a0ff;
    --accent-soft: rgba(108, 139, 255, 0.16);
    --danger: #e06661;
    --danger-soft: rgba(224, 102, 97, 0.16);
    --success-soft: #14301d;
    --success-text: #6fd694;
    --error-soft: #3a1a19;
    --error-text: #f0908c;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
    margin: 0;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    background: var(--bg);
    color: var(--text);
}

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

h1 { font-size: 1.5rem; margin: 0 0 0.25rem; }
h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* Top bar */
.topbar {
    background: var(--card);
    border-bottom: 1px solid var(--border);
}

.topbar-inner {
    max-width: 1040px;
    margin: 0 auto;
    padding: 0.65rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.brand {
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text);
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}
.brand:hover { text-decoration: none; }
.brand-mark { font-size: 1.15rem; }

.nav {
    display: flex;
    align-items: center;
    gap: 1rem;
}
.nav a { color: var(--muted); font-weight: 500; }
.nav a:hover { color: var(--text); text-decoration: none; }

.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.3rem 0.55rem;
    cursor: pointer;
    font-size: 0.95rem;
    line-height: 1;
    color: var(--text);
}
.theme-toggle:hover { background: var(--accent-soft); }
[data-theme="light"] .theme-icon-light { display: none; }
[data-theme="dark"] .theme-icon-dark { display: none; }

/* Layout */
.container {
    flex: 1;
    width: 100%;
    max-width: 1040px;
    margin: 0 auto;
    padding: 1.5rem 1rem 3rem;
}

.site-footer {
    text-align: center;
    padding: 1rem;
    color: var(--muted);
    font-size: 0.8rem;
    border-top: 1px solid var(--border);
}

.page-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
}

/* Cards */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem;
    margin-bottom: 1.25rem;
    box-shadow: var(--shadow);
}

.center-card {
    text-align: center;
}

.auth-card {
    max-width: 420px;
    margin: 3rem auto;
    text-align: left;
}
.auth-card h1 { text-align: center; }
.auth-card > p { text-align: center; }

/* Forms */
label { display: block; }
label > span {
    display: block;
    font-weight: 600;
    font-size: 0.85rem;
    margin-bottom: 0.3rem;
}
label em { font-style: normal; font-weight: 400; }

input[type="text"],
input[type="url"],
input[type="password"] {
    width: 100%;
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 0.95rem;
}
input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

.stack { display: flex; flex-direction: column; gap: 1rem; }

.create-form {
    display: flex;
    gap: 1rem;
    align-items: flex-end;
    flex-wrap: wrap;
}
.create-form label { flex: 1 1 160px; }
.create-form .grow { flex: 2 1 260px; }
.create-form .btn { flex: 0 0 auto; }

.inline-form { display: inline; }

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.55rem 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card);
    color: var(--text);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { background: var(--accent-soft); text-decoration: none; }

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

.btn-ghost { background: transparent; }

.btn-danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn-danger:hover { background: var(--danger-soft); }

.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.8rem; }

/* Flash messages */
.flash {
    padding: 0.7rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    font-size: 0.9rem;
    word-break: break-all;
}
.flash-success { background: var(--success-soft); color: var(--success-text); }
.flash-error { background: var(--error-soft); color: var(--error-text); }

/* Table */
.table-card { padding: 0.5rem 0; }
.table-scroll { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }
th, td {
    text-align: left;
    padding: 0.65rem 1rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
th {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--accent-soft); }

.num { text-align: right; }
.actions-col { white-space: nowrap; text-align: right; }

.link-cell {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.short-link { font-weight: 600; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.dest-cell {
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.dest-cell a { font-size: 0.85rem; }

.copy-btn.copied { color: var(--success-text); }

/* Stats */
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem;
    margin-bottom: 1.25rem;
}
.stat-card { margin-bottom: 0; text-align: center; }
.stat-value { font-size: 1.8rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.8rem; margin-top: 0.15rem; }

.chart-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}
.chart-head h2 { margin: 0; }

.period-pills { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.pill {
    padding: 0.3rem 0.75rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    color: var(--muted);
    font-size: 0.8rem;
    font-weight: 600;
}
.pill:hover { text-decoration: none; color: var(--text); background: var(--accent-soft); }
.pill.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.chart-wrap { width: 100%; }
.chart-wrap canvas { display: block; width: 100%; }

/* Referrers */
.referrer-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
}
.referrer-list li {
    display: grid;
    grid-template-columns: minmax(120px, 220px) 1fr 3rem;
    align-items: center;
    gap: 0.75rem;
}
.ref-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
}
.ref-bar {
    height: 10px;
    background: var(--accent-soft);
    border-radius: 999px;
    overflow: hidden;
}
.ref-bar span {
    display: block;
    height: 100%;
    background: var(--accent);
    border-radius: 999px;
    min-width: 4px;
}
.ref-count { text-align: right; font-weight: 600; font-size: 0.9rem; }

@media (max-width: 640px) {
    .create-form { flex-direction: column; align-items: stretch; }
    .page-head { flex-direction: column; }
    .actions-col { text-align: left; }
}
