:root {
    --bg: #f5f6fa;
    --card: #ffffff;
    --surface-muted: #f4f5f9;
    --text: #1c1e26;
    --muted: #6b7280;
    --border: #e3e5ea;
    --accent: #4f46e5;
    --accent-hover: #4338ca;
    --accent-soft: #eeecfd;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --success: #16a34a;
    --radius: 10px;
    --radius-sm: 8px;
    --radius-pill: 999px;
    --shadow-card: 0 1px 2px rgba(20, 22, 30, 0.04);
    --transition: 0.15s ease;

    /* categorical chart series - fixed order, validated for CVD-safe adjacent contrast */
    --viz-1: #2a78d6; /* blue */
    --viz-2: #eb6834; /* orange */
    --viz-3: #1baf7a; /* aqua */
    --viz-4: #eda100; /* yellow */
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

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

h1, h2, h3 { line-height: 1.25; font-weight: 700; margin: 0 0 14px; }
h1 { font-size: 1.9rem; }
h2 { font-size: 1.4rem; }
h3 { font-size: 1.05rem; }

hr, .divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

/* ---------- layout ---------- */

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 24px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
    gap: 10px;
}

.topbar-left { display: flex; align-items: center; gap: 22px; flex-wrap: wrap; }
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.3rem; color: var(--text); }
.brand-icon { display: inline-flex; }
.brand-icon svg { display: block; width: 24px; height: auto; }

.topbar-main-nav { display: flex; align-items: center; gap: 18px; }
.topbar-right { display: flex; align-items: center; gap: 14px; }

.user-chip { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.9rem; }
.user-chip:hover, .user-chip:focus-visible { text-decoration: none; color: var(--text); }
.user-chip svg { width: 17px; height: 17px; flex-shrink: 0; }
.user-chip-text { display: inline-flex; white-space: nowrap; }
.user-chip-rest {
    max-width: 0;
    overflow: hidden;
    opacity: 0;
    transition: max-width 0.25s ease, opacity 0.2s ease;
}
.user-chip:hover .user-chip-rest, .user-chip:focus-visible .user-chip-rest { max-width: 240px; opacity: 1; }

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    color: var(--muted);
    transition: background-color var(--transition), color var(--transition);
}
.icon-btn:hover, .icon-btn:focus-visible { background: var(--surface-muted); color: var(--danger); text-decoration: none; }
.icon-btn svg { width: 18px; height: 18px; }

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 24px 16px 60px;
}

.page-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 20px;
}
.page-head h2 { margin: 0; }

.panel { max-width: 420px; margin: 0 auto; }
.panel-wide { max-width: 560px; margin: 0 auto; }

.auth-page { position: relative; overflow-x: hidden; }
.auth-bg {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 1100px;
    max-width: 100%;
    opacity: 0.12;
    pointer-events: none;
    z-index: 0;
}
.auth-bg svg { width: 100%; max-width: none; height: auto; display: block; }
.auth-page .panel { position: relative; z-index: 1; }

.site-footer { text-align: center; color: var(--muted); font-size: 0.85rem; padding: 20px; }

/* ---------- surfaces ---------- */

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-card);
    padding: 24px;
    margin-bottom: 20px;
}

.flash { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 16px; font-size: 0.95rem; border: 1px solid transparent; }
.flash-success { background: #e7f8ee; color: var(--success); border-color: #b7e6c8; }
.flash-error { background: #fdecec; color: var(--danger); border-color: #f4bcbc; }

/* ---------- buttons ---------- */

.btn {
    display: inline-block;
    background: var(--accent);
    color: #fff;
    border: 1px solid var(--accent);
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.3;
    transition: background-color var(--transition), border-color var(--transition), color var(--transition);
}
.btn:hover { background: var(--accent-hover); border-color: var(--accent-hover); text-decoration: none; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn-secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface-muted); border-color: var(--border); }

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

.btn-sm { padding: 6px 12px; font-size: 0.85rem; border-radius: var(--radius-sm); }

/* ---------- forms ---------- */

form.stack { display: flex; flex-direction: column; gap: 14px; max-width: 480px; }
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; }

label { font-weight: 600; font-size: 0.9rem; display: block; margin-bottom: 4px; }

input[type=text], input[type=email], input[type=password], input[type=url], textarea, select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-family: inherit;
    background: #fff;
    color: var(--text);
    transition: border-color var(--transition), box-shadow var(--transition);
}
input[type=text]:focus, input[type=email]:focus, input[type=password]:focus,
input[type=url]:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

input[type=color] { width: 60px; height: 40px; padding: 2px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: #fff; }
input[type=range] { width: 100%; accent-color: var(--accent); }
textarea { min-height: 100px; resize: vertical; }
.field-hint { color: var(--muted); font-size: 0.82rem; margin-top: 4px; }

.file-input-wrap { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.file-input-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
.file-input-hidden:focus-visible + .btn { outline: 2px solid var(--accent); outline-offset: 2px; }
.file-input-name { color: var(--muted); font-size: 0.85rem; }

.checkbox-row { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.checkbox-row input { width: auto; accent-color: var(--accent); }
.checkbox-row label { margin: 0; }

.inline-form { display: inline; }

.color-row { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.color-field { display: flex; flex-direction: column; gap: 4px; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 560px) { .two-col { grid-template-columns: 1fr; } }

fieldset.style-fieldset {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin: 0;
}
.style-fieldset legend { font-weight: 600; padding: 0 6px; font-size: 0.9rem; }
#gradientOptions, #angleField { margin-top: 10px; }

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

/* ---------- link mode selector ---------- */

.link-mode-options { display: flex; flex-direction: column; gap: 10px; }
.link-mode-option {
    display: block;
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px 12px 38px;
    cursor: pointer;
    transition: border-color var(--transition), background-color var(--transition);
}
.link-mode-option:hover { border-color: var(--accent); }
.link-mode-option.active { border-color: var(--accent); background: var(--accent-soft); }
.link-mode-option input[type=radio] {
    position: absolute;
    left: 14px;
    top: 14px;
    width: auto;
    accent-color: var(--accent);
}
.link-mode-option-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.link-mode-option-title { font-weight: 600; font-size: 0.92rem; }
.link-mode-badge {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--accent);
    background: var(--card);
    border: 1px solid var(--accent);
    border-radius: var(--radius-pill);
    padding: 1px 8px;
}
.link-mode-option-desc { display: block; color: var(--muted); font-size: 0.82rem; line-height: 1.45; }

.mode-badge {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    text-transform: uppercase;
    letter-spacing: 0.02em;
}
.mode-badge-dynamic { color: var(--accent); background: var(--accent-soft); }
.mode-badge-static { color: var(--muted); background: var(--surface-muted); border: 1px solid var(--border); }
.qr-tile .meta .mode-badge { margin-left: 6px; vertical-align: middle; }

.type-tabs { display: flex; gap: 8px; margin-bottom: 18px; flex-wrap: wrap; }
.type-tab {
    display: inline-block;
    padding: 8px 16px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    transition: background-color var(--transition), border-color var(--transition), color var(--transition);
}
.type-tab:hover { border-color: var(--accent); text-decoration: none; }
.type-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- hero (landing page) ---------- */

.hero { text-align: center; padding: 60px 20px 20px; }
.hero h1 { font-size: 2.4rem; margin-bottom: 12px; }
.hero p { color: var(--muted); font-size: 1.1rem; margin-bottom: 28px; }
.hero .btn { margin: 0 6px; }

.mascot-wrap { display: flex; justify-content: center; padding: 10px 16px 50px; }
.mascot-svg { width: 100%; max-width: 460px; height: auto; }

/* ---------- dashboard: horizontal QR row ---------- */

.qr-row {
    display: flex;
    gap: 16px;
    overflow-x: auto;
    padding-bottom: 12px;
    scroll-snap-type: x proximity;
}
.qr-row::-webkit-scrollbar { height: 8px; }
.qr-row::-webkit-scrollbar-thumb { background: var(--border); border-radius: var(--radius-pill); }

.qr-tile {
    flex: 0 0 220px;
    scroll-snap-align: start;
    text-align: center;
}
.qr-tile img { max-width: 100%; border: 1px solid var(--border); border-radius: var(--radius-sm); background: #fff; }
.qr-tile h3 { margin: 10px 0 4px; font-size: 1rem; word-break: break-word; }
.qr-tile .meta { color: var(--muted); font-size: 0.82rem; }

.tile-stats {
    display: flex;
    gap: 8px;
    margin: 10px 0;
}
.tile-stat {
    flex: 1;
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 4px;
}
.tile-stat-num { font-size: 1.1rem; font-weight: 700; }
.tile-stat-label { color: var(--muted); font-size: 0.72rem; }

.qr-tile .actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------- QR create/edit two-column layout ---------- */

.qr-editor {
    display: grid;
    grid-template-columns: minmax(260px, 380px) 1fr;
    gap: 24px;
    align-items: start;
}
@media (max-width: 760px) {
    .qr-editor { grid-template-columns: 1fr; }
}

.qr-preview-pane { position: sticky; top: 20px; }
@media (max-width: 760px) { .qr-preview-pane { position: static; } }

.qr-preview-box {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-card);
    padding: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}
.qr-preview-box img {
    width: 100%;
    max-width: 340px;
    height: auto;
    aspect-ratio: 1 / 1;
    image-rendering: pixelated;
    border-radius: var(--radius-sm);
}

.qr-form-pane { max-width: 100%; }
.qr-form-pane form.stack { max-width: 100%; }

/* ---------- scan landing page ---------- */

.landing-card { max-width: 420px; margin: 40px auto; text-align: center; }
.landing-card .qr-preview { margin: 20px 0; }
.landing-card .qr-preview img { border: 1px solid var(--border); border-radius: var(--radius-sm); }

.detail-list { text-align: left; background: var(--surface-muted); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; margin: 16px 0; }
.detail-list dt { font-weight: 600; color: var(--muted); font-size: 0.82rem; }
.detail-list dd { margin: 0 0 10px; font-size: 1rem; }

.message-text { white-space: pre-wrap; text-align: left; }

.scan-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.scan-list li { padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.scan-list li:last-child { border-bottom: none; }
.scan-list li:nth-child(odd) { background: var(--surface-muted); }

/* ---------- stats ---------- */

.stats-row { display: flex; flex-wrap: wrap; gap: 14px; margin: 16px 0; }
.stat-box {
    flex: 1 1 140px;
    text-align: center;
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px 12px;
}
.stat-box .num { font-size: 1.8rem; font-weight: 700; }
.stat-box .label { color: var(--muted); font-size: 0.78rem; margin-top: 2px; }

.code-inline {
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 2px 6px;
    font-size: 0.88rem;
}

/* ---------- scan chart ---------- */

.chart-wrap { position: relative; margin: 20px 0 4px; }
.scan-chart { width: 100%; height: auto; aspect-ratio: 720 / 200; display: block; overflow: visible; }

.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-baseline { stroke: var(--muted); stroke-width: 1; }
.chart-axis-label {
    fill: var(--muted);
    font-size: 10px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.chart-axis-label-y { text-anchor: start; }

.bar { cursor: pointer; outline: none; }
.bar path { transition: opacity var(--transition); }
.bar:hover path, .bar:focus-visible path { opacity: 0.72; }
.bar:focus-visible .bar-hit { outline: 2px solid var(--accent); outline-offset: -2px; }

.chart-tooltip {
    position: absolute;
    transform: translate(-50%, calc(-100% - 8px));
    background: var(--text);
    color: #fff;
    padding: 4px 9px;
    border-radius: var(--radius-sm);
    font-size: 0.78rem;
    white-space: nowrap;
    pointer-events: none;
    z-index: 5;
}

.chart-table-toggle { margin-top: 10px; }
.chart-table-toggle summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 0.85rem; }
.table-scroll { overflow-x: auto; margin-top: 10px; }

.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table th, .data-table td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; }
.data-table th { color: var(--muted); font-weight: 600; font-size: 0.78rem; }
.data-table td { font-variant-numeric: tabular-nums; }

/* ---------- device breakdown ---------- */

.device-bars { display: flex; flex-direction: column; gap: 10px; }
.device-row { display: grid; grid-template-columns: 90px 1fr 90px; align-items: center; gap: 10px; }
.device-row-label { font-size: 0.85rem; font-weight: 600; }
.device-row-track { background: var(--surface-muted); border-radius: var(--radius-pill); height: 10px; overflow: hidden; }
.device-row-fill { height: 100%; border-radius: var(--radius-pill); }
.device-row-value { font-size: 0.8rem; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
@media (max-width: 560px) {
    .device-row { grid-template-columns: 66px 1fr 74px; gap: 8px; }
}
