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

body {
    font-family: 'Arial', 'Helvetica', sans-serif;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 10px;
    overflow: hidden;
}

.calculator-container {
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
}

.calculator-body {
    background: linear-gradient(145deg, #1a1a1a, #0d0d0d);
    border-radius: 15px 15px 20px 20px;
    padding: 18px 12px 20px 12px;
    box-shadow: 
        0 25px 70px rgba(0, 0, 0, 0.8),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
    position: relative;
    border: 1px solid #000;
}

.calculator-body::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: 
        repeating-linear-gradient(
            0deg,
            rgba(255, 255, 255, 0.01) 0px,
            rgba(255, 255, 255, 0.01) 1px,
            transparent 1px,
            transparent 2px
        );
    border-radius: 15px 15px 20px 20px;
    pointer-events: none;
    z-index: 0;
}

.calculator-body > * {
    position: relative;
    z-index: 1;
}

/* Branding */
.branding {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding: 0 5px;
}

.brand-left {
    font-size: 16px;
    font-weight: bold;
    color: #888;
    letter-spacing: 1.5px;
}

.model-id {
    font-size: 9px;
    color: #d42a4a;
    letter-spacing: 1px;
    font-weight: 600;
}

.brand-right {
    font-size: 12px;
    color: #888;
    font-weight: 600;
}

/* Display */
.display-container {
    background: linear-gradient(180deg, #7a9b7a, #6b8b6b);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 12px;
    box-shadow: 
        inset 0 3px 10px rgba(0, 0, 0, 0.4),
        0 1px 0 rgba(255, 255, 255, 0.05);
}

.display-screen {
    background: #8ca88c;
    border-radius: 6px;
    padding: 8px 10px;
    aspect-ratio: 2.2 / 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.display-indicators {
    display: flex;
    gap: 6px;
    margin-bottom: 4px;
    font-size: 9px;
    font-weight: bold;
    color: #1a2e1a;
}

.display-indicators span {
    padding: 1px 3px;
    background: rgba(0, 0, 0, 0.08);
    border-radius: 2px;
}

.display-indicators .hidden {
    opacity: 0;
}

.display-output {
    color: #0d1d0d;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
}

.display-expression {
    font-size: 18px;
    min-height: 22px;
    margin-bottom: 4px;
    opacity: 0.9;
    text-align: left;
    white-space: normal;
    overflow-x: auto;
    overflow-y: auto;
    text-overflow: clip;
    scrollbar-width: none;
    line-height: 1.1;
}

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

.display-expression.has-cursor::after {
    content: '';
    display: inline-block;
    width: 2px;
    height: 0.95em;
    margin-left: 2px;
    background: #132313;
    vertical-align: -0.12em;
    animation: lcd-cursor-blink 1s steps(1, end) infinite;
}

@keyframes lcd-cursor-blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

.display-result {
    font-size: 30px;
    font-weight: bold;
    min-height: 108px;
    text-align: right;
    white-space: normal;
    overflow: auto;
    text-overflow: clip;
    line-height: 1.08;
}

/* Control Buttons Row (4 circular buttons) */
.control-buttons-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 10px;
    padding-top: 6px;
    justify-items: center;
}

/* Circular Control Buttons */
.key-control-round {
    background: linear-gradient(145deg, #3a3a3a, #2a2a2a);
    border: none;
    border-radius: 50%;
    width: 52px;
    height: 52px;
    cursor: pointer;
    position: relative;
    transition: all 0.08s;
    box-shadow: 
        0 3px 6px rgba(0, 0, 0, 0.5),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    z-index: 10;
    margin-top: 12px;
}

.key-control-round:active {
    transform: translateY(2px);
    box-shadow: 
        0 1px 3px rgba(0, 0, 0, 0.5),
        inset 0 2px 5px rgba(0, 0, 0, 0.4);
}

.key-control-round .key-label-main {
    color: #bbb;
    font-size: 9px;
    font-weight: 700;
    text-align: center;
    line-height: 1.1;
}

.key-control-round .key-label-main.small {
    font-size: 10px;
}

.key-control-round .key-label-above {
    position: absolute;
    bottom: 100%;
    font-size: 10px;
    font-weight: 900;
    line-height: 1;
    margin-bottom: 10px;
    white-space: nowrap;
}

.key-control-round .key-label-above.blue {
    color: #4499ff;
    left: 50%;
    transform: translateX(-50%);
}

.key-control-round .key-label-above.orange {
    color: #ff8800;
    left: 50%;
    transform: translateX(-50%);
}

.key-control-round .key-label-above.red {
    color: #ff4455;
    left: 50%;
    transform: translateX(-50%);
}

/* D-Pad Grid: 2 buttons | D-Pad | 2 buttons */
.dpad-grid{
  display: grid;
  grid-template-columns: 1fr 1fr 1fr; /* left / center / right */
  gap: 10px;
  margin-bottom: 12px;
}

.dpad-left-col,
.dpad-right-col{
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 10px;
}

.dpad-center-col{
  display: flex;
  justify-content: center;
  align-items: center;
}

/* ✅ NEW D-PAD (single visual + invisible hit zones) */
.dpad{
  position: relative;
  width: 118px;
  height: 70px;
  margin: 0 auto;
  filter: drop-shadow(0 6px 8px rgba(0,0,0,.52));
}

.dpad-visual{
  position:absolute;
  inset:0;
  z-index:1;
  border-radius: 999px;
  background:
    radial-gradient(90px 44px at 50% 10%, rgba(255,255,255,.14), rgba(255,255,255,0) 56%),
    linear-gradient(180deg, #585d64 0%, #40454d 58%, #30353c 100%);
  border: 1px solid rgba(255,255,255,.14);
  box-shadow:
    inset 0 1px 3px rgba(255,255,255,.12),
    inset 0 -6px 8px rgba(0,0,0,.34),
    0 3px 6px rgba(0,0,0,.34);
}

.dpad-visual::before{
  content:"";
  position:absolute;
  left: 7px;
  right: 7px;
  top: 6px;
  bottom: 6px;
  border-radius: 999px;
  background:
    conic-gradient(
      from -45deg,
      #c9ced3 0 16%,
      #2a2f35 16% 25%,
      #c9ced3 25% 41%,
      #2a2f35 41% 50%,
      #c9ced3 50% 66%,
      #2a2f35 66% 75%,
      #c9ced3 75% 91%,
      #2a2f35 91% 100%
    );
  box-shadow:
    inset 0 1px 3px rgba(255,255,255,.18),
    inset 0 -3px 5px rgba(0,0,0,.44);
  pointer-events:none;
}

.dpad-visual::after{
  content:"";
  position:absolute;
  left:50%;
  top:50%;
  transform: translate(-50%,-50%);
  width: 30px;
  height: 30px;
  border-radius: 8px;
  transform: translate(-50%,-50%) rotate(45deg);
  background:
    linear-gradient(180deg, #3e434b, #2b3037);
  border: 1px solid rgba(255,255,255,.14);
  box-shadow:
    0 2px 4px rgba(0,0,0,.45),
    inset 0 1px 2px rgba(255,255,255,.12),
    inset 0 -2px 4px rgba(0,0,0,.48);
  pointer-events:none;
}

.dpad-hit{
  position:absolute;
  z-index:2;
  border:0;
  background: transparent;
  cursor:pointer;
  -webkit-tap-highlight-color: transparent;
}

.dpad-hit.up{ left:31px; right:31px; top:0; height:22px; }
.dpad-hit.down{ left:31px; right:31px; bottom:0; height:22px; }
.dpad-hit.left{ top:13px; bottom:13px; left:0; width:35px; }
.dpad-hit.right{ top:13px; bottom:13px; right:0; width:35px; }
.dpad-hit.ok{
  width: 36px;
  height: 36px;
  left:50%;
  top:50%;
  transform: translate(-50%,-50%);
  border-radius: 999px;
}

.dpad-hit:active{ background: rgba(255,255,255,.08); border-radius: 14px; }
.dpad-hit.ok:active{ background: rgba(255,255,255,.12); }

/* Remove old conflicting styles */
.control-row,
.control-dpad-row,
.dpad-container,
.control-function-grid,
.grid-row-1,
.grid-row-2,
.grid-row-full,
.dpad-wrapper,
.dpad-spacer {
    display: none;
}

/* Function Rows */
.function-rows {
    margin-bottom: 6px;
    padding-top: 2px;
}

.key-row {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 4px;
    margin-bottom: 3px;
}

.key-row:last-child {
    grid-template-columns: repeat(3, 1fr);
    margin-bottom: 0;
}

/* Number Pad */
.numpad-container {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding-top: 2px;
}

.numpad-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
}

/* Key Styles */
.key {
    background: linear-gradient(145deg, #d8d8d8, #c0c0c0);
    border: none;
    border-radius: 4px;
    padding: 6px 3px;
    min-height: 40px;
    cursor: pointer;
    position: relative;
    transition: all 0.08s;
    box-shadow: 
        0 2px 4px rgba(0, 0, 0, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.4);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    z-index: 10;
    margin-top: 11px;
}

.key:active {
    transform: translateY(2px);
    box-shadow: 
        0 1px 2px rgba(0, 0, 0, 0.4),
        inset 0 2px 4px rgba(0, 0, 0, 0.25);
}

/* Key Labels - Above and Outside the Button */
.key-label-above {
    position: absolute;
    bottom: 100%;
    font-size: 8.5px;
    font-weight: 700;
    line-height: 1;
    text-transform: uppercase;
    margin-bottom: 1px;
    white-space: nowrap;
}

.key-label-above.white { color: #fff; }
.key-label-above.orange { color: #ff8800; left: 0; }
.key-label-above.red { color: #ff4455; right: 0; }
.key-label-above.blue {
    color: #4499ff;
    left: 50%;
    transform: translateX(-50%);
    bottom: calc(100% + 7px);
}

.key-label-above.orange ~ .key-label-above.blue,
.key-label-above.blue:not(:only-of-type) {
    bottom: calc(100% + 7px);
}

.key-label-main {
    font-size: 13px;
    font-weight: 600;
    color: #1a1a1a;
    text-align: center;
    line-height: 1.1;
}

.key-label-main.small {
    font-size: 9px;
}

/* Key Type Variations */
.key-control {
    background: linear-gradient(145deg, #3a3a3a, #2a2a2a);
    color: #bbb;
    box-shadow: 
        0 2px 4px rgba(0, 0, 0, 0.5),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
    margin-top: 11px;
}

.key-control .key-label-main {
    color: #bbb;
    font-size: 10px;
    font-weight: 700;
}

.key-function {
    background: linear-gradient(145deg, #3a3a3a, #2a2a2a);
    box-shadow: 
        0 2px 4px rgba(0, 0, 0, 0.5),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
    margin-top: 14px;
}

.key-function .key-label-main {
    color: #ddd;
    font-size: 11px;
}

.key-number {
    background: linear-gradient(145deg, #f0f0f0, #d8d8d8);
    font-weight: bold;
    box-shadow: 
        0 2px 4px rgba(0, 0, 0, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.5);
    margin-top: 10px;
}

.key-number .key-label-main {
    font-size: 16px;
    font-weight: 700;
}

.key-operator {
    background: linear-gradient(145deg, #e8e8e8, #d0d0d0);
    margin-top: 6px;
}

.key-operator .key-label-main {
    font-size: 18px;
    font-weight: 700;
}

.key-action,
.key-equals {
    background: linear-gradient(145deg, #4477dd, #3366cc);
    box-shadow: 
        0 2px 4px rgba(0, 0, 0, 0.5),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
    margin-top: 10px;
}

.key-action .key-label-main {
    color: #fff;
    font-weight: 700;
    font-size: 13px;
}

.key-equals .key-label-main {
    color: #fff;
    font-size: 20px;
    font-weight: 700;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    pointer-events: none;
}

.modal.active {
    display: flex;
    pointer-events: auto;
}

.modal-content {
    background: linear-gradient(145deg, #2a2a2a, #1a1a1a);
    border-radius: 10px;
    width: 90%;
    max-width: 340px;
    padding: 20px;
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.8);
    pointer-events: auto;
    border: 1px solid #333;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 18px;
    color: #ddd;
}

.modal-header h2 {
    font-size: 15px;
    letter-spacing: 1px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: #bbb;
    font-size: 26px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
    width: 28px;
    height: 28px;
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.mode-btn {
    background: linear-gradient(145deg, #3a3a3a, #2a2a2a);
    border: 1px solid #444;
    border-radius: 6px;
    padding: 14px;
    cursor: pointer;
    transition: all 0.2s;
    display: grid;
    grid-template-areas:
        "num name"
        "num desc";
    grid-template-columns: 38px 1fr;
    gap: 4px 10px;
    align-items: center;
    text-align: left;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
}

.mode-btn:hover {
    background: linear-gradient(145deg, #444, #333);
    border-color: #555;
}

.mode-btn:active {
    transform: scale(0.98);
}

.mode-number {
    grid-area: num;
    font-size: 22px;
    font-weight: bold;
    color: #4477dd;
    justify-self: center;
}

.mode-name {
    grid-area: name;
    font-size: 15px;
    font-weight: 700;
    color: #ddd;
}

.mode-desc {
    grid-area: desc;
    font-size: 10px;
    color: #999;
}

/* Mobile: Full width */
@media (max-width: 768px) {
    body {
        padding: 0;
        align-items: flex-start;
    }
    
    .calculator-container {
        max-width: 100%;
        width: 100%;
        height: 100dvh;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .calculator-body {
        max-width: 420px;
        width: 100%;
        margin: 0 auto;
    }
}

/* Responsive adjustments */
@media (max-width: 480px) {
    body {
        padding: 0;
        background: #1a1a2e;
    }
    
    .calculator-container {
        height: 100dvh;
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
    
    .calculator-body {
        width: 100%;
        max-width: 100%;
        border-radius: 0;
        min-height: 0;
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        padding: 15px 10px;
    }
    
    .branding {
        margin-bottom: 8px;
    }
    
    .display-container {
        margin-bottom: 10px;
    }

    .display-screen {
        aspect-ratio: 2.15 / 1;
        padding: 7px 9px;
    }

    .display-expression {
        font-size: 16px;
        min-height: 18px;
    }

    .display-result {
        font-size: 22px;
        min-height: 84px;
        line-height: 1.08;
    }
    
    .control-buttons-row {
        margin-bottom: 8px;
    }
    
    .dpad-grid {
        margin-bottom: 6px;
    }
    
    .function-rows {
        margin-bottom: 4px;
    }
    
    .key {
        min-height: 38px;
    }
    
    .key-function {
        min-height: 38px;
    }
    
    .key-number {
        min-height: 42px;
    }
}

@media (max-width: 380px) {
    .calculator-body {
        padding: 15px 10px 18px 10px;
    }
    
    .control-buttons-row {
        gap: 6px;
    }
    
    .key {
        min-height: 36px;
        padding: 5px 2px;
        margin-top: 10px;
    }
    
    .key-function {
        margin-top: 12px;
    }
    
    .key-label-main {
        font-size: 11px;
    }
    
    .key-function .key-label-main {
        font-size: 10px;
    }
    
    .key-label-above {
        font-size: 6px;
    }
    
    .display-result {
        font-size: 18px;
        min-height: 72px;
    }

    .display-expression {
        font-size: 14px;
        min-height: 16px;
    }
    
    .key-control-round {
        width: 46px;
        height: 46px;
        margin-top: 10px;
    }
    
    .key-control-round .key-label-main {
        font-size: 8px;
    }

    /* scale dpad slightly */
    .dpad{ width: 118px; height: 70px; }
    .dpad-hit.ok{ width: 36px; height: 36px; }
}

@media (max-width: 340px) {
    .key {
        min-height: 34px;
        margin-top: 9px;
    }
    
    .key-function {
        margin-top: 11px;
    }
    
    .key-label-main {
        font-size: 10px;
    }
    
    .key-number .key-label-main {
        font-size: 14px;
    }
    
    .key-label-above {
        font-size: 5px;
    }
    
    .key-control-round {
        width: 42px;
        height: 42px;
        margin-top: 9px;
    }
    
    .key-control-round .key-label-main {
        font-size: 7px;
    }

    /* scale dpad more */
    .dpad{ width: 110px; height: 64px; }
    .dpad-hit.ok{ width: 36px; height: 36px; }
}

/* Landscape mobile */
@media (max-width: 768px) and (orientation: landscape) {
    .calculator-body {
        min-height: auto;
        padding: 10px 8px;
    }
    
    .display-container {
        margin-bottom: 8px;
    }
    
    .key {
        min-height: 32px;
        margin-top: 8px;
    }
    
    .key-function {
        margin-top: 10px;
    }
    
    .control-buttons-row {
        margin-bottom: 6px;
    }
    
    .dpad-grid {
        margin-bottom: 4px;
    }
    
    .function-rows {
        margin-bottom: 3px;
    }
    
    .numpad-container {
        gap: 2px;
    }
    
    .key-row {
        margin-bottom: 2px;
    }
}

/* Ensure modals don't interfere when hidden */
.modal:not(.active) {
    pointer-events: none;
    display: none;
}
