:root {
  color-scheme: light dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  --page: #dfe5e9;
  --page-glow: #ffffff;
  --case: #f1f4f6;
  --case-top: #ffffff;
  --case-edge: #c5cdd3;
  --display: #bdc7a2;
  --display-ink: #172015;
  --display-frame: #4d565b;
  --key: #dce2e6;
  --number: #f8fafb;
  --key-hover: #cfd7dc;
  --key-edge: #b5bec5;
  --key-highlight: rgb(255 255 255 / 70%);
  --science: #e9edf0;
  --operator: #39718f;
  --accent: #e9942f;
  --danger: #b94f49;
  --text: #172129;
  --muted: #5d6972;
  --control: #f8fafb;
  --control-border: #bcc6cd;
  --control-text: #26343e;
  --focus: #8b570e;
  --update-border: #9a651e;
  --update-text: #704306;
  --panel: #f8fafb;
  --panel-raised: #e7ecef;
  --panel-border: #cbd3d8;
  --divider: #d5dce0;
  --backdrop: rgb(20 26 31 / 52%);
  --shadow: rgb(38 49 58 / 22%);
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #090b0d;
    --page-glow: #25303a;
    --case: #15181b;
    --case-top: #1c2024;
    --case-edge: #30363b;
    --display-frame: #0e1012;
    --key: #30363b;
    --number: #353c42;
    --key-hover: #3b4248;
    --key-edge: #181b1e;
    --key-highlight: rgb(255 255 255 / 7%);
    --science: #23282c;
    --operator: #3c596b;
    --accent: #f0a340;
    --danger: #7c3f3b;
    --text: #f4f5f6;
    --muted: #9ba4aa;
    --control: #242a2e;
    --control-border: #3c444a;
    --control-text: #d5d9dc;
    --focus: #ffd18d;
    --update-border: #876332;
    --update-text: #ffd89e;
    --panel: #171b1e;
    --panel-raised: #282e33;
    --panel-border: #485159;
    --divider: #30373c;
    --backdrop: rgb(0 0 0 / 68%);
    --shadow: rgb(0 0 0 / 48%);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding:
    max(0.75rem, env(safe-area-inset-top))
    max(0.75rem, env(safe-area-inset-right))
    max(0.75rem, env(safe-area-inset-bottom))
    max(0.75rem, env(safe-area-inset-left));
  background:
    radial-gradient(circle at 50% -10%, var(--page-glow) 0, transparent 38rem),
    var(--page);
  color: var(--text);
}

button,
textarea {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled {
  cursor: default;
}

.calculator {
  container-type: inline-size;
  width: min(100%, 34rem);
  height: min(58rem, calc(100dvh - 1.5rem));
  display: flex;
  flex-direction: column;
  padding: clamp(0.7rem, 3cqw, 1.25rem);
  overflow: hidden;
  border: 1px solid var(--case-edge);
  border-radius: 1.4rem;
  background: linear-gradient(145deg, var(--case-top), var(--case) 44%);
  box-shadow:
    0 1.5rem 4rem var(--shadow),
    inset 0 1px rgb(255 255 255 / 6%);
}

.calculator__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 2.75rem;
  padding: 0 0.2rem 0.7rem;
}

.brand,
.model {
  margin: 0;
}

.brand {
  font-size: 0.92rem;
  font-weight: 800;
  letter-spacing: 0.14em;
}

.model {
  margin-top: 0.12rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.1em;
}

.header-actions {
  display: flex;
  gap: 0.45rem;
}

.mode-button,
.settings-button,
.update-button,
.install-button {
  min-height: 2.35rem;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--control-border);
  border-radius: 999px;
  background: var(--control);
  color: var(--control-text);
  font-size: 0.84rem;
  font-weight: 800;
  letter-spacing: 0.05em;
}

.update-button,
.install-button {
  border-color: var(--update-border);
  color: var(--update-text);
}

.settings-button {
  width: 2.35rem;
  padding-inline: 0;
  font-size: 1rem;
}

.console {
  min-height: 10rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  margin-bottom: 0.8rem;
  overflow: hidden;
  border: 0.35rem solid var(--display-frame);
  border-radius: 0.85rem;
  background:
    repeating-linear-gradient(0deg, rgb(23 32 21 / 2%) 0 1px, transparent 1px 3px),
    var(--display);
  color: var(--display-ink);
  box-shadow:
    inset 0 0.16rem 0.45rem rgb(0 0 0 / 28%),
    0 1px rgb(255 255 255 / 8%);
}

.console__toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 2rem;
  padding: 0.35rem 0.55rem;
  border-bottom: 1px solid rgb(23 32 21 / 18%);
}

.console__status {
  display: flex;
  gap: 0.7rem;
  color: rgb(23 32 21 / 68%);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.71rem;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.console__toolbar button {
  padding: 0.22rem 0.38rem;
  border: 0;
  border-radius: 0.3rem;
  background: transparent;
  color: #384b32;
  font-size: 0.72rem;
  font-weight: 800;
}

.console__toolbar button:disabled {
  color: rgb(23 32 21 / 35%);
}

.console__scroll {
  min-height: 0;
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-color: rgb(23 32 21 / 40%) transparent;
}

.console__history {
  margin: 0;
  padding: 0.3rem 0.75rem 0;
  list-style: none;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.9rem;
}

.console__history li {
  padding: 0.45rem 0;
}

.console__history li:not(:last-child) {
  border-bottom: 1px solid rgb(23 32 21 / 12%);
}

.console__history span,
.console__history strong {
  display: block;
  overflow-wrap: anywhere;
}

.console__history strong {
  margin-top: 0.14rem;
  font-size: 1rem;
  text-align: right;
}

.console__entry {
  position: relative;
  min-height: 6rem;
  margin-top: 0.25rem;
  padding: 0.55rem 0.75rem 0.65rem;
  border-top: 2px solid rgb(23 32 21 / 22%);
}

.console__editor {
  position: relative;
  min-height: 3rem;
}

.console__expression,
.console__caret-layer {
  width: 100%;
  min-height: 3rem;
  padding: 0;
  overflow: auto;
  border: 0;
  background: transparent;
  color: inherit;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 1.08rem;
  font-weight: 650;
  line-height: 1.35;
  tab-size: 4;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.console__expression {
  position: relative;
  z-index: 1;
  resize: none;
  outline: 0;
  caret-color: transparent;
}

/*
 * A real textarea only paints its caret while focused. Focusing it from a
 * calculator key would also open the phone keyboard, so keypad editing uses
 * this matching, non-interactive caret instead. The same caret remains visible
 * in keyboard mode so it never shifts; the textarea underneath still provides
 * the browser's native selection and mobile cursor gestures.
 */
.console__caret-layer {
  position: absolute;
  z-index: 2;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.console__caret-layer > span:first-child {
  visibility: hidden;
}

.console__caret {
  display: inline-block;
  width: 0;
  height: 1.12em;
  border-left: 1px solid currentColor;
  vertical-align: -0.18em;
  animation: blink-caret 1s steps(1, end) infinite;
}

@keyframes blink-caret {
  50% {
    opacity: 0;
  }
}

.console__expression::placeholder {
  color: rgb(23 32 21 / 46%);
}

.console__answer {
  display: block;
  min-height: 1.65rem;
  overflow: hidden;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: clamp(1.45rem, 7vw, 2rem);
  font-weight: 750;
  line-height: 1.1;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.console--error {
  background-color: #d3b2a3;
}

.keypad {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: clamp(0.3rem, 1.5cqw, 0.58rem);
}

.key {
  min-width: 0;
  min-height: clamp(2.45rem, 6.2dvh, 3.75rem);
  padding: 0.35rem 0.15rem;
  border: 0;
  border-bottom: 0.18rem solid var(--key-edge);
  border-radius: 0.7rem;
  background: var(--key);
  color: var(--text);
  font-size: clamp(1rem, 4.1cqw, 1.35rem);
  font-weight: 720;
  line-height: 1;
  box-shadow: inset 0 1px var(--key-highlight);
  user-select: none;
}

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

.key:active {
  translate: 0 0.12rem;
  border-bottom-width: 0.06rem;
}

.key:focus-visible,
.mode-button:focus-visible,
.settings-button:focus-visible,
.update-button:focus-visible,
.install-button:focus-visible {
  outline: 0.18rem solid var(--focus);
  outline-offset: 0.12rem;
}

.key--science,
.key--utility {
  background: var(--science);
  color: var(--text);
  font-size: clamp(0.86rem, 3.5cqw, 1.12rem);
}

.key--operator {
  background: var(--operator);
  color: white;
  font-size: clamp(1.2rem, 4.5cqw, 1.6rem);
}

.key--number {
  background: var(--number);
  color: var(--text);
  font-size: clamp(1.3rem, 5.2cqw, 1.75rem);
}

.key--cursor {
  font-size: clamp(1.15rem, 4.5cqw, 1.55rem);
}

.key--active {
  background: #8b642c;
  color: #fff3df;
}

.key--danger {
  background: var(--danger);
  color: #fff1ef;
  font-size: clamp(0.82rem, 3.2cqw, 1rem);
  letter-spacing: 0.03em;
}

.key--equals {
  grid-row: span 3;
  background: var(--accent);
  color: #251708;
  font-size: clamp(1.4rem, 5cqw, 1.85rem);
}

.key--wide {
  grid-column: span 2;
}

.key__small {
  display: block;
  margin-top: 0.14rem;
  font-size: 0.62rem;
  letter-spacing: 0.06em;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.settings {
  width: min(calc(100% - 2rem), 30rem);
  max-height: min(calc(100dvh - 2rem), 42rem);
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--panel-border);
  border-radius: 1rem;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1.5rem 5rem var(--shadow);
}

.settings::backdrop {
  background: var(--backdrop);
  backdrop-filter: blur(0.2rem);
}

.settings__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--divider);
}

.settings__header h2 {
  margin: 0;
  font-size: 1.08rem;
}

.settings__close {
  width: 2.25rem;
  height: 2.25rem;
  border: 0;
  border-radius: 50%;
  background: var(--panel-raised);
  color: var(--text);
  font-size: 1.35rem;
}

.settings__body {
  padding: 0 1rem 1rem;
  overflow-y: auto;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 5rem;
  border-bottom: 1px solid var(--divider);
}

.setting-row span {
  min-width: 0;
}

.setting-row strong,
.setting-row small {
  display: block;
}

.setting-row strong {
  font-size: 0.92rem;
}

.setting-row small {
  margin-top: 0.25rem;
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.35;
}

.setting-row select {
  flex: 0 0 auto;
  min-height: 2.4rem;
  padding: 0.35rem 1.8rem 0.35rem 0.55rem;
  border: 1px solid var(--panel-border);
  border-radius: 0.5rem;
  background: var(--panel-raised);
  color: var(--text);
  font: inherit;
}

.settings__updates {
  padding-top: 1rem;
}

.settings__updates h3,
.settings__updates p {
  margin: 0;
}

.settings__updates h3 {
  font-size: 0.92rem;
}

#update-status {
  margin-top: 0.3rem;
  color: var(--text);
  font-size: 0.82rem;
}

.settings__version {
  margin-top: 0.25rem !important;
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.7rem;
}

.settings__update-actions {
  display: flex;
  gap: 0.55rem;
  margin-top: 0.8rem;
}

.settings__update-actions button {
  min-height: 2.5rem;
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--panel-border);
  border-radius: 0.55rem;
  background: var(--panel-raised);
  color: var(--text);
  font-weight: 750;
}

.settings__update-actions .update-button {
  border-color: var(--update-border);
  color: var(--update-text);
}

@media (max-height: 46rem) {
  body {
    place-items: start center;
  }

  .calculator {
    padding: 0.8rem;
  }

  .calculator__header {
    min-height: 2.25rem;
    padding-bottom: 0.45rem;
  }

  .console {
    min-height: 8rem;
    margin-bottom: 0.55rem;
  }

  .console__expression {
    min-height: 2.8rem;
  }

  .keypad {
    gap: 0.35rem;
  }

  .key {
    min-height: clamp(2.2rem, 5.7dvh, 2.65rem);
  }
}

@media (max-width: 23rem) {
  body {
    padding-inline: 0.35rem;
    padding-block: max(0.35rem, env(safe-area-inset-top)) max(0.35rem, env(safe-area-inset-bottom));
  }

  .calculator {
    height: min(56rem, calc(100dvh - 0.7rem));
    padding: 0.7rem;
    border-radius: 1rem;
  }

  .keypad {
    gap: 0.3rem;
  }

  .key {
    min-height: clamp(2.2rem, 5.8dvh, 2.65rem);
    border-radius: 0.55rem;
    font-size: 1rem;
  }

  .key--science,
  .key--utility {
    font-size: 0.8rem;
  }

  .key--danger {
    font-size: 0.78rem;
  }
}

@media (pointer: coarse) and (orientation: portrait) {
  .key {
    min-height: clamp(2.65rem, 6.2dvh, 4rem);
  }
}

/*
 * A phone held sideways has plenty of width but very little height. Put the
 * display beside the keypad so all keys remain comfortably tappable without
 * making the page excessively tall.
 */
@media (orientation: landscape) and (max-height: 35rem) and (min-width: 38rem) {
  body {
    place-items: center;
    padding: max(0.35rem, env(safe-area-inset-top)) max(0.5rem, env(safe-area-inset-right)) max(0.35rem, env(safe-area-inset-bottom)) max(0.5rem, env(safe-area-inset-left));
  }

  .calculator {
    width: min(100%, 58rem);
    display: grid;
    grid-template-columns: minmax(12rem, 0.72fr) minmax(27rem, 1.28fr);
    grid-template-rows: auto 1fr;
    gap: 0.6rem 0.85rem;
    padding: 0.7rem;
  }

  .calculator__header {
    grid-column: 1;
    grid-row: 1;
    padding: 0 0.2rem;
  }

  .console {
    grid-column: 1;
    grid-row: 2;
    min-height: 0;
    height: 100%;
    margin: 0;
  }

  .console__expression {
    min-height: 3.2rem;
  }

  .keypad {
    grid-column: 2;
    grid-row: 1 / span 2;
    gap: clamp(0.2rem, 1dvh, 0.35rem);
  }

  .key {
    min-height: clamp(2rem, 9.8dvh, 2.75rem);
  }
}

@media (prefers-reduced-motion: reduce) {
  .key:active {
    translate: none;
  }

  .console__caret {
    animation: none;
  }
}

@media (forced-colors: active) {
  .calculator,
  .console,
  .key,
  .mode-button,
  .settings-button,
  .update-button,
  .install-button,
  .settings {
    border: 1px solid ButtonText;
    box-shadow: none;
    forced-color-adjust: auto;
  }

  .console__caret {
    border-color: CanvasText;
  }
}
