:root {
    color-scheme: light;
    --ink: #172326;
    --muted: #66767a;
    --subtle: #8a989c;
    --page: #f5f8f8;
    --surface: #ffffff;
    --surface-soft: #eef5f4;
    --line: #dce6e7;
    --line-strong: #c2d0d1;
    --accent: #0f766e;
    --accent-strong: #0b5f59;
    --accent-soft: #d9f1ed;
    --blue: #2563eb;
    --amber: #b7791f;
    --red: #c2413a;
    --green: #23754a;
    --violet: #6d5bd0;
    --shadow: 0 20px 50px rgba(30, 47, 51, 0.10);
    --radius: 8px;
}

* {
    box-sizing: border-box;
}

html {
    height: 100%;
}

body {
    height: 100vh;
    margin: 0;
    color: var(--ink);
    font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, "Microsoft YaHei", sans-serif;
    line-height: 1.5;
    background:
        linear-gradient(180deg, #fbfdfd 0%, var(--page) 45%, #eef4f6 100%);
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
}

a {
    color: inherit;
}

button,
input {
    font: inherit;
}

button {
    cursor: pointer;
}

.app-header {
    min-height: 104px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: clamp(18px, 4vw, 56px);
    padding: 22px clamp(18px, 5vw, 72px);
    border-bottom: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
}

.header-copy {
    display: grid;
    gap: 8px;
    justify-items: start;
}

.eyebrow,
.section-kicker {
    margin: 0;
    color: var(--accent);
    font-size: 0.76rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

h1,
h2,
p {
    margin-top: 0;
}

h1 {
    margin-bottom: 0;
    font-size: clamp(1.65rem, 3vw, 2.85rem);
    line-height: 1;
    letter-spacing: 0;
}

h2 {
    margin-bottom: 0;
    font-size: 1.05rem;
    line-height: 1.2;
    letter-spacing: 0;
}

.header-nav {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 20px;
}

.mode-preview {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 30px;
    padding: 0 10px;
    border: 1px solid var(--line);
    border-radius: 999px;
    color: #315357;
    background: #f7fbfb;
    font-size: 0.82rem;
    font-weight: 700;
    text-decoration: none;
}

.header-nav a {
    display: inline-flex;
    align-items: center;
    min-height: 32px;
    padding: 0;
    color: var(--accent-strong);
    font-weight: 900;
    text-decoration: none;
    transition: color 160ms ease, transform 160ms ease;
}

.header-nav a:hover {
    color: var(--ink);
    transform: translateY(-1px);
}

.workspace {
    width: min(1480px, calc(100% - 36px));
    min-height: 0;
    margin: 18px auto 18px;
    display: grid;
    grid-template-columns: minmax(340px, 440px) minmax(0, 1fr);
    gap: 24px;
    align-items: stretch;
    overflow: hidden;
}

.control-surface,
.results-surface {
    min-height: 0;
    display: grid;
    gap: 16px;
    align-content: start;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
}

.control-surface {
    padding-right: 4px;
}

.tool-panel,
.results-surface {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.92);
    box-shadow: var(--shadow);
}

.tool-panel {
    padding: 18px;
}

.primary-panel {
    border-color: rgba(15, 118, 110, 0.28);
}

.results-surface {
    padding: 20px;
}

.panel-heading,
.results-toolbar {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.panel-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    flex-wrap: wrap;
}

.collapse-toggle {
    display: none;
    min-height: 32px;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0 11px;
    color: #315357;
    background: #f7fbfb;
    font-size: 0.82rem;
    font-weight: 900;
    transition: transform 160ms ease, border-color 160ms ease, background 160ms ease;
}

.collapse-toggle:hover {
    border-color: var(--line-strong);
    background: #ffffff;
    transform: translateY(-1px);
}

.field-label {
    display: block;
    margin-bottom: 7px;
    color: #344d52;
    font-size: 0.86rem;
    font-weight: 800;
}

input[type="number"] {
    width: 100%;
    min-height: 44px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius);
    padding: 9px 11px;
    color: var(--ink);
    background: #fbfdfd;
    outline: none;
    transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

input[type="number"]:focus {
    border-color: var(--accent);
    background: #ffffff;
    box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.segmented-control {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    margin: 14px 0 0;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface-soft);
}

.segmented-control label {
    min-width: 0;
}

.segmented-control input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.segmented-control span {
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    color: #4d6368;
    font-size: 0.92rem;
    font-weight: 800;
    transition: background 160ms ease, color 160ms ease, box-shadow 160ms ease;
}

.segmented-control input:checked + span {
    color: #ffffff;
    background: var(--accent);
    box-shadow: 0 8px 18px rgba(15, 118, 110, 0.24);
}

.button-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 14px;
}

.button {
    min-height: 44px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    padding: 0 14px;
    font-weight: 800;
    transition: transform 160ms ease, box-shadow 160ms ease, border-color 160ms ease, background 160ms ease;
}

.button:hover,
.link-button:hover {
    transform: translateY(-1px);
}

.button-primary {
    color: #ffffff;
    background: var(--accent);
    box-shadow: 0 12px 28px rgba(15, 118, 110, 0.22);
}

.button-primary:hover {
    background: var(--accent-strong);
}

.button-secondary {
    color: #315357;
    border-color: var(--line);
    background: #f8fbfb;
}

.button-secondary:hover {
    border-color: var(--line-strong);
    background: #ffffff;
}

.link-button {
    min-height: 32px;
    border: 0;
    padding: 0;
    color: var(--accent-strong);
    background: transparent;
    font-size: 0.88rem;
    font-weight: 800;
    text-decoration: underline;
    text-underline-offset: 4px;
}

.form-status {
    min-height: 22px;
    margin: 12px 0 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.form-status.is-error {
    color: var(--red);
}

.form-status.is-success {
    color: var(--green);
}

.limit-header,
.limit-row {
    display: grid;
    grid-template-columns: minmax(82px, 1fr) minmax(72px, 0.8fr) minmax(72px, 0.8fr);
    gap: 8px;
    align-items: center;
}

.limit-header {
    margin-bottom: 8px;
    color: var(--subtle);
    font-size: 0.78rem;
    font-weight: 800;
}

.element-limit-grid {
    display: grid;
    gap: 8px;
}

.limit-row {
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fbfdfd;
}

.limit-input {
    min-width: 0;
    display: grid;
    gap: 5px;
}

.limit-input-label {
    display: none;
    color: #5d7075;
    font-size: 0.76rem;
    font-weight: 900;
}

.element-name {
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
    font-weight: 800;
}

.element-symbol {
    width: 34px;
    flex: 0 0 auto;
    color: var(--accent-strong);
    font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
    font-size: 1rem;
}

.element-cn {
    min-width: 0;
    overflow: hidden;
    color: #425c61;
    font-size: 0.86rem;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.limit-row input {
    min-height: 36px;
    padding: 6px 8px;
    text-align: center;
}

.check-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    margin: 14px 0 0;
    border: 0;
    padding: 0;
}

.check-grid legend {
    grid-column: 1 / -1;
    margin-bottom: 2px;
    color: #344d52;
    font-size: 0.86rem;
    font-weight: 800;
}

.check-grid label {
    min-width: 0;
    min-height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    color: #435b60;
    background: #fbfdfd;
    font-size: 0.86rem;
    font-weight: 800;
    user-select: none;
}

.check-grid input {
    accent-color: var(--accent);
}

.history-section {
    display: grid;
    gap: 16px;
}

.intro-note {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.intro-note div {
    min-height: 88px;
    display: grid;
    align-content: center;
    gap: 8px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 14px;
    background: #fbfdfd;
}

.intro-note span {
    color: var(--subtle);
    font-size: 0.8rem;
    font-weight: 800;
}

.intro-note strong {
    color: var(--ink);
    font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
    font-size: 1.05rem;
    line-height: 1.2;
}

.intro-note.is-hidden {
    display: none;
}

.result-card {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(30, 47, 51, 0.07);
}

.result-card-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 14px;
    align-items: start;
    padding: 16px;
    border-bottom: 1px solid var(--line);
    background: #f9fcfc;
}

.result-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 900;
}

.result-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.pill {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    padding: 0 8px;
    border-radius: 999px;
    color: #3d565b;
    background: #eef5f4;
    font-size: 0.76rem;
    font-weight: 800;
}

.result-time {
    color: var(--subtle);
    font-size: 0.78rem;
    font-weight: 700;
    white-space: nowrap;
}

.timeout-warning,
.no-results {
    margin: 16px;
    border-radius: var(--radius);
    padding: 12px 14px;
    font-weight: 800;
}

.timeout-warning {
    color: var(--red);
    background: #fff1ef;
}

.no-results {
    color: #54686d;
    background: #f4f8f8;
}

.result-group {
    padding: 16px;
}

.result-group + .result-group {
    border-top: 1px solid var(--line);
}

.category-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 10px;
}

.category-tag {
    display: inline-flex;
    align-items: center;
    min-height: 30px;
    padding: 0 10px;
    border-radius: 999px;
    color: #ffffff;
    font-size: 0.82rem;
    font-weight: 900;
}

.category-count {
    color: var(--subtle);
    font-size: 0.82rem;
    font-weight: 800;
}

.halogen {
    background: var(--amber);
}

.sulfur-phosphor {
    background: var(--violet);
}

.nitrogen {
    background: var(--blue);
}

.cho {
    background: var(--green);
}

.boron {
    background: var(--accent);
}

.other {
    background: #69777a;
}

.formula-table {
    display: grid;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
}

.formula-table-header,
.formula-row {
    display: grid;
    grid-template-columns: minmax(120px, 1.1fr) repeat(4, minmax(96px, 0.9fr));
    gap: 0;
    align-items: stretch;
}

.formula-table-header {
    color: #5d7075;
    background: #f2f7f7;
    font-size: 0.78rem;
    font-weight: 900;
}

.formula-table-header span,
.formula-row span {
    min-width: 0;
    padding: 10px;
    border-right: 1px solid var(--line);
}

.formula-table-header span:last-child,
.formula-row span:last-child {
    border-right: 0;
}

.formula-row {
    background: #ffffff;
    font-size: 0.9rem;
}

.formula-row + .formula-row {
    border-top: 1px solid var(--line);
}

.formula {
    color: var(--ink);
    font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
    font-weight: 900;
}

.formula sub {
    font-size: 0.72em;
    line-height: 0;
}

.numeric {
    font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
    font-variant-numeric: tabular-nums;
}

.error-positive {
    color: var(--red);
}

.error-negative {
    color: var(--green);
}

@media (max-width: 1100px) {
    .app-header {
        grid-template-columns: 1fr;
        align-items: start;
        gap: 18px;
    }

    .header-nav {
        justify-content: flex-start;
    }

    .workspace {
        grid-template-columns: 1fr;
    }

    .control-surface {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        align-items: stretch;
    }

    .primary-panel {
        grid-column: 1 / -1;
    }
}

@media (max-width: 760px) {
    body {
        height: auto;
        min-height: 100dvh;
        display: block;
        overflow: auto;
    }

    .app-header {
        min-height: auto;
        padding: 18px;
    }

    .workspace {
        width: calc(100% - 24px);
        grid-template-columns: 1fr;
        margin: 12px auto 18px;
        overflow: visible;
    }

    .control-surface {
        grid-template-columns: 1fr;
        overflow: visible;
        padding-right: 0;
    }

    .results-surface {
        overflow: visible;
    }

    .results-surface,
    .tool-panel {
        padding: 14px;
    }

    .panel-heading,
    .results-toolbar {
        gap: 12px;
        margin-bottom: 14px;
    }

    .collapse-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .collapsible-panel.is-collapsed .collapsible-body {
        display: none;
    }

    .collapsible-panel.is-collapsed .panel-heading {
        margin-bottom: 0;
    }

    .button-row,
    .segmented-control {
        grid-template-columns: 1fr;
    }

    .limit-header {
        display: none;
    }

    .limit-row {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        grid-template-areas:
            "name name"
            "min max";
        gap: 8px 10px;
        padding: 10px;
    }

    .limit-row .element-name {
        grid-area: name;
    }

    .limit-row .limit-input:first-of-type {
        grid-area: min;
    }

    .limit-row .limit-input:last-of-type {
        grid-area: max;
    }

    .limit-input-label {
        display: block;
    }

    .limit-row input {
        min-height: 44px;
        text-align: left;
    }

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

    .intro-note {
        grid-template-columns: 1fr;
    }

    .result-card-header {
        grid-template-columns: 1fr;
    }

    .result-time {
        white-space: normal;
    }

    .formula-table {
        border: 0;
        gap: 8px;
        overflow: visible;
    }

    .formula-table-header {
        display: none;
    }

    .formula-row {
        grid-template-columns: 1fr 1fr;
        border: 1px solid var(--line);
        border-radius: var(--radius);
        overflow: hidden;
    }

    .formula-row + .formula-row {
        border-top: 1px solid var(--line);
    }

    .formula-row span {
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .formula-row span:nth-last-child(-n + 2) {
        border-bottom: 0;
    }

    .formula-row span:first-child {
        grid-column: 1 / -1;
        background: #f8fbfb;
    }
}

@media (max-width: 460px) {
    h1 {
        font-size: 1.75rem;
    }

    .eyebrow {
        font-size: 0.68rem;
    }

    .header-nav {
        gap: 14px;
    }

    .workspace {
        width: calc(100% - 18px);
    }

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

@media (max-width: 360px) {
    .panel-heading,
    .results-toolbar {
        align-items: stretch;
        flex-direction: column;
    }

    .panel-actions {
        width: 100%;
        justify-content: space-between;
    }

    .button-row {
        gap: 8px;
    }

    .check-grid {
        grid-template-columns: 1fr;
    }
}
