:root {
    --bg-color: #f0f2f5;
    --calc-bg: #ffffff;
    --text-primary: #1a1a1a;
    --text-secondary: #2196f3;
    --btn-bg: #f4f6f8;
    --btn-bg-hover: #e8ecf1;
    --btn-operator-color: #2196f3;
    --btn-action-color: #2196f3;
    --btn-equal-bg: #2196f3;
    --btn-equal-color: #ffffff;
    --btn-equal-hover: #1976d2;
    --shadow: 0 20px 50px rgba(0,0,0,0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

.calculator-container {
    background-color: var(--calc-bg);
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 16px;
    position: relative;
    overflow-y: auto;
}

/* Top Bar */
.top-bar {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    width: 100%;
    padding: 16px 0 10px 0;
    margin-bottom: 20px;
    color: #555;
    position: sticky;
    top: -16px;
    background-color: var(--calc-bg);
    z-index: 100;
}

.menu-icon {
    display: flex;
    justify-content: center;
    align-items: center;
}

.menu-icon i {
    font-size: 26px;
    cursor: pointer;
    transition: color 0.2s ease;
}

.menu-icon i:hover {
    color: var(--text-primary);
}

.math-icon-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
}

.scientific-icon {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    width: 30px;
    height: 30px;
    border: 2px solid #555;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 700;
    color: #555;
    cursor: pointer;
    align-items: center;
    justify-items: center;
    transition: all 0.2s ease;
}

.scientific-icon:hover {
    color: var(--text-primary);
    border-color: var(--text-primary);
}

.scientific-icon span {
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

/* Display */
.display {
    flex-grow: 1;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: auto auto;
    align-content: end;
    gap: 12px;
    padding: 20px 0;
    margin-bottom: 20px;
}

.expression {
    grid-column: 1 / 5;
    justify-self: end;
    font-size: 28px;
    color: var(--text-secondary);
    font-weight: 400;
    margin-bottom: 8px;
    height: 50px;
    display: flex;
    align-items: center;
    white-space: nowrap;
    overflow-x: auto;
    max-width: 100%;
    transition: color 0.2s ease;
}

.expression::-webkit-scrollbar, .result::-webkit-scrollbar {
    display: none;
}

.expression.active {
    font-size: 48px;
    color: var(--text-primary);
    font-weight: 500;
}

.result {
    grid-column: 4 / 5;
    justify-self: center;
    font-size: 28px;
    color: var(--text-secondary);
    font-weight: 400;
    height: 60px;
    display: flex;
    align-items: center;
    white-space: nowrap;
    overflow: visible;
    transition: color 0.2s ease;
}

.result.active {
    font-size: 48px;
    color: var(--text-primary);
    font-weight: 500;
}

.history-icon-wrapper {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    width: 100%;
    padding-bottom: 30px;
    color: #555;
    font-size: 22px;
}

.history-icon-wrapper i {
    justify-self: center; /* Center it in the first column */
    cursor: pointer;
    transition: color 0.2s ease;
}

@keyframes historyBlink {
    0% { transform: scale(1); color: #555; }
    50% { transform: scale(1.4); color: var(--btn-operator-color); }
    100% { transform: scale(1); color: #555; }
}
.history-add-anim {
    animation: historyBlink 0.5s ease;
}

.history-icon-wrapper i:hover {
    color: var(--text-primary);
}

/* Keypad */
.keypad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    padding-bottom: 20px;
    width: 100%;
}

.btn {
    border: none;
    border-radius: 50%;
    width: 100%;
    aspect-ratio: 1;
    max-width: 90px;
    margin: 0 auto;
    font-size: 28px;
    font-weight: 500;
    font-family: inherit;
    background-color: var(--btn-bg);
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.2s ease;
    user-select: none;
}

.btn:active {
    transform: scale(0.95);
}

.btn:hover {
    background-color: var(--btn-bg-hover);
}

.btn-action {
    color: var(--btn-action-color);
}

.btn-operator {
    color: var(--btn-operator-color);
}

.btn-equal {
    background-color: var(--btn-equal-bg);
    color: var(--btn-equal-color);
    box-shadow: 0 4px 12px rgba(33, 150, 243, 0.3);
}

.btn-equal:hover {
    background-color: var(--btn-equal-hover);
}

.btn i {
    font-size: 20px;
}

/* History Modal */
.history-modal {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 75%;
    background-color: var(--calc-bg);
    border-radius: 30px 30px 0 0;
    box-shadow: 0 -10px 40px rgba(0,0,0,0.1);
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    padding: 24px;
    z-index: 100;
}

.history-modal.active {
    transform: translateY(0);
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    color: var(--text-primary);
}

.history-header h3 {
    font-size: 20px;
    font-weight: 600;
}

.history-header i {
    font-size: 24px;
    cursor: pointer;
    color: #555;
}

.history-list {
    flex-grow: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-bottom: 20px;
}

.history-item {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    border-bottom: 1px solid #eee;
    padding: 12px;
    border-radius: 12px;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.history-item:hover {
    background-color: var(--btn-bg-hover);
}

.history-item .hist-expr {
    font-size: 18px;
    color: #555;
    margin-bottom: 4px;
}

.history-item .hist-res {
    font-size: 24px;
    color: var(--text-primary);
    font-weight: 500;
}

.clear-history-btn {
    width: 100%;
    padding: 16px;
    border: none;
    border-radius: 16px;
    background-color: #ff5252;
    color: white;
    font-size: 18px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: background-color 0.2s ease;
}

.clear-history-btn:hover {
    background-color: #ff1744;
}

/* Scientific & Landscape Mode */
.keypads-wrapper {
    display: block;
    width: 100%;
}

.calculator-container.landscape-mode .keypads-wrapper {
    display: flex;
    flex: 1;
    height: 100%;
    gap: 15px;
    padding-bottom: 10px;
}

.scientific-keypad {
    display: none;
}

.calculator-container.landscape-mode .scientific-keypad {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    width: 55%;
    height: 100%;
    align-content: stretch;
    padding-right: 15px;
    border-right: 1px solid #444;
    margin-right: 0;
}

.calculator-container.landscape-mode .keypad {
    width: 45%;
    height: 100%;
    gap: 8px;
    align-content: stretch;
    padding-bottom: 0;
}

.calculator-container.landscape-mode {
    max-width: none;
    width: 100vw;
    height: 100vh;
    padding: 10px 30px;
}

.calculator-container.landscape-mode .top-bar {
    display: none;
}

.calculator-container.landscape-mode .history-icon-wrapper {
    padding-bottom: 5px;
    margin-top: -10px;
}

.calculator-container.landscape-mode .display {
    min-height: 60px;
    padding-bottom: 5px;
}

.calculator-container.landscape-mode .result {
    font-size: 36px;
}

/* Ensure icons on landscape match iOS pill shape and fit vertically */
.calculator-container.landscape-mode .btn {
    max-width: 100%;
    aspect-ratio: auto;
    height: 100%;
    border-radius: 40px;
    font-size: 18px;
}

.btn-sci {
    background-color: var(--btn-bg);
    color: var(--text-primary);
    font-size: 16px;
    font-weight: 500;
}

.btn-sci:hover {
    background-color: var(--btn-bg-hover);
}

/* Deg/Rad Toggle */
.deg-rad-toggle {
    grid-column: span 2;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    background-color: var(--btn-bg);
    border-radius: 30px;
    font-size: 14px;
    color: var(--text-primary);
    user-select: none;
}

.deg-rad-toggle label {
    display: flex;
    align-items: center;
    gap: 4px;
    cursor: pointer;
}

.deg-rad-toggle input[type="radio"] {
    accent-color: var(--btn-operator-color);
    cursor: pointer;
}

/* Sidebar Menu */
.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
    border-radius: 30px;
}
.sidebar-overlay.active {
    opacity: 1;
    visibility: visible;
}
.sidebar {
    position: fixed;
    top: 0;
    left: -100%;
    width: 280px;
    height: 100%;
    background-color: var(--calc-bg);
    z-index: 1001;
    transition: left 0.3s ease;
    border-radius: 30px 0 0 30px;
    box-shadow: 5px 0 15px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}
.sidebar.active {
    left: 0;
}
.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 24px;
    border-bottom: 2px solid var(--btn-operator-color);
    background-color: rgba(0, 0, 0, 0.02);
}
.sidebar-header h2 {
    font-size: 20px;
    font-weight: 700;
    color: var(--btn-operator-color);
    margin: 0;
}
.sidebar-header i {
    font-size: 24px;
    cursor: pointer;
    color: #888;
    transition: color 0.2s ease;
}
.sidebar-header i:hover {
    color: #ff5252;
}
.sidebar-menu {
    list-style: none;
    padding: 0;
    margin: 0;
}
.sidebar-menu > li {
    border-bottom: 1px solid #f5f5f5;
}
.has-submenu {
    border-bottom: 2px solid #eee !important;
}
.sidebar-menu > li:not(.has-submenu) {
    padding: 16px 24px;
    font-size: 16px;
    color: var(--text-primary);
    cursor: pointer;
    transition: background-color 0.2s ease;
    display: flex;
    align-items: center;
    gap: 12px;
}
.sidebar-menu > li:not(.has-submenu):hover {
    background-color: var(--btn-bg-hover);
}
.submenu-toggle {
    padding: 18px 24px;
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    transition: background-color 0.2s ease;
}
.submenu-toggle:hover {
    background-color: var(--btn-bg-hover);
}
.submenu-toggle i:first-child {
    margin-right: 12px;
    width: 20px;
    text-align: center;
}
.submenu-toggle span {
    flex-grow: 1;
}
.submenu-toggle .arrow {
    transition: transform 0.3s ease;
}
.submenu-toggle.expanded .arrow {
    transform: rotate(180deg);
}
.submenu {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 0;
    overflow: hidden;
    background-color: var(--btn-bg);
    transition: max-height 0.3s ease;
}
.submenu.expanded {
    max-height: 12000px;
}
.submenu li {
    padding: 12px 24px 12px 48px;
    font-size: 15px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: background-color 0.2s ease;
    display: flex;
    align-items: center;
    gap: 12px;
}
.submenu li:hover {
    background-color: #ddd;
    color: var(--text-primary);
}
.submenu li.has-nested-submenu {
    display: block;
    padding: 0;
    cursor: default;
}
.submenu li.has-nested-submenu:hover {
    background-color: transparent;
}
.nested-submenu-toggle {
    padding: 12px 24px 12px 48px;
    font-size: 15px;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: background-color 0.2s ease, color 0.2s ease;
}
.nested-submenu-toggle:hover {
    background-color: #ddd;
    color: var(--text-primary);
}
.nested-submenu-toggle i:first-child {
    width: 20px;
    text-align: center;
}
.nested-submenu-toggle span {
    flex-grow: 1;
}
.nested-submenu-toggle .nested-arrow {
    transition: transform 0.3s ease;
}
.nested-submenu-toggle.expanded .nested-arrow {
    transform: rotate(180deg);
}
.nested-submenu {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 0;
    overflow: hidden;
    background-color: rgba(0, 0, 0, 0.05);
    transition: max-height 0.3s ease;
}
.nested-submenu.expanded {
    max-height: 8000px;
}
.nested-submenu li {
    padding: 12px 24px 12px 64px;
}

/* Search Overlay */
.search-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(255, 255, 255, 0.95);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding-top: 60px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}
.search-overlay.active {
    opacity: 1;
    pointer-events: auto;
}
.search-container {
    width: 90%;
    max-width: 500px;
    background-color: var(--calc-bg);
    border-radius: 20px;
    box-shadow: var(--shadow);
    overflow: hidden;
    transform: translateY(-20px);
    transition: transform 0.3s ease;
}
.search-overlay.active .search-container {
    transform: translateY(0);
}
.search-header {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid rgba(0,0,0,0.05);
}
.search-header i {
    font-size: 20px;
    color: var(--text-secondary);
}
.search-header input {
    flex-grow: 1;
    border: none;
    outline: none;
    font-size: 18px;
    padding: 0 16px;
    background: transparent;
    color: var(--text-primary);
    font-family: inherit;
}
.search-header #close-search {
    cursor: pointer;
    color: #ff5252;
}
.search-results {
    max-height: 350px;
    overflow-y: auto;
    padding: 10px 0;
}
.search-item {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    gap: 16px;
    cursor: pointer;
    transition: background-color 0.2s ease;
    color: var(--text-primary);
    text-decoration: none;
}
.search-item:hover {
    background-color: var(--btn-bg-hover);
}
.search-item i {
    font-size: 20px;
    color: var(--btn-operator-color);
    width: 24px;
    text-align: center;
}
.search-item span {
    font-size: 16px;
    font-weight: 500;
}
.search-no-results {
    padding: 30px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 16px;
}

.error-msg {
    color: #ff5252;
    font-size: 14px;
    margin-bottom: 16px;
    text-align: center;
    display: none;
}
