:root {
    --surface: #ffffff;
    --surface-2: #f8f9fb;
    --surface-3: #f2f4f7;
    --surface-4: #e6e9ef;
    --ink: #111216;
    --ink-2: #1f2126;
    --ink-3: #3b3d44;
    --muted: #6c6a72;
    --muted-2: #a09ea6;
    --line: #e2e6ee;
    --line-2: #cfd6e1;
    --accent: #6b4cff;
    --accent-ink: #3e26cc;
    --accent-wash: #efebff;
    --ok: #3e6600;
    --ok-wash: #eef7d7;
    --warn: #e38a2b;
    --error: #d1403a;
    --font-sans: "Inter", ui-sans-serif, system-ui, "Helvetica Neue", Arial, sans-serif;
    --font-mono: "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;
    --radius-1: 4px;
    --radius-2: 8px;
    --radius-3: 12px;
    --shadow-2: 0 1px 2px rgba(17, 18, 22, 0.05), 0 8px 24px rgba(17, 18, 22, 0.06);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    min-height: 100%;
    background: var(--surface-2);
    color: var(--ink-3);
    font-family: var(--font-sans);
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; }

.app-shell {
    display: grid;
    grid-template-columns: 246px minmax(0, 1fr);
    min-height: 100vh;
}

.side-rail {
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 16px 12px 20px;
    overflow-y: auto;
    background: var(--surface);
    border-right: 1px solid var(--line);
}

.brand-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 8px 16px;
    margin-bottom: 6px;
    border-bottom: 1px solid var(--line);
}

.brand-mark {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    display: block;
}

.brand-name {
    display: block;
    color: var(--ink);
    font-size: 15px;
    font-weight: 800;
    letter-spacing: -0.02em;
    line-height: 1.05;
}

.brand-subtitle {
    display: block;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    line-height: 1.35;
}

.nav-section-label {
    padding: 18px 10px 6px;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.16em;
    text-transform: uppercase;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 10px;
    border: 0;
    border-radius: var(--radius-2);
    background: transparent;
    color: var(--ink-3);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    text-align: left;
    text-decoration: none;
    transition: background 120ms ease, color 120ms ease;
}

.nav-item:hover {
    background: var(--surface-3);
    color: var(--ink-2);
}

.nav-item.is-current {
    background: var(--accent-wash);
    color: var(--accent-ink);
    font-weight: 700;
}

.nav-icon {
    width: 18px;
    flex-shrink: 0;
    font-size: 16px;
    text-align: center;
}

.nav-spacer { flex: 1; }

.nav-version {
    padding: 8px 10px;
    color: var(--muted-2);
    font-family: var(--font-mono);
    font-size: 11px;
    letter-spacing: 0.04em;
}

.nav-button { margin: 0; }

.main-layer {
    min-width: 0;
    min-height: 100vh;
    background: var(--surface-2);
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 15;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 13px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.topbar-kicker {
    margin: 0 0 2px;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.topbar-title {
    margin: 0;
    color: var(--ink);
    font-size: 18px;
    font-weight: 800;
    letter-spacing: -0.025em;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.toolbar {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.filter-form {
    display: flex;
    align-items: end;
    gap: 10px;
    flex-wrap: wrap;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.field label {
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.13em;
    text-transform: uppercase;
}

.field > span {
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.13em;
    text-transform: uppercase;
}

.control {
    height: 34px;
    min-width: 150px;
    border: 1px solid var(--line-2);
    border-radius: 999px;
    background: var(--surface);
    color: var(--ink-2);
    padding: 0 12px;
    font-weight: 600;
    outline: none;
}

.control:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(107, 76, 255, 0.14);
}

.btn {
    height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border-radius: 999px;
    border: 1px solid var(--line-2);
    background: var(--surface);
    color: var(--ink-3);
    cursor: pointer;
    font-size: 13px;
    font-weight: 700;
    padding: 0 14px;
    text-decoration: none;
    white-space: nowrap;
    transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}

.btn:hover {
    background: var(--surface-3);
    border-color: var(--muted-2);
    transform: translateY(-1px);
}

.btn-primary {
    border-color: var(--accent);
    background: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    border-color: var(--accent-ink);
    background: var(--accent-ink);
}

.messages { padding: 12px 24px 0; }

.message {
    display: inline-flex;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--ink-3);
    padding: 7px 12px;
    font-weight: 700;
    box-shadow: var(--shadow-2);
}

.message.success {
    border-color: #cfe597;
    background: var(--ok-wash);
    color: var(--ok);
}

.sheet-area {
    height: calc(100vh - 126px);
    min-height: 420px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.compare-area {
    height: calc(100vh - 126px);
    min-height: 420px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.compare-list {
    flex: 1;
    overflow: auto;
    border-top: 1px solid var(--line);
}

.compare-row {
    display: grid;
    grid-template-columns: minmax(280px, 34%) minmax(0, 1fr);
    gap: 0;
    border-top: 2px solid var(--line);
    background: var(--surface);
}

.compare-row:nth-child(even) {
    background: #f6f8fb;
}

.compare-row:hover {
    background: rgba(239, 235, 255, 0.34);
}

.compare-question {
    position: sticky;
    left: 0;
    z-index: 4;
    background: inherit;
    border-right: 1px solid var(--line);
    padding: 16px 14px;
}

.largest-gap {
    display: inline-flex;
    margin-top: 10px;
    border: 1px solid var(--accent);
    border-radius: 999px;
    background: var(--accent-wash);
    color: var(--accent-ink);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 800;
    padding: 5px 8px;
}

.compare-groups {
    display: grid;
    grid-template-columns: repeat(3, minmax(280px, 1fr));
}

.compare-group {
    min-width: 0;
    border-right: 1px solid var(--surface-3);
    padding: 14px 12px;
}

.compare-summary {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin: 8px 0;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
}

.category-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
    gap: 6px;
}

.category-result {
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.76);
    padding: 7px 8px;
}

.category-name {
    display: block;
    overflow: hidden;
    color: var(--muted);
    font-size: 10px;
    font-weight: 750;
    line-height: 1.15;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.category-percent {
    display: block;
    margin-top: 4px;
    color: var(--ink);
    font-family: var(--font-mono);
    font-size: 14px;
    font-weight: 850;
    line-height: 1;
}

.category-base {
    display: block;
    overflow: hidden;
    margin-top: 3px;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sheet-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 24px;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 11px;
}

.sheet-scroll {
    flex: 1;
    overflow: auto;
    border-top: 1px solid var(--line);
}

.sheet-table {
    width: 100%;
    min-width: 1180px;
    border-collapse: collapse;
    table-layout: fixed;
}

.col-question-width { width: 34%; }
.col-group-width { width: 22%; }

.sheet-table thead th {
    position: sticky;
    top: 0;
    z-index: 8;
    height: 38px;
    padding: 0 12px;
    background: var(--surface-3);
    border-bottom: 1px solid var(--line);
    border-right: 1px solid var(--line);
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-align: left;
    text-transform: uppercase;
    white-space: nowrap;
}

.sheet-table tbody tr {
    background: var(--surface);
    border-top: 2px solid var(--line);
    border-bottom: 1px solid var(--surface-3);
}

.sheet-table tbody tr:nth-child(even) {
    background: #f6f8fb;
}

.sheet-table tbody tr:hover {
    background: rgba(239, 235, 255, 0.42);
}

.sheet-table tbody tr:first-child {
    border-top-width: 1px;
}

.sheet-table td {
    padding: 14px 12px;
    border-right: 1px solid var(--surface-3);
    color: var(--ink-2);
    font-size: 13px;
    font-weight: 600;
    vertical-align: top;
}

.col-question {
    position: sticky;
    left: 0;
    z-index: 6;
    background: inherit;
    border-right: 1px solid var(--line) !important;
}

thead .col-question {
    z-index: 12;
    background: var(--surface-3);
}

.question-text {
    display: -webkit-box;
    overflow: hidden;
    color: var(--ink);
    font-size: 13px;
    font-weight: 750;
    line-height: 1.32;
    -webkit-line-clamp: 4;
    -webkit-box-orient: vertical;
}

.question-open {
    display: block;
    width: 100%;
    border: 0;
    background: transparent;
    cursor: pointer;
    padding: 0;
    text-align: left;
}

.question-open:hover .question-text {
    color: var(--accent-ink);
}

.question-open-hint {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--accent-ink);
    font-size: 11px;
    font-weight: 800;
    padding: 4px 7px;
}

.edit-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    color: var(--accent-ink);
    font-size: 11px;
    font-weight: 800;
    text-decoration: none;
}

body.modal-open {
    overflow: hidden;
}

.breakdown-modal[hidden] {
    display: none;
}

.breakdown-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    place-items: center;
    padding: 28px;
}

.breakdown-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(17, 18, 22, 0.42);
    backdrop-filter: blur(2px);
}

.breakdown-dialog {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    width: min(1180px, calc(100vw - 48px));
    max-height: min(820px, calc(100vh - 48px));
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface);
    box-shadow: 0 1px 2px rgba(17, 18, 22, 0.05), 0 28px 80px rgba(17, 18, 22, 0.22);
}

.breakdown-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 18px;
    padding: 18px 20px;
    border-bottom: 1px solid var(--line);
}

.breakdown-header h2 {
    margin: 0;
    color: var(--ink);
    font-size: 18px;
    font-weight: 850;
    letter-spacing: -0.02em;
    line-height: 1.25;
}

.modal-close-btn {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line-2);
    border-radius: 999px;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
}

.modal-close-btn:hover {
    background: var(--surface-3);
    color: var(--ink);
}

.breakdown-controls {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 14px;
    padding: 14px 20px;
    border-bottom: 1px solid var(--line);
    background: var(--surface-2);
}

.breakdown-body {
    overflow: auto;
    padding: 16px 20px 20px;
}

.modal-group {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    padding: 14px;
}

.modal-group + .modal-group {
    margin-top: 14px;
}

.modal-category-grid {
    margin: 10px 0;
    grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
}

.category-result.is-overall {
    border-color: var(--accent);
    background: var(--accent-wash);
}

.category-result.is-overall .category-name,
.category-result.is-overall .category-base {
    color: var(--accent-ink);
}

@media (max-width: 900px) {
    .breakdown-modal {
        padding: 12px;
    }

    .breakdown-dialog {
        width: calc(100vw - 24px);
        max-height: calc(100vh - 24px);
    }

    .breakdown-controls {
        align-items: stretch;
        flex-direction: column;
    }
}

.group-cell { background: transparent; }

.group-card {
    display: flex;
    min-height: 108px;
    flex-direction: column;
    gap: 8px;
}

.group-card-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 8px;
}

.group-label {
    display: block;
    color: var(--ink);
    font-size: 13px;
    font-weight: 800;
    line-height: 1.2;
}

.change-pill {
    flex-shrink: 0;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 800;
    line-height: 1;
    padding: 4px 6px;
}

.change-wrap {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.change-note {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--surface);
    color: var(--accent-ink);
    cursor: help;
    outline: none;
}

.change-note i {
    font-size: 12px;
    line-height: 1;
}

.change-note-popover {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    z-index: 30;
    width: 280px;
    max-width: min(280px, 70vw);
    border: 1px solid var(--line-2);
    border-radius: 10px;
    background: var(--surface);
    box-shadow: 0 1px 2px rgba(17, 18, 22, 0.05), 0 14px 34px rgba(17, 18, 22, 0.13);
    color: var(--ink-3);
    font-family: var(--font-sans);
    font-size: 12px;
    font-weight: 650;
    line-height: 1.38;
    opacity: 0;
    padding: 9px 10px;
    pointer-events: none;
    text-transform: none;
    transform: translateY(-3px);
    transition: opacity 120ms ease, transform 120ms ease;
}

.change-note-popover::before {
    content: "";
    position: absolute;
    top: -5px;
    right: 12px;
    width: 9px;
    height: 9px;
    border-left: 1px solid var(--line-2);
    border-top: 1px solid var(--line-2);
    background: var(--surface);
    transform: rotate(45deg);
}

.change-note:hover .change-note-popover,
.change-note:focus-visible .change-note-popover {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

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

.metric-grid > div:only-child {
    grid-column: 1 / -1;
}

.metric-grid > div {
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.76);
    padding: 7px 8px;
}

.metric-label {
    display: block;
    overflow: hidden;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.08em;
    line-height: 1.2;
    text-overflow: ellipsis;
    text-transform: uppercase;
    white-space: nowrap;
}

.metric-value {
    display: block;
    margin-top: 3px;
    color: var(--ink);
    font-family: var(--font-mono);
    font-size: 15px;
    font-weight: 800;
    line-height: 1.1;
}

.metric-base {
    display: block;
    overflow: hidden;
    margin-top: 2px;
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.included {
    display: block;
    overflow: hidden;
    color: var(--muted);
    font-size: 11px;
    font-weight: 500;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.included::before {
    content: "Includes: ";
    color: var(--muted-2);
    font-weight: 700;
}

.group-empty {
    display: inline-flex;
    align-items: center;
    border: 1px dashed var(--line-2);
    border-radius: 999px;
    color: var(--muted-2);
    font-size: 11px;
    font-weight: 700;
    padding: 5px 9px;
}

.metric {
    color: var(--ink);
    font-family: var(--font-mono);
    font-size: 12px;
    font-weight: 700;
}

.empty-state {
    padding: 42px 24px;
    color: var(--muted);
    font-weight: 700;
    text-align: center;
}

.editor-page {
    max-width: 980px;
    padding: 24px;
}

.editor-card {
    border: 1px solid var(--line);
    border-radius: var(--radius-3);
    background: var(--surface);
    box-shadow: var(--shadow-2);
    overflow: hidden;
}

.editor-card-head {
    padding: 18px 20px;
    border-bottom: 1px solid var(--line);
}

.editor-card-head h2 {
    margin: 0 0 6px;
    color: var(--ink);
    font-size: 18px;
    letter-spacing: -0.02em;
}

.editor-card-head p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.group-table {
    width: 100%;
    border-collapse: collapse;
}

.group-table th {
    padding: 10px 12px;
    background: var(--surface-3);
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    letter-spacing: 0.12em;
    text-align: left;
    text-transform: uppercase;
}

.group-table td {
    padding: 10px 12px;
    border-top: 1px solid var(--surface-3);
    color: var(--ink-2);
    font-weight: 600;
}

.group-label-input {
    width: 100%;
    height: 34px;
    border: 1px solid var(--line-2);
    border-radius: 8px;
    padding: 0 10px;
    font-weight: 600;
}

.group-order-select {
    height: 34px;
    border: 1px solid var(--line-2);
    border-radius: 8px;
    padding: 0 8px;
    font-weight: 700;
}

.editor-actions {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid var(--line);
}

.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background: var(--surface-2);
}

.login-card {
    width: min(420px, 100%);
    border: 1px solid var(--line);
    border-radius: 16px;
    background: var(--surface);
    box-shadow: var(--shadow-2);
    padding: 24px;
}

.login-card h1 {
    margin: 12px 0 4px;
    color: var(--ink);
    font-size: 24px;
    letter-spacing: -0.03em;
}

.login-card p {
    margin: 0 0 18px;
    color: var(--muted);
}

.login-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

.login-field label {
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.login-field input {
    height: 38px;
    border: 1px solid var(--line-2);
    border-radius: 10px;
    padding: 0 12px;
}

@media (max-width: 1050px) {
    .app-shell { grid-template-columns: 1fr; }
    .side-rail {
        position: static;
        height: auto;
        flex-direction: row;
        align-items: center;
        overflow-x: auto;
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }
    .brand-row {
        border-bottom: 0;
        border-right: 1px solid var(--line);
        margin: 0 4px 0 0;
        padding: 0 12px 0 4px;
        flex-shrink: 0;
    }
    .nav-section-label, .nav-spacer, .nav-version { display: none; }
    .nav-item { flex-shrink: 0; width: auto; }
    .toolbar { align-items: stretch; flex-direction: column; }
    .topbar { align-items: flex-start; flex-direction: column; }
}
