:root {
    color-scheme: light;

    --bg: #f5f7fa;
    --surface: #ffffff;
    --text: #1e293b;
    --muted: #64748b;
    --border: #e2e8f0;
    --accent: #2563eb;
    --hover: #eff6ff;
}

@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;

        --bg: #0f172a;
        --surface: #1e293b;
        --text: #f1f5f9;
        --muted: #94a3b8;
        --border: #334155;
        --accent: #60a5fa;
        --hover: #334155;
    }
}

:root[data-theme="light"] {
    color-scheme: light;

    --bg: #f5f7fa;
    --surface: #ffffff;
    --text: #1e293b;
    --muted: #64748b;
    --border: #e2e8f0;
    --accent: #2563eb;
    --hover: #eff6ff;
}

:root[data-theme="dark"] {
    color-scheme: dark;

    --bg: #0f172a;
    --surface: #1e293b;
    --text: #f1f5f9;
    --muted: #94a3b8;
    --border: #334155;
    --accent: #60a5fa;
    --hover: #334155;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0 auto;
    padding: 32px 20px;
    max-width: 1100px;

    background: var(--bg);
    color: var(--text);

    font-family: system-ui, sans-serif;
    line-height: 1.5;
    transition: background-color 0.2s, color 0.2s;
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 24px;
}

.page-header h1 {
    margin: 0;
}

#theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    font-size: 24px;
    transition: background-color 0.2s, border-color 0.2s;
    padding: 0;
    flex-shrink: 0;
}

#theme-icon {
    display: block;
    width: 22px;
    height: 22px;
    filter: brightness(0);
}

:root[data-theme="dark"] #theme-icon {
    filter: brightness(0) invert(1)
}

#theme-toggle:hover {
    background: var(--hover);
}

section {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
}

h2, h3 {
    margin-top: 0;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
}

thead {
    background: var(--bg);
}

th, td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

thead th {
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 600;
}

tbody tr:last-child th,
tbody tr:last-child td {
    border-bottom: none;
}

tbody tr:hover {
    background: var(--hover);
}

table input[type="number"] {
    width: 100%;
    min-width: 0;
    padding: 8px 10px;
    background: transparent;
    color: var(--text);
    border: 1px solid transparent;
    border-radius: 6px;
    font: inherit;
    font-variant-numeric: tabular-nums;
    text-align: right;
    transition: background-color 0.15s, border-color 0.15s;
}

#grading-table,
#curved-grading-table {
    table-layout: fixed;
}

#grading-table tbody tr,
#curved-grading-table tbody tr {
    height: 60px;
}

#curved-grading-table input[type="number"] {
    height: 32px;
    padding: 4px 8px;
}

table input[type="number"]:hover {
    background: var(--bg);
    border-color: var(--border);
}

table input[type="number"]:focus {
    outline: none;
    background: var(--surface);
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--hover)
}

table th:not(:first-child),
table td:not(:first-child) {
    text-align: right;
}

table th:first-child {
    font-weight: 600;
}

@media (max-width: 600px) {
    body {
        padding: 16px 12px;
    }

    section {
        padding: 16px 12px;
    }

    th, td {
        padding: 10px 6px;
        font-size: 0.9rem;
    }

    table input[type="number"] {
        padding: 6px 4px;
        font-size: 0.9rem;
    }
}

.grading-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 20px;
    margin-bottom: 20px;
}

.grading-grid > section {
    min-width: 0;
    margin-bottom: 0;
}

@media (max-width: 800px) {
    .grading-grid {
        grid-template-columns: 1fr;
    }
}

.calculator-controls {
    display: flex;
    gap: 12px;
    margin-top: 10px;
    margin-bottom: 20px;
}

#raw-score {
    flex: 1;
    min-width: 0;
    padding: 12px 16px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
}

#raw-score:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#calculate,
#create-link,
#copy-link {
    padding: 12px 24px;
    background: var(--accent);
    color: #ffffff;
    border-color: none;
    border-radius: 8px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: filter 0.2s, transform 0.1s;
}

#calculate:hover,
#create-link:hover,
#copy-link:hover {
    filter: brightness(1.1);
}

#calculate:active,
#create-link:active,
#copy-link:active {
    transform: scale(0.98);
}

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

.result-grid[hidden] {
    display: none;
}

.result-card {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 24px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 12px;
}

#stats-table {
    width: 100%;
}

#stats-table th:first-child {
    width: 60%;
}

#stats-table th:last-child,
#stats-table td:last-child {
    width: 40%;
    text-align: right;
}

#stats-table input[type="number"] {
    max-width: 180px;
}

#custom-grades > label {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    font-weight: 500;
}

#custom-grades-enabled {
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
    cursor: pointer;
}

#custom-grades-inputs {
    margin-top: 20px;
    background: transparent;
    border: none;
    padding: 0;
    margin-bottom: 0;
}

#custom-grades-inputs[hidden] {
    display: none;
}

#link-creation label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
}

#link-title,
#generated-link {
    display: block;
    width: 100%;
    padding: 12px 16px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
}

#link-title:focus,
#generated-link:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#generated-link {
    margin-top: 16px;
    color: var(--muted);
}

#create-link,
#copy-link {
    margin-top: 16px;
}

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

    h2 {
        font-size: 1.25rem;
    }

    #calculate,
    #create-link,
    #copy-link {
        width: 100%;
    }

    #stats-table input[type="number"] {
        max-width: 120px;
    }

    .result-card {
        padding: 18px;
    }
}

@media (max-width: 600px) {
    .calculator-controls {
        flex-direction: column;
    }

    #raw-score {
        width: 100%;
        flex: none;
    }

    #calculate {
        width: 100%;
    }
}

#math-explanation {
    padding: 20px 24px;
}

#math-explanation summary {
    cursor: pointer;
    font-weight: 600;
    font-size: 1rem;
    list-style-position: inside;
}

#math-explanation summary:hover {
    color: var(--accent);
}

.math-content {
    margin-top: 20px;
    color: var(--muted);
    line-height: 1.7;
}

.math-formula {
    padding: 20px;
    margin: 20px 0;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    text-align: center;
    font-size: 1.1rem;
    overflow-x: auto;
}

@media (max-width: 600px) {
    .math-formula {
        font-size: 0.85rem;
        padding: 12px 8px;
    }
}