:root {
    --bg: #eef0f2;
    --grid-line: rgba(30, 30, 30, 0.045);
    --grid-size: 40px;
    --white: #ffffff;
    --text: #1c1e21;
    --muted: #6b7076;
    --border: #e3e5e8;
    --accent: #d81f2a;
    --accent-dark: #a8151d;
    --accent-tint: rgba(216, 31, 42, 0.08);
    --toc-inactive: rgba(28, 30, 33, 0.38);
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    margin: 0;
    min-height: 100vh;
    background-color: var(--bg);
    background-image:
        linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
        linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
    background-size: var(--grid-size) var(--grid-size);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Table of contents (inline sticky sidebar) ---------- */

.doc-layout {
    display: flex;
    align-items: flex-start;
    gap: 48px;
}

.toc-sidebar {
    flex: 0 0 auto;
    width: 220px;
    position: sticky;
    top: 24px;
    align-self: flex-start;
    overflow: hidden;
    transition: width 0.3s ease;
}

.toc-sidebar.collapsed {
    width: 28px;
}

#doc-content {
    flex: 1 1 auto;
    min-width: 0;
    transition: flex-basis 0.3s ease;
}

.toc-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    margin-bottom: 14px;
    border: none;
    background: transparent;
    box-shadow: none;
    appearance: none;
    color: var(--toc-inactive);
    cursor: pointer;
    flex-shrink: 0;
    transition: color 0.2s ease;
}

.toc-toggle:hover,
.toc-toggle:focus-visible {
    background: transparent;
    color: var(--text);
}

.toc-toggle-icon {
    display: block;
    font-size: 1.15rem;
    line-height: 1;
}

/* Expanded state shows a "collapse" affordance; once collapsed, the icon
   switches to a hamburger meaning "open the list". */
.toc-toggle-icon::before {
    content: "\00AB";
}

.toc-sidebar.collapsed .toc-toggle-icon::before {
    content: "\2630";
}

.toc-empty {
    color: var(--muted);
    font-size: 0.9rem;
    white-space: nowrap;
}

.toc-nav {
    max-height: 2000px;
    opacity: 1;
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.25s ease;
}

.toc-sidebar.collapsed .toc-nav {
    max-height: 0;
    opacity: 0;
    transition: max-height 0.3s ease, opacity 0.15s ease;
}

.toc-nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 220px;
}

.toc-nav li {
    position: relative;
    padding: 0 0 22px 20px;
}

.toc-nav li:last-child {
    padding-bottom: 0;
}

.toc-nav li::before {
    content: "";
    position: absolute;
    left: 0;
    top: 6px;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--border);
    transition: background-color 0.2s ease;
}

.toc-nav li::after {
    content: "";
    position: absolute;
    left: 3px;
    top: 15px;
    bottom: 0;
    width: 1px;
    background: var(--border);
}

.toc-nav li:last-child::after {
    display: none;
}

.toc-nav li:has(a.active)::before {
    background: var(--accent);
}

.toc-nav a {
    color: var(--toc-inactive);
    text-decoration: none;
    font-size: 0.92rem;
    font-weight: 300;
    line-height: 1.3;
    transition: color 0.2s ease;
}

.toc-nav a:hover {
    color: var(--text);
}

.toc-nav a.active {
    color: var(--accent);
    font-weight: 400;
}

.toc-nav li.toc-level-h3 a {
    display: inline-block;
    margin-left: 14px;
    font-size: 0.86rem;
}

@media (max-width: 760px) {
    .doc-layout {
        flex-direction: column;
        gap: 8px;
    }

    .toc-sidebar {
        position: static;
        width: 100%;
        flex-basis: auto;
    }
}

/* ---------- Hero ---------- */

.hero {
    text-align: center;
    padding: 72px 24px 40px;
}

.hero h1 {
    margin: 0;
    font-size: clamp(2.2rem, 5vw, 3.4rem);
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--text);
}

.hero-accent {
    width: 72px;
    height: 5px;
    background: var(--accent);
    border-radius: 3px;
    margin: 20px auto;
}

.hero-subtitle {
    max-width: 640px;
    margin: 0 auto;
    color: var(--muted);
    font-size: 1.05rem;
    line-height: 1.5;
}

/* ---------- Full-width sections ---------- */

.calculator-section {
    width: 100%;
    padding: 32px 0 56px;
    /* Left transparent on purpose: the grid background shows through here,
       from the hero all the way down to the description section. */
}

.doc-section {
    width: 100%;
    background: var(--white);
    box-shadow: 0 -1px 0 var(--border), 0 12px 32px rgba(20, 20, 20, 0.05);
    padding: 56px 0 80px;
}

.section-inner {
    max-width: 1080px;
    margin: 0 auto;
    padding: 0 clamp(20px, 5vw, 56px);
}

.doc-section .section-inner {
    padding-left: clamp(0px, 1vw, 8px);
}

/* ---------- Calculator form ---------- */

.calculator-form {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: 24px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex: 1 1 180px;
}

label {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

input[type="number"] {
    padding: 14px 16px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 1.1rem;
    background: var(--white);
    color: var(--text);
}

input[type="number"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-tint);
}

#submit-btn {
    flex: 0 0 auto;
    padding: 14px 32px;
    border: none;
    border-radius: 10px;
    background: var(--accent);
    color: #fff;
    font-size: 1.05rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.15s ease;
    white-space: nowrap;
}

#submit-btn:hover:not(:disabled) {
    background: var(--accent-dark);
}

#submit-btn:disabled {
    opacity: 0.6;
    cursor: default;
}

.result {
    margin-top: 28px;
    padding: 20px;
    border-radius: 10px;
    background: var(--accent-tint);
    border: 1px solid rgba(216, 31, 42, 0.18);
    text-align: center;
}

.result .count {
    font-size: 2.4rem;
    font-weight: 800;
    color: var(--accent);
}

.result .label {
    font-size: 0.9rem;
    color: var(--muted);
}

.error {
    margin-top: 28px;
    padding: 16px 20px;
    border-radius: 10px;
    background: #f7f7f8;
    border-left: 4px solid var(--accent);
    color: #58181c;
    font-size: 0.95rem;
}

/* ---------- Markdown description ---------- */

.markdown-body {
    line-height: 1.65;
    font-size: 1rem;
    overflow-wrap: break-word;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
    line-height: 1.3;
    margin: 1.5em 0 0.6em;
    color: var(--text);
    scroll-margin-top: 32px;
}

.markdown-body h1 {
    font-size: 1.9rem;
    border-bottom: 2px solid var(--accent);
    padding-bottom: 0.3em;
}

.markdown-body h2 {
    font-size: 1.4rem;
}

.markdown-body h1:first-child,
.markdown-body h2:first-child,
.markdown-body h3:first-child {
    margin-top: 0;
}

.markdown-body p {
    margin: 0 0 1em;
}

.markdown-body ul,
.markdown-body ol {
    margin: 0 0 1em;
    padding-left: 1.4em;
}

.markdown-body li {
    margin-bottom: 0.4em;
}

.markdown-body code {
    background: var(--bg);
    border-radius: 4px;
    padding: 0.15em 0.4em;
    font-family: "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.88em;
}

.markdown-body pre {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px 18px;
    overflow-x: auto;
    margin: 0 0 1em;
}

.markdown-body pre code {
    background: none;
    padding: 0;
    color: var(--text);
}

.markdown-body blockquote {
    margin: 0 0 1em;
    padding: 6px 18px;
    border-left: 3px solid var(--accent);
    color: var(--muted);
    background: var(--bg);
}

.markdown-body a {
    color: var(--accent);
    font-weight: 600;
}

.markdown-body hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 2em 0;
}

/* ---------- Footer ---------- */

.site-footer {
    text-align: center;
    padding: 32px 24px 48px;
    color: var(--muted);
    font-size: 0.9rem;
}

.site-footer a {
    color: var(--text);
    font-weight: 600;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: color 0.2s ease, border-color 0.2s ease;
}

.site-footer a:hover {
    color: var(--accent);
    border-color: var(--accent);
}

@media (max-width: 560px) {
    .hero {
        padding: 48px 16px 32px;
    }

    .calculator-form {
        flex-direction: column;
        align-items: stretch;
    }

    button {
        width: 100%;
    }
}
