:root {
    color-scheme: light dark;
    --bg: light-dark(#f7f8fa, #0e1116);
    --surface: light-dark(#ffffff, #161a22);
    --surface-2: light-dark(#f0f3f7, #1d2330);
    --border: light-dark(#dde1e7, #2a3142);
    --text: light-dark(#1a1f2c, #e6ebf5);
    --muted: light-dark(#5f6776, #95a0b3);
    --accent: light-dark(#0a66ff, #6ea8ff);
    --accent-bg: color-mix(in srgb, var(--accent) 14%, transparent);
    --danger: light-dark(#d92d20, #ff6b6b);
    --warn: light-dark(#b54708, #ffb454);
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    --radius: 10px;
    --shadow: 0 1px 2px color-mix(in srgb, black 8%, transparent),
              0 4px 16px color-mix(in srgb, black 6%, transparent);
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

    &:hover {
        text-decoration: underline;
    }
}

button {
    font: inherit;
    color: inherit;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 12px;
    cursor: pointer;
    transition: background 120ms ease, border-color 120ms ease;

    &:hover:not(:disabled) {
        border-color: var(--accent);
    }

    &:disabled {
        opacity: 0.5;
        cursor: not-allowed;
    }

    &[type="submit"] {
        background: var(--accent);
        color: white;
        border-color: transparent;

        &:hover {
            filter: brightness(1.05);
        }
    }
}

input, select, textarea {
    font: inherit;
    color: inherit;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;

    &:focus {
        outline: 2px solid var(--accent);
        outline-offset: 1px;
    }
}

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);

    & h1 {
        font-size: 16px;
        margin: 0;
        letter-spacing: -0.01em;
    }

    & nav {
        display: flex;
        gap: 4px;
        margin-left: auto;
    }

    & .nav-link {
        padding: 6px 12px;
        border-radius: 6px;
        color: var(--muted);

        &:hover {
            background: var(--surface-2);
            text-decoration: none;
            color: var(--text);
        }

        &.is-active {
            background: var(--accent-bg);
            color: var(--accent);
        }
    }

    & .domain-selector {
        padding: 4px 8px;
        font-size: 13px;
        min-width: 180px;
    }
}

.dashboard {
    padding: 24px;
    display: grid;
    gap: 20px;
    max-width: 1400px;
    margin: 0 auto;
}

.aggregates-section {
    display: grid;
    gap: 12px;
}

.section-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.section-title {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin: 0;
}

.inline-filter {
    display: flex;
    align-items: center;
    gap: 12px;

    & label {
        display: inline-flex;
        align-items: center;
        gap: 8px;

        & span {
            color: var(--muted);
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.04em;
        }
    }
}

.aggregates {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 16px;
}

.violations-filter {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: flex-end;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);

    & label {
        display: flex;
        flex-direction: column;
        gap: 4px;
        flex: 0 1 200px;
        min-width: 0;

        & span {
            color: var(--muted);
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.04em;
        }
    }
}

.violations-filter-actions {
    display: flex;
    gap: 8px;
    flex: 0 0 auto;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    container-type: inline-size;

    & h2 {
        font-size: 13px;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--muted);
        margin: 0 0 12px;
    }

    &.card--wide {
        grid-column: 1 / -1;
    }
}

.ranked {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 6px;

    & li {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 12px;
        padding: 6px 8px;
        border-radius: 6px;
        background: var(--surface-2);
        font-family: var(--mono);
        font-size: 12px;
        min-width: 0;

        & .label {
            flex: 1 1 0;
            min-width: 0;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            cursor: default;
        }

        & .count {
            flex: 0 0 auto;
            font-weight: 600;
            color: var(--accent);
        }
    }
}

.sparkline {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(8px, 1fr);
    align-items: end;
    gap: 2px;
    height: 80px;

    & .bar {
        background: var(--accent);
        border-radius: 2px 2px 0 0;
        opacity: 0.85;
        position: relative;

        &:hover::after {
            content: attr(data-tooltip);
            position: absolute;
            bottom: 100%;
            left: 50%;
            transform: translateX(-50%);
            background: var(--surface-2);
            border: 1px solid var(--border);
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 11px;
            font-family: var(--mono);
            white-space: nowrap;
            box-shadow: var(--shadow);
        }
    }
}

.violations {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    gap: 12px;

    & h2 {
        font-size: 14px;
        margin: 0;
    }

    & .count {
        margin: 0;
        color: var(--muted);
        font-size: 12px;
    }
}

.ignore-section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0;
    overflow: hidden;

    & > summary {
        cursor: pointer;
        padding: 12px 16px;
        font-size: 13px;
        list-style: none;
        display: flex;
        align-items: center;
        gap: 10px;

        &::-webkit-details-marker { display: none; }

        &::before {
            content: '▶';
            font-size: 9px;
            color: var(--muted);
            transition: transform 120ms;
        }

        & .count {
            color: var(--muted);
            font-size: 12px;
            font-family: var(--mono);
        }
    }

    &[open] > summary::before {
        transform: rotate(90deg);
    }

    & .ignore-body {
        padding: 12px 16px 16px;
        border-top: 1px solid var(--border);
    }

    & .ignore-form {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        margin-bottom: 12px;

        & input {
            flex: 1 1 160px;
            min-width: 0;
        }

        & button {
            flex: 0 0 auto;
        }
    }

    & .ignore-list {
        list-style: none;
        margin: 0;
        padding: 0;
        display: grid;
        gap: 6px;

        & li {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 12px;
            background: var(--surface-2);
            padding: 8px 12px;
            border-radius: 6px;
            font-size: 12px;

            & code {
                font-family: var(--mono);
                background: var(--surface);
                padding: 1px 6px;
                border-radius: 3px;
            }

            & .any {
                color: var(--muted);
                font-style: italic;
            }

            & .notes {
                color: var(--muted);
            }
        }

        & .ignore-empty {
            background: transparent;
            color: var(--muted);
            font-style: italic;
            justify-content: center;
        }
    }
}

table .row-actions {
    display: flex;
    gap: 4px;
    justify-content: flex-end;

    & button {
        font-size: 11px;
        padding: 2px 8px;
    }
}

.tabs {
    display: inline-flex;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 2px;
    gap: 2px;

    & .tab {
        background: transparent;
        border: 0;
        padding: 4px 12px;
        border-radius: 4px;
        font-size: 12px;
        color: var(--muted);
        cursor: pointer;

        &.is-active {
            background: var(--surface);
            color: var(--text);
            box-shadow: 0 1px 2px color-mix(in srgb, black 10%, transparent);
        }

        &:hover:not(.is-active) {
            color: var(--text);
        }
    }
}

.table-wrap {
    overflow: auto;
    max-height: 60vh;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;

    & th, & td {
        padding: 8px 12px;
        text-align: left;
        border-bottom: 1px solid var(--border);
        vertical-align: top;
    }

    & th {
        position: sticky;
        top: 0;
        background: var(--surface-2);
        font-weight: 600;
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--muted);
        z-index: 1;
    }

    & td {
        font-family: var(--mono);
        font-size: 12px;
    }

    & tr:has(.is-active) {
        background: var(--accent-bg);
    }

    & .uri {
        max-width: 38ch;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    & .directive {
        color: var(--accent);
    }

    & .row-detail {
        background: transparent;
        border: 1px solid var(--border);
        padding: 2px 8px;
        border-radius: 4px;
        font-size: 11px;
        cursor: pointer;
    }
}

.pager {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 12px;
    border-top: 1px solid var(--border);

    & #page-status {
        color: var(--muted);
        font-family: var(--mono);
        font-size: 12px;
    }
}

dialog {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
    max-width: min(900px, 95vw);
    width: 100%;
    max-height: 85vh;
    overflow: hidden;

    &::backdrop {
        background: color-mix(in srgb, black 60%, transparent);
    }
}

.dialog-form {
    display: flex;
    flex-direction: column;
    max-height: 85vh;

    & header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 12px 16px;
        border-bottom: 1px solid var(--border);

        & h3 {
            margin: 0;
            font-size: 14px;
        }

        & button {
            background: transparent;
            border: 0;
            font-size: 22px;
            line-height: 1;
            padding: 4px 10px;
        }
    }

    & dl {
        display: grid;
        grid-template-columns: max-content 1fr;
        gap: 4px 16px;
        padding: 16px;
        margin: 0;
        font-family: var(--mono);
        font-size: 12px;
        overflow: auto;

        & dt {
            color: var(--muted);
            text-transform: uppercase;
            font-size: 11px;
            letter-spacing: 0.04em;
        }

        & dd {
            margin: 0;
            word-break: break-all;
        }
    }

    & details {
        border-top: 1px solid var(--border);
        padding: 12px 16px;
        overflow: auto;

        & summary {
            cursor: pointer;
            color: var(--muted);
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.04em;
        }

        & pre {
            font-family: var(--mono);
            font-size: 12px;
            background: var(--surface-2);
            padding: 12px;
            border-radius: 6px;
            margin: 12px 0 0;
            overflow: auto;
            max-height: 40vh;
        }
    }
}

.generator {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
    padding: 24px;
    max-width: 1400px;
    margin: 0 auto;

    @media (min-width: 900px) {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);

        & .gen-column {
            max-height: 70vh;
            overflow-y: auto;
        }
    }
}

.gen-fetch {
    grid-column: 1 / -1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    display: flex;
    gap: 12px;
    align-items: end;
    flex-wrap: wrap;

    & label {
        flex: 1;
        min-width: 250px;
        display: flex;
        flex-direction: column;
        gap: 4px;

        & span {
            color: var(--muted);
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.04em;
        }
    }
}

.gen-column {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    container-type: inline-size;

    & h2 {
        font-size: 14px;
        margin: 0;
    }

    & .column-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        position: sticky;
        top: -16px;
        margin: -16px -16px 12px;
        padding: 12px 16px;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        z-index: 1;
    }

    & .meta {
        color: var(--muted);
        font-size: 12px;
    }
}

.inline-control {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--muted);

    & select {
        padding: 4px 6px;
        font-size: 12px;
    }
}

.directive-block {
    margin-bottom: 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 12px;
    background: var(--surface-2);

    & h3 {
        font-family: var(--mono);
        font-size: 13px;
        margin: 0 0 8px;
        color: var(--accent);
    }

    & ul {
        list-style: none;
        margin: 0 0 8px;
        padding: 0;
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
    }

    & .source {
        background: var(--surface);
        border: 1px solid var(--border);
        padding: 3px 8px;
        border-radius: 14px;
        font-family: var(--mono);
        font-size: 11px;
        display: inline-flex;
        align-items: center;
        gap: 6px;

        & button {
            background: transparent;
            border: 0;
            color: var(--muted);
            cursor: pointer;
            padding: 0 2px;

            &:hover {
                color: var(--danger);
            }
        }
    }

    & .add-form {
        display: flex;
        gap: 6px;

        & input {
            flex: 1;
        }
    }
}

.violation-suggestion {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: 6px;

    &:hover {
        background: var(--surface-2);
    }

    & .blocked {
        font-family: var(--mono);
        font-size: 12px;
        flex: 1;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    & .count {
        color: var(--muted);
        font-size: 11px;
    }

    & button {
        font-size: 11px;
        padding: 3px 8px;
    }

    & .already-added {
        font-size: 11px;
        color: light-dark(#1f6e34, #5cd47b);
        background: color-mix(in srgb, #2e9648 14%, transparent);
        padding: 3px 8px;
        border-radius: 4px;
        font-family: var(--mono);
    }

    &.is-resolved {
        opacity: 0.65;
    }
}

.gen-output {
    grid-column: 1 / -1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;

    & h2 {
        margin: 0;
        font-size: 14px;
    }

    & .column-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        margin-bottom: 8px;
    }

    & .header-name-hint {
        margin: 0 0 8px;
        color: var(--muted);
        font-size: 12px;

        & code {
            font-family: var(--mono);
            font-size: 12px;
            color: var(--accent);
            background: var(--accent-bg);
            padding: 2px 6px;
            border-radius: 4px;
        }
    }

    & textarea {
        width: 100%;
        min-height: 120px;
        font-family: var(--mono);
        font-size: 12px;
        resize: vertical;
    }

    & .actions {
        display: flex;
        gap: 8px;
        margin-top: 12px;
        flex-wrap: wrap;
        align-items: center;

        & .copy-status {
            color: var(--muted);
            font-size: 12px;
        }
    }
}

.mode-switch {
    display: inline-flex;
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
    background: var(--surface-2);

    & label {
        position: relative;
        display: flex;
        align-items: center;
        cursor: pointer;

        & input {
            position: absolute;
            inset: 0;
            opacity: 0;
            cursor: pointer;
        }

        & span {
            padding: 4px 12px;
            font-size: 12px;
            color: var(--muted);
            transition: background 120ms, color 120ms;
        }

        &:has(input:checked) span {
            background: var(--accent);
            color: white;
        }

        &:hover:not(:has(input:checked)) span {
            color: var(--text);
        }
    }
}

.mode-badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-family: var(--mono);
    margin: 0 4px 0 4px;

    &.mode-badge--enforce {
        background: var(--accent-bg);
        color: var(--accent);
    }

    &.mode-badge--report-only {
        background: color-mix(in srgb, var(--warn) 15%, transparent);
        color: var(--warn);
    }
}

.size-readout {
    margin: 6px 0 0;
    color: var(--muted);
    font-size: 12px;
    font-family: var(--mono);

    & .size-warn {
        color: var(--warn);
    }
}

.risk-warnings {
    list-style: none;
    margin: 12px 0 0;
    padding: 0;
    display: grid;
    gap: 6px;

    & li {
        background: color-mix(in srgb, var(--warn) 12%, transparent);
        color: var(--warn);
        border-left: 3px solid var(--warn);
        padding: 6px 10px;
        border-radius: 4px;
        font-size: 12px;
        font-family: var(--mono);
    }
}

.test-result {
    margin-top: 12px;
    padding: 10px 12px;
    border-radius: 6px;
    font-size: 12px;
    font-family: var(--mono);
    background: var(--surface-2);
    border: 1px solid var(--border);

    &.is-clean {
        background: color-mix(in srgb, #2e9648 12%, transparent);
        border-color: #2e9648;
        color: light-dark(#1f6e34, #5cd47b);
    }

    &.is-warning {
        background: color-mix(in srgb, var(--warn) 12%, transparent);
        border-color: var(--warn);
        color: var(--warn);
    }

    & details {
        margin-top: 6px;

        & summary {
            cursor: pointer;
        }

        & ul {
            margin: 6px 0 0;
            padding: 0 0 0 16px;
            max-height: 180px;
            overflow: auto;
        }
    }
}

.diff-summary {
    list-style: none;
    margin: 0;
    padding: 16px;
    display: grid;
    gap: 8px;
    overflow: auto;
    max-height: 60vh;
    font-family: var(--mono);
    font-size: 12px;

    & .diff-block {
        background: var(--surface-2);
        border-radius: 6px;
        padding: 8px 12px;

        & h4 {
            margin: 0 0 6px;
            font-size: 12px;
            color: var(--accent);
        }

        & .added { color: light-dark(#1f6e34, #5cd47b); }
        & .removed { color: var(--danger); text-decoration: line-through; }
        & .changes {
            display: grid;
            gap: 2px;
        }
    }
}

.versions {
    grid-column: 1 / -1;

    & summary {
        cursor: pointer;
        font-weight: 600;
        padding: 8px 0;
    }

    & ul {
        list-style: none;
        margin: 0;
        padding: 0;
        display: grid;
        gap: 6px;
    }

    & li {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: 6px;
        padding: 8px 12px;
        font-family: var(--mono);
        font-size: 12px;
        display: flex;
        justify-content: space-between;
        gap: 12px;

        & .meta {
            color: var(--muted);
            font-size: 11px;
        }
    }
}

.toast {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 16px;
    box-shadow: var(--shadow);
    font-size: 13px;
    transform: translateY(20px);
    opacity: 0;
    transition: opacity 200ms, transform 200ms;
    pointer-events: none;

    &.is-visible {
        opacity: 1;
        transform: translateY(0);
    }
}
