:root {
  --paper: #fbf7ea;
  --ink: #2c3440;
  --ink-soft: #5e6672;
  --line: #bcd0de;
  --red: #c8473f;
  --cover: #3f7d6e;
  /* English letters come from the first font, Chinese from the ones after it. */
  --hand: "Patrick Hand", "ZCOOL KuaiLe", "PingFang SC", "Microsoft YaHei", sans-serif;
  --brush: "Kalam", "Ma Shan Zheng", "ZCOOL KuaiLe", "PingFang SC", serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--hand);
}

[hidden] {
  display: none !important;
}

/* Patrick Hand runs small next to the Chinese fonts, so English text gets a little larger. */
html:lang(en) {
  font-size-adjust: 0.52;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  touch-action: manipulation;
}

canvas {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#world {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: pointer;
}

#hud {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: 16px;
  padding: 6px 12px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 3px 8px 4px 9px;
  transform: rotate(1deg);
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 10px;
}

#switch-lang {
  padding: 1px 8px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 5px 3px 6px 3px;
  font-size: 14px;
  cursor: pointer;
}

#hint {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  margin: 0;
  text-align: center;
  font-size: 15px;
  color: var(--ink-soft);
  pointer-events: none;
}

#show-help {
  padding: 1px 8px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 5px 3px 6px 3px;
  font-size: 14px;
  cursor: pointer;
}

#toast {
  position: fixed;
  top: 64px;
  left: 50%;
  margin: 0;
  padding: 2px 16px;
  transform: translateX(-50%) rotate(-3deg);
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 8px 4px 9px 5px;
  font-family: var(--brush);
  font-size: 28px;
  z-index: 5;
  pointer-events: none;
}

#card {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: safe center;
  padding: 16px;
  overflow-y: auto;
  background: rgba(44, 52, 64, 0.35);
}

#card-box {
  width: min(520px, 100%);
  padding: 18px 22px 20px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 4px 10px 6px 12px;
  box-shadow: 5px 6px 0 var(--ink);
}

#card-title {
  margin: 0 0 10px;
  font-family: var(--brush);
  font-weight: 400;
  font-size: 34px;
  line-height: 1.15;
}

#card-list {
  margin: 0 0 16px;
  padding-left: 22px;
  font-size: 17px;
  line-height: 1.55;
}

#card-list li {
  margin-bottom: 6px;
}

#card-go {
  padding: 8px 26px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  box-shadow: 2px 2px 0 var(--ink);
  font-size: 18px;
  cursor: pointer;
}

#action {
  position: fixed;
  left: 50%;
  bottom: max(44px, calc(env(safe-area-inset-bottom) + 28px));
  transform: translateX(-50%) rotate(-1deg);
  padding: 10px 20px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 8px 4px 9px 5px;
  box-shadow: 3px 3px 0 var(--ink);
  font-size: 18px;
  cursor: pointer;
  white-space: nowrap;
}

/* Cover: two notebooks, one per language */
#cover {
  position: fixed;
  inset: 0;
  display: flex;
  flex-wrap: wrap;
  align-content: safe center;
  align-items: stretch;
  justify-content: center;
  gap: 28px;
  padding: 20px 16px;
  overflow-y: auto;
  background: #d9d3c1;
  transition: opacity 0.5s;
}

#cover.opening {
  opacity: 0;
  pointer-events: none;
}

.book {
  flex: 0 1 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 18px 22px 22px;
  background: var(--cover);
  color: #fdf9ea;
  border: 0;
  border-left: 14px solid #2c5c50;
  border-radius: 3px 10px 10px 3px;
  box-shadow: 8px 10px 0 rgba(44, 52, 64, 0.28);
  text-align: center;
  cursor: pointer;
  transform-origin: left center;
  transition: transform 0.5s, box-shadow 0.15s;
}

.book span {
  display: block;
}

.book-en {
  background: #3f5f8f;
  border-left-color: #2b4369;
}

.book:hover,
.book:focus-visible {
  transform: translateY(-5px) rotate(-0.6deg);
  box-shadow: 10px 14px 0 rgba(44, 52, 64, 0.28);
}

.book.chosen {
  transform: perspective(1400px) rotateY(-60deg);
}

.book .book-edition {
  padding: 3px 14px;
  background: #ffe48a;
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 6px 3px 7px 4px;
  font-size: 18px;
}

.book .book-school {
  margin-top: 14px;
  letter-spacing: 0.3em;
  font-size: 14px;
  opacity: 0.85;
}

.book .book-title {
  margin: 8px 0 16px;
  font-family: var(--brush);
  font-size: clamp(34px, 9vw, 42px);
  line-height: 1.1;
}

.book .book-form {
  width: 100%;
  padding: 8px 12px;
  background: var(--paper);
  color: var(--ink);
  text-align: left;
  font-size: 15px;
}

.book .book-form > span {
  display: flex;
  gap: 10px;
  padding: 5px 0;
  border-bottom: 1px solid var(--line);
}

.book .book-form > span:last-child {
  border-bottom: 0;
}

.book-form a {
  color: #2c5c9e;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 3px;
}

.book-form b {
  flex: none;
  font-weight: 400;
  color: var(--ink-soft);
}

.book .book-note {
  margin: 12px 0 14px;
  font-size: 13px;
  line-height: 1.5;
  opacity: 0.9;
}

.book .book-open {
  margin-top: auto;
  font: inherit;
  cursor: pointer;
  padding: 8px 30px;
  background: #ffe48a;
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 8px 4px 9px 5px;
  box-shadow: 3px 3px 0 var(--ink);
  font-size: 18px;
}

/* Dialog */
#dialog {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(780px, calc(100% - 32px));
  height: min(72dvh, 680px);
  display: flex;
  flex-direction: column;
  background-color: var(--paper);
  background-image: linear-gradient(transparent 31px, var(--line) 32px);
  background-size: 100% 32px;
  border: 2px solid var(--ink);
  border-radius: 4px 10px 6px 12px;
  box-shadow: 5px 6px 0 var(--ink);
}

#dialog header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 16px 8px;
  background: var(--paper);
  border-bottom: 2px solid var(--ink);
}

#dialog h2 {
  margin: 0;
  font-family: var(--brush);
  font-weight: 400;
  font-size: 28px;
  line-height: 1.1;
}

#dialog-role {
  margin: 2px 0 0;
  font-size: 14px;
  color: var(--ink-soft);
}

#dialog-close {
  flex: none;
  padding: 4px 12px;
  background: transparent;
  border: 2px solid var(--ink);
  border-radius: 6px 3px 7px 4px;
  cursor: pointer;
}

#dialog-log {
  flex: 1;
  min-height: 120px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 20px;
  font-size: clamp(18px, 2.2vw, 21px);
  line-height: 1.6;
}

#dialog-log p {
  margin: 0 0 10px;
  overflow-wrap: anywhere;
}

#dialog-log .me {
  text-align: right;
  color: #2c5c9e;
}

#dialog-mode {
  margin: 0;
  padding: 4px 16px;
  font-size: 13px;
  color: var(--red);
  background: var(--paper);
}

/* Suggested questions: one row that scrolls sideways, so phones keep room for the log. */
#dialog-asks {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--paper);
  border-top: 2px dashed var(--ink-soft);
}

#dialog-asks[hidden] {
  display: none;
}

#dialog-asks button {
  flex: none;
  padding: 5px 12px;
  background: #fff;
  color: #2c5c9e;
  border: 2px solid #2c5c9e;
  border-radius: 12px 6px 12px 6px;
  font-size: 15px;
  white-space: nowrap;
  cursor: pointer;
}

#dialog-asks button:disabled {
  opacity: 0.45;
  cursor: default;
}

#dialog-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: var(--paper);
  border-top: 2px solid var(--ink);
}

#dialog-input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  font-size: 16px;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 4px 7px 3px 6px;
}

#dialog-form button {
  flex: none;
  padding: 8px 18px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  cursor: pointer;
}

/* The playground show */
#show {
  position: fixed;
  inset: 0;
  background: #eef1d6;
}

#show-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: pointer;
}

#show-card {
  z-index: 1;
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%) rotate(-0.4deg);
  width: min(600px, calc(100% - 32px));
  padding: 10px 16px 12px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 4px 10px 6px 12px;
  box-shadow: 4px 5px 0 var(--ink);
}

#show-speaker {
  margin: 0;
  font-family: var(--brush);
  font-size: 22px;
  color: #6f4894;
}

#show-text {
  margin: 2px 0 10px;
  font-size: clamp(16px, 2.2vw, 19px);
  line-height: 1.5;
}

#show-meter {
  margin: 0 0 4px;
}

#show-meter-label {
  margin: 0 0 4px;
  font-size: 14px;
}

#show-meter .track {
  height: 12px;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 4px;
  overflow: hidden;
}

#show-meter-fill {
  display: block;
  height: 100%;
  width: 0;
  background: #c8473f;
  transition: width 0.25s;
}

#show-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#show-actions button {
  padding: 6px 14px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  cursor: pointer;
}

#show-actions #show-skip {
  margin-left: auto;
  background: transparent;
  font-size: 14px;
}

#show-actions #show-giveup {
  background: var(--paper);
}

#show-leave {
  position: absolute;
  right: 16px;
  bottom: max(14px, env(safe-area-inset-bottom));
  padding: 6px 14px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  cursor: pointer;
}

#show-leave.done {
  background: #ffe48a;
  box-shadow: 3px 3px 0 var(--ink);
}

#dialog-log .aside {
  color: var(--ink-soft);
  font-size: 0.9em;
}

#dialog-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 16px;
  background: var(--paper);
  border-top: 2px solid var(--ink);
}

#dialog-links a {
  padding: 4px 12px;
  background: #ffe48a;
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  font-size: 15px;
  text-decoration: none;
}

#dialog-game {
  margin: 0 16px 8px;
  padding: 7px 12px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  box-shadow: 2px 2px 0 var(--ink);
  font-size: 16px;
  cursor: pointer;
}

/* Mini games */
#game {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: safe center;
  padding: 16px;
  overflow-y: auto;
  background-color: #fbf7ea;
  background-image: linear-gradient(#cfdfe9 1px, transparent 1px), linear-gradient(90deg, #cfdfe9 1px, transparent 1px);
  background-size: 40px 40px;
}

#game-card {
  width: min(660px, 100%);
  padding: 14px 18px 18px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 4px 10px 6px 12px;
  box-shadow: 5px 6px 0 var(--ink);
}

#game-card header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#game-title {
  margin: 0;
  font-family: var(--brush);
  font-weight: 400;
  font-size: 30px;
}

#game-leave {
  flex: none;
  padding: 4px 12px;
  background: transparent;
  border: 2px solid var(--ink);
  border-radius: 6px 3px 7px 4px;
  cursor: pointer;
}

#game-text {
  min-height: 3em;
  margin: 8px 0 10px;
  font-size: 18px;
  line-height: 1.5;
}

.game-canvas {
  display: block;
  width: 100%;
  height: auto;
  border: 2px solid var(--ink);
  touch-action: none;
  cursor: pointer;
}

#game-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

#game-card button.primary {
  padding: 8px 22px;
  background: #ffe48a;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  box-shadow: 2px 2px 0 var(--ink);
  font-size: 18px;
  cursor: pointer;
}

#game-card button.primary:disabled {
  opacity: 0.45;
  cursor: default;
}

#game-actions button:not(.primary) {
  padding: 8px 18px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  font-size: 18px;
  cursor: pointer;
}

.game-bar {
  height: 12px;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 4px;
  overflow: hidden;
}

.game-bar span {
  display: block;
  height: 100%;
  width: 100%;
  background: #c8473f;
}

.shop-ask {
  margin: 14px 0;
  font-family: var(--brush);
  font-size: clamp(24px, 5vw, 32px);
  text-align: center;
}

.shop-shelf {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.shop-shelf button,
.sentence-pickers button {
  padding: 10px 8px;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 7px 4px 8px 5px;
  font-size: 17px;
  cursor: pointer;
}

.shop-stats {
  margin: 12px 0 0;
  font-size: 15px;
  color: var(--ink-soft);
}

.sentence-line {
  margin: 8px 0 14px;
  font-family: var(--brush);
  font-size: clamp(22px, 4.6vw, 30px);
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.sentence-pickers {
  display: grid;
  gap: 8px;
}

.sentence-pickers > div {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.sentence-pickers button {
  font-size: 15px;
  padding: 8px 4px;
}

.sentence-pickers button.picked {
  background: #ffe48a;
}

.sentence-stamp {
  display: inline-block;
  margin: 4px 0 0;
  padding: 4px 14px;
  border: 3px solid #3d8a55;
  border-radius: 8px;
  color: #3d8a55;
  font-family: var(--brush);
  font-size: 28px;
  transform: rotate(-5deg);
}

.sentence-stamp.bad {
  border-color: #c8473f;
  color: #c8473f;
}

/* Quiet room: the one place in town with no paper, no jokes, no reward. */
#quiet {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 16px;
  background: #15171b;
  color: #d9d7cf;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  text-align: center;
}

#quiet-lines p {
  margin: 0 0 14px;
  font-size: clamp(17px, 3vw, 21px);
  opacity: 0;
  animation: quiet-in 2.4s forwards;
}

@keyframes quiet-in {
  to {
    opacity: 1;
  }
}

#quiet-phone {
  width: min(280px, 100%);
  padding: 14px 16px;
  background: #24272d;
  border: 1px solid #454952;
  border-radius: 18px;
  animation: quiet-in 2.4s forwards;
}

#quiet-phone p {
  margin: 8px 0;
  text-align: left;
  font-size: 16px;
}

#quiet-leave {
  position: absolute;
  bottom: max(20px, env(safe-area-inset-bottom));
  padding: 6px 16px;
  background: transparent;
  color: #8d8f93;
  border: 1px solid #454952;
  border-radius: 6px;
  font-family: inherit;
  cursor: pointer;
}

@media (prefers-reduced-motion: reduce) {
  #quiet-lines p,
  #quiet-phone {
    animation-duration: 0.01s;
  }
  #cover {
    transition: none;
  }
}

/* Phones: the dialog becomes a tall sheet, and a short landscape screen gets compact text. */
@media (max-width: 600px) {
  #dialog {
    width: calc(100% - 16px);
    height: 80dvh;
    bottom: max(8px, env(safe-area-inset-bottom));
  }

  #hud {
    right: 8px;
    font-size: 13px;
    gap: 6px;
  }

  #game {
    padding: 8px;
  }
}

@media (max-height: 480px) {
  #dialog {
    height: calc(100dvh - 16px);
    bottom: 8px;
  }

  #dialog header {
    padding: 6px 12px;
  }

  #dialog h2 {
    font-size: 22px;
  }

  #dialog-role {
    display: none;
  }

  #show-text {
    margin-bottom: 6px;
    font-size: 15px;
    line-height: 1.35;
  }

  #show-speaker {
    display: none;
  }

  #show-card {
    padding: 6px 12px 8px;
  }

  /* Keep the game's buttons on screen when the phone is on its side. */
  .game-canvas {
    max-height: calc(100dvh - 170px);
    object-fit: contain;
    background: #fbf7ea;
  }

  #game-text {
    min-height: 0;
    margin: 4px 0 6px;
    font-size: 15px;
    line-height: 1.35;
  }

  #game-title {
    font-size: 24px;
  }

  #game-actions {
    margin-top: 6px;
  }

  .book {
    flex-basis: 300px;
    padding: 12px 18px 14px;
  }

  .book .book-school {
    display: none;
  }

  .book .book-title {
    margin: 8px 0 10px;
    font-size: 32px;
  }
}
