:root{
    --bg-from: #16213e;
    --bg-to:   #1a1a2e;

    --orb-1: rgba(120, 60, 220, 0.55);
    --orb-2: rgba(30, 120, 220, 0.45);
    --orb-3: rgba(220, 60, 120, 0.35);

    --glass-bg: rgba(255, 255, 255, 0.06);
    --glass-border: rgba(255, 255, 255, 0.12);
    --glass-shadow: rgba(0, 0, 0, 0.45);

    --display-bg: rgba(0, 0, 0, 0.35);
    --text-primary: #f0eeff;
    --text-dim: rgba(240, 238, 255, 0.45);
    --text-accent: #c9b8ff;

    --btn-num-bg: rgba(255, 255, 255, 0.08);
    --btn-num-hover:rgba(255, 255, 255, 0.16);
    --btn-num-text: #f0eeff;

    --btn-op-bg:    rgba(160, 120, 255, 0.22);
    --btn-op-hover: rgba(160, 120, 255, 0.40);
    --btn-op-text:  #d4b8ff;
    
    --btn-fn-bg:    rgba(255, 255, 255, 0.10);
    --btn-fn-hover: rgba(255, 255, 255, 0.20);
    --btn-fn-text:  #a0c8ff;

    --btn-eq-bg: linear-gradient(135deg, #7c3aed, #4f46e5);
    --btn-eq-hover: linear-gradient(135deg, #9461ff, #6260f5);
    --btn-eq-shadow:rgba(124, 58, 237, 0.55);
    
    --history-bg:   rgba(255, 255, 255, 0.04);
    --history-item-bg: rgba(255, 255, 255, 0.06);
    --history-item-hover: rgba(255, 255, 255, 0.10);
    
    --toggle-bg:    rgba(255, 255, 255, 0.10);
    --toggle-hover: rgba(255, 255, 255, 0.18);
    --toggle-text:  #f0eeff;

    --transition-speed: 0.35s;
}

[data-theme="light"]{
    --bg-from: #e8e4f8;
    --bg-to: #d4e4f8;
    
    --glass-bg:     rgba(255, 255, 255, 0.50);
    --glass-border: rgba(255, 255, 255, 0.75);
    --glass-shadow: rgba(100, 80, 180, 0.18);
    
    --display-bg:   rgba(255, 255, 255, 0.45);
    --text-primary: #1a1030;
    --text-dim:     rgba(26, 16, 48, 0.50);
    --text-accent:  #6030c0;
    
    --btn-num-bg:   rgba(255, 255, 255, 0.65);
    --btn-num-hover:rgba(255, 255, 255, 0.90);
    --btn-num-text: #1a1030;
    
    --btn-op-bg:    rgba(140, 90, 240, 0.18);
    --btn-op-hover: rgba(140, 90, 240, 0.32);
    --btn-op-text:  #5520a0;
    
    --btn-fn-bg:    rgba(100, 160, 255, 0.20);
    --btn-fn-hover: rgba(100, 160, 255, 0.38);
    --btn-fn-text:  #1050a0;
    
    --btn-eq-bg:    linear-gradient(135deg, #7c3aed, #4f46e5);
    --btn-eq-hover: linear-gradient(135deg, #9461ff, #6260f5);
    --btn-eq-shadow:rgba(124, 58, 237, 0.40);
    
    --history-bg:   rgba(255, 255, 255, 0.35);
    --history-item-bg: rgba(255, 255, 255, 0.55);
    --history-item-hover: rgba(255, 255, 255, 0.80);
    
    --toggle-bg:    rgba(255, 255, 255, 0.55);
    --toggle-hover: rgba(255, 255, 255, 0.80);
    --toggle-text:  #1a1030;
}

*, *::before, ::after{
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html{
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body{
    font-family:'Syne', sans-serif;
    min-height: 100vh;
    background: linear-gradient(135deg, var(--bg-from), var(--bg-to));
    display: flex;
    align-items: center;
    justify-content: center;
    overflow-x: hidden;
    transition: background var(--transition-speed) ease;
    padding: 60px 16px 32px;
}

.theme-toggle{
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 100;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    background: var(--toggle-bg);
    border: 1px solid var(--glass-border);
    border-radius: 50px;
    cursor: pointer;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    color: var(--toggle-text);
    font-family: 'Syne', sans-serif;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.06rem;
    transition: background var(--transition-speed) ease, 
                color var(--transition-speed) ease,
                transform 0.15s ease,
                box-shadow 0.15 ease;
}

.theme-toggle:hover{
    background: var(--toggle-hover);
    transform: translateY(-2px);
    box-shadow: 0 8px 24px var(--glass-shadow);
}
.theme-toggle:active{
    transform: translateY(0) scale(0.96);
}
.toggle-icon{
    font-size: 1rem;
}

.app-container{
    position: relative;
    z-index: 1;
    display: flex;
    gap: 20px;
    align-items: flex-start;
    width: 100%;
    max-width: 780px;
}

.calculator-card{
    flex-shrink: 0;
    width: 340px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 28px;
    box-shadow: 0 24px 64px var(--glass-shadow),
                inset 0 1px 0 rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    padding: 24px;
    transition: background var(--transition-speed) ease,
                border-color var(--transition-speed) ease;
}

.calc-header{
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 16px;
}

.calc-title{
    font-size: 0.75rem;
    font-weight: 800;
    letter-spacing: 0.2rem;
    color: var(--text-accent);
    transition: color var(--transition-speed) ease;
}

.display{
    background: var(--display-bg);
    border-radius: 16px;
    padding: 16px 20px 14px;
    margin-bottom: 20px;
    min-height: 100px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    border: 1px solid var(--glass-border);
    transition: background var(--transition-speed) ease;
    overflow: hidden;
}

.expression{
    font-family: 'DM Mono', monospace;
    font-size: 0.85rem;
    color: var(--text-dim);
    text-align: right;
    min-height: 1.2em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    letter-spacing: 0.02em;
    transition: color var(--transition-speed) ease;
}

.result{
    font-family: 'DM Mono', monospace;
    font-size: 2.6rem;
    font-weight: 500;
    color: var(--text-primary);
    text-align: right;
    line-height: 1;
    word-break: break-all;
    transition: color var(--transition-speed) ease,
                font-size 0.15s ease;
    letter-spacing: -0.02em;
}

.result.error{
  font-size: 1.2rem;
  color: #ff6b8a;
}

.result.animate-pop{
  animation: popIn 0.18s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popIn{
    from{
        transform: scale(0.92);
        opacity: 0.5;
    }
    to{
        transform: scale(1);
        opacity: 1;
    }
}

.btn-grid{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
}

.btn{
    position: relative;
    font-family: 'Syne', sans-serif;
    font-size: 1.1rem;
    font-weight: 600;
    height: 64px;
    border: 1px solid var(--glass-border);
    border-radius: 14px;
    cursor: pointer;
    transition: background 0.18s ease,
                transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1),
                box-shadow 0.18s ease,
                color var(--transition-speed) ease,
                border-color var(--transition-speed) ease;
    outline: none;
    overflow: hidden;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    user-select: none;
}

.btn::after{
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background: rgba(255,255,255,0.15);
    opacity: 0;
    transform: scale(0.6);
    transition: opacity 0.3s ease, transform 0.3s ease;
}
.btn:active::after{
    opacity: 1;
    transform: scale(1);
    transition: none;
}

.btn-num{
  background: var(--btn-num-bg);
  color: var(--btn-num-text);
}
.btn-num:hover{
  background: var(--btn-num-hover);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.2);
}
.btn-num:active{
    transform: scale(0.93); 
}

.btn-op{
  background: var(--btn-op-bg);
  color: var(--btn-op-text);
  font-size: 1.3rem;
}
.btn-op:hover{
  background: var(--btn-op-hover);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(140, 90, 255, 0.25);
}
.btn-op:active{
    transform: scale(0.93);
}
.btn-op.active-op{
  background: var(--btn-op-hover);
  box-shadow: 0 0 0 2px var(--btn-op-text), 0 6px 20px rgba(140, 90, 255, 0.3);
}

.btn-fn{
  background: var(--btn-fn-bg);
  color: var(--btn-fn-text);
}
.btn-fn:hover{
  background: var(--btn-fn-hover);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(100, 160, 255, 0.2);
}
.btn-fn:active{
    transform: scale(0.93); 
}

.btn-eq{
  background: var(--btn-eq-bg);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 6px 24px var(--btn-eq-shadow);
  font-size: 1.4rem;
}

.btn-eq:hover{
  background: var(--btn-eq-hover);
  transform: translateY(-3px);
  box-shadow: 0 10px 32px var(--btn-eq-shadow);
}
.btn-eq:active{
    transform: scale(0.93); box-shadow: 0 4px 16px var(--btn-eq-shadow);
}

.btn-wide{
  grid-column: span 2;
  text-align: left;
  padding-left: 22px;
}

.history-panel{
  flex: 1;
  min-width: 0;
  background: var(--history-bg);
  border: 1px solid var(--glass-border);
  border-radius: 28px;
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  box-shadow: 0 24px 64px var(--glass-shadow),
              inset 0 1px 0 rgba(255,255,255,0.08);
  padding: 22px;
  display: flex;
  flex-direction: column;
  max-height: 520px;
  transition: background var(--transition-speed) ease,
              border-color var(--transition-speed) ease;
}

.history-header{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.history-title{
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.2em;
  color: var(--text-accent);
  transition: color var(--transition-speed) ease;
}

.clear-history-btn{
  font-family: 'Syne', sans-serif;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: color 0.2s ease, background 0.2s ease;
}

.clear-history-btn:hover{
  color: #ff6b8a;
  background: rgba(255, 107, 138, 0.1);
}

.history-list{
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--glass-border) transparent;
}
.history-list::-webkit-scrollbar{
    width: 4px; 
}
.history-list::-webkit-scrollbar-track{
    background: transparent;
}
.history-list::-webkit-scrollbar-thumb{
  background: var(--glass-border);
  border-radius: 4px;
}

.history-empty{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 10px;
  color: var(--text-dim);
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  padding: 32px 0;
  transition: color var(--transition-speed) ease;
}

.empty-icon{
  font-size: 2rem;
  opacity: 0.5;
}

.history-item{
  background: var(--history-item-bg);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
  transition: background 0.18s ease,
              transform 0.15s ease,
              border-color var(--transition-speed) ease;
  animation: slideIn 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.history-item:hover{
  background: var(--history-item-hover);
  transform: translateX(3px);
}

.history-item:active{
    transform: scale(0.98);
}
 
@keyframes slideIn{
  from{
    transform: translateX(20px);
    opacity: 0;
}
  to{
    transform: translateX(0);
    opacity: 1;
}
}

.history-expr{
  font-family: 'DM Mono', monospace;
  font-size: 0.75rem;
  color: var(--text-dim);
  letter-spacing: 0.04em;
  margin-bottom: 4px;
  transition: color var(--transition-speed) ease;
}

.history-val{
  font-family: 'DM Mono', monospace;
  font-size: 1.25rem;
  font-weight: 500;
  color: var(--text-primary);
  transition: color var(--transition-speed) ease;
}
.history-val span{
  color: var(--text-accent);
  margin-right: 4px;
}

@media(max-width: 700px){
  body{
    padding: 70px 12px 24px; 
}
 
  .app-container{
    flex-direction: column;
    align-items: center;
  }
 
  .calculator-card{
    width: 100%;
    max-width: 360px;
  }
 
  .history-panel{
    width: 100%;
    max-width: 360px;
    max-height: 280px;
  }
 
  .result{
    font-size: 2.2rem;
  }
  .btn{
    height: 58px;
    font-size: 1rem;
  }
}
 
@media(max-width: 380px){
  .btn-grid{
    gap: 8px;
  }
  .btn{
    height: 52px;
    border-radius: 12px;
  }
  .result{
    font-size: 1.9rem;
  }
  .calculator-card{
    padding: 18px;
  }
}