* { box-sizing: border-box; }
body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; color: #1a1a1a; background: #f6f7f9; }
a { color: #2563eb; text-decoration: none; }
a:hover { text-decoration: underline; }

/* Top bar */
.topbar { background: #111827; color: #fff; padding: 12px 24px; display: flex; align-items: center; justify-content: space-between; }
.topbar .brand a { color: #fff; font-weight: 600; font-size: 18px; }
.topbar nav a { color: #d1d5db; margin-left: 18px; }
.topbar nav a:hover { color: #fff; }
.topbar nav .logout { color: #9ca3af; }

.container { max-width: 1200px; margin: 24px auto; padding: 0 24px; }

/* Login */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box { background: #fff; padding: 36px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.08); width: 360px; }
.login-box h1 { margin: 0 0 20px; }
.login-box label { display: block; margin-bottom: 14px; }
.login-box input { display: block; width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 4px; margin-top: 4px; font-size: 14px; }
.login-box button { width: 100%; padding: 10px; background: #2563eb; color: #fff; border: 0; border-radius: 4px; font-size: 15px; cursor: pointer; }

/* Toolbar */
.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; gap: 12px; }
.toolbar .search { display: flex; gap: 6px; flex: 1; max-width: 900px; flex-wrap: wrap; align-items: center; }
.toolbar .search input { flex: 1; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 4px; }
.toolbar .search button { padding: 8px 14px; background: #fff; border: 1px solid #d1d5db; border-radius: 4px; cursor: pointer; }
.toolbar .search select { padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 4px; background: #fff; }
.toolbar-actions { display: flex; gap: 8px; }

/* Grouped view */
.groups { display: flex; flex-direction: column; gap: 10px; }
.group { background: #fff; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); overflow: hidden; }
.group > summary {
    padding: 12px 16px; cursor: pointer; display: flex; justify-content: space-between; align-items: center;
    gap: 12px; font-size: 14px; list-style: none; user-select: none;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::before {
    content: '▸'; display: inline-block; margin-right: 8px; color: #6b7280; transition: transform 0.15s;
}
.group[open] > summary::before { transform: rotate(90deg); }
.group .g-dest { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; color: #1a1a1a; word-break: break-all; flex: 1; }
.group .g-meta { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 12px; }
.group .g-count { font-weight: 600; color: #2563eb; }
.group > summary:hover { background: #f9fafb; }
.group table.links.nested { box-shadow: none; border-radius: 0; border-top: 1px solid #eef0f3; }
.group table.links.nested th { background: #f9fafb; }
.tag { display: inline-block; background: #eef2ff; color: #3730a3; padding: 2px 8px; border-radius: 10px; font-size: 11px; margin-right: 4px; font-weight: 500; }

/* Mini tables in reports */
table.mini { width: 100%; font-size: 13px; border-collapse: collapse; }
table.mini td { padding: 4px 0; vertical-align: middle; }
table.mini td.k { width: 40%; }
table.mini td.n { text-align: right; white-space: nowrap; color: #374151; font-variant-numeric: tabular-nums; }
table.mini td.bar { width: 40%; padding: 0 8px; }
table.mini td.bar span { display: block; height: 8px; background: #2563eb; border-radius: 2px; min-width: 2px; }

/* Buttons */
.btn, button { padding: 8px 14px; border-radius: 4px; border: 1px solid #d1d5db; background: #fff; cursor: pointer; text-decoration: none; color: #1a1a1a; font-size: 14px; }
.btn.primary, button.primary { background: #2563eb; color: #fff; border-color: #2563eb; }
.btn.danger, button.danger { background: #fff; color: #b91c1c; border-color: #fca5a5; }
button:hover, .btn:hover { filter: brightness(0.97); }

/* Tables */
table.links { width: 100%; background: #fff; border-radius: 6px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.05); border-collapse: collapse; }
table.links th, table.links td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #f1f1f1; vertical-align: top; font-size: 14px; }
table.links th { background: #f9fafb; font-weight: 600; font-size: 12px; text-transform: uppercase; color: #6b7280; }
table.links tr.inactive { opacity: 0.55; }
table.links td.num { text-align: right; }
table.links .slug { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-weight: 600; }
table.links .copy { padding: 2px 6px; margin-left: 4px; font-size: 12px; }
table.links .title { font-weight: 600; }
table.links .desc { color: #6b7280; font-size: 12px; margin-top: 2px; }
table.links .dest { color: #6b7280; font-size: 13px; }
table.links .actions a { margin-right: 10px; font-size: 13px; }
.badge { padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.ok { background: #d1fae5; color: #065f46; }
.badge.off { background: #fee2e2; color: #991b1b; }
.badge.bad { background: #fecaca; color: #7f1d1d; }
.badge.muted { background: #e5e7eb; color: #6b7280; }

/* Forms */
.link-form { background: #fff; padding: 0; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.link-form fieldset { border: 0; border-bottom: 1px solid #eef0f3; padding: 20px 24px; margin: 0; }
.link-form legend { font-weight: 600; font-size: 13px; text-transform: uppercase; color: #6b7280; padding: 0 6px; }
.link-form .hint { color: #6b7280; font-size: 13px; margin-top: 4px; }
.link-form label { display: block; font-size: 13px; color: #374151; margin-bottom: 12px; font-weight: 500; }
.link-form input[type=text], .link-form input[type=url], .link-form input[type=number],
.link-form input[type=datetime-local], .link-form input:not([type]), .link-form select, .link-form textarea {
    display: block; width: 100%; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 4px; font-size: 14px; margin-top: 4px; font-family: inherit;
}
.link-form .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.link-form .grid label.full { grid-column: 1 / -1; }
.link-form .checkbox { display: flex; align-items: center; gap: 8px; }
.link-form .checkbox input { margin: 0; }
.link-form .preview { background: #f9fafb; padding: 10px 12px; border-radius: 4px; margin-top: 8px; word-break: break-all; font-size: 13px; }
.link-form .preview code { font-family: ui-monospace, monospace; color: #1a1a1a; }
.link-form .slug-input { display: flex; align-items: center; gap: 4px; margin-top: 4px; }
.link-form .slug-input span { color: #6b7280; font-size: 13px; }
.link-form .form-actions { padding: 16px 24px; display: flex; gap: 10px; align-items: center; }

/* QR builder */
.qr-builder { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; align-items: start; }
.qr-preview { text-align: center; background: #f9fafb; padding: 20px; border-radius: 6px; position: sticky; top: 12px; }
.qr-preview #qrCanvas { display: inline-block; width: 100%; max-width: 320px; aspect-ratio: 1 / 1; overflow: hidden; }
.qr-preview #qrCanvas > * { max-width: 100% !important; max-height: 100% !important; width: 100% !important; height: auto !important; }
.qr-preview #qrCanvas svg, .qr-preview #qrCanvas canvas { max-width: 100% !important; max-height: 100% !important; width: 100% !important; height: auto !important; display: block; }
.qr-preview .preview-note { font-size: 11px; color: #6b7280; margin-top: 6px; }
.qr-buttons { margin-top: 12px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.qr-controls label { display: block; font-size: 13px; }
.qr-controls input[type=color] { width: 60px; height: 32px; padding: 0; border: 1px solid #d1d5db; }

/* Stats */
.kpis { display: flex; gap: 16px; margin-bottom: 24px; }
.kpi { background: #fff; padding: 16px 24px; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); flex: 1; max-width: 200px; }
.kpi .n { font-size: 28px; font-weight: 700; }
.kpi .l { font-size: 12px; color: #6b7280; text-transform: uppercase; }
.bars { display: flex; align-items: flex-end; height: 120px; gap: 4px; background: #fff; padding: 12px; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.bars .bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 18px; }
.bars .bar .fill { width: 100%; background: #2563eb; border-radius: 2px 2px 0 0; min-height: 1px; }
.bars .bar .lbl { font-size: 9px; color: #6b7280; margin-top: 4px; }
.stats-grid { grid-template-columns: repeat(3, 1fr) !important; gap: 16px; margin: 24px 0; display: grid; }
.stat-block { background: #fff; padding: 16px; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.stat-block h4 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; color: #6b7280; }
.stat-block table { width: 100%; font-size: 13px; }
.stat-block td { padding: 4px 0; }
.stat-block td.num { text-align: right; color: #6b7280; }

/* Misc */
.error { background: #fee2e2; color: #991b1b; padding: 10px 14px; border-radius: 4px; margin-bottom: 16px; }
.success { background: #d1fae5; color: #065f46; padding: 10px 14px; border-radius: 4px; margin-bottom: 16px; }
.muted { color: #6b7280; font-size: 13px; }
.empty { background: #fff; padding: 40px; text-align: center; border-radius: 6px; }

@media (max-width: 800px) {
    .qr-builder, .link-form .grid, .stats-grid { grid-template-columns: 1fr !important; }
    .topbar { flex-direction: column; gap: 8px; }
    /* Release the sticky QR preview on mobile so controls scroll with the page */
    .qr-preview { position: static; }

    /* Toolbar stacks cleanly, no horizontal overflow */
    .toolbar { flex-direction: column; align-items: stretch; }
    .toolbar .search { max-width: none; width: 100%; }
    .toolbar .search > * { flex: 1 1 calc(50% - 3px); min-width: 0; }
    .toolbar .search input[type=search] { flex: 1 1 100%; }
    .toolbar .search button[type=submit] { flex: 1 1 100%; }
    .toolbar-actions { width: 100%; }
    .toolbar-actions .btn { flex: 1; text-align: center; }

    /* Grouped view headers wrap on mobile */
    .group > summary { flex-direction: column; align-items: flex-start; gap: 6px; }
    .group .g-dest { font-size: 12px; }

    /* Turn the links table into stacked cards */
    table.links, table.links tbody, table.links tr, table.links td { display: block; width: 100%; }
    table.links { background: transparent; box-shadow: none; border-radius: 0; }
    table.links thead { display: none; }
    table.links tr {
        background: #fff; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        padding: 12px 14px; margin-bottom: 12px; border: 0;
    }
    table.links td { padding: 4px 0; border: 0; text-align: left; }
    table.links td.num { text-align: left; }
    table.links td.dest span { word-break: break-all; white-space: normal; }
    table.links td[data-label]::before {
        content: attr(data-label);
        display: inline-block; min-width: 90px;
        font-size: 11px; text-transform: uppercase; color: #6b7280; font-weight: 600;
        margin-right: 8px;
    }
    table.links td.actions { margin-top: 8px; padding-top: 8px; border-top: 1px solid #f1f1f1; }
}
