:root {
    --font-text: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont, system-ui, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
    /* Fira Code에는 한글이 없으므로 한글은 Pretendard로 넘긴다. */
    --font-num: "Fira Code", "Pretendard Variable", Pretendard, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    --content-width: 1200px;
    color-scheme: dark;

    --bg: #111214;
    --card: #1a1c20;
    --text: #e8e9ec;
    --text-muted: #9a9ea8;
    --border: #2c2f36;
    --hover: #23262c;
    --focus: #849dff;
    --danger: #ff6369;

    /* 필드 배색: 부호 빨강, 지수 초록, 가수 파랑 */
    --sign: #ff6369;
    --exponent: #3dd68c;
    --significand: #7b93f7;
    --combination: #bf7af0;
    --marker: #ff9a3d;
}

*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-text);
    font-size: 16px;
    line-height: 1.55;
    -webkit-text-size-adjust: 100%;
}

a {
    color: var(--significand);
}

h1 {
    font-size: 1.3rem;
    font-weight: 700;
    margin: 0;
    letter-spacing: -0.01em;
}

h1:focus {
    outline: none;
}

button {
    font: inherit;
    color: inherit;
}

sup, sub {
    font-size: 0.68em;
    line-height: 0;
}

.num {
    font-family: var(--font-num);
    font-variant-ligatures: none;
}

.inline-link {
    margin-right: 0.6rem;
}

.k-sign { --kind: var(--sign); }
.k-int { --kind: var(--significand); }
.k-frac { --kind: var(--exponent); }

/* 형식 탭: 넓은 화면에서는 왼쪽 세로 목록, 좁은 화면에서는 위쪽 가로 목록 */
.explorer {
    display: grid;
    grid-template-columns: 9.5rem minmax(0, 1fr);
    gap: 1.5rem;
    align-items: start;
    margin-top: 0.75rem;
}

.explorer-main {
    min-width: 0;
}

.tabs {
    position: sticky;
    top: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.tab {
    font-family: var(--font-num);
    font-size: 0.9rem;
    font-weight: 500;
    padding: 0.32rem 0.75rem;
    border-radius: 8px;
    color: var(--text);
    text-decoration: none;
}

/* 고정소수점과 다른 정수 인코딩은 색으로 구분한다. */
.tab.fixed {
    color: var(--exponent);
}

/* 2의 보수·부호 없음이 아닌 정수 인코딩 (1의 보수, BCD 등) */
.tab.encoded {
    color: var(--combination);
}

.tab:hover {
    background: var(--hover);
}

.tab.active {
    background: var(--text);
    color: var(--bg);
}

.format-head {
    padding-bottom: 0.25rem;
}

.format-note {
    margin: 0.3rem 0 0;
    color: var(--text-muted);
    font-size: 0.92rem;
}

/* 항목: 가운데 맞춘 제목과 내용을 구분선으로 나눈다. */
.block {
    padding: 1.4rem 0;
    border-bottom: 1px solid var(--border);
    text-align: center;
}

.block:last-child {
    border-bottom: none;
}

.block-label {
    font-family: var(--font-num);
    font-size: 0.88rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.badge {
    font-family: var(--font-text);
    font-size: 0.75rem;
    padding: 0.05rem 0.5rem;
    margin-left: 0.35rem;
    border-radius: 999px;
    background: var(--hover);
}

.message {
    margin-top: 0.4rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.message.error {
    color: var(--danger);
}

/* Value */
.value-fit {
    width: 100%;
    container-type: inline-size;
    max-width: 52rem;
    margin: 0 auto;
}

.value-input {
    width: 100%;
    padding: 0.3rem 0.5rem;
    border: 1px solid transparent;
    border-radius: 10px;
    background: transparent;
    color: var(--text);
    font-size: min(clamp(1.6rem, 4.5vw, 3rem), calc(((100cqw - 1rem - 6px) / var(--chars) - 1px) / 0.6));
    font-weight: 600;
    text-align: center;
}

.value-input:hover {
    border-color: var(--border);
}

.value-input:focus {
    outline: none;
    border-color: var(--focus);
    background: var(--card);
}

.value-input.invalid {
    border-color: var(--danger);
}

/* 16진수 */
.hex-input {
    width: 100%;
    max-width: 44rem;
    padding: 0.35rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font-size: 1rem;
    text-align: center;
}

.hex-input:focus {
    outline: none;
    border-color: var(--focus);
    background: var(--card);
}

.hex-input.invalid {
    border-color: var(--danger);
}

/* Bit Pattern */
.bit-pattern {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.35rem 1rem;
    /* 한 줄에 4바이트(32비트)씩 */
    max-width: 52rem;
    margin: 0 auto;
}

.bit-group {
    display: inline-flex;
    gap: 0.12rem;
}

.bit {
    width: 1.35rem;
    padding: 0 0 0.15rem;
    border: none;
    border-bottom: 3px solid var(--kind);
    border-radius: 1px;
    background: transparent;
    color: var(--text);
    font-family: var(--font-num);
    font-size: 1.3rem;
    font-weight: 600;
    line-height: 1.4;
    cursor: pointer;
}

.bit.alt {
    border-bottom-style: double;
}

.bit:hover:not(:disabled) {
    background: var(--hover);
}

.bit:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 1px;
}

.bit:disabled {
    cursor: not-allowed;
    color: var(--text-muted);
}

.legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.3rem 1rem;
    margin-top: 0.9rem;
    font-size: 0.82rem;
    color: var(--text-muted);
}

.swatch {
    display: inline-block;
    width: 0.65rem;
    height: 0.65rem;
    border-radius: 2px;
    background: var(--kind);
    margin-right: 0.35rem;
    vertical-align: -0.02rem;
}

/* 필드 해석 */
.table-wrap {
    overflow-x: auto;
    max-width: 56rem;
    margin: 0 auto;
}

table.fields {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    text-align: left;
}

table.fields th {
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid var(--border);
}

table.fields td {
    padding: 0.45rem 0.6rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
    overflow-wrap: anywhere;
}

table.fields tr:last-child td {
    border-bottom: none;
}

table.fields td:first-child {
    white-space: nowrap;
}

.bitstring {
    word-break: break-all;
    min-width: 8ch;
    max-width: 30ch;
}

dl.facts {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.5rem 1.25rem;
    margin: 0 auto;
    max-width: 44rem;
    text-align: left;
}

dl dt {
    color: var(--text-muted);
    font-size: 0.88rem;
    padding-top: 0.1rem;
}

dl dd {
    margin: 0;
    min-width: 0;
    overflow-wrap: anywhere;
}

@media (max-width: 820px) {
    .explorer {
        grid-template-columns: minmax(0, 1fr);
        gap: 0.75rem;
    }

    .tabs {
        position: static;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.3rem;
    }

    .tab {
        border: 1px solid var(--border);
        background: var(--card);
        border-radius: 999px;
        font-size: 0.85rem;
        padding: 0.28rem 0.65rem;
    }
}

@media (max-width: 640px) {
    /* 좁은 화면에서는 필드 표를 행마다 쌓는다. */
    table.fields thead {
        display: none;
    }

    table.fields, table.fields tbody, table.fields tr, table.fields td {
        display: block;
    }

    table.fields tr {
        padding: 0.5rem 0;
        border-bottom: 1px solid var(--border);
    }

    table.fields tr:last-child {
        border-bottom: none;
    }

    table.fields td {
        border: none;
        padding: 0.1rem 0;
    }

    .bitstring {
        max-width: none;
    }

    .bit {
        width: 1.05rem;
        font-size: 1.05rem;
    }

    .bit-pattern {
        gap: 0.3rem 0.6rem;
    }

    dl.facts {
        grid-template-columns: 1fr;
        gap: 0.1rem;
    }

    dl dd {
        margin-bottom: 0.5rem;
    }
}
