:root {
  color-scheme: dark;
  --bg: #000000;
  --text: #f8f8f8;
  --muted: rgba(255, 255, 255, 0.56);
  --number: #f6f6f6;
  --number-active: #ffffff;
  --function: #1c1c1e;
  --function-active: #303034;
  --function-text: #fafafa;
  --operator: #ff9f0a;
  --operator-rgb: 255, 159, 10;
  --operator-text: #ffffff;
  --operator-selected-text: var(--operator);
  --thousand-color: #34c759;
  --million-color: #34c759;
  --billion-color: #ff9f0a;
  --screen-panel: #1c1c1e;
  --screen-divider: rgba(255, 255, 255, 0.08);
  --utility-icon: rgba(255, 255, 255, 0.55);
  --expression-font-size: clamp(2.2rem, calc(10 * var(--app-unit)), 5rem);
  /* 1% bề rộng khung máy tính. Khung rộng tối đa 430px nên chữ không phình theo màn hình lớn. */
  --app-unit: min(1vw, 4.3px);
  --expression-font-scale: 1;
  --expression-max-lines: 4;
  --expression-line-height: 1.03;
  --keypad-gap: 10px;
  --keypad-top-padding: 14px;
  --key-min-height: 74px;
  --key-equals-height: 158px;
  --key-font-base: 1.7rem;
  --key-number-font-size: clamp(1.8rem, calc(6 * var(--app-unit)), 2.8rem);
  --key-operator-font-size: clamp(2rem, calc(6.8 * var(--app-unit)), 3rem);
  --key-triple-zero-font-size: clamp(1.15rem, calc(4 * var(--app-unit)), 1.8rem);
  --key-arrow-font-size: 1.95rem;
  --key-icon-size: 28px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: manipulation;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

.app-shell {
  width: 100%;
}

.calculator {
  width: min(100%, 430px);
  min-height: 100vh;
  min-height: 100dvh;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding:
    max(18px, env(safe-area-inset-top))
    14px
    max(14px, env(safe-area-inset-bottom))
    14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: #000000;
}

.display-zone {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 10px;
  overflow: hidden;
  padding-bottom: 12px;
  min-height: 0;
}

.history-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 8px;
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  text-align: right;
  scrollbar-width: none;
  touch-action: pan-y;
  transition: opacity 160ms ease, max-height 160ms ease;
}

/* Đẩy lịch sử xuống đáy khi còn chỗ; khi tràn, margin bằng 0 nên vẫn cuộn được tới mục cũ nhất. */
.history-list > :first-child {
  margin-top: auto;
}

.history-list::-webkit-scrollbar {
  display: none;
}

.display-zone.is-history-hidden .history-list {
  flex: 0 0 auto;
  min-height: 0;
  max-height: 0;
  overflow: hidden;
  opacity: 0;
}

.display-zone.is-history-hidden {
  gap: 0;
}

.active-display {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 6px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  touch-action: pan-y;
  min-height: calc(
    (var(--expression-font-size) * var(--expression-font-scale) * var(--expression-line-height)) +
    1.2rem
  );
  max-height: min(
    40vh,
    calc(
      (var(--expression-font-size) * var(--expression-font-scale) * var(--expression-line-height) * var(--expression-max-lines)) +
      3rem
    )
  );
  padding-top: 2px;
}

.display-meta,
.display-meta[hidden] ~ .expression-display,
.display-meta[hidden] ~ .expression-editor {
  margin-top: auto;
}

.display-meta {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

/* Lắc vùng số khi bấm = mà phép tính chưa hoàn chỉnh. Chỉ lắc sang trái vì mép phải sát khung. */
@keyframes invalid-input-shake {
  25% {
    transform: translateX(-8px);
  }

  50% {
    transform: translateX(-2px);
  }

  75% {
    transform: translateX(-6px);
  }
}

@keyframes invalid-input-flash {
  50% {
    opacity: 0.35;
  }
}

.active-display.is-invalid {
  animation: invalid-input-shake 280ms ease;
}

@media (prefers-reduced-motion: reduce) {
  .active-display.is-invalid {
    animation-name: invalid-input-flash;
  }
}

.edit-indicator {
  flex: none;
  padding: 3px 8px;
  border: 1px solid var(--operator);
  border-radius: 999px;
  color: var(--text);
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1;
}

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

.display-zone.is-history-hidden .active-display {
  flex: 1 1 auto;
  max-height: none;
}

.history-item {
  position: relative;
  display: block;
  font-size: clamp(0.84rem, calc(2.7 * var(--app-unit)), 1.12rem);
  line-height: 1.12;
  color: rgba(255, 255, 255, 0.52);
  white-space: normal;
  overflow-wrap: anywhere;
  padding: 2px 0 2px 18px;
  border-radius: 10px;
  transition: color 120ms ease, background-color 120ms ease, transform 120ms ease;
}

.history-item strong {
  color: var(--text);
  font-weight: 500;
}

.history-empty-hint {
  display: block;
  margin-left: auto;
  max-width: min(100%, 22ch);
  font-size: clamp(0.84rem, calc(2.8 * var(--app-unit)), 1.02rem);
  line-height: 1.3;
  color: rgba(255, 255, 255, 0.42);
  text-align: right;
}

.history-list[data-recall-enabled="true"] .history-item {
  cursor: pointer;
}

.history-list[data-recall-enabled="true"] .history-item::before {
  content: "↺";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.78em;
  color: rgba(255, 255, 255, 0.26);
}

.history-list[data-recall-enabled="true"] .history-item:hover,
.history-list[data-recall-enabled="true"] .history-item:focus-visible,
.history-list[data-recall-enabled="true"] .history-item:active {
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.78);
}

.number-scale-thousand {
  color: var(--thousand-color);
}

.number-scale-million {
  color: var(--million-color);
}

.number-scale-billion {
  color: var(--billion-color);
}

.number-block-dim {
  opacity: 0.46;
}

.preview-display,
.expression-display {
  display: block;
  width: 100%;
  text-align: right;
  line-height: var(--expression-line-height);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow-x: hidden;
  overflow-y: hidden;
  scrollbar-width: none;
  scrollbar-color: transparent transparent;
}

.display-meta > .preview-display {
  flex: 1 1 0;
  min-width: 0;
}

/* overflow: visible vì line-height nhỏ hơn chiều cao chữ, nếu cắt sẽ mất đuôi dấu phẩy. */
.preview-display {
  min-height: 0;
  overflow: visible;
  font-size: clamp(1.2rem, calc(5 * var(--app-unit)), 2.7rem);
  font-weight: 520;
  line-height: 1.02;
  color: rgba(255, 255, 255, 0.92);
}

.preview-display:empty {
  min-height: 0;
}

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

/* Phép tính dài hơn vùng hiển thị phải cuộn: làm mờ mép trên để báo còn chữ số phía trên. */
.active-display[data-expression-overflow="true"] .expression-display,
.active-display[data-expression-overflow="true"] .expression-editor {
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 1.1em);
  mask-image: linear-gradient(to bottom, transparent, #000 1.1em);
}

.expression-display {
  font-size: calc(var(--expression-font-size) * var(--expression-font-scale));
  font-weight: 300;
  letter-spacing: -0.055em;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  max-height: calc(
    var(--expression-font-size) * var(--expression-font-scale) * var(--expression-line-height) * var(--expression-max-lines)
  );
}

.expression-editor {
  display: block;
  width: 100%;
  min-height: calc(var(--expression-font-size) * var(--expression-font-scale) * var(--expression-line-height));
  margin: 0;
  padding: 0;
  border: 0;
  resize: none;
  appearance: none;
  -webkit-appearance: none;
  border-radius: 0;
  box-shadow: none;
  /* Gạch chân màu chủ đề như ô nhập liệu, báo đang ở chế độ sửa. Vẽ bằng nền để không đổi kích thước ô. */
  background: linear-gradient(var(--operator), var(--operator)) left bottom / 100% 2px no-repeat;
  color: var(--text);
  font-family: inherit;
  font-size: calc(var(--expression-font-size) * var(--expression-font-scale));
  font-weight: 300;
  letter-spacing: -0.055em;
  text-align: right;
  line-height: var(--expression-line-height);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow-x: hidden;
  overflow-y: auto;
  caret-color: rgba(255, 255, 255, 0.95);
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
  touch-action: pan-y;
  scrollbar-width: none;
  scrollbar-color: transparent transparent;
  max-height: calc(
    var(--expression-font-size) * var(--expression-font-scale) * var(--expression-line-height) * var(--expression-max-lines)
  );
}

.expression-editor:focus {
  outline: none;
}

.expression-editor::placeholder {
  color: var(--text);
  opacity: 1;
}

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

.display-meta[hidden],
.edit-indicator[hidden],
.preview-display[hidden],
.expression-display[hidden],
.expression-editor[hidden] {
  display: none !important;
}

.expression-display.is-editing {
  cursor: text;
  outline: none;
  caret-color: rgba(255, 255, 255, 0.95);
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: var(--keypad-gap);
  position: sticky;
  bottom: 0;
  z-index: 2;
  padding-top: var(--keypad-top-padding);
  background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.74) 16%, rgba(0, 0, 0, 0.95) 34%, #000000 54%);
}

.key {
  border: 0;
  border-radius: 999px;
  min-height: min(var(--key-min-height), 11.5vh);
  min-height: min(var(--key-min-height), 11.5dvh);
  padding: 0;
  font: inherit;
  font-size: var(--key-font-base);
  line-height: 1;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  transition: transform 120ms ease, background-color 120ms ease, color 120ms ease, box-shadow 120ms ease;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  /* Không để trình duyệt coi cú chạm hơi trượt là thao tác cuộn rồi hủy phím đang bấm.
     Vẫn hủy khi ngón tay trượt hẳn ra khỏi phím (xem isPointerStillOnButton). */
  touch-action: none;
}

.key:not([data-action="decimal"]) {
  display: flex;
  align-items: center;
  justify-content: center;
}

.key:active {
  transform: scale(0.965);
}

.key:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.35);
  outline-offset: 2px;
}

.key-number {
  background: var(--number);
  color: #000000;
  font-size: var(--key-number-font-size);
}

.key-number:focus-visible {
  background: var(--number-active);
}

.key-function,
.key-utility {
  background: var(--function);
  color: var(--function-text);
}

.key-function:focus-visible,
.key-utility:focus-visible {
  background: var(--function-active);
}

.key-operator {
  background: var(--operator);
  color: var(--operator-text);
  font-size: var(--key-operator-font-size);
}

.key-operator:focus-visible {
  filter: brightness(1.12);
}

.key-operator.is-active {
  background: #ffffff;
  color: var(--operator-selected-text);
}

.key-utility {
  color: var(--utility-icon);
}

.key-equals {
  grid-row: span 2;
  min-height: min(var(--key-equals-height), calc(23vh + var(--keypad-gap)));
  min-height: min(var(--key-equals-height), calc(23dvh + var(--keypad-gap)));
}

.key-triple-zero {
  font-size: var(--key-triple-zero-font-size);
  letter-spacing: 0.04em;
}

@media (hover: hover) {
  .key-number:hover {
    background: var(--number-active);
  }

  .key-function:hover,
  .key-utility:hover {
    background: var(--function-active);
  }

  .key-operator:hover {
    filter: brightness(1.12);
  }

  button.settings-row:hover,
  .settings-step:hover {
    background: rgba(255, 255, 255, 0.04);
  }
}

/* Phản hồi khi đang nhấn, kể cả trên màn hình cảm ứng (ở đó không có :hover). */
.key-number:active {
  background: #d9d9d9;
}

.key-function:active,
.key-utility:active {
  background: var(--function-active);
}

.key-operator:active {
  filter: brightness(1.12);
}

.key-text-arrow {
  font-size: var(--key-arrow-font-size);
  line-height: 1;
}

.icon-svg {
  width: var(--key-icon-size);
  height: var(--key-icon-size);
  display: block;
  margin: 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.settings-screen {
  position: fixed;
  inset: 0;
  min-height: 100vh;
  min-height: 100dvh;
  z-index: 10;
  background: #000000;
  color: var(--text);
  overflow-y: auto;
}

.settings-screen:focus {
  outline: none;
}

.settings-screen-header {
  min-height: 86px;
  background: #262626;
}

.settings-screen-spacer {
  min-width: 40px;
}

.settings-screen-title {
  display: flex;
  align-items: center;
  min-height: 44px;
  margin: 0;
  text-align: center;
  font-size: 1rem;
  font-weight: 600;
}

.settings-done {
  justify-self: end;
  min-width: 44px;
  min-height: 44px;
  padding: 0 2px;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.settings-screen-body {
  max-width: 460px;
  min-height: calc(100vh - 86px);
  min-height: calc(100dvh - 86px);
  margin: 0 auto;
  padding: 10px 0 calc(18px + env(safe-area-inset-bottom));
  background: #000000;
}

.settings-group {
  margin: 0 0 22px;
  background: var(--screen-panel);
}

.settings-row {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--screen-divider);
  padding: 0 16px;
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  text-align: left;
  touch-action: manipulation;
}

.settings-group .settings-row:last-child {
  border-bottom: 0;
}

.settings-row:focus-visible,
.settings-done:focus-visible,
.settings-step:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.72);
  outline-offset: -2px;
}

.settings-label {
  min-width: 0;
  line-height: 1.2;
}

.settings-detail-value {
  color: rgba(255, 255, 255, 0.7);
}

.settings-row-stepper {
  padding-right: 0;
  cursor: default;
}

.settings-stepper {
  flex: none;
  align-self: stretch;
  display: flex;
  align-items: center;
}

/* Rộng ổn định để mũi tên hai bên thẳng hàng giữa các dòng, chỉ giá trị dài mới giãn thêm. */
.settings-stepper > .settings-detail-value,
.settings-color-value {
  min-width: 5.2em;
  text-align: center;
}

.settings-color-value {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.settings-step {
  flex: none;
  align-self: stretch;
  width: 44px;
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.72);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.settings-step:active {
  background: rgba(255, 255, 255, 0.08);
}

/* Mũi tên vẽ bằng viền, cùng màu chữ. */
.settings-step::before {
  content: "";
  width: 9px;
  height: 9px;
  border-left: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: translateX(2px) rotate(45deg);
}

.settings-step[data-value="1"]::before {
  transform: translateX(-2px) rotate(-135deg);
}

.settings-color-dot {
  flex: none;
  width: 16px;
  height: 16px;
  border-radius: 999px;
  background: rgb(var(--operator-rgb));
}

.ios-switch {
  width: 51px;
  height: 31px;
  border-radius: 999px;
  background: #3a3a3c;
  position: relative;
  transition: background-color 160ms ease;
}

.ios-switch span {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 27px;
  height: 27px;
  border-radius: 999px;
  background: #ffffff;
  transition: transform 160ms ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.28);
}

.ios-switch.is-on {
  background: #30d158;
}

.ios-switch.is-on span {
  transform: translateX(20px);
}

.settings-footer {
  margin: 84px 0 0;
  text-align: center;
  font-size: 0.74rem;
  color: rgba(255, 255, 255, 0.55);
}

.history-list[data-font-size="sm"] .history-item {
  font-size: clamp(0.82rem, calc(2.7 * var(--app-unit)), 1.08rem);
}

.history-list[data-font-size="sm"] .history-item-recent {
  font-size: clamp(0.9rem, calc(3 * var(--app-unit)), 1.25rem);
}

.history-list[data-font-size="md"] .history-item {
  font-size: clamp(0.9rem, calc(3 * var(--app-unit)), 1.25rem);
}

.history-list[data-font-size="md"] .history-item-recent {
  font-size: clamp(1rem, calc(3.3 * var(--app-unit)), 1.42rem);
}

.history-list[data-font-size="lg"] .history-item {
  font-size: clamp(1rem, calc(3.3 * var(--app-unit)), 1.42rem);
}

.history-list[data-font-size="lg"] .history-item-recent {
  font-size: clamp(1.1rem, calc(3.6 * var(--app-unit)), 1.58rem);
}

@media (max-width: 430px) {
  .calculator {
    width: 100%;
    padding-left: 10px;
    padding-right: 10px;
  }
}

html,
body {
  touch-action: auto;
}

/* Giữ nguyên cả số trên một dòng khi có thể: phép tính xuống dòng ở dấu toán tử trước.
   Số dài hơn cả dòng mới bị ngắt, tại các dấu tách hàng nghìn. */
.number-token {
  display: inline-block;
  max-width: 100%;
}

.number-token-chunk {
  display: inline;
  white-space: nowrap;
}

.history-item .number-token-chunk {
  display: inline-block;
}

.history-list[data-recall-enabled="true"] .history-item::before {
  content: "↺";
}

.settings-screen-header-inner {
  max-width: 460px;
  min-height: 86px;
  margin: 0 auto;
  padding:
    max(18px, env(safe-area-inset-top))
    14px
    2px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  gap: 8px;
}

/* Điện thoại xoay ngang không đủ cao cho 5 hàng phím cộng vùng hiển thị: xếp hai cột.
   Các biến cỡ phím được khai báo lại trên .keypad để thắng giá trị JS đặt ở :root. */
@media (orientation: landscape) and (max-height: 540px) {
  .calculator {
    width: 100%;
    max-width: 960px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr);
    grid-template-rows: minmax(0, 1fr);
    gap: 0 16px;
    padding:
      max(8px, env(safe-area-inset-top))
      max(14px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(14px, env(safe-area-inset-left));
  }

  .display-zone {
    grid-area: 1 / 1;
    padding-bottom: 0;
    --expression-font-size: clamp(1.6rem, 11vh, 3rem);
  }

  .active-display {
    max-height: 60vh;
  }

  .keypad {
    grid-area: 1 / 2;
    grid-auto-rows: minmax(0, 1fr);
    position: static;
    padding-top: 0;
    background: none;
    --keypad-gap: 6px;
    --key-font-base: clamp(1rem, 6vh, 1.4rem);
    --key-number-font-size: clamp(1.1rem, 7vh, 1.7rem);
    --key-operator-font-size: clamp(1.2rem, 8vh, 1.9rem);
    --key-triple-zero-font-size: clamp(0.85rem, 5vh, 1.2rem);
    --key-arrow-font-size: clamp(1.1rem, 7vh, 1.5rem);
    --key-icon-size: clamp(18px, 6vh, 24px);
  }

  .key,
  .key-equals {
    min-height: 0;
  }
}

/* ---- Tương thích với index.html bản v1.16 ----
   Máy chủ không gửi Cache-Control nên trình duyệt có thể dùng lại HTML cũ thêm khoảng 18 ngày
   sau lần deploy kế tiếp, trong khi HTML đó luôn tải CSS và JS mới nhất. Sau thời gian đó có
   thể xóa khối này cùng tests/fixtures/index-v1.16.html. */
.version-badge {
  display: none;
}

.active-display > .preview-display,
.preview-display[hidden] ~ .expression-display,
.preview-display[hidden] ~ .expression-editor {
  margin-top: auto;
}

.icon-grid {
  width: var(--key-icon-size);
  height: var(--key-icon-size);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin: 0 auto;
}

.icon-grid span {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 999px;
  background: currentColor;
}

.settings-color-trailing .settings-color-dot {
  display: block;
}
