:root {
  --bg: #000;
  --btn-fn: #a5a5a5;
  --btn-num: #333333;
  --btn-op: #ff9f0a;
  --text: #fff;
  --text-dim: rgba(235, 235, 245, 0.55);
  --shadow: rgba(255, 255, 255, 0.15);

  /* Slightly tighter outer spacing, bigger controls */
  --gap: clamp(8px, 2.2vw, 12px);
  --pad: clamp(10px, 3.4vw, 20px);
}

* {
  box-sizing: border-box;
}

html,
body {
  /* Use JS-provided app height to avoid keyboard open/close layout jumps on mobile */
  height: var(--app-h, 100dvh);
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "SF Pro Display", "SF Pro Text",
    "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  /* App-like: prevent scroll chaining / overscroll bounce and touch panning */
  overscroll-behavior: none;
  touch-action: none;
  overflow-x: hidden;
}

body {
  min-height: var(--app-h, 100dvh);
  display: grid;
  place-items: center;
  overflow: hidden;
  padding-top: max(8px, env(safe-area-inset-top));
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  /* Keep the app body visually centered even when left/right safe areas differ */
  padding-left: max(8px, env(safe-area-inset-left), env(safe-area-inset-right));
  padding-right: max(8px, env(safe-area-inset-left), env(safe-area-inset-right));
}

.phone {
  /* Use 100% so body padding/safe-area is respected (avoids horizontal clipping on mobile) */
  width: min(420px, 100%);
  /* Use 100% so body padding/safe-area is respected (avoids bottom clipping on mobile) */
  height: min(700px, 100%);
  display: grid;
  /* Fixed top icons row, then the display stretches, keypad sticks to bottom */
  grid-template-rows: var(--topbar-h, 64px) 1fr auto;
  gap: var(--gap);
  padding: var(--pad);
}

.topbar {
  height: var(--topbar-h, 30px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: max(2px, env(safe-area-inset-top));
}

.topbtn {
  width: clamp(44px, 11.8vw, 56px);
  aspect-ratio: 1 / 1;
  border-radius: 9999px;
  border: 0;
  background: rgba(58, 58, 60, 0.75);
  color: rgba(255, 255, 255, 0.92);
  display: grid;
  place-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 90ms ease, filter 90ms ease;
}

.topbtn:active {
  transform: scale(0.98);
  filter: brightness(1.15);
}

.topbtn svg {
  width: 66%;
  height: 66%;
}

.topbtn svg * {
  stroke: currentColor;
}

/* Visual hint for magic mode: swap clock hands */
.clock-normal {
  display: inline;
}
.clock-magic {
  display: none;
}
.phone.magic .clock-normal {
  display: none;
}
.phone.magic .clock-magic {
  display: inline;
}

.display {
  min-height: 140px;
  text-align: right;
  padding: 0;
  display: grid;
  align-content: end;
  gap: 6px;
  user-select: none;
}

.expr {
  min-height: 1.2em;
  font-size: clamp(16px, 4.3vw, 22px);
  letter-spacing: 0.2px;
  color: var(--text-dim);
  opacity: 0.9;
  font-weight: 400;
}

.result {
  font-variant-numeric: tabular-nums;
  font-size: clamp(40px, 13vmin, 86px);
  line-height: 1.05;
  letter-spacing: -0.02em;
  font-weight: 300;
  display: flex;
  justify-content: flex-end;
  padding-right: 0.04em;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: clip;
  /* Keep the container untransformed; scale the inner text only. */
}

.resultText {
  display: inline-block;
  transform: scale(var(--result-scale, 1));
  transform-origin: right center;
}

.keys {
  /*
    Size buttons from width, but cap by viewport height so on short screens
    the keypad can shrink and still fit without any scrolling/cropping.
  */
  --btn: min(88px, calc((100% - (3 * var(--gap))) / 4), 14.2svh);
  display: grid;
  grid-template-columns: repeat(4, var(--btn));
  grid-auto-rows: var(--btn);
  gap: var(--gap);
  width: 100%;
  justify-content: center;
  align-self: end;
}

button.key {
  appearance: none;
  border: 0;
  aspect-ratio: 1 / 1;
  border-radius: 9999px;
  padding: 0;
  margin: 0;
  font-size: clamp(24px, 7.1vmin, 42px);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  font-weight: 400;
  color: var(--text);
  background: var(--btn-num);
  display: grid;
  place-items: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  /* Allow taps while body disables touch panning */
  touch-action: manipulation;
  transition: transform 90ms ease, filter 90ms ease, background-color 90ms ease, color 90ms ease;
}

.topbtn {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Don't allow long-press selection inside the calculator surface */
.phone,
.keys,
.expr,
.result {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* But keep text selectable/editable inside the settings widget */
.widgetCard,
.widgetCard * {
  -webkit-touch-callout: default;
}
.widgetInput {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

button.key:active {
  transform: scale(0.98);
  filter: brightness(1.15);
}

button.key.fn {
  background: var(--btn-fn);
  color: #fff;
}

button.key.icon {
  padding: 0;
}

button.key.icon svg {
  width: 52%;
  height: 52%;
}

button.key.icon svg * {
  stroke: currentColor;
}

button.key.op {
  background: var(--btn-op);
  color: #fff;
}

button.key.op.selected {
  background: #fff;
  color: var(--btn-op);
  box-shadow: 0 0 0 1px var(--shadow) inset;
}

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

@media (max-height: 640px) {
  .phone {
    height: 100%;
    padding-top: 8px;
  }
  .result {
    font-size: clamp(40px, 11.5vmin, 72px);
  }
}

/* Small settings widget (opened by long-press on the time icon) */
.widgetBackdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 10;
}

.widgetBackdrop[hidden] {
  display: none !important;
}

.widgetCard {
  width: min(360px, calc(100vw - 36px));
  border-radius: 16px;
  background: rgba(28, 28, 30, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.55);
  padding: 14px 14px 12px;
  color: var(--text);
}

.widgetTitle {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.2px;
  margin-bottom: 10px;
}

.widgetRow {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.widgetLabel {
  color: var(--text-dim);
  font-size: 14px;
}

.widgetInput {
  width: 120px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(58, 58, 60, 0.75);
  color: var(--text);
  padding: 8px 10px;
  font-size: 15px;
  outline: none;
}

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

.widgetActions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.widgetBtn {
  border: 0;
  border-radius: 9999px;
  padding: 10px 14px;
  background: rgba(58, 58, 60, 0.85);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}

.widgetBtn.primary {
  background: var(--btn-op);
}

.widgetHint {
  margin-top: 10px;
  font-size: 12px;
  color: rgba(235, 235, 245, 0.45);
}

