/* The vacuum-sealed chat: the group chat of a shared flat on a white page. The field is a soft grey skin; my
   messages are sunflower yellow, the flatmates' grey, and each flatmate has a colour for their name. */
:root {
  --page: #ffffff;          /* the page */
  --card: #ffffff;          /* the chat's box */
  --edge: #e6e6ec;          /* its edge */
  --ink: #17171c;           /* the words */
  --muted: #85868f;         /* the line under the name, the placeholder */
  --skin: #f0f0f4;          /* the field's skin, and the flatmates' bubbles */
  --ring: #a3a4b0;          /* the field's edge while you type in it */
  --mine: #ffd23f;          /* my bubbles */
  --button: #17171c;        /* the send button */
  --arrow: #ffd23f;         /* its arrow */
  --who-1: #c0266d;         /* the first flatmate's colour */
  --who-2: #1f68d1;         /* the second flatmate's colour */
}

* {
  box-sizing: border-box;
}

html {
  touch-action: manipulation;
}

html,
body {
  margin: 0;
  overflow-x: clip;
}

body {
  min-height: 100svh;
  background: var(--page);
  color: var(--ink);
  font-family: 'Rubik Variable', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.stage {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.chat {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(384px, 100%);
  height: min(560px, calc(100svh - 48px));
  min-height: 420px;
  border-radius: 30px;
  background: var(--card);
  box-shadow: 0 0 0 1px var(--edge), 0 2px 4px rgba(23, 23, 28, 0.04), 0 24px 48px -24px rgba(23, 23, 28, 0.18);
  overflow: hidden;
  isolation: isolate;
}

/* ---------------------------------------------------------------- the head */
.chat__head {
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 18px 14px;
  border-bottom: 1px solid #efeff3;
  background: var(--card);
}

.chat__faces {
  display: flex;
  flex: none;
}

.face {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--who) 13%, #ffffff);
  box-shadow: 0 0 0 2.5px var(--card);
  color: var(--who);
  font-size: 14px;
  font-weight: 600;
}

.face + .face {
  margin-left: -9px;
}

.chat__title {
  min-width: 0;
}

.chat__name {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 20px;
  letter-spacing: -0.01em;
}

/* the line under the name: who is in the flat, or who is writing */
.chat__status {
  position: relative;
  height: 18px;
  margin: 1px 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  line-height: 18px;
}

.status__line {
  display: block;
  white-space: nowrap;
}

.status__line.is-going {
  position: absolute;
  left: 0;
  top: 0;
}

.status__line b {
  font-weight: 560;
  color: var(--who);
}

/* ---------------------------------------------------------------- the conversation */
.chat__thread {
  position: relative;
  z-index: 1;
  flex: 1;
  min-height: 0;
  padding: 18px 14px 8px;
  overflow-y: auto;
  overflow-anchor: none;
  overscroll-behavior: contain;
  scrollbar-width: none;
  /* the oldest messages fade out under the head */
  -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 22px);
  mask-image: linear-gradient(180deg, transparent 0, #000 22px);
}

.chat__thread::-webkit-scrollbar {
  display: none;
}

.chat__log {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 4px;
  min-height: 100%;
}

/* a bubble's lines are exactly as long as the field's (the conversation is 28px narrower than the box, the field's
   words 108px): a message keeps its line breaks on its way up */
.msg {
  display: flex;
  flex-direction: column;
  max-width: calc(100% - 50px);
}

.msg--me {
  align-self: flex-end;
  align-items: flex-end;
}

.msg--them {
  align-self: flex-start;
  align-items: flex-start;
}

/* a new speaker leaves a little more room */
.msg.is-new-run {
  margin-top: 10px;
}

.msg__who {
  margin: 0 0 6px 14px;
  color: var(--who);
  font-size: 12.5px;
  font-weight: 560;
  line-height: 16px;
}

.bubble {
  position: relative;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 11px 15px;
  border-radius: 20px;
  font-size: 16px;
  line-height: 22px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  isolation: isolate;
}

.msg--me .bubble {
  border-bottom-right-radius: 6px;
  background: var(--mine);
  color: var(--ink);
}

.msg--them .bubble {
  border-bottom-left-radius: 6px;
  background: var(--skin);
  color: var(--ink);
}

/* while a skin is still settling, the skin is the bubble's background: the bubble itself is see-through */
.msg .bubble.is-skin {
  background: transparent;
}

/* my message on its way: its words fly above everything, the ones in the bubble wait */
.bubble.is-landing .w {
  opacity: 0;
}

/* a flatmate's draft: the words are there to be measured, never seen */
.bubble.is-draft {
  min-width: 56px;
  color: transparent;
}

.bubble__skin {
  position: absolute;
  left: 0;
  top: 100%;
  z-index: -1;
  width: 1px;
  height: 1px;
  overflow: visible;
  pointer-events: none;
}

/* the shape of the words being written */
.bubble__bars {
  position: absolute;
  left: 0;
  top: 100%;
  width: 0;
  height: 0;
  pointer-events: none;
}

.bar {
  position: absolute;
  left: 0;
  top: 0;
  height: 9px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--who) 34%, transparent);
  transition: width 0.1s ease-out, transform 0.22s cubic-bezier(0.2, 0.9, 0.25, 1);
}

.bar--caret {
  width: 2px;
  height: 18px;
  border-radius: 1px;
  background: var(--who);
  transition: transform 0.07s linear;
}

@media (prefers-reduced-motion: reduce) {
  .bar {
    transition: none;
  }
}

/* ---------------------------------------------------------------- the field */
.chat__composer {
  position: relative;
  z-index: 3;
  margin: 0;
  padding: 8px 12px 12px;
}

.field {
  position: relative;
  display: flex;
  align-items: flex-end;
  min-height: 56px;
}

/* the textarea and its mirror (where the words are measured) share every measure */
.field__input,
.mirror {
  flex: 1;
  min-width: 0;
  margin: 0;
  padding: 17px 8px 17px 20px;
  border: 0;
  font: 400 16px/22px 'Rubik Variable', system-ui, sans-serif;
  letter-spacing: 0;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}

.field__input {
  display: block;
  height: 56px;
  max-height: 144px;
  resize: none;
  overflow-y: hidden;
  background: transparent;
  color: var(--ink);
  caret-color: var(--ink);
  outline: none;
  scrollbar-width: none;
  -webkit-tap-highlight-color: transparent;
}

.field__input::-webkit-scrollbar {
  display: none;
}

.field__input::placeholder {
  color: var(--muted);
  opacity: 1;
  transition: opacity 0.25s ease;
}

/* while the skin grows back, the placeholder waits */
.field.is-bare .field__input::placeholder {
  opacity: 0;
  transition: none;
}

.mirror {
  position: absolute;
  left: 0;
  top: 0;
  visibility: hidden;
  pointer-events: none;
}

/* the caret of the library's preview, which writes without taking the focus */
.field__caret {
  position: absolute;
  left: 0;
  top: 0;
  width: 2px;
  height: 20px;
  border-radius: 1px;
  background: var(--ink);
  opacity: 0;
  pointer-events: none;
}

.field__caret.is-on {
  opacity: 1;
}

/* ---------------------------------------------------------------- the send button */
.send {
  position: relative;
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  margin: 6px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: #b3b4be;
  cursor: pointer;
  transition: color 0.2s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.send:disabled {
  cursor: default;
}

/* the colour is a layer of its own: it swells in when there is something to send */
.send__bg {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--button);
  opacity: 0;
  transform: scale(0.55);
  transition: transform 0.32s cubic-bezier(0.3, 1.6, 0.5, 1), opacity 0.18s ease;
}

.send.is-armed {
  color: var(--arrow);
}

.send.is-armed .send__bg {
  opacity: 1;
  transform: scale(1);
}

@media (hover: hover) and (pointer: fine) {
  .send.is-armed:hover .send__bg {
    transform: scale(1.07);
  }
}

.send__arrow {
  position: relative;
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.send:focus-visible {
  outline: 2px solid var(--button);
  outline-offset: 2px;
}

/* ---------------------------------------------------------------- above everything */
.chat__skins {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  width: 1px;
  height: 1px;
  overflow: visible;
  pointer-events: none;
}

.chat__fly {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.fly-word {
  position: absolute;
  left: 0;
  top: 0;
  color: var(--ink);
  font: 400 16px/22px 'Rubik Variable', system-ui, sans-serif;
  white-space: pre;
  will-change: transform;
}

/* for screen readers only */
.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-width: 420px) {
  .stage {
    padding: 16px;
  }

  .chat {
    height: min(560px, calc(100svh - 32px));
  }
}
