@font-face {
  font-family: 'Space Grotesk';
  src: url('assets/fonts/SpaceGrotesk-Medium.woff2') format('woff2');
  font-weight: 500;
  font-display: swap;
}
@font-face {
  font-family: 'Space Grotesk';
  src: url('assets/fonts/SpaceGrotesk-Bold.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}

:root{
  --bg: #000000;
  --graphite: #2a2a2e;
  --graphite-active: #3c3c41;
  --coffee: #8b5a2b;
  --coffee-active: #a56b36;
  --red: #d9433d;
  --red-active: #e6564f;
  --green: #2fae60;
  --green-active: #38c470;
  --white: #ffffff;
  --muted: rgba(255,255,255,0.42);
  --muted-strong: rgba(255,255,255,0.62);
  --hairline: rgba(255,255,255,0.08);

  --key-gap: 10px;
  --pad-x: 12px;

  color-scheme: dark;
}

*{ box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body{
  height: 100%;
  margin: 0;
  background: var(--bg);
  overscroll-behavior: none;
}

body{
  font-family: 'Space Grotesk', -apple-system, 'Segoe UI', sans-serif;
  color: var(--white);
  user-select: none;
  -webkit-user-select: none;
}

#app{
  height: 100dvh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  overflow: hidden;
  position: relative;
}

/* ---------- DISPLAY ---------- */

#display-wrap{
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  background: var(--bg);
  overflow: hidden;
}

#display{
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-end;
  padding: 18px var(--pad-x) 14px;
  scrollbar-width: none;
  cursor: text;
}
#display::-webkit-scrollbar{ display:none; }

#expr-line{
  font-weight: 700;
  font-size: 56px;
  line-height: 1.16;
  letter-spacing: -0.01em;
  text-align: right;
  white-space: pre-wrap;
  word-break: break-all;
  width: 100%;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  /* No transition on font-size: fitFont() measures layout synchronously
     in a loop, a running transition would corrupt the measurements
     (wrong step -> visible flicker). Steps switch instantly. */
  margin-top: auto;
}

#caret{
  display: inline-block;
  width: 2px;
  margin: 0 1px;
  background: var(--green);
  vertical-align: -0.15em;
  height: 0.95em;
  animation: blink 1s step-end infinite;
}
@keyframes blink{ 50%{ opacity: 0; } }

#preview-line{
  font-weight: 500;
  font-size: 22px;
  color: var(--muted-strong);
  min-height: 30px;
  text-align: right;
  width: 100%;
  font-variant-numeric: tabular-nums;
  margin-top: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#expr-line.error{ animation: shake .32s ease; }
#expr-line.error #expr-before,
#expr-line.error #expr-after{ color: var(--red); }
@keyframes shake{
  20%{ transform: translateX(-8px); }
  40%{ transform: translateX(7px); }
  60%{ transform: translateX(-5px); }
  80%{ transform: translateX(3px); }
  100%{ transform: translateX(0); }
}

/* ---------- KEYPAD ---------- */

#keypad{
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: var(--key-gap);
  padding: var(--key-gap) var(--pad-x) calc(var(--key-gap) + 4px);
  height: 58vh;
  max-height: 480px;
  min-height: 340px;
}

.key{
  border: none;
  border-radius: 999px;
  color: var(--white);
  font-family: inherit;
  font-weight: 500;
  font-size: clamp(26px, 7vw, 38px);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .08s ease, filter .08s ease, background-color .08s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.key:active{
  transform: scale(0.92);
  filter: brightness(1.12);
}

.key-graphite{ background: var(--graphite); }
.key-graphite:active{ background: var(--graphite-active); }

.key-coffee{ background: var(--coffee); font-weight: 600; }
.key-coffee:active{ background: var(--coffee-active); }

.key-red{ background: var(--red); font-weight: 600; }
.key-red:active{ background: var(--red-active); }

.key-green{ background: var(--green); font-weight: 700; }
.key-green:active{ background: var(--green-active); }

/* ---------- HISTORY PANEL ---------- */

#history-panel{
  position: absolute;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  transform: translateY(-100%);
  transition: transform .28s cubic-bezier(.22,.8,.36,1);
  z-index: 20;
  padding-top: env(safe-area-inset-top);
}
#history-panel.open{ transform: translateY(0); }
#history-panel.dragging{ transition: none; }

#history-panel-header{
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px var(--pad-x) 12px;
  font-size: 20px;
  font-weight: 700;
  border-bottom: 1px solid var(--hairline);
}
#history-close{
  background: var(--graphite);
  color: var(--white);
  border: none;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}
#history-close:active{ background: var(--graphite-active); }

#history-list{
  flex: 1;
  overflow-y: auto;
  padding: 6px var(--pad-x) 20px;
}

.history-row{
  padding: 14px 6px;
  border-bottom: 1px solid var(--hairline);
  text-align: right;
}
.history-row:active{ background: rgba(255,255,255,0.05); }
.history-row .h-expr{
  font-size: 16px;
  color: var(--muted-strong);
  word-break: break-all;
}
.history-row .h-result{
  font-size: 26px;
  font-weight: 700;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}
.history-row .h-time{
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

#history-empty{
  display: none;
  text-align: center;
  color: var(--muted);
  font-size: 15px;
  padding: 60px 20px;
}
#history-panel.empty #history-empty{ display: block; }
#history-panel.empty #history-list{ display: none; }

/* ---------- CONTEXT MENU (history row actions) ---------- */

#menu-overlay{
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s ease;
  z-index: 29;
}
#menu-overlay.open{ opacity: 1; pointer-events: auto; }

#context-menu{
  position: fixed;
  left: 50%;
  bottom: -220px;
  transform: translateX(-50%);
  width: min(92vw, 420px);
  background: #151517;
  border-radius: 20px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
  transition: bottom .22s cubic-bezier(.22,.8,.36,1);
  box-shadow: 0 -6px 30px rgba(0,0,0,0.5);
}
#context-menu.open{ bottom: max(18px, env(safe-area-inset-bottom)); }

#context-menu-expr{
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 8px 8px;
  word-break: break-all;
}

#context-menu button{
  border: none;
  background: var(--graphite);
  color: var(--white);
  font-family: inherit;
  font-size: 17px;
  font-weight: 500;
  padding: 16px;
  border-radius: 14px;
}
#context-menu button:active{ background: var(--graphite-active); }
#context-menu button[data-action="res"]{ background: var(--green); font-weight: 700; }
#context-menu button[data-action="res"]:active{ background: var(--green-active); }

@media (max-width: 360px){
  #expr-line{ font-size: 44px; }
}
