/* Chillculator — full-screen phone calculator with swappable photo themes. */

:root {
  /* overwritten per theme by app.js */
  --op:      #ff8a3d;
  --op-2:    #ff5f6d;
  --seg:     #7dfcd0;
  --panel:   16, 20, 28;
  --dim:     0.42;

  --key-face:   rgba(255, 255, 255, 0.20);
  --key-face-2: rgba(255, 255, 255, 0.08);
  --key-edge:   rgba(255, 255, 255, 0.42);
  /* the base under the frosted gloss — retuned per theme by applyTheme, because
     the dark base that rescues photo themes turns to mud on a light flat colour */
  --key-base:   rgba(16, 20, 30, .38);
  --key-text:   #fff;
  --key-shadow: 0 2px 8px rgba(0, 0, 0, .45);
  --gap: clamp(7px, 2.1vw, 13px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #0d1016;
  color: #fff;
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI Variable Display",
               system-ui, -apple-system, "Helvetica Neue", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: 100%;
}

/* ---------- background ---------- */
.bg, .bg-scrim {
  position: fixed;
  inset: 0;
  z-index: 0;
}
.bg {
  background-size: cover;
  background-position: center;
  transition: opacity .28s ease;
}
.bg-scrim {
  background:
    linear-gradient(180deg,
      rgba(0, 0, 0, .45) 0%,
      rgba(0, 0, 0, .12) 26%,
      rgba(0, 0, 0, .35) 62%,
      rgba(0, 0, 0, .62) 100%);
  opacity: var(--dim);
  transition: opacity .2s ease;
}

/* ---------- layout ---------- */
.app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  /* keep clear of the notch and the phone's gesture bar */
  padding:
    calc(env(safe-area-inset-top) + 8px)
    calc(env(safe-area-inset-right) + 12px)
    calc(env(safe-area-inset-bottom) + 10px)
    calc(env(safe-area-inset-left) + 12px);
  gap: var(--gap);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 11px;
  flex: 0 0 auto;
  height: 54px;
}
.theme-label { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.theme-name {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: .04em;
  text-transform: uppercase;
  text-shadow: 0 1px 6px rgba(0, 0, 0, .7);
}
.theme-hint {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .03em;
  opacity: .7;
  text-shadow: 0 1px 5px rgba(0, 0, 0, .8);
}

/* background picker: a live preview of the current theme, not a gear */
.theme-btn {
  position: relative;
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  flex: 0 0 52px;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, .9);
  border-radius: 16px;
  overflow: hidden;
  cursor: pointer;
  background: #222;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .45), 0 0 0 3px color-mix(in srgb, var(--op) 55%, transparent);
  transition: transform .12s ease;
}
.theme-btn:active { transform: scale(.92); }
.theme-btn-thumb {
  position: absolute;
  inset: 0;
  background: center/cover no-repeat;
}
.theme-btn svg {
  position: relative;
  width: 24px;
  height: 24px;
  fill: #fff;
  /* dark disc behind the glyph so it reads over any thumbnail */
  filter: drop-shadow(0 0 3px rgba(0, 0, 0, .95)) drop-shadow(0 1px 2px rgba(0, 0, 0, .9));
}
.theme-btn::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 50%, rgba(0, 0, 0, .55) 38%, rgba(0, 0, 0, .12) 70%);
}
.theme-btn svg { z-index: 1; }

/* ---------- display ---------- */
.screen {
  flex: 0 1 auto;
  min-height: 118px;
  border-radius: 22px;
  padding: 10px 16px 12px;
  display: flex;
  align-items: flex-end;
  background:
    linear-gradient(180deg, rgba(0, 0, 0, .30), rgba(var(--panel), .70));
  border: 1px solid rgba(255, 255, 255, .16);
  box-shadow:
    inset 0 2px 14px rgba(0, 0, 0, .55),
    0 8px 22px rgba(0, 0, 0, .28);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  overflow: hidden;
}
.screen-inner { width: 100%; }

.expr {
  height: 22px;
  /* flex-end rather than text-align:right so a long equation is clipped at the
     START — the number you are currently typing must stay visible */
  display: flex;
  justify-content: flex-end;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: .06em;
  color: rgba(255, 255, 255, .78);
  white-space: nowrap;
  overflow: hidden;
}

.lcd {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  gap: .10em;
  height: clamp(52px, 11vh, 84px);
  transform-origin: 100% 100%;
  transform: skewX(-5deg);
}
.lcd svg {
  height: 100%;
  width: auto;
  display: block;
  overflow: visible;
}
/* unlit segments stay faintly visible, like a real LCD */
.lcd .seg-off { fill: #fff; opacity: .055; }
.lcd .seg-on  {
  fill: var(--seg);
  filter: drop-shadow(0 0 6px color-mix(in srgb, var(--seg) 65%, transparent));
}
.lcd .dp { width: .34em; }

/* ---------- keypad ---------- */
.pad {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(58px, 1fr);
  gap: var(--gap);
  min-height: 0;
}

.key {
  position: relative;
  border: 1px solid var(--key-edge);
  border-radius: 20px;
  /* the dark base under the gloss is what keeps white labels legible
     on the pale themes (mountains, beach) — don't drop it */
  background:
    linear-gradient(180deg, var(--key-face), var(--key-face-2)),
    var(--key-base);
  backdrop-filter: blur(16px) saturate(150%);
  -webkit-backdrop-filter: blur(16px) saturate(150%);
  color: var(--key-text);
  font-family: inherit;
  font-size: clamp(26px, 7.6vw, 40px);
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  overflow: hidden;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .5),
    inset 0 -3px 8px rgba(0, 0, 0, .22),
    0 6px 16px rgba(0, 0, 0, .30);
  text-shadow: var(--key-shadow);
  transition: transform .09s ease, filter .09s ease, box-shadow .09s ease;
}
.key svg { width: 34px; height: 34px; fill: currentColor; }

.key:active, .key.is-down {
  transform: translateY(3px) scale(.965);
  filter: brightness(1.22);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .35),
    inset 0 -1px 4px rgba(0, 0, 0, .3),
    0 2px 6px rgba(0, 0, 0, .3);
}

/* Afterglow. :active ends the moment you lift a finger, which reads as a
   flicker; .lit is added in JS and fades out over ~320ms instead.
   `transition: none` here is the important part — otherwise the glow FADES IN
   over the same 320ms and gets removed just as it reaches full brightness. */
.key.lit {
  transition: none;
  border-color: #fff;
  filter: brightness(1.12);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .75),
    inset 0 0 26px color-mix(in srgb, var(--op) 70%, transparent),
    0 0 0 2px rgba(255, 255, 255, .85),
    0 0 34px 10px color-mix(in srgb, var(--op) 85%, transparent),
    0 6px 16px rgba(0, 0, 0, .3);
}
.key {
  transition: transform .09s ease, filter .32s ease,
              box-shadow .32s ease, border-color .32s ease;
}

.key.fn {
  font-size: clamp(21px, 6vw, 30px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .55), rgba(255, 255, 255, .28)),
    rgba(255, 255, 255, .55);
  color: #10131a;
  text-shadow: none;
}
.key.fn svg { width: 30px; height: 30px; }

.key.op, .key.eq {
  background: linear-gradient(165deg, var(--op), var(--op-2));
  /* operators stay white on every theme — they sit on their own colour, so they
     must not follow --key-text when a light flat background flips it dark */
  color: #fff;
  text-shadow: 0 2px 6px rgba(0, 0, 0, .45);
  border-color: rgba(255, 255, 255, .55);
  font-size: clamp(30px, 8.6vw, 46px);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .55),
    inset 0 -4px 10px rgba(0, 0, 0, .25),
    0 6px 18px color-mix(in srgb, var(--op) 45%, transparent);
}
/* the pressed operator stays lit so you can see what's pending */
.key.op.armed {
  filter: saturate(1.25);
  box-shadow:
    inset 0 0 0 3px rgba(255, 255, 255, .95),
    inset 0 -40px 60px rgba(0, 0, 0, .22),
    0 6px 22px color-mix(in srgb, var(--op) 65%, transparent);
}
.key.eq { font-size: clamp(34px, 9.4vw, 50px); }

/* ripple on tap */
.ripple {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, .5);
  transform: translate(-50%, -50%) scale(0);
  animation: ripple .45s ease-out forwards;
  pointer-events: none;
}
@keyframes ripple {
  to { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

/* ---------- setup sheet ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 8;
  background: rgba(0, 0, 0, .5);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade .2s ease;
}
@keyframes fade { from { opacity: 0 } }

.sheet {
  position: fixed;
  z-index: 9;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 82dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 18px calc(env(safe-area-inset-bottom) + 20px);
  border-radius: 26px 26px 0 0;
  background: rgba(18, 21, 30, .93);
  backdrop-filter: blur(22px);
  -webkit-backdrop-filter: blur(22px);
  border-top: 1px solid rgba(255, 255, 255, .18);
  box-shadow: 0 -14px 44px rgba(0, 0, 0, .55);
  animation: slideUp .26s cubic-bezier(.2, .9, .3, 1);
}
@keyframes slideUp { from { transform: translateY(100%) } }
.sheet[hidden], .sheet-backdrop[hidden] { display: none; }

.sheet-grip {
  width: 42px;
  height: 5px;
  border-radius: 3px;
  background: rgba(255, 255, 255, .3);
  margin: 4px auto 10px;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sheet-head h2 { margin: 0 0 10px; font-size: 22px; letter-spacing: -.01em; }
.close-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, .25);
  background: rgba(255, 255, 255, .1);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 12px;
  padding: 4px 0 14px;
}
.theme-card {
  position: relative;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 16px;
  overflow: hidden;
  aspect-ratio: 9 / 14;
  background: #222 center/cover no-repeat;
  cursor: pointer;
  color: #fff;
  font: inherit;
  transition: transform .12s ease;
}
.theme-card:active { transform: scale(.95); }
.theme-card span {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 16px 6px 6px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  background: linear-gradient(180deg, transparent, rgba(0, 0, 0, .8));
  text-shadow: 0 1px 3px #000;
}
.theme-card.active { border-color: #fff; box-shadow: 0 0 0 3px var(--op); }
.theme-card.active::after {
  content: "✓";
  position: absolute;
  top: 5px;
  right: 5px;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--op);
  font-size: 13px;
  font-weight: 800;
}

.sheet-sub {
  margin: 6px 2px 8px;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: .03em;
  text-transform: uppercase;
  opacity: .72;
}
.sub-hint {
  margin-left: 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  opacity: .65;
}

/* plain colours: same card shape as the photos, shorter */
.color-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 10px;
  padding: 0 0 14px;
}
.color-card { aspect-ratio: 1 / 1; }
.color-card span { padding: 14px 4px 5px; font-size: 11px; }

/* sound picker — icon tiles you can hammer on */
.sound-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 12px 8px;
  padding: 0 0 16px;
}
.sound-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  padding: 8px 2px 9px;
  border: 2px solid transparent;
  border-radius: 18px;
  background: none;
  color: #fff;
  font: inherit;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}
.sound-ic {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  font-size: 26px;
  line-height: 1;
  background: linear-gradient(150deg, var(--c1), var(--c2));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .45),
              inset 0 -3px 8px rgba(0, 0, 0, .22),
              0 4px 12px rgba(0, 0, 0, .38);
}
.sound-nm {
  font-size: 11.5px;
  font-weight: 700;
  text-align: center;
  line-height: 1.15;
  opacity: .9;
}
.sound-tile.active {
  background: rgba(255, 255, 255, .10);
  border-color: rgba(255, 255, 255, .55);
}
.sound-tile.active .sound-ic {
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .5),
              0 0 0 3px rgba(255, 255, 255, .9),
              0 5px 18px color-mix(in srgb, var(--c1) 65%, transparent);
}
.sound-tile.is-random .sound-ic {
  background: conic-gradient(from 0deg, #ff2bd6, #ffb200, #00e5ff, #7bf1a8, #ff2bd6);
}
/* squish + settle, restarted on every tap */
.sound-ic.boing { animation: boing .34s cubic-bezier(.3, 1.4, .5, 1); }
@keyframes boing {
  0%   { transform: scale(.72); }
  45%  { transform: scale(1.14); }
  70%  { transform: scale(.95); }
  100% { transform: scale(1); }
}

.sheet-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 2px;
  border-top: 1px solid rgba(255, 255, 255, .1);
  font-size: 15px;
  font-weight: 600;
}
.sheet-row input[type="range"] { flex: 1 1 auto; max-width: 60%; accent-color: var(--op); }
.switch { width: 22px; height: 22px; accent-color: var(--op); }
.sheet-foot {
  margin: 14px 0 0;
  text-align: center;
  font-size: 12px;
  opacity: .45;
}

/* short phones / landscape: give the keypad the room */
@media (max-height: 620px) {
  .screen { min-height: 96px; }
  .lcd { height: 46px; }
  .expr { height: 18px; font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
