/* 전역 박스 모델 설정 */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh; /* 구형 브라우저 호환성 */
    min-height: 100dvh; /* 최신 브라우저 (모바일 주소창 제외) */
    margin: 0;
    padding: 20px 8px; /* 상하 20px, 좌우 8px */
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
    overflow-x: hidden; /* 가로 스크롤 방지 */
}

.calculator {
    background-color: #333;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    width: min(450px, 100%);
    max-width: 100%; /* 추가: 화면을 절대 넘지 않음 */
    display: flex;
    flex-direction: column;
    align-items: center;
}

.calculator-title {
    text-align: center;
    font-size: 1.2em;
    font-weight: bold;
    margin-bottom: 1px;
    color: white;
}

.display {
    background-color: #eee;
    padding: 10px;
    padding-bottom: 15px;
    margin-bottom: 0px;
    margin-top: 20px; /* 상단 여백 증가 */
    border-radius: 5px;
    width: 100%;
    box-sizing: border-box; /* 중요: padding이 width에 포함됨 */
    position: relative;
}

#main-display {
    position: absolute;
    opacity: 0;
    pointer-events: none;
    width: 0;
    height: 0;
    border: 0;
    padding: 0;
    margin: 0;
}

#expr-display {
    min-height: 0;
    font-size: 1.05rem;
    text-align: right;
    color: #666;
    padding-bottom: 0;
    line-height: 1.2;
}

#expr-display:empty {
    min-height: 0;
    padding-bottom: 0;
}

#expr-display:not(:empty) {
    min-height: 18px;
    padding-bottom: 4px;
}

#main-display-visual {
    width: 100%;
    min-height: 60px;
    font-size: 2.5rem;
    text-align: right;
    color: #333;
    line-height: 1.15;
    word-break: break-all;
    box-sizing: border-box;
}

#sub-display {
    min-height: 30px;
    font-size: 1rem;
    text-align: right;
    color: #666;
    padding-top: 2px;
}

.settings {
    margin-top: 20px; /* 여백 증가 */
    margin-bottom: 20px; /* 여백 증가 */
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    font-size: 1rem;
    width: 100%;
    gap: 6px;
}

.setting-block {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* 라벨 텍스트를 진하게 설정 */
.setting-block label {
    font-weight: bold;
    font-size: 0.92rem;
    color: inherit;
}

#decimal-places,
#rounding-method,
#color-select {
    width: 100%;
    padding: 6px;
    border-radius: 5px;
    border: none;
    font-size: 0.92rem;
}

.buttons {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    width: 100%;
    grid-auto-rows: 60px;
}

/* 전체 버튼 공통 스타일 */
button {
    padding: 6px 6px;
    min-height: 38px;
    font-size: 1rem;
    border: none;
    background-color: #4a4a4a;
    color: white;
    cursor: pointer;
    border-radius: 8px;
    transition: background-color 0.25s ease, transform 0.1s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

button:hover {
    background-color: #5a5a5a;
}

.small-text {
    font-size: 1rem;
}

/* 숫자 버튼 */
button[data-value],
button[data-action="appendDoubleZero"],
button[data-action="appendDecimal"] {
    background-color: #ffffff;
    color: #000;
    font-weight: 900;
    /* font-size: 1.7rem; 크기 축소 */
    font-size: clamp(1.5rem, 4.2vw, 1.7rem);
}

/* 숫자 버튼 hover */
button[data-value]:hover,
button[data-action="appendDoubleZero"]:hover,
button[data-action="appendDecimal"]:hover {
    background-color: #f0f0f0;
}

/* 사칙연산 기호 버튼 - 화이트 색상 */
button[data-action="operate"] {
    background-color: #ffffff;
    color: #000;
    font-size: 2.4rem; /* 크기 축소 */
    font-weight: 900;
    line-height: 1;
}

button[data-action="operate"]:hover {
    background-color: #f0f0f0; /* 밝은 회색 호버 */
    transform: scale(0.96);
}

/* = 버튼 - 화이트 색상 */
button[data-action="calculate"] {
    background-color: #ffffff;
    color: #000;
    font-size: 2.4rem; /* 크기 축소 */
    font-weight: 900;
    line-height: 1;
}

button[data-action="calculate"]:hover {
    background-color: #f0f0f0; /* 밝은 회색 호버 */
    transform: scale(0.96);
}

.double-height {
    grid-row: span 2;
}

/* 기능 버튼들 크기 조정 */
button[data-action="clearAll"],
button[data-action="clearEntry"],
button[data-action="toggleSign"],
button[data-action="percentage"],
button[data-action="memoryAdd"],
button[data-action="memorySub"],
button[data-action="memoryRecall"],
button[data-action="memoryClear"],
button[data-action="toggleBracket"],
button[data-action="grandTotal"],
button[data-action="squareRoot"],
button[data-action="taxPlus"],
button[data-action="taxMinus"],
button[data-action="toggleAreaConversion"] {
    /* font-size: 1.05rem; 크기 축소 */
    font-size: clamp(1.0rem, 3.2vw, 1.3rem);
    font-weight: 600;
}

/* 숫자 외 기능 버튼 */
button:not([data-value]):not([data-action="appendDoubleZero"]):not(
        [data-action="appendDecimal"]
    ):not([data-action="operate"]):not([data-action="calculate"]) {
    background-color: #5a5a5a;
}

/* 숫자 외 기능 버튼 hover */
button:not([data-value]):not([data-action="appendDoubleZero"]):not(
        [data-action="appendDecimal"]
    ):not([data-action="operate"]):not([data-action="calculate"]):hover {
    background-color: #6c6c6c;
}

/* 버튼 클릭 효과 */
button:active {
    transform: scale(0.96);
}

/* 에러 표시 스타일 */
.error {
    color: #ff3333;
}

/* 괄호 불균형 표시 */
.bracket-warning {
    border: 1px solid #ff9900;
}

/* 모바일 환경 최적화 */
@media (max-width: 480px) {
    body {
        align-items: flex-start; /* 상단 정렬 */
        padding: 16px 8px; /* 상단 여백 증가 */
    }

    .calculator {
        width: 100%;
        max-width: calc(100vw - 16px); /* 좌우 여백 확보 */
        padding: 16px 12px; /* 내부 패딩 조정 */
    }

    .display {
        margin-top: 12px; /* 상단 여백 확보 */
        padding: 8px; /* 패딩 조정 */
    }

    #main-display {
        width: 0;
        height: 0;
    }

    #expr-display {
        min-height: 0;
        font-size: 0.92rem;
    }

    #expr-display:not(:empty) {
        min-height: 14px;
    }

    #main-display-visual {
        min-height: 50px;
        font-size: 2rem;
    }

    #sub-display {
        font-size: 0.9rem;
        min-height: 25px;
    }

    .settings {
        margin-top: 16px;
        margin-bottom: 16px;
    }

    .buttons {
        gap: 5px;
        grid-auto-rows: 56px;
    }

    button {
        padding: 10px 6px;
        min-height: 56px;
        /* height: 56px; */
    }

    /* 숫자 버튼 텍스트 크기 */
    button[data-value],
    button[data-action="appendDoubleZero"],
    button[data-action="appendDecimal"] {
        font-size: 1.6rem;
    }

    /* 사칙연산 기호 텍스트 크기 */
    button[data-action="operate"] {
        font-size: 2.2rem;
    }

    /* = 버튼 텍스트 크기 */
    button[data-action="calculate"] {
        font-size: 2.2rem;
    }

    /* 기능 버튼들 텍스트 크기 */
    button[data-action="clearAll"],
    button[data-action="clearEntry"],
    button[data-action="toggleSign"],
    button[data-action="percentage"],
    button[data-action="memoryAdd"],
    button[data-action="memorySub"],
    button[data-action="memoryRecall"],
    button[data-action="memoryClear"],
    button[data-action="toggleBracket"],
    button[data-action="grandTotal"],
    button[data-action="taxPlus"],
    button[data-action="taxMinus"] {
        font-size: 1rem;
    }

    .small-text {
        font-size: 0.75rem;
    }

    
}

/* 더 작은 화면 */
@media (max-width: 360px) {
    body {
        padding: 12px 6px;
    }

    .calculator {
        max-width: calc(100vw - 12px);
        padding: 14px 10px;
    }

    button {
        padding: 8px 4px;
        min-height: 52px;
      /* height: 52px; */
    }

    .buttons {
        grid-auto-rows: 52px;
        gap: 4px;
    }

    button[data-value],
    button[data-action="appendDoubleZero"],
    button[data-action="appendDecimal"] {
        font-size: 1.5rem;
    }

    button[data-action="operate"],
    button[data-action="calculate"] {
        font-size: 2rem;
    }

    .small-text {
        font-size: 0.7rem;
    }
}
