* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --page-bg: #eef3f9;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --surface-blue: #eff6ff;
    --border: #dbe3ef;
    --border-strong: #cbd5e1;
    --text: #1f2937;
    --text-strong: #111827;
    --text-muted: #64748b;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-soft: #dbeafe;
    --success: #059669;
    --success-soft: #ecfdf5;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --shadow: 0 18px 45px rgba(15, 23, 42, 0.09);
}

body {
    min-width: 320px;
    font-family: Arial, sans-serif;
    background:
        radial-gradient(circle at top left, rgba(37, 99, 235, 0.08), transparent 34%),
        var(--page-bg);
    color: var(--text);
}

button,
input,
select {
    font: inherit;
}

button {
    -webkit-tap-highlight-color: transparent;
}

.page {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 34px 24px 52px;
}

.solver-card,
.auth-card {
    width: 100%;
    background: var(--surface);
    border: 1px solid rgba(203, 213, 225, 0.72);
    border-radius: 22px;
    box-shadow: var(--shadow);
}

.solver-card {
    max-width: 1500px;
    padding: 30px;
}

.auth-card {
    max-width: 460px;
    padding: 35px;
}

h1 {
    font-size: clamp(1.8rem, 3vw, 2.25rem);
    line-height: 1.15;
    margin-bottom: 8px;
    color: var(--text-strong);
}

.subtitle {
    color: var(--text-muted);
    line-height: 1.55;
    margin-bottom: 30px;
}

.form-group {
    margin-bottom: 18px;
}

label {
    display: block;
    margin-bottom: 7px;
    color: var(--text-strong);
    font-weight: 700;
}

input,
select {
    width: 100%;
    min-height: 48px;
    padding: 12px 14px;
    color: var(--text);
    background: #ffffff;
    border: 1px solid var(--border-strong);
    border-radius: 12px;
    font-size: 15px;
    transition:
        border-color 0.18s ease,
        box-shadow 0.18s ease,
        background 0.18s ease;
}

input::placeholder {
    color: #94a3b8;
}

input:focus,
select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
}

.form-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 14px;
}

.btn-primary,
.btn-secondary,
.btn-guest {
    min-height: 46px;
    border-radius: 11px;
    font-weight: 700;
    transition:
        transform 0.15s ease,
        background 0.15s ease,
        border-color 0.15s ease,
        opacity 0.15s ease;
}

.btn-primary {
    width: 100%;
    padding: 13px 18px;
    color: #ffffff;
    background: var(--primary);
    border: none;
    font-size: 16px;
    cursor: pointer;
}

.btn-primary:hover {
    background: var(--primary-dark);
    transform: translateY(-1px);
}

.btn-primary:disabled {
    background: #93c5fd;
    cursor: not-allowed;
    transform: none;
}

.btn-secondary {
    padding: 10px 15px;
    color: #ffffff;
    background: #111827;
    border: 1px solid #111827;
    font-size: 14px;
    cursor: pointer;
    white-space: nowrap;
}

.btn-secondary:hover {
    background: #334155;
    border-color: #334155;
    transform: translateY(-1px);
}

.btn-secondary:disabled {
    opacity: 0.55;
    cursor: not-allowed;
    transform: none;
}

.auth-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-top: 8px;
}

.auth-secondary {
    width: 100%;
    padding: 13px;
}

.btn-guest {
    width: 100%;
    padding: 13px;
    margin-top: 14px;
    color: #ffffff;
    background: #0f172a;
    border: none;
    font-size: 16px;
    cursor: pointer;
}

.btn-guest:hover {
    background: #1e293b;
}

.auth-message {
    margin-top: 18px;
    font-size: 14px;
    line-height: 1.45;
}

.auth-message.success {
    color: #047857;
}

.auth-message.error {
    color: #b91c1c;
}

/* Header */

.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 24px;
    padding-bottom: 24px;
    margin-bottom: 26px;
    border-bottom: 1px solid #e2e8f0;
}

.top-bar .subtitle {
    max-width: 720px;
    margin-bottom: 0;
}

.user-panel {
    min-width: 230px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 9px;
}

.user-actions {
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
}

#userStatus {
    color: #334155;
    font-size: 14px;
    font-weight: 700;
    text-align: right;
}

/* Main two-column workspace */

.solver-layout {
    display: grid;
    grid-template-columns: minmax(350px, 440px) minmax(0, 1fr);
    align-items: start;
    gap: 26px;
}

.solver-controls,
.solver-results {
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 18px;
}

.solver-controls {
    padding: 22px;
    background: var(--surface-soft);
}

.solver-results {
    min-height: 620px;
    padding: 22px;
    background: #ffffff;
}

.panel-heading {
    margin-bottom: 20px;
}

.panel-heading h2 {
    margin-bottom: 6px;
    color: var(--text-strong);
    font-size: 20px;
}

.panel-heading p {
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.5;
}

.solve-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 12px;
    margin-top: 4px;
}

.solve-actions .btn-primary,
.solve-actions .btn-secondary {
    width: 100%;
    min-height: 50px;
    padding: 13px 16px;
    font-size: 15px;
}

/* Criteria multi-select dropdown */

.criteria-box {
    position: relative;
    margin-bottom: 20px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 14px;
}

.criteria-toggle {
    width: 100%;
    min-height: 62px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 13px 15px;
    color: var(--text);
    background: #ffffff;
    border: 1px solid var(--border-strong);
    border-radius: 13px;
    cursor: pointer;
    text-align: left;
    transition:
        border-color 0.18s ease,
        box-shadow 0.18s ease,
        background 0.18s ease;
}

.criteria-toggle:hover {
    background: #f8fbff;
    border-color: #93c5fd;
}

.criteria-toggle:focus-visible {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
}

.criteria-toggle-main {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.criteria-toggle-title {
    color: var(--text-strong);
    font-weight: 750;
}

.criteria-summary {
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.35;
}

.criteria-chevron {
    flex: 0 0 auto;
    color: #475569;
    font-size: 17px;
    transition: transform 0.2s ease;
}

.criteria-box.open .criteria-chevron {
    transform: rotate(180deg);
}

.criteria-dropdown {
    position: relative;
    z-index: 5;
    margin-top: 8px;
    padding: 12px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 16px 34px rgba(15, 23, 42, 0.12);
}

.criteria-description {
    padding: 2px 3px 11px;
    color: var(--text-muted);
    font-size: 13px;
    line-height: 1.45;
}

.criteria-menu {
    max-height: 330px;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 4px;
    scrollbar-width: thin;
    scrollbar-color: #cbd5e1 transparent;
}

.criteria-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.criteria-menu label {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    min-height: 46px;
    margin: 0;
    padding: 11px 12px;
    color: #334155;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 11px;
    font-size: 14px;
    font-weight: 600;
    line-height: 1.35;
    cursor: pointer;
    transition:
        border-color 0.15s ease,
        background 0.15s ease,
        transform 0.15s ease;
}

.criteria-menu label:hover {
    background: var(--surface-blue);
    border-color: #93c5fd;
    transform: translateY(-1px);
}

.criteria-menu input {
    width: 17px;
    min-height: 17px;
    height: 17px;
    flex: 0 0 17px;
    margin-top: 1px;
    padding: 0;
    accent-color: var(--primary);
    cursor: pointer;
}

.criteria-menu label:has(input:checked) {
    color: #1e3a8a;
    background: var(--primary-soft);
    border-color: var(--primary);
}

/* Result area */

.result-box {
    min-height: 190px;
    margin-top: 0;
}

.result-placeholder {
    min-height: 190px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 9px;
    padding: 28px;
    color: var(--text-muted);
    background: var(--surface-soft);
    border: 1px dashed #cbd5e1;
    border-radius: 15px;
    text-align: center;
}

.result-placeholder strong {
    color: #334155;
    font-size: 17px;
}

.result-placeholder p {
    max-width: 480px;
    font-size: 14px;
    line-height: 1.5;
}

.result-loading,
.result-success,
.result-error {
    padding: 22px;
    border-radius: 15px;
}

.result-loading {
    min-height: 190px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 18px;
    color: var(--primary-dark);
    background: var(--surface-blue);
    border: 1px solid #bfdbfe;
    text-align: center;
}

.result-loading strong {
    line-height: 1.5;
}

.solve-timer {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    min-width: 94px;
    padding: 9px 13px;
    color: #1e3a8a;
    background: #ffffff;
    border: 1px solid #bfdbfe;
    border-radius: 999px;
    font-size: 15px;
    font-variant-numeric: tabular-nums;
    font-weight: 800;
    box-shadow: 0 5px 14px rgba(37, 99, 235, 0.09);
}

.loading-spinner {
    width: 18px;
    height: 18px;
    display: inline-block;
    flex: 0 0 18px;
    border: 3px solid #bfdbfe;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: loading-spin 0.75s linear infinite;
}

@keyframes loading-spin {
    to {
        transform: rotate(360deg);
    }
}

.result-success {
    color: #065f46;
    background: var(--success-soft);
    border: 1px solid #a7f3d0;
    border-left: 5px solid #10b981;
}

.result-success h3 {
    margin-bottom: 15px;
    color: #064e3b;
    font-size: 22px;
}

.result-success p {
    display: grid;
    grid-template-columns: minmax(125px, auto) 1fr;
    gap: 12px;
    padding: 10px 0;
    margin: 0;
    border-bottom: 1px solid rgba(16, 185, 129, 0.15);
    line-height: 1.45;
}

.result-success p:last-of-type {
    border-bottom: none;
}

.result-success strong {
    color: #064e3b;
}

.result-error {
    color: #991b1b;
    background: var(--danger-soft);
    border: 1px solid #fecaca;
    border-left: 5px solid #ef4444;
    line-height: 1.5;
}

.result-error > strong {
    display: block;
    margin-bottom: 8px;
    color: #7f1d1d;
}

.result-error p {
    margin-top: 7px;
}

.result-note {
    margin-top: 14px;
    padding: 12px 13px;
    color: #075985;
    background: #f0f9ff;
    border-left: 4px solid #0284c7;
    border-radius: 10px;
    font-size: 14px;
    line-height: 1.45;
}

/* Graph */

.graph-box {
    margin-top: 22px;
    padding: 18px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.graph-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 8px;
}

.graph-header h3 {
    margin: 0;
    color: var(--text-strong);
    font-size: 20px;
}

.graph-help {
    margin-bottom: 15px;
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.45;
}

#functionChart {
    width: 100%;
    height: 480px;
    display: block;
}

.graph-box #functionChart {
    cursor: grab;
}

.graph-box #functionChart:active {
    cursor: grabbing;
}

/* History */

.history-box {
    margin-bottom: 25px;
    padding: 18px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 16px;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 14px;
}

.history-header h3 {
    color: var(--text-strong);
    font-size: 20px;
}

.history-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.history-message,
.history-item {
    padding: 14px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
}

.history-message {
    color: #374151;
    line-height: 1.45;
}

.history-item-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 14px;
    margin-bottom: 10px;
}

.history-item-top strong {
    color: var(--text-strong);
    font-size: 15px;
}

.history-item-top span {
    color: var(--text-muted);
    font-size: 13px;
    white-space: nowrap;
}

.history-item-details {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    color: #374151;
    font-size: 14px;
}

.hidden {
    display: none !important;
}

/* Responsive */

@media (max-width: 1100px) {
    .solver-layout {
        grid-template-columns: minmax(320px, 390px) minmax(0, 1fr);
        gap: 20px;
    }

    .solver-card {
        padding: 24px;
    }

    #functionChart {
        height: 420px;
    }
}

@media (max-width: 900px) {
    .solver-layout {
        grid-template-columns: 1fr;
    }

    .solver-results {
        min-height: auto;
    }

    .history-item-details {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 650px) {
    .page {
        padding: 20px 12px 34px;
    }

    .solver-card,
    .auth-card {
        padding: 20px;
        border-radius: 18px;
    }

    .top-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .user-panel {
        width: 100%;
        min-width: 0;
        align-items: stretch;
    }

    .user-actions {
        width: 100%;
        display: grid;
        grid-template-columns: 1fr 1fr;
    }

    #userStatus {
        text-align: left;
    }

    .solver-controls,
    .solver-results {
        padding: 17px;
    }

    .form-row,
    .auth-actions,
    .solve-actions {
        grid-template-columns: 1fr;
    }

    .btn-secondary {
        width: 100%;
    }

    .history-header,
    .graph-header,
    .history-item-top {
        align-items: stretch;
        flex-direction: column;
    }

    .history-item-top span {
        white-space: normal;
    }

    .result-success p {
        grid-template-columns: 1fr;
        gap: 3px;
    }

    #functionChart {
        height: 340px;
    }
}