:root {
    --bg-color: #1e1e1e;
    --text-color: #ffffff;
    --button-bg: #2d2d2d;
    --button-text: #ffffff;
    --highlight-color: #ff9500;
    --number-color: #3a3a3a;
    --function-color: #2a2a2a;
    --memory-color: #252525;
    --shadow-color: rgba(0, 0, 0, 0.2);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-size: 1rem; /* Decreased for better readability */
    font-family: 'Roboto', sans-serif;
    font-size: 1.1rem; /* Increased from default */
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    height: 100vh;
    overflow: hidden;
}

.calculator-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    padding: 20px;
}

.calculator {
    width: 100%;
    height: 100%;
    max-width: 800px;
    max-height: 900px;
    background-color: #2a2a2a; /* Darker shade for better contrast */
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 10px 30px var(--shadow-color);
    display: flex;
    flex-direction: column;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px; /* Reduced padding for a more compact header */
    background-color: var(--bg-color);
}

.header-buttons {
    display: flex;
    gap: 15px;
}

.ai-button,
.history-toggle {
    background: none;
    border: none;
    color: var(--text-color);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 5px;
    transition: color 0.3s ease;
}

.ai-button:hover,
.history-toggle:hover {
    color: var(--highlight-color);
}

.title {
    font-size: 1.5rem; /* Increased title size for better visibility */
    font-weight: 500;
}

.display {
    position: relative;
    overflow: hidden;
    padding: 20px 15px; /* Adjusted padding for a more balanced look */
    background-color: var(--bg-color);
    width: 100%;
    text-align: left;
    flex-grow: 0;
    flex-shrink: 0;
}

#history {
    height: 24px;
    color: #a0a0a0;
    font-size: 1rem;
    margin-bottom: 8px;
    text-align: right;
}

#result {
    width: 100%;
    background-color: transparent;
    color: var(--text-color);
    font-size: 2.5rem; /* Slightly reduced font size for better fit */
    font-weight: 300;
    text-align: right;
    border: none;
    outline: none;
    resize: none;
    overflow: hidden;
    min-height: 60px;
    transition: height 0.3s ease, width 0.3s ease;
    min-width: 100%;
    max-width: 100%;
    line-height: 1.2;
    white-space: pre-wrap;
    word-wrap: break-word;
    padding: 10px;
    box-sizing: border-box;
    direction: ltr;
}

.buttons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px; /* Increased gap for better spacing */
    flex-grow: 1;
}

button {
    border: none;
    outline: none;
    font-size: clamp(1rem, 3vw, 1.3rem);
    padding: 8px 0; /* Reduced padding for a more compact button */
    cursor: pointer;
    transition: all 0.3s ease;
}

.memory {
    background-color: #3b3b3b; /* Slightly lighter for better visibility */
    color: var(--text-color);
}

.function {
    background-color: #2c2c2c; /* Darker for better contrast */
    color: var(--text-color);
}

.number, .decimal {
    background-color: #4a4a4a; /* Lighter for better visibility */
    color: var(--text-color);
}

.equals {
    background-color: #ffb300; /* Changed to a more visible color */
    color: var(--bg-color);
}

.operator {
    background-color: var(--function-color);
    color: var(--text-color);
}

.clear, .clear-entry, .backspace {
    background-color: var(--function-color);
    color: var(--text-color);
}

button:hover {
    filter: brightness(1.2);
}

.history-panel {
    position: fixed;
    top: 0;
    right: -300px;
    width: 300px;
    height: 100%;
    background-color: var(--button-bg);
    padding: 15px; /* Reduced padding for a more compact history panel */
    transition: right 0.3s ease, opacity 0.3s ease;
    overflow-y: auto;
    box-shadow: -5px 0 15px var(--shadow-color);
    opacity: 0;
    visibility: hidden;
}

.history-panel.show {
    right: 0;
    opacity: 1;
    visibility: visible;
}

#history-list {
    margin-top: 20px;
}

#clear-history {
    margin-top: 20px;
    padding: 10px; /* Adjusted padding for better fit */
    background-color: var(--function-color);
    color: var(--text-color);
    border: none;
    cursor: pointer;
    border-radius: 5px;
    transition: background-color 0.3s ease;
}

@media (max-width: 430px) {
    .calculator {
        border-radius: 0;
        padding: 10px; /* Added padding for better mobile experience */
    }
}

.history-panel {
    width: 250px;
    background-color: var(--button-bg);
    border-radius: 20px;
    padding: 20px;
    box-shadow: 0 10px 30px var(--shadow-color);
    color: var(--text-color);
}

.history-panel h2 {
    margin-top: 0;
    font-size: 1.2em; /* Reduced size for better fit */
    margin-bottom: 15px;
}

#history-list {
    max-height: 350px;
    overflow-y: auto;
    margin-bottom: 15px;
}

#history-list div {
    margin-bottom: 8px;
    padding: 8px; /* Reduced padding for a more compact list item */
    background-color: var(--number-color);
    border-radius: 8px;
    font-size: 0.95em;
}

#clear-history {
    width: 100%;
    padding: 10px; /* Adjusted padding for better fit */
    background-color: var(--function-color);
    color: var(--text-color);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    font-size: 1rem;
}

#clear-history:hover {
    background-color: var(--highlight-color);
}

@media (max-width: 768px) {
    .calculator-container {
        padding: 0;
    }

    .calculator {
        border-radius: 0;
        max-width: none;
        max-height: none;
    }

    .history-panel {
        width: 100%;
        max-width: none;
        border-radius: 0;
        right: -100%;
        padding: 10px; /* Reduced padding for better fit */
    }

    .history-panel.show {
        right: 0;
    }
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.close-history {
    background: none;
    border: none;
    color: var(--text-color);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 5px;
    transition: color 0.3s ease;
}

.close-history:hover {
    color: var(--highlight-color);
}

.response-box {
    height: auto;
    max-height: 300px;
    overflow-y: auto;
    word-wrap: break-word;
    line-height: 1.4; /* Adjusted for better readability */
    letter-spacing: 0.5px;
    text-align: justify;
}