html {
  --large-size: calc(max(20px, ((min(100vw, 100vh - 20em) - 16px) / var(--size)) - 4px));
  --small-size: calc(max(16px, ((min(100vw, 100vh - 20em) - 16px) / (2 * var(--size) + 1)) - 4px));
  --background: white;
  --foreground: black;
  --black: #999;
  --possible: #ddd;
  --orange: orange;
  --yellow: yellow;
  --green: #0f0;
  --red-fg: #900;
  --green-fg: #060;
  color-scheme: dark light;
  font-family: system-ui, sans-serif;
}

html.high-contrast {
  --orange: #D81B60;
  --yellow: #0bf;
  --green: #f80;
  --red-fg: #509;
  --green-fg: #940;
}

.help {
  --large-size: 35px;
  --small-size: 30px;
}

.help .grid {
  grid-auto-columns: min-content;
}

.help .message {
  width: 75%;
}

@media (prefers-color-scheme: dark) {
  html {
    --background: black;
    --foreground: white;
    --black: #333;
    --possible: #888;
    --orange: #b60;
    --yellow: #a94;
    --green: green;
    --red-fg: #f77;
    --green-fg: #7f7;
  }

  html.high-contrast {
    --orange: #D81B60;
    --yellow: #07b;
    --green: #940;
    --red-fg: #a6f;
    --green-fg: #f80;
  }
}

.title {
  grid-area: title;
  justify-self: center;
}

@media only screen and (max-width: 600px) {
  .title {
    /* Left align the title to avoid the menu buttons. */
    justify-self: left;
  }
}

.keyboard {
  grid-area: keyboard;
  width: 100%;
  display: grid;
  max-width: 700px;
  grid-template-areas:
      'q q w w e e r r t t y y u u i i o o p p'
      '. a a s s d d f f g g h h j j k k l l .'
      'R R R z z x x c c v v b b n n m m B B B';
  margin: 15px auto;
}

.menu {
  position: absolute;
  right: 0;
  top: 0;
}
.menu > button {
  font-size: 2em;
  padding: 3px;
  cursor: pointer;
  /* The default button style on iOS consumes too much space. */
  -webkit-appearance: none;
  appearance: none;
  border-radius: 3px;
}

html, body, .main {
  height: 100%;
  margin: 0;
}

.title, .clues, .grid {
  padding: 8px;
}

.screen {
  display: none;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 1;
}

.screen > .message {
  position: absolute;
  box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.7);
  top: 50%;
  left: 50%;
  background: var(--background);
  border: 2px solid #777;
  transform: translate(-50%, -50%);
}

.message > div {
  overflow: auto;
  max-height: 80vh;
  padding: 10px;
  width: 100%;
  box-sizing: border-box;
}

.close {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 24px;
}

#custom-link, #custom-error {
  display: none;
}

.modal .message {
  font-size: 2em;
}

.main {
  display: grid;
  grid-template-rows: min-content 1fr min-content;
  align-items: start;
  grid-template-areas:
      'title'
      'clues'
      'grid'
      'keyboard';
  grid-template-rows: min-content 1fr min-content;
}

/* On wide screen devices, put the clues to the right of the grid. */
@media only screen and (min-width: 768px) {
  .main {
    grid-template-areas:
        'title    title'
        'grid     clues'
        'keyboard keyboard';
  }
  html {
    --large-size: calc(max(20px, ((min(100vw / 2, 100vh - 20em) - 16px) / var(--size)) - 4px));
    --small-size: calc(max(16px, ((min(100vw / 2, 100vh) - 16px) / (2 * var(--size) + 1)) - 4px));
  }
}

.clues {
  justify-self: center;
  grid-area: clues;
}

.grid {
  justify-self: center;
  grid-area: grid;
}

.clues .tile,
.clues .empty {
  width: var(--small-size);
  height: var(--small-size);
  display: inline-block;
  border-radius: 2px;
}

.grid {
  display: grid;
  justify-self: center;
}

.grid > div {
  width: var(--large-size);
  height: var(--large-size);
  margin: 2px;
  perspective: 200px;
  perspective-origin: center center;
}

.grid .tile {
  border-radius: 3px;
  transform-style: preserve-3d;
  cursor: pointer;
}

.tile {
  box-sizing: border-box;
  position: relative;
  width: 100%;
  height: 100%;
  margin: 2px;
  border: 2px solid #777;
}

.crossing {
  text-decoration: underline;
}

.tile > div {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 120%;
  font-size: calc(var(--base-size) * .75);
}

.clues .tile > div {
  --base-size: var(--small-size);
}

.grid .tile > div {
  --base-size: var(--large-size);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  position: absolute;
}

.grid.flipped .tile > div:nth-child(1) {
  transform: none;
}

.grid .tile > div:nth-child(1) {
  transform: rotateY(180deg);
}

.grid .tile > div:nth-child(2) {
  /* Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1201471 */
  transform: translateZ(0);
}

.tile > div:nth-child(1) {
  background: var(--black);
}

.tile.yellow > div:nth-child(1) {
  background: var(--yellow);
}

.tile.orange-horiz > div:nth-child(1) {
  background: var(--orange);
}

.tile.orange-vert > div:nth-child(1) {
  background: var(--orange);
}

.tile.green > div:nth-child(1) {
  background: var(--green);
}

.tile.error > div:nth-child(2) {
  color: var(--red-fg);
}

.tile.green-hint > div:nth-child(2) {
  color: var(--green-fg);
}

.key {
  line-height: 300%;
  text-align: center;
  margin: 0.5ch;
  border-radius: 3px;
  background: var(--possible);
  cursor: pointer;
  /* Ensure that all letter keys are given the same contribution to the grid size. */
  min-width: 1em;
}

/* Decrease the text size on Enter and Erase keys to allow for uniform size keys. */
.key.small {
  font-size: small;
  line-height: 380%;
}

.key.black {
  background: var(--black);
}

.key.yellow {
  background: var(--yellow);
}

.key.green {
  background: var(--green);
}

.selected {
  outline: 4px groove #88f;
}

#stats table {
  width: 100%;
}

#stats table td:nth-child(2) {
  position: relative;
  width: 100%;
}

.bar {
  background: var(--green-fg);
  height: 10px;
}
