* {
    box-sizing: border-box;
}

:root {
    color-scheme: dark light;
    font-family: "Segoe UI", Tahoma, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    --bg: #0f172a;
    --panel: rgba(15, 23, 42, 0.75);
    --panel-border: rgba(148, 163, 184, 0.25);
    --text: #e2e8f0;
    --muted: #94a3b8;
    --accent: #38bdf8;
    --accent-strong: #0ea5e9;
    --error: #f87171;
}

body {
    margin: 0;
    background: radial-gradient(circle at top, rgba(59, 130, 246, 0.15), transparent 45%), var(--bg);
    color: var(--text);
    min-height: 100vh;
    height: auto;
    display: flex;
    flex-direction: column;
    overflow: auto;
}

.page-header,
.page-footer {
    padding: 1.5rem 2rem;
    background: rgba(15, 23, 42, 0.85);
    border-bottom: 1px solid var(--panel-border);
    flex-shrink: 0;
}

.page-footer {
    border-top: 1px solid var(--panel-border);
    border-bottom: none;
    margin-top: auto;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 1rem;
}

.page-header h1 {
    margin: 0 0 0.25rem;
    font-size: clamp(1.75rem, 2.8vw, 2.4rem);
}

.tagline {
    margin: 0;
    color: var(--muted);
}

.muted {
    color: var(--muted);
    font-size: 0.8rem;
}


.page-content {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    padding: 1.25rem 2rem 1.75rem;
    flex: 1 1 auto;
    min-height: 0;
    overflow: visible;
    align-items: stretch;
}

.controls {
    flex: 0 0 auto;
}

.controls-panel {
    overflow: hidden;
}

.controls-panel.panel {
    padding: 0;
}

.controls-panel summary {
    list-style: none;
    cursor: pointer;
    padding: 1.25rem 1.5rem;
    font-size: 1.25rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.controls-panel summary::-webkit-details-marker {
    display: none;
}

.controls-panel summary::after {
    content: "-";
    font-size: 1.1rem;
    line-height: 1;
}

.controls-panel summary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 4px;
}

.controls-panel:not([open]) summary::after {
    content: "+";
}

.controls-body {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0 1.5rem 1.5rem;
    border-top: 1px solid var(--panel-border);
}

.results {
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel.results {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.panel.results.breakdown-visible {
    gap: 0.75rem;
}

.results > h2,
.results > p,
.summary,
#breakdown-panel {
    flex-shrink: 0;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    padding: 1.25rem 1.5rem;
    box-shadow: 0 18px 30px rgba(15, 23, 42, 0.3);
}

.panel h2 {
    margin-top: 0;
    margin-bottom: 0.5rem;
    font-size: 1.25rem;
}

.fieldset {
    border: 1px solid var(--panel-border);
    border-radius: 10px;
    padding: 0.75rem 1rem 1rem;
    margin-bottom: 1rem;
}

.fieldset legend {
    padding: 0 0.4rem;
    color: var(--muted);
}

.field-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 0.75rem;
}

.field-group.inline {
    flex-direction: row;
    align-items: center;
    gap: 0.75rem;
}

.spread-toggle {
    align-items: flex-start;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
}

.toggle-label input {
    transform: scale(1.05);
}

.spread-slider {
    gap: 0.5rem;
}

.spread-slider input[type="range"] {
    width: 100%;
}

.spread-slider .hint {
    display: inline-block;
}

.field-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.field-grid label {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.field-grid label input,
.field-grid label select {
    width: 100%;
}

label {
    font-size: 0.9rem;
}

input,
textarea,
button,
select {
    font: inherit;
    border-radius: 8px;
    border: 1px solid rgba(148, 163, 184, 0.45);
    background: rgba(15, 23, 42, 0.6);
    color: inherit;
    padding: 0.5rem 0.65rem;
}

input:focus,
textarea:focus,
button:focus,
select:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.hidden {
    display: none !important;
}

button {
    cursor: pointer;
    border: 1px solid var(--accent);
    background: linear-gradient(135deg, rgba(14, 165, 233, 0.95), rgba(59, 130, 246, 0.85));
    color: #f8fafc;
    transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.link-button {
    background: none;
    border: 1px solid rgba(148, 163, 184, 0.35);
    color: var(--accent);
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
    margin-top: 0.3rem;
}

.link-button:hover {
    background: rgba(56, 189, 248, 0.12);
    color: var(--accent-strong);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.55;
    transform: none;
    box-shadow: none;
}

button:not(:disabled):hover {
    transform: translateY(-1px);
    box-shadow: 0 12px 18px rgba(56, 189, 248, 0.35);
}

textarea {
    resize: vertical;
}

.actions {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-top: 0.5rem;
}

#status-message {
    color: var(--muted);
}

#status-message[data-kind="error"] {
    color: var(--error);
}

#status-message[data-kind="warning"] {
    color: #fbbf24;
}

.hint {
    color: var(--muted);
    font-size: 0.85rem;
}

.table-wrapper {
    overflow: auto;
    border: 1px solid var(--panel-border);
    border-radius: 12px;
}

.results .table-wrapper {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
}

.panel.results.breakdown-visible .table-wrapper {
    margin-bottom: 0.5rem;
}

#results-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 960px;
}

#results-table thead {
    position: sticky;
    top: 0;
    background: rgba(15, 23, 42, 0.95);
    z-index: 1;
}

#results-table th,
#results-table td {
    padding: 0.4rem 0.55rem;
    border-bottom: 1px solid rgba(148, 163, 184, 0.2);
    text-align: left;
    vertical-align: middle;
    font-size: 0.92rem;
}

#results-table td.rank-cell {
    white-space: nowrap;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

#results-table tbody tr:nth-child(even) {
    background: rgba(30, 41, 59, 0.55);
}

#results-table td.numeric,
#results-table th.numeric {
    text-align: right;
    white-space: nowrap;
}

#results-table td.item-cell {
    vertical-align: top;
}

.placeholder-row td {
    text-align: center;
    padding: 1.2rem;
    color: var(--muted);
}

.actual-rate-input {
    width: 100%;
    border-radius: 6px;
    padding: 0.35rem 0.45rem;
    background: rgba(15, 23, 42, 0.75);
    border: 1px solid rgba(148, 163, 184, 0.4);
    color: inherit;
}

.actual-editor {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.4rem;
}

.actual-editor input {
    width: 72px;
    text-align: right;
    padding: 0.3rem 0.35rem;
    border-radius: 6px;
    background: rgba(15, 23, 42, 0.7);
}

.actual-editor input::-webkit-outer-spin-button,
.actual-editor input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.actual-editor input[type="number"] {
    appearance: textfield;
    -moz-appearance: textfield;
}

.actual-editor.locked input {
    background: rgba(15, 23, 42, 0.4);
    border-color: rgba(148, 163, 184, 0.25);
}

.actual-editor input[readonly] {
    cursor: default;
}

.actual-negative {
    color: var(--muted);
}

.locked-input {
    background: rgba(15, 23, 42, 0.4);
    border-color: rgba(148, 163, 184, 0.25);
    cursor: default;
}

.ratio-separator {
    color: var(--muted);
    font-weight: 600;
}

.ratio-hint {
    margin-top: 0.3rem;
    font-size: 0.75rem;
}

#results-table td.numeric .ratio-hint {
    text-align: left;
}

.cell-value {
    display: inline-block;
}

.item-title {
    font-weight: 600;
}

.step-flow {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    margin: 0.35rem 0;
    font-size: 0.85rem;
    color: inherit;
    text-decoration: none;
}

.step-flow .flow-target {
    font-weight: 600;
}

.step-flow .flow-arrow {
    color: var(--muted);
}

.step-flow .flow-source {
    text-align: left;
}

.step-flow.is-link {
    cursor: pointer;
}

.step-flow.is-link .flow-target,
.step-flow.is-link .flow-source,
.step-flow.is-link .flow-arrow {
    transition: color 0.15s ease;
}

.step-flow.is-link:hover .flow-target,
.step-flow.is-link:hover .flow-source,
.step-flow.is-link:hover .flow-arrow {
    color: var(--accent);
}

.step-flow.is-link:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.muted-value {
    color: var(--muted);
}

.summary {
    margin-top: 1rem;
    padding: 0.75rem;
    border: 1px solid var(--panel-border);
    border-radius: 10px;
    background: rgba(30, 41, 59, 0.65);
    flex-shrink: 0;
}

.summary:empty {
    display: none;
}

.rank-label {
    font-weight: 600;
}

#breakdown-panel {
    margin-top: 1rem;
    flex-shrink: 0;
}

#breakdown-panel[hidden] {
    display: none !important;
}

#breakdown-panel[open] {
    border: 1px solid var(--accent);
    border-radius: 10px;
    padding: 0.75rem;
    display: grid;
    grid-template-rows: auto 1fr;
    row-gap: 0.5rem;
    max-height: min(45vh, 420px);
}

.breakdown-body {
    overflow: auto;
}

#breakdown-content {
    white-space: pre-wrap;
    margin: 0;
}

@media (max-width: 1100px) {
    .table-wrapper {
        max-height: none;
    }
}

@media (max-width: 768px) {
    .page-header,
    .page-footer {
        padding: 1rem 1.25rem;
    }

    .page-header h1 {
        font-size: clamp(1.5rem, 6vw, 2rem);
    }

    .page-content {
        padding: 1rem 1.25rem 1.25rem;
        gap: 1rem;
    }

    .panel {
        padding: 1rem 1.1rem;
    }

    .controls-panel summary {
        padding: 1rem 1.1rem;
        font-size: 1.1rem;
    }

    .controls-body {
        padding: 0 1.1rem 1.1rem;
    }

    .field-group.inline {
        flex-direction: column;
        align-items: stretch;
    }

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

    .actual-editor {
        flex-wrap: wrap;
        gap: 0.3rem;
    }

    .actual-editor input {
        flex: 1 1 45%;
        min-width: 120px;
    }

    .ratio-hint {
        font-size: 0.7rem;
    }

    .table-wrapper {
        border-radius: 10px;
    }

    #results-table th,
    #results-table td {
        font-size: 0.85rem;
        padding: 0.35rem 0.45rem;
    }

    #results-table td.rank-cell {
        flex-direction: row;
        gap: 0.4rem;
    }
}

@media (min-width: 1100px) {
    .page-content {
        display: grid;
        grid-template-columns: minmax(280px, 360px) 1fr;
        align-items: start;
        gap: 1.5rem;
    }

    .controls {
        position: sticky;
        top: 1rem;
        align-self: start;
        max-height: calc(100vh - 2rem);
        overflow: visible;
    }

    .controls-panel {
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .controls-panel[open] .controls-body {
        flex: 1 1 auto;
        overflow-y: auto;
        padding-bottom: 1.5rem;
    }

    .results {
        min-height: 0;
    }

    .table-wrapper {
        max-height: none;
    }
}

@media (max-width: 520px) {
    body {
        height: auto;
        overflow: auto;
    }

    .page-content {
        padding: 0.85rem 1rem 1.1rem;
    }

    .panel {
        padding: 0.9rem;
    }

    .actual-editor input {
        flex: 1 1 auto;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        text-align: right;
    }

    .ratio-separator {
        display: none;
    }

    .summary {
        margin-top: 0.75rem;
        font-size: 0.9rem;
    }

    .table-wrapper {
        overflow-x: visible;
        border: none;
        padding: 0;
    }

    #results-table {
        min-width: 0;
        border-collapse: separate;
    }

    #results-table thead {
        display: none;
    }

    #results-table tbody {
        display: grid;
        gap: 0.65rem;
    }

    #results-table tr {
        display: flex;
        flex-direction: column;
        gap: 0.6rem;
        padding: 0.75rem 0.85rem;
        border: 1px solid var(--panel-border);
        border-radius: 12px;
        background: rgba(15, 23, 42, 0.92);
        box-shadow: 0 12px 24px rgba(15, 23, 42, 0.35);
    }

    #results-table tbody tr:nth-child(even) {
        background: rgba(15, 23, 42, 0.92);
    }

    #results-table tr.actual-negative {
        border-color: rgba(248, 113, 113, 0.45);
    }

    #results-table tr.placeholder-row {
        display: block;
        padding: 0;
        border: none;
        background: transparent;
        box-shadow: none;
    }

    #results-table th,
    #results-table td {
        font-size: 0.8rem;
        padding: 0;
        border: none;
    }

    #results-table td {
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
        width: 100%;
    }

    #results-table td::before {
        content: attr(data-label);
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.03em;
        color: var(--muted);
    }

    #results-table td.numeric {
        display: grid;
        grid-template-columns: max-content 1fr;
        column-gap: 0.5rem;
        align-items: center;
    }

    #results-table td.numeric::before {
        grid-column: 1;
        grid-row: 1;
        align-self: center;
    }

    #results-table td.numeric .cell-value {
        grid-column: 2;
        grid-row: 1;
        justify-self: end;
        text-align: right;
    }

    #results-table td.numeric .ratio-hint {
        grid-column: 1 / -1;
    }

    #results-table td.item-cell {
        display: grid;
        grid-template-columns: max-content 1fr;
        column-gap: 0.45rem;
        row-gap: 0.35rem;
        align-items: center;
    }

    #results-table td.item-cell::before {
        grid-column: 1;
        grid-row: 1;
        align-self: center;
    }

    #results-table td.item-cell .item-title {
        grid-column: 2;
        grid-row: 1;
        font-weight: 600;
        justify-self: start;
    }

    #results-table td.item-cell .actual-editor,
    #results-table td.item-cell .step-flow {
        grid-column: 1 / -1;
    }

    #results-table td.item-cell .step-flow {
        grid-column: 1 / -1;
        display: flex;
        flex-wrap: wrap;
        gap: 0.4rem 0.75rem;
        align-items: baseline;
    }

    #results-table td.item-cell .step-flow .flow-target {
        font-weight: 600;
    }

    #results-table td.item-cell .step-flow .flow-source {
        margin-left: auto;
        text-align: right;
    }

    #results-table td.item-cell .step-flow .flow-arrow {
        color: var(--muted);
    }

    #results-table td .actual-editor {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.35rem;
        margin-top: 0.1rem;
        width: 100%;
        align-items: center;
    }

    .rank-label {
        font-size: 1rem;
    }

    .link-button {
        font-size: 0.75rem;
        padding: 0.2rem 0.45rem;
    }

    #results-table td.rank-cell {
        flex-direction: row;
        align-items: center;
        gap: 0.45rem;
        padding-bottom: 0.1rem;
    }

    #results-table td.rank-cell::before {
        content: none;
    }

    #results-table td.rank-cell .link-button {
        margin-left: auto;
    }

    #results-table td .muted {
        font-size: 0.75rem;
        line-height: 1.25;
    }

    #results-table td .ratio-hint {
        margin-top: 0;
    }

    #results-table td .actual-editor input {
        display: block;
        width: 100%;
        max-width: 100%;
    }

    #results-table tr.placeholder-row td {
        padding: 1rem 0.5rem;
        text-align: center;
    }

    #results-table tr.placeholder-row td::before {
        content: none;
    }
}
