:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --ink: #111827;
    --muted: #667085;
    --line: #d8e0e8;
    --brand: #0f766e;
    --brand-dark: #0b5f59;
    --warn: #b42318;
    --soft: #edf7f5;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
}

button,
input,
select {
    font: inherit;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px max(16px, env(safe-area-inset-right)) 14px max(16px, env(safe-area-inset-left));
    background: rgba(255, 255, 255, 0.96);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(12px);
}

h1,
h2 {
    margin: 0;
    letter-spacing: 0;
}

h1 {
    font-size: 22px;
}

h2 {
    font-size: 18px;
}

.topbar p {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 13px;
}

.top-actions,
.row-actions,
.login-row,
.import-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.layout {
    width: min(1180px, 100%);
    margin: 0 auto;
    padding: 14px;
}

.search-panel,
.admin-panel,
.results,
.install {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    margin-bottom: 12px;
    padding: 14px;
}

.install {
    max-width: 760px;
    margin: 28px auto;
}

.filters,
.form-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 10px;
    align-items: end;
}

.keyword-filter {
    grid-column: span 2;
}

.product-picker {
    grid-column: span 2;
}

.product-picker[hidden] {
    display: none;
}

.filter-group {
    grid-column: 1 / -1;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
}

.filter-group legend {
    margin-bottom: 6px;
    padding: 0;
    color: var(--muted);
    font-size: 13px;
}

.segmented {
    display: flex;
    flex-wrap: wrap;
    gap: 0;
}

.segmented input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

.segmented label {
    display: grid;
    min-width: 74px;
    min-height: 38px;
    place-items: center;
    margin-right: -1px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    cursor: pointer;
}

.segmented label:first-of-type {
    border-radius: 6px 0 0 6px;
}

.segmented label:last-of-type {
    margin-right: 0;
    border-radius: 0 6px 6px 0;
}

.segmented input:checked + label {
    z-index: 1;
    border-color: var(--brand);
    background: var(--soft);
    color: var(--brand-dark);
    font-weight: 700;
}

.segmented input:focus-visible + label {
    outline: 2px solid rgba(15, 118, 110, 0.3);
    outline-offset: 2px;
}

.search-submit {
    grid-column: 6;
    min-width: 92px;
}

label {
    display: grid;
    gap: 6px;
    color: var(--muted);
    font-size: 13px;
}

input,
select {
    width: 100%;
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--ink);
    padding: 8px 10px;
}

input:focus,
select:focus {
    outline: 2px solid rgba(15, 118, 110, 0.22);
    border-color: var(--brand);
}

.button,
.icon-btn {
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--ink);
    padding: 8px 12px;
    cursor: pointer;
}

.button.primary {
    background: var(--brand);
    border-color: var(--brand);
    color: #fff;
}

.button.primary:hover {
    background: var(--brand-dark);
}

.button.ghost {
    background: #fff;
}

.icon-btn {
    width: 42px;
    padding: 0;
    font-size: 20px;
}

.record-form,
.import-row {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--line);
}

.admin-tabs {
    display: flex;
    gap: 0;
    margin-top: 12px;
    border-bottom: 1px solid var(--line);
}

.admin-tab {
    min-height: 40px;
    margin-bottom: -1px;
    border: 0;
    border-bottom: 2px solid transparent;
    background: transparent;
    color: var(--muted);
    padding: 8px 16px;
    cursor: pointer;
}

.admin-tab.active {
    border-bottom-color: var(--brand);
    color: var(--brand-dark);
    font-weight: 700;
}

#recordProductSelect {
    min-height: 164px;
    padding: 6px;
}

#recordProductSelect option {
    padding: 5px 8px;
}

.product-form {
    display: grid;
    grid-template-columns: 160px minmax(240px, 1fr) auto auto;
    gap: 10px;
    align-items: end;
    margin-top: 12px;
    padding-top: 12px;
}

.product-admin-controls {
    display: grid;
    grid-template-columns: 180px minmax(220px, 1fr);
    gap: 10px;
    margin: 12px 0 8px;
}

.product-admin-list {
    border-top: 1px solid var(--line);
}

.product-admin-row {
    display: grid;
    grid-template-columns: 72px minmax(180px, 1fr) 100px auto;
    gap: 10px;
    align-items: center;
    min-height: 52px;
    padding: 8px 0;
    border-bottom: 1px solid var(--line);
}

.product-rivet-count {
    color: var(--muted);
    font-size: 13px;
    text-align: right;
}

.product-row-actions {
    display: flex;
    gap: 6px;
}

.product-row-actions .button {
    min-height: 34px;
    padding: 6px 10px;
}

.wide {
    grid-column: span 2;
}

.result-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

#countText {
    color: var(--muted);
    font-size: 13px;
}

.record-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 10px;
}

.record-card {
    border: 1px solid var(--line);
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
}

.record-card.preview-open {
    grid-column: 1 / -1;
}

.record-main {
    padding: 12px;
}

.record-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.record-title strong {
    font-size: 17px;
}

.badge {
    display: inline-flex;
    align-items: center;
    min-height: 24px;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--soft);
    color: var(--brand-dark);
    font-size: 12px;
    white-space: nowrap;
}

.meta {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px 12px;
    color: var(--muted);
    font-size: 13px;
}

.dims {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    margin-top: 10px;
}

.dims span {
    border: 1px solid var(--line);
    border-radius: 5px;
    padding: 4px 5px;
    color: var(--muted);
    font-size: 12px;
    text-align: center;
}

.preview {
    display: grid;
    place-items: center;
    min-height: 220px;
    margin: 0 12px 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    overflow: hidden;
    background: #fbfcfd;
}

.preview[hidden] {
    display: none;
}

.preview-actions {
    padding: 0 12px 12px;
}

.preview-actions .button {
    min-height: 34px;
    padding: 6px 10px;
}

.dimension-image-frame {
    width: 100%;
}

.dimension-image-frame.primary-only {
    aspect-ratio: 12 / 5;
    align-self: start;
    overflow: hidden;
}

.dimension-image-frame img {
    display: block;
    width: 100%;
    height: auto;
    object-fit: contain;
}

.sketch {
    width: 100%;
    height: auto;
}

.sketch-title {
    fill: #111827;
    font-size: 16px;
    font-weight: 700;
}

.center-line {
    stroke: #94a3b8;
    stroke-width: 1;
    stroke-dasharray: 8 5;
}

.dimension-line {
    stroke: #111827;
    stroke-width: 1.3;
}

.extension-line {
    stroke: #aab4c0;
    stroke-width: 1;
}

.dimension-text,
.drill-label {
    fill: #334155;
    font-size: 12px;
    letter-spacing: 0;
}

.drill-label {
    font-weight: 700;
}

.card-actions {
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--line);
    background: #fbfcfd;
}

.card-actions button {
    min-height: 34px;
    padding: 6px 10px;
}

.danger {
    color: var(--warn);
}

.toast {
    position: fixed;
    left: 50%;
    bottom: 18px;
    transform: translateX(-50%);
    z-index: 20;
    width: min(520px, calc(100% - 28px));
    padding: 12px 14px;
    border-radius: 8px;
    background: #111827;
    color: #fff;
    box-shadow: 0 18px 50px rgba(17, 24, 39, 0.28);
}

.ok {
    color: var(--brand-dark);
}

.error {
    color: var(--warn);
}

code {
    background: #eef2f7;
    border-radius: 4px;
    padding: 2px 4px;
}

@media (max-width: 860px) {
    .filters,
    .form-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .filters .button {
        grid-column: span 2;
    }

    .keyword-filter {
        grid-column: span 2;
    }

    .product-picker {
        grid-column: span 2;
    }

    .wide {
        grid-column: span 2;
    }

    .product-form {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 560px) {
    .topbar {
        align-items: flex-start;
    }

    h1 {
        font-size: 19px;
    }

    .layout {
        padding: 10px;
    }

    .search-panel,
    .admin-panel,
    .results {
        padding: 10px;
    }

    .record-list {
        grid-template-columns: 1fr;
    }

    .segmented {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
    }

    .segmented label,
    .segmented label:first-of-type,
    .segmented label:last-of-type {
        min-width: 0;
        margin: 0;
        border-radius: 6px;
    }

    .preview {
        min-height: 180px;
    }

    .product-form,
    .product-admin-controls {
        grid-template-columns: 1fr;
    }

    .product-admin-row {
        grid-template-columns: 64px minmax(0, 1fr);
    }

    .product-rivet-count {
        text-align: left;
    }

    .product-row-actions {
        justify-content: flex-end;
    }
}
