/* Sticker Book Calculator — all styles */

:root {
  --bg: #F5EFE0;
  --paper: #FFFEF9;
  --ink: #2B2118;
  --ruled: #D9E6F2;
  --num: #FBD56B;
  --num-dk: #E8A930;
  --fn:  #7BC9E8;
  --fn-dk: #2E8BB6;
  --op:  #F58A72;
  --op-dk: #C24A3A;
  --ctrl: #C5D7A8;
  --ctrl-dk: #7A9A4F;
  --eq: #D45B8A;
  --eq-dk: #9B2E5C;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
html, body {
  height: 100%; width: 100%;
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-font-smoothing: antialiased;
}

/* Respect iOS safe areas */
#app {
  position: fixed; inset: 0;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
  display: flex; flex-direction: column;
}

/* Paper shell with ruled lines */
.shell {
  flex: 1; margin: 10px;
  background:
    repeating-linear-gradient(to bottom, transparent 0, transparent 31px, var(--ruled) 31px, var(--ruled) 32px),
    var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 10px;
  box-shadow: 4px 4px 0 rgba(43, 33, 24, 0.25);
  display: flex; flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ── Display ─────────────────────────────────────── */
.display {
  flex: 0 0 auto;
  padding: 28px 20px 18px;
  display: flex; flex-direction: column;
  justify-content: flex-end;
  min-height: 34vh;   /* roomier input area in portrait */
}

.brand {
  display: inline-block; align-self: flex-start;
  padding: 3px 11px;
  background: var(--num); color: var(--ink);
  border: 2px solid var(--ink); border-radius: 999px;
  font-weight: 900; font-size: 11px; letter-spacing: 0.6px;
  transform: rotate(-3deg); margin-bottom: 8px;
  box-shadow: 2px 2px 0 rgba(43, 33, 24, 0.4);
}

.expression {
  font-size: 15px; font-weight: 700;
  color: rgba(43, 33, 24, 0.55);
  text-align: right;
  font-variant-numeric: tabular-nums;
  min-height: 20px;
  word-break: break-all;
  line-height: 1.2;
}

.result {
  font-size: 84px; font-weight: 900;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  letter-spacing: -1.5px;
  text-align: right;
  white-space: nowrap;   /* never wrap mid-number; fitResult() shrinks to fit */
  overflow: hidden;
  padding-bottom: 0.4em; /* room so the group underlines aren't clipped by overflow */
}

/* ── Result-line comma groups + pastel rainbow underlines ───────── */
.result .grp {
  border-bottom: 0.13em solid transparent;  /* em-scaled: shrinks with font */
  padding-bottom: 0.06em;
  border-radius: 2px;
}
.result .comma, .result .frac { border-bottom: none; }
.result .grp-0 { border-color: #F6B0AA; } /* red    · units      */
.result .grp-1 { border-color: #FBD2A6; } /* orange · thousands  */
.result .grp-2 { border-color: #F7E6A0; } /* yellow · millions   */
.result .grp-3 { border-color: #C6E4B5; } /* green  · billions   */
.result .grp-4 { border-color: #AAD6EC; } /* blue   · trillions  */
.result .grp-5 { border-color: #B7BEE8; } /* indigo · reserve    */
.result .grp-6 { border-color: #D8C2EC; } /* violet · reserve    */

/* ── Number-property icon row (Direction C) ─────────────────────── */
.icons {
  display: flex; flex-wrap: nowrap;
  justify-content: space-between; align-items: flex-end;
  gap: 2px; margin-top: 18px;
}
.ic {
  appearance: none; -webkit-appearance: none;
  background: none; border: none; cursor: pointer; font-family: inherit;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  font-weight: 800; font-size: 12px; letter-spacing: 0.3px;
  color: #C7C0B4;                 /* very light grey when inactive */
  padding: 6px 4px; border-radius: 12px; flex: 0 1 auto; min-width: 0;
  transition: color 120ms ease, background 120ms ease;
  -webkit-tap-highlight-color: transparent;
}
.ic .g { font-size: 28px; line-height: 1; }
.ic .g svg { display: block; }
.ic.on { color: var(--op-dk); }   /* coral when the property matches */
.ic:hover { background: rgba(43, 33, 24, 0.05); }
.ic:active { background: rgba(43, 33, 24, 0.10); }
.ic:focus-visible { outline: 3px solid var(--fn-dk); outline-offset: 2px; }

/* Tap-to-explain tooltip (anchored above the tapped icon) */
.ic-tip {
  position: absolute; z-index: 60;
  max-width: min(280px, 78vw);
  background: var(--paper); color: var(--ink);
  border: 2.5px solid var(--ink); border-radius: 14px;
  box-shadow: 3px 3px 0 var(--ink);
  padding: 10px 13px;
  font-size: 14px; font-weight: 700; line-height: 1.35;
  text-align: left;
  opacity: 0; transform: translateY(4px);
  pointer-events: none;
  transition: opacity 120ms ease, transform 120ms ease;
}
.ic-tip.show { opacity: 1; transform: translateY(0); }

.peek {
  margin-top: 14px;
  align-self: flex-end;
  border: 2.5px solid var(--ink);
  cursor: pointer;
  background: var(--eq); color: white;
  font-family: inherit; font-weight: 900; font-size: 13px;
  padding: 9px 15px; border-radius: 14px;
  box-shadow: 3px 3px 0 var(--ink);
  letter-spacing: 0.3px;
  transform: rotate(1deg);
  transition: transform 80ms, box-shadow 80ms;
}
.peek:active {
  transform: rotate(1deg) translate(2px, 2px);
  box-shadow: 1px 1px 0 var(--ink);
}
.peek:focus-visible { outline: 3px solid var(--fn-dk); outline-offset: 2px; }

/* ── Keypad ──────────────────────────────────────── */
.keypad {
  flex: 1 1 auto;
  padding: 10px 12px 16px;
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 1fr;
  min-height: 0;
}

.key {
  border: 2.5px solid var(--ink);
  cursor: pointer;
  background: var(--num); color: var(--ink);
  font-family: inherit; font-weight: 900;
  font-size: 22px;
  border-radius: 12px;
  box-shadow: 3px 3px 0 var(--ink);
  padding: 0;
  transition: transform 70ms cubic-bezier(.3,.7,.4,1), box-shadow 70ms;
  touch-action: manipulation;
  line-height: 1;
  min-width: 0; min-height: 0;
}
.key.kind-num  { background: var(--num); }
.key.kind-fn   { background: var(--fn); font-size: 19px; }
.key.kind-op   { background: var(--op); font-size: 21px; }
.key.kind-ctrl { background: var(--ctrl); font-size: 19px; }
.key.kind-clear { background: var(--op); color: var(--ink); font-size: 19px; }
.key.kind-eq   { background: var(--eq); color: white; text-shadow: 0 1px 0 rgba(0,0,0,.2); }

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

.key:active, .key.is-pressed {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 var(--ink);
}

.key:focus-visible { outline: 3px solid var(--fn-dk); outline-offset: 2px; }

/* Gentle randomised sticker rotations, applied per-position */
.key[data-rot="-3"] { transform: rotate(-1.5deg); }
.key[data-rot="-2"] { transform: rotate(-1deg); }
.key[data-rot="-1"] { transform: rotate(-0.5deg); }
.key[data-rot="0"]  { transform: rotate(0deg); }
.key[data-rot="1"]  { transform: rotate(0.5deg); }
.key[data-rot="2"]  { transform: rotate(1deg); }
.key[data-rot="3"]  { transform: rotate(1.5deg); }
.key.is-pressed[data-rot="-3"] { transform: rotate(-1.5deg) translate(2px, 2px); }
.key.is-pressed[data-rot="-2"] { transform: rotate(-1deg) translate(2px, 2px); }
.key.is-pressed[data-rot="-1"] { transform: rotate(-0.5deg) translate(2px, 2px); }
.key.is-pressed[data-rot="0"]  { transform: translate(2px, 2px); }
.key.is-pressed[data-rot="1"]  { transform: rotate(0.5deg) translate(2px, 2px); }
.key.is-pressed[data-rot="2"]  { transform: rotate(1deg) translate(2px, 2px); }
.key.is-pressed[data-rot="3"]  { transform: rotate(1.5deg) translate(2px, 2px); }

/* ── Landscape / tablet layout ──────────────────── */
@media (orientation: landscape) and (min-width: 640px) {
  html, body, #app { overflow: hidden; }
  .shell {
    flex-direction: row;
    margin: 8px;
    /* shell fills the viewport minus margins */
    height: calc(100dvh - 16px);
  }
  .display {
    flex: 1 1 50%;
    min-height: 0;
    min-width: 0;   /* let fitResult shrink the number instead of widening the column (squeezing the keypad) */
    justify-content: space-between; align-items: stretch;
    padding: 22px 28px;
    display: flex; flex-direction: column;
    gap: 10px;
  }
  .display-mid {
    flex: 1 1 auto;
    display: flex; flex-direction: column;
    justify-content: center;
    align-items: stretch;
    min-height: 0;
  }
  .brand { font-size: 14px; padding: 5px 14px; align-self: flex-start; }
  .expression { font-size: 22px; text-align: right; min-height: 28px; }
  .result {
    font-size: clamp(96px, 18vw, 220px);
    text-align: right;
    flex: 0 0 auto;
  }
  .peek {
    align-self: flex-end;
    font-size: 18px;
    padding: 14px 22px;
    margin-top: 4px;
  }

  .keypad {
    flex: 1 1 50%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(6, 1fr);
    grid-auto-rows: 1fr;
    padding: 14px;
    gap: 10px;
    min-height: 0;
    height: 100%;
    align-self: stretch;
  }
  .key {
    font-size: clamp(22px, 2.6vw, 34px);
    padding: 0;
    min-height: 0;
    height: 100%;
    width: 100%;
  }
  .key.kind-ctrl, .key.kind-clear { font-size: clamp(20px, 2.4vw, 30px); }

  /* No separate landscape-only column — top row cycles in both modes */
  .key.landscape-only { display: none; }
}

/* Hide landscape-only keys in portrait */
@media not all and (orientation: landscape) {
  .key.landscape-only { display: none; }
}
@media (orientation: landscape) and (max-width: 639px) {
  .key.landscape-only { display: none; }
}

/* ── Bottom sheet ────────────────────────────────── */
.scrim {
  position: absolute; inset: 0; z-index: 10;
  background: rgba(43, 33, 24, 0.0);
  display: flex; align-items: flex-end;
  pointer-events: none;
  transition: background 220ms ease;
}
.scrim.open { pointer-events: auto; background: rgba(43, 33, 24, 0.4); }

.sheet {
  width: 100%;
  max-height: 80%;
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-bottom: none;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  padding: 10px 0 calc(20px + env(safe-area-inset-bottom));
  box-shadow: 0 -4px 0 rgba(43, 33, 24, 0.15);
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 300ms cubic-bezier(.2,.8,.3,1);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.scrim.open .sheet { transform: translateY(0); }

.sheet-grab {
  width: 50px; height: 5px;
  background: var(--ink); opacity: 0.55;
  border-radius: 3px;
  margin: 6px auto 14px;
}

.sheet-head {
  padding: 0 18px 14px;
  display: flex; align-items: center; gap: 12px;
  flex-wrap: wrap;
}
.sheet-chip {
  font-size: 13px; font-weight: 900;
  padding: 4px 11px; background: var(--num);
  border: 2.5px solid var(--ink); border-radius: 8px;
  transform: rotate(-2deg);
  box-shadow: 2px 2px 0 var(--ink);
}
.sheet-value {
  font-size: 32px; font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.5px;
  word-break: break-all;
  min-width: 0;
}
.sheet-close {
  margin-left: auto;
  border: 2.5px solid var(--ink);
  background: var(--paper);
  width: 34px; height: 34px;
  border-radius: 10px;
  font-size: 20px; font-weight: 900;
  cursor: pointer;
  line-height: 1;
  box-shadow: 2px 2px 0 var(--ink);
  font-family: inherit;
}
.sheet-close:active { transform: translate(1px, 1px); box-shadow: 1px 1px 0 var(--ink); }

.cards {
  display: flex; flex-direction: column; gap: 10px;
  padding: 0 16px;
}

.card {
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: 3px 3px 0 var(--ink);
  animation: cardIn 380ms cubic-bezier(.2,.8,.3,1) both;
}
.card:nth-child(1) { animation-delay: 50ms; transform: rotate(-0.4deg); }
.card:nth-child(2) { animation-delay: 100ms; transform: rotate(0.3deg); }
.card:nth-child(3) { animation-delay: 150ms; transform: rotate(-0.2deg); }
.card:nth-child(4) { animation-delay: 200ms; transform: rotate(0.4deg); }
.card:nth-child(5) { animation-delay: 250ms; transform: rotate(-0.3deg); }
.card:nth-child(6) { animation-delay: 300ms; transform: rotate(0.2deg); }
.card:nth-child(7) { animation-delay: 350ms; transform: rotate(-0.4deg); }

@keyframes cardIn {
  from { opacity: 0; transform: translateY(12px) rotate(var(--rot, 0deg)); }
  to   { opacity: 1; }
}

.card-title {
  display: inline-block;
  background: var(--num); color: var(--ink);
  padding: 2px 10px; border: 2px solid var(--ink);
  border-radius: 999px;
  font-size: 11px; font-weight: 900; letter-spacing: 0.5px;
  margin-bottom: 8px; text-transform: uppercase;
}
.card.t-fn .card-title { background: var(--fn); }
.card.t-op .card-title { background: var(--op); }
.card.t-ctrl .card-title { background: var(--ctrl); }
.card.t-eq .card-title { background: var(--eq); color: white; }

.card-sub { font-size: 13px; color: rgba(43, 33, 24, 0.7); margin-bottom: 8px; margin-top: -2px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 14px; font-weight: 900; font-variant-numeric: tabular-nums;
  padding: 3px 10px; border-radius: 8px;
  background: var(--paper); color: var(--ink);
  border: 2px solid var(--ink);
}
.chip.on { background: var(--ctrl); }
.chip.off { background: #EFE7D4; opacity: 0.75; }

.kv { display: flex; flex-direction: column; gap: 4px; }
.kv-row { display: flex; justify-content: space-between; font-size: 15px; }
.kv-row .k { font-weight: 700; opacity: 0.8; }
.kv-row .v { font-weight: 900; font-variant-numeric: tabular-nums; }

.big-text { font-size: 22px; font-weight: 900; font-variant-numeric: tabular-nums; word-break: break-all; }
.words-text { font-size: 17px; font-weight: 800; text-transform: capitalize; }
.fun-text { font-size: 15px; font-weight: 700; line-height: 1.45; }

/* ── Sticker confetti scatter on the scrim ─────── */
.sticker-bg {
  position: absolute; inset: 0; pointer-events: none; overflow: hidden;
}
.sticker-bg svg { position: absolute; opacity: 0.1; }

/* Splash / install prompt */
.install-hint {
  position: absolute; bottom: 12px; left: 12px; right: 12px;
  background: var(--paper); border: 2.5px solid var(--ink);
  border-radius: 14px; padding: 10px 14px;
  box-shadow: 3px 3px 0 var(--ink);
  display: none; align-items: center; gap: 10px;
  font-weight: 700; font-size: 13px;
  z-index: 20;
}
.install-hint.show { display: flex; }
.install-hint button {
  margin-left: auto;
  border: 2px solid var(--ink); background: var(--eq); color: white;
  padding: 6px 12px; border-radius: 10px;
  font-family: inherit; font-weight: 900; cursor: pointer;
  box-shadow: 2px 2px 0 var(--ink);
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .card, .sheet, .scrim, .key, .peek { animation: none !important; transition: none !important; }
}
