/* --- 1. GLOBAL & FULL-SCREEN LAYOUT --- */
html, body {
    height: 100%; margin: 0; overflow: hidden;
  }
  body {
      background-color: #000; font-family: 'Poppins', 'Roboto', sans-serif;
      display: flex; justify-content: center;
  }
  
  .calculator {
      width: 100%; max-width: 450px; background-color: #22252D;
      display: flex; flex-direction: column; height: 100%;
      --key-size: 80px;
      --key-font: 28px;
      /* Keep bottom spacing minimal; only respect safe area + a tiny cushion */
      --footer-pad-bottom: calc(env(safe-area-inset-bottom) + 8px);
      transition: --key-size 0.35s ease, --key-font 0.35s ease, padding-bottom 0.35s ease;
  }
  .calculator.details-visible {
      --key-size: 64px;
      --key-font: 24px;
      --footer-pad-bottom: calc(env(safe-area-inset-bottom) + 4px);
  }
  
  /* --- 2. HEADER & FOOTER (Static Parts) --- */
  .calculator-header, .calculator-footer {
      flex-shrink: 0; /* Prevent these from shrinking */
      background-color: #22252D;
      position: relative;
      z-index: 2;
  }
  .calculator-header { padding-top: env(safe-area-inset-top); }
  .calculator-header { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
  
  /* Unified Display Block */
  .calculator-display {
      padding: 12px 20px 4px 20px;
      display: flex; 
      /* align-items: flex-end; */
      flex: 1 1 auto; min-height: 0;
  }
  .display-expression {
      background: none; border: none; outline: none; box-shadow: none;
      width: 100%; padding: 0; font-family: inherit;
      font-size: 52px; font-weight: 500; color: #FFFFFF;
      caret-color: #1991ff; text-align: right;
      resize: none; overflow-y: auto; overflow-x: hidden; line-height: 1.2;
      white-space: normal; word-break: keep-all; height: auto; max-height: 100%;
      word-spacing: 0.02em; /* minimal extra spacing around spaced operators */
  }
  
  .final-balance-container {
      padding: 0 25px 2px 25px;
      text-align: right;
  }
  #final-balance {
      font-size: 32px; font-weight: 500;
      color: #999; /* Single, consistent color for the result */
      min-height: 40px;
  }
  
  /* --- 3. BREAKDOWN MODAL SHEET --- */
  .breakdown-modal {
      position: fixed; inset: 0; background: rgba(0,0,0,0.5);
      display: flex; align-items: flex-end; justify-content: center;
      z-index: 20; transition: opacity 0.25s ease; opacity: 1;
  }
  .breakdown-modal.is-hidden { opacity: 0; pointer-events: none; }
  .breakdown-sheet {
      width: 100%; max-width: 520px; background: #1c1f26;
      border-top-left-radius: 16px; border-top-right-radius: 16px;
      max-height: 85vh; display: flex; flex-direction: column;
      transform: translateY(0); transition: transform 0.25s ease;
      margin-bottom: calc(env(safe-area-inset-bottom) + 8px);
  }
  .breakdown-modal.is-hidden .breakdown-sheet { transform: translateY(16px); }
  .breakdown-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid #333; }
.breakdown-heading { margin: 0; font-size: 16px; color: #cbd5e1; }
.breakdown-actions { display: flex; align-items: center; gap: 8px; }
.breakdown-save { background: #1991ff; border: none; color: white; border-radius: 8px; padding: 6px 12px; font-size: 14px; cursor: pointer; transition: background-color 0.2s; }
.breakdown-save:hover { background: #147acc; }
.breakdown-save:active { transform: scale(0.98); }
.breakdown-close { background: none; border: 1px solid #3a3f48; color: #cbd5e1; border-radius: 8px; width: 32px; height: 28px; }

  /* History modal shares styles with breakdown */
  .history-modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: flex-end; justify-content: center; z-index: 20; transition: opacity 0.25s ease; opacity: 1; }
  .history-modal.is-hidden { opacity: 0; pointer-events: none; }
  .history-sheet { width: 100%; max-width: 520px; background: #1c1f26; border-top-left-radius: 16px; border-top-right-radius: 16px; max-height: 85vh; display: flex; flex-direction: column; transform: translateY(0); transition: transform 0.25s ease; margin-bottom: calc(env(safe-area-inset-bottom) + 8px); }
  .history-modal.is-hidden .history-sheet { transform: translateY(16px); }
  .history-content { padding: 12px 16px; color: #e5e7eb; flex: 1 1 auto; min-height: 0; overflow-y: auto; }
  .history-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; }
  .history-item { background: #252830; padding: 12px 12px; border-radius: 12px; display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto; grid-template-areas: "expr meta" "tags tags"; gap: 6px; }
  .history-item .expr { grid-area: expr; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .history-item .meta { grid-area: meta; color: #9ca3af; font-size: 12px; text-align: right; }
  .history-item .tags { grid-area: tags; display: flex; gap: 6px; flex-wrap: wrap; }
  .chip { padding: 4px 8px; border-radius: 999px; font-size: 12px; line-height: 1; border: 1px solid transparent; }
  .chip.sub { background: rgba(25,145,255,0.1); border-color: #1991ff; color: #cfe7ff; }
  .chip.pay { background: rgba(165,245,195,0.1); border-color: #a5f5c3; color: #e3fbea; }
  .chip.bal { background: rgba(255,255,255,0.06); border-color: #666; color: #e5e7eb; }
  
  .breakdown-content {
      padding: 0 20px;
      height: 100%;
      overflow-y: auto;
      color: #fff;
  }
  
  /* Custom Scrollbar */
  .breakdown-content::-webkit-scrollbar { width: 6px; }
  .breakdown-content::-webkit-scrollbar-track { background: transparent; }
  .breakdown-content::-webkit-scrollbar-thumb { background-color: #4E5460; border-radius: 6px; }
  
  /* Polished list styles */
  .breakdown-section { margin: 20px 0; }
  .breakdown-title {
      font-size: 18px; font-weight: 600; margin: 0 0 15px;
      padding-bottom: 8px; border-bottom: 2px solid;
  }
  .costs-title { border-color: #ff8a8a; color: #ff8a8a; }
  .payments-title { border-color: #a5f5c3; color: #a5f5c3; }
  .breakdown-list {
      list-style: none; padding: 0; margin: 0; display: flex;
      flex-direction: column; gap: 10px;
  }
  #costs-list li, #payments-list li {
      background-color: #252830; padding: 12px 15px; border-radius: 12px;
  }
  #costs-list li {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.cost-item-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    grid-template-areas: "price stepper total";
    gap: 12px;
    align-items: center;
}
  #payments-list li { display: flex; justify-content: space-between; align-items: center; font-size: 20px; font-weight: 500; }
  .item-price { grid-area: price; font-size: 24px; font-weight: 600; }
  .quantity-stepper { grid-area: stepper; display: flex; align-items: center; gap: 12px; justify-self: end; }
  .btn-qty { background-color: #4E5460; border: none; color: white; width: 32px; height: 32px; border-radius: 50%; font-size: 20px; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: background-color 0.2s; }
  .btn-qty:hover { background-color: #636b7a; }
  .item-quantity { font-size: 18px; min-width: 24px; text-align: center; }
  .item-total { grid-area: total; font-size: 16px; color: #a0a0a0; justify-self: end; }
  .breakdown-total { text-align: right; font-weight: 600; font-size: 20px; margin-top: 15px; border-top: 1px solid #555; padding-top: 10px; }
  .breakdown-list:empty::after { content: "No items"; display: block; text-align: center; padding: 20px; color: #777; font-style: italic; background-color: rgba(0,0,0,0.1); border-radius: 8px; }
  
  /* --- 4. TOGGLE BAR & KEYS LAYOUT --- */
  .toggle-bar { display: none; }
  .fn-toggle { padding: 0 8px; }
  .toggle-handle { display: block; width: 30px; height: 3px; background: #666; border-radius: 999px; }

  /* Inline function key row - very compact */
  .function-keys {
      display: flex; flex-direction: row; align-items: center; justify-content: space-between;
      gap: 4px; padding: 4px 8px;
  }
  .fn-key {
      background: #2a2f38; color: #cbd5e1; border-radius: 8px; height: 28px; font-size: 14px;
      display: flex; align-items: center; justify-content: center; border: 1px solid #3a3f48;
      padding: 0 8px; flex: 1 1 0; min-width: 0;
  }
  .fn-key:active { transform: scale(0.98); }
  
  .calculator-keys {
      padding: 4px 12px; padding-bottom: var(--footer-pad-bottom);
      display: grid; grid-template-columns: repeat(4, 1fr);
      gap: 6px;
  }
  button {
      border: none; background-color: #31353e; color: #ffffff;
      font-size: var(--key-font); font-weight: 500; border-radius: 50%;
      width: var(--key-size); height: var(--key-size);
      display: flex; justify-content: center; align-items: center;
      cursor: pointer; transition: all 0.15s ease-in-out;
      user-select: none; -webkit-user-select: none; -ms-user-select: none;
  }
  button:active { transform: scale(0.92); }
  .key-function { background-color: #4E5460; color: #a5f5c3; }
  .key-icon { padding: 0; }
  .key-icon img { width: 36px; height: 36px; pointer-events: none; user-select: none; }
  .key-operator { background-color: #005DB2; font-size: 32px; }
  .key-equals { background-color: #1991ff; }
  button[data-key="backspace"] { font-size: 30px; }

/* --- SELECT2 DROPDOWN STYLES --- */
.select2-container--default .select2-selection--single {
    background-color: #2a2f38;
    border: 1px solid #4E5460;
    border-radius: 8px;
    height: 36px;
    color: #e5e7eb;
}

.select2-container--default .select2-selection--single .select2-selection__rendered {
    color: #e5e7eb;
    line-height: 34px;
    padding-left: 12px;
    font-size: 14px;
}

.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 34px;
    right: 8px;
}

.select2-container--default .select2-selection--single .select2-selection__arrow b {
    border-color: #e5e7eb transparent transparent transparent;
}

.select2-container--default.select2-container--open .select2-selection--single .select2-selection__arrow b {
    border-color: transparent transparent #e5e7eb transparent;
}

.select2-dropdown {
    background-color: #2a2f38;
    border: 1px solid #4E5460;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.select2-container--default .select2-results__option {
    color: #e5e7eb;
    padding: 8px 12px;
    font-size: 14px;
}

.select2-container--default .select2-results__option--highlighted[aria-selected] {
    background-color: #1991ff;
    color: #ffffff;
}

.select2-container--default .select2-results__option[aria-selected=true] {
    background-color: #4E5460;
    color: #e5e7eb;
}

.select2-search--dropdown .select2-search__field {
    background-color: #1c1f26;
    border: 1px solid #4E5460;
    color: #e5e7eb;
    border-radius: 4px;
    padding: 6px 8px;
}

.select2-search--dropdown .select2-search__field::placeholder {
    color: #9ca3af;
}

.select2-container--default .select2-selection--single .select2-selection__placeholder {
    color: #9ca3af;
}

/* Product variant dropdown specific styles */
.product-variant-select {
    width: 100%;
    margin-top: 8px;
}

.variant-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.variant-name {
    font-weight: 500;
}

.variant-details {
    font-size: 12px;
    color: #9ca3af;
    margin-top: 2px;
}

.variant-price {
    color: #1991ff;
    font-weight: 600;
}