body {
  margin: 0;
  font-family: sans-serif;
}

main {
  display: flex;
  justify-content: center;
  padding: 2rem 0;
}

/* phone-shaped window, roughly a modern phone's 9:19.5 ratio */
.phone {
  height: 75vh;
  aspect-ratio: 9 / 19.5;
  border: 3px solid #333;
  border-radius: 2rem;
  padding: 1rem;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* scrolls on its own so the compose box stays pinned below it */
.messages {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
}

.message {
  max-width: 75%;
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-radius: 1rem;
}

.sent {
  align-self: flex-end;
  background: #0b84ff;
  color: white;
}

.received {
  align-self: flex-start;
  background: #e5e5ea;
  color: black;
}

/* iMessage-style compose box */
.compose {
  display: flex;
  align-items: flex-end;
  gap: 0.25rem;
  border: 1px solid #c7c7cc;
  border-radius: 1.25rem;
  padding: 0.25rem 0.25rem 0.25rem 0.75rem;
  background: white;
}

/* grows with its content (see drill.js) up to 3 lines, then scrolls; the
   vertical padding makes one line as tall as the send button */
.compose textarea {
  flex: 1;
  min-width: 0;
  box-sizing: border-box;
  padding: 0.25rem 0;
  line-height: 1.25rem;
  max-height: calc(3 * 1.25rem + 0.5rem);
  overflow-y: auto;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  font: inherit;
}

.compose button {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  border: none;
  border-radius: 50%;
  background: #0b84ff;
  color: white;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
}

/* phone and side column centred together as a pair; the side column is as
   tall as the phone: the context box (up to a third of it) above the results */
.drill {
  width: 100%;
  display: flex;
  justify-content: center;
  gap: 2rem;
}

.side {
  height: 75vh;
  width: min(24rem, 90%);
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.context {
  flex: 0 1 auto;
  max-height: calc(75vh / 3);
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

/* the small grey "Context" / "Grading" labels */
.side-heading {
  margin: 0;
  font-size: 0.75rem;
  font-weight: normal;
  text-transform: uppercase;
  color: #8e8e93;
}

/* a received bubble as tall as its text, scrolling once the context box
   hits its max height */
.context-body {
  min-height: 0;
  align-self: stretch;
  max-width: none;
  box-sizing: border-box;
  overflow-y: auto;
}

/* fills the rest of the column; the list scrolls and Next stays pinned to
   the bottom, level with the phone's bottom edge */
.results {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  align-items: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.6s ease;
}

.drill.graded .results {
  opacity: 1;
  pointer-events: auto;
}

/* the "Grading" heading over the results, spaced like .context */
.grading {
  flex: 1;
  min-height: 0;
  align-self: stretch;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.results-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

/* one sent-style bubble per criterion, stacked like .messages */
.results-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* unlike chat bubbles, may use the column's full width */
.results-list .message {
  max-width: none;
}

/* pill-shaped button, used for both <button> and <a> */
.pill {
  border: none;
  border-radius: 1.25rem;
  padding: 0.5rem 1.25rem;
  background: #0b84ff;
  color: white;
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}

.pill:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* sits where Next scenario appears; once hidden, .results fills its space */
.submit-reply {
  align-self: center;
}

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid #ddd;
}

.brand {
  font-size: 1.25rem;
  font-weight: bold;
  text-decoration: none;
  color: inherit;
}

.compose textarea:disabled,
.compose button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* submit-scenario page: a centred column of message rows, then the + button
   centred below them with Submit beside it */
.scenario-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

/* the compose box's look, stretched to the rows' width; with no send button
   the right padding matches the left */
.scenario-context {
  align-self: stretch;
  padding: 0.25rem 0.75rem;
}

.message-rows {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.message-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.message-row select,
.message-row input {
  font: inherit;
  padding: 0.25rem 0.5rem;
  border: 1px solid #c7c7cc;
  border-radius: 0.5rem;
}

.message-row input {
  width: 20rem;
}

/* same as the compose send button, but grey */
.remove-row {
  flex-shrink: 0;
  width: 1.75rem;
  height: 1.75rem;
  border: none;
  border-radius: 50%;
  background: #8e8e93;
  color: white;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
}

.remove-row:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* the middle column holds + so it stays centred regardless of Submit's width */
.form-actions {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.5rem;
  width: 100%;
}

.form-actions .add-row {
  grid-column: 2;
}

.add-row:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.form-actions [type="submit"] {
  justify-self: start;
}

.scenario-form .flash {
  margin: 0;
  font-weight: bold;
}

.form-response {
  font-weight: bold;
}

/* "completed every scenario" modal, opened by drill.js */
.all-seen {
  max-width: 24rem;
  border: none;
  border-radius: 1rem;
  padding: 1.5rem;
  text-align: center;
}

.all-seen::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.all-seen h2 {
  margin-top: 0;
}

.all-seen-actions {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* phones (portrait, or landscape via the height check): the screen itself is
   the phone. The drill page fills exactly one screen with the context above
   the chat, and after grading the results replace the compose box */
@media (max-width: 700px), (max-height: 500px) {
  .navbar {
    padding: 0.5rem 1rem;
  }

  /* only the drill page is locked to one screen; dvh follows the address bar
     (and, on Android, the keyboard; see the viewport meta in layout.html) */
  body:has(.drill) {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
  }

  body:has(.drill) > section {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  body:has(.drill) main {
    flex: 1;
    min-height: 0;
    padding: 0;
  }

  /* .side's box is dropped so its children can be ordered around the phone */
  .drill {
    flex-direction: column;
    height: 100%;
    gap: 0;
  }

  .side {
    display: contents;
  }

  /* padding rather than margin so the line runs edge to edge, like the one
     above .results */
  .context {
    order: -1;
    max-height: 20dvh;
    padding: 0.5rem 1rem 1.5rem;
    border-bottom: 1px solid #ddd;
  }

  .phone {
    flex: 1;
    min-height: 0;
    height: auto;
    width: 100%;
    aspect-ratio: auto;
    border: none;
    border-radius: 0;
    padding: 1.5rem 1rem 0.5rem;
  }

  .results,
  .submit-reply {
    order: 1;
  }

  /* hidden outright until graded, since opacity 0 would still take space */
  .drill:not(.graded) .results {
    display: none;
  }

  .drill.graded .compose {
    display: none;
  }

  /* Submit only reappears after a 429, so bring the compose box back too */
  .drill.graded:has(.submit-reply:not([hidden])) .compose {
    display: flex;
  }

  .results {
    flex: 0 1 auto;
    max-height: 50%;
    gap: 0.75rem;
    padding: 0.5rem 1rem 1rem;
    border-top: 1px solid #ddd;
    animation: fade-in 0.6s ease;
  }

  .submit-reply {
    margin: 0.5rem auto 1rem;
  }

  .compose button,
  .remove-row {
    width: 2.5rem;
    height: 2.5rem;
  }

  /* keeps one line as tall as the bigger send button */
  .compose textarea {
    padding: 0.625rem 0;
    max-height: calc(3 * 1.25rem + 1.25rem);
  }

  /* submit-scenario page: rows span the screen and the input takes the slack */
  .scenario-form {
    padding: 0 1rem;
  }

  .message-rows,
  .message-row {
    align-self: stretch;
  }

  .message-row input {
    width: auto;
    flex: 1;
    min-width: 0;
  }
}
