* {
  box-sizing: border-box;
}

:root {
  --bg: #f6f7f8;
  --fg: #181a1f;
  --muted: #68707a;
  --accent: #3f6f9f;
  --surface: #ffffff;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1rem;
  font-family: system-ui, sans-serif;
  color: var(--fg);
  background: var(--bg);
}

main {
  width: min(92vw, 34rem);
  text-align: center;
}

.history {
  position: fixed;
  top: 1rem;
  bottom: 1rem;
  left: 1rem;
  width: 10rem;
  padding-left: 0.25rem;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.history h2 {
  margin: 0 0 0.75rem;
  font: inherit;
  color: var(--muted);
}

.history ol {
  margin: 0;
  padding-left: 0;
  list-style-position: inside;
}

.history li + li {
  margin-top: 0.35rem;
}

.history li {
  cursor: default;
}

.history li:focus {
  outline: 1px solid var(--accent);
  outline-offset: 2px;
}

.history-preview {
  position: fixed;
  left: 12rem;
  z-index: 2;
  width: min(28rem, calc(100vw - 13rem));
  max-height: min(16rem, 70vh);
  padding: 0.75rem;
  overflow: auto;
  border: 1px solid var(--fg);
  background: var(--surface);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9rem;
  visibility: hidden;
}

.history-preview.is-visible {
  visibility: visible;
}

.history-preview p {
  margin: 0 0 0.5rem;
}

.history-preview ol {
  margin: 0;
  padding-left: 0;
  list-style: none;
  counter-reset: solution;
}

.history-preview li + li {
  margin-top: 0.35rem;
}

.history-preview li {
  counter-increment: solution;
  display: grid;
  grid-template-columns: 2ch 1fr;
  column-gap: 0.5rem;
}

.history-preview li::before {
  content: counter(solution) ".";
}

.history-preview .more {
  margin: 0.5rem 0 0;
  color: var(--muted);
}

.settings-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 4;
  min-width: 3rem;
  width: 3rem;
  height: 3rem;
  padding: 0;
  border: 0;
  font-size: 1.6rem;
  background: transparent;
}

.settings-toggle[aria-expanded="true"] {
  visibility: hidden;
}

.settings-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  width: min(20rem, 88vw);
  padding: 1rem;
  border-left: 1px solid var(--fg);
  background: var(--surface);
  transform: translateX(100%);
  transition: transform 140ms ease;
}

.settings-drawer.is-open {
  transform: translateX(0);
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.settings-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.icon-button {
  min-width: 3rem;
  width: 3rem;
  height: 3rem;
  padding: 0;
  border: 0;
  font-size: 1.6rem;
  background: transparent;
}

fieldset {
  margin: 0 0 1rem;
  padding: 0;
  border: 0;
}

legend {
  margin-bottom: 0.5rem;
  color: var(--muted);
}

fieldset label {
  display: block;
  margin-bottom: 0.5rem;
}

.custom-colors {
  display: grid;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.custom-colors[hidden] {
  display: none;
}

.custom-colors label {
  display: grid;
  grid-template-columns: 1fr 2.5rem 6rem;
  align-items: center;
  gap: 0.5rem;
}

.custom-colors input[type="color"] {
  width: 2.5rem;
  height: 2rem;
  padding: 0;
  border: 1px solid var(--fg);
  background: var(--surface);
}

.custom-colors input[type="text"] {
  width: 100%;
  padding: 0.4rem;
  border: 1px solid var(--fg);
  border-radius: 0;
  font: inherit;
  color: var(--fg);
  background: var(--surface);
}

.custom-colors input.is-invalid {
  border-color: var(--accent);
  outline: 2px solid var(--accent);
}

h1 {
  margin: 0 0 1.5rem;
  font-size: 2rem;
  font-weight: 600;
}

#status {
  min-height: 1.5rem;
  margin: 0 0 1rem;
  color: var(--muted);
}

.cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.card {
  min-height: 5rem;
  display: grid;
  place-items: center;
  border: 1px solid var(--fg);
  border-radius: 0;
  font-size: clamp(1.5rem, 8vw, 2.5rem);
  font-weight: 600;
  background: var(--surface);
}

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

button {
  min-width: 7rem;
  padding: 0.7rem 1rem;
  border: 1px solid var(--fg);
  border-radius: 0;
  font: inherit;
  color: var(--fg);
  background: var(--surface);
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: var(--accent);
  color: var(--surface);
}

button:disabled {
  color: var(--muted);
  border-color: var(--muted);
  cursor: default;
}

.answer {
  height: 14rem;
  margin: 1.25rem 0 0;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-align: left;
  visibility: hidden;
}

.answer.is-visible {
  visibility: visible;
}

.answer p {
  margin: 0 0 0.5rem;
}

.answer ol {
  margin: 0;
  padding-left: 0;
  list-style: none;
  counter-reset: solution;
}

.answer li + li {
  margin-top: 0.35rem;
}

.answer li {
  counter-increment: solution;
  display: grid;
  grid-template-columns: 3ch 1fr;
  column-gap: 0.5rem;
}

.answer li::before {
  content: counter(solution) ".";
}

@media (max-width: 60rem) {
  body {
    min-height: auto;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    padding: 1rem;
  }

  main {
    order: 1;
    width: min(100%, 34rem);
    margin: 0 auto;
    padding-top: 2.75rem;
  }

  .history {
    order: 2;
    position: static;
    width: min(100%, 34rem);
    max-height: 8rem;
    margin: 1.5rem auto 0;
  }

  .settings-toggle {
    top: 0.5rem;
    right: 0.5rem;
  }

  .settings-drawer {
    width: min(22rem, 100vw);
    overflow: auto;
  }

  .history-preview {
    left: 1rem;
    right: 1rem;
    width: auto;
    max-height: min(14rem, 55vh);
  }
}

@media (max-width: 30rem) {
  body {
    padding: 0.75rem;
  }

  main {
    padding-top: 2.5rem;
  }

  h1 {
    margin-bottom: 1rem;
  }

  .cards {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }

  .card {
    min-height: 4.5rem;
    font-size: 2rem;
  }

  .actions {
    gap: 0.5rem;
  }

  button {
    min-width: 0;
    flex: 1;
    padding: 0.65rem 0.75rem;
  }

  .answer {
    height: 11rem;
    font-size: 0.9rem;
  }

  .custom-colors label {
    grid-template-columns: 1fr 2.5rem;
  }

  .custom-colors input[type="text"] {
    grid-column: 1 / -1;
  }
}
