/* Keyline: pen on a notebook page. Thin ink, no panels, notes in the margin. */

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  background-color: var(--ak-bg);
  background-image: var(--ak-bg-art);
  background-size: var(--ak-bg-size);
  color: var(--ak-ink);
  font-family: var(--ak-font-body);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

body.has-page {
  background-image: var(--ak-backdrop-layer);
  background-size: cover;
  background-position: center top;
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
  cursor: pointer;
}

button:focus-visible,
.board.is-keys:focus {
  outline: 3px solid var(--ak-focus);
  outline-offset: 3px;
}

/* ---------- page layout: upright, board over the margin notes ---------- */

.play {
  --inset-x: calc(env(safe-area-inset-left) + env(safe-area-inset-right));
  --inset-y: calc(env(safe-area-inset-top) + env(safe-area-inset-bottom));
  --board: min(calc(100dvw - 20px - var(--inset-x)), calc(100dvh - 362px - var(--inset-y)), 620px);
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: minmax(0, min(100%, 620px));
  grid-template-rows: auto auto minmax(0, 1fr);
  grid-template-areas:
    "head"
    "board"
    "margin";
  justify-content: center;
  align-content: start;
  row-gap: 4px;
  padding:
    max(8px, env(safe-area-inset-top))
    max(10px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
}

.play[hidden] {
  display: none;
}

.head {
  grid-area: head;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 50px;
  padding: 0 60px;
}

.logo {
  display: block;
  height: 44px;
  width: auto;
  max-width: 100%;
}

.board-wrap {
  grid-area: board;
  justify-self: center;
  width: var(--board);
  height: var(--board);
  touch-action: manipulation;
}

/* Only the board itself takes taps; nothing drawn on it can swallow a press. */
.board * {
  pointer-events: none;
}

.board {
  display: block;
  outline: none;
  width: 100%;
  height: 100%;
  overflow: visible;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}

/* The margin: a single red rule above the notes when upright. */
.margin {
  grid-area: margin;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  padding-top: 10px;
  border-top: 2px solid var(--ak-accent);
  font-size: 18px;
  line-height: 1.3;
}

.goal {
  margin: 0;
  font-weight: 700;
  color: var(--ak-ink);
}

.say {
  margin: 0;
  min-height: 1.3em;
  color: var(--ak-ink-muted);
  font-style: italic;
}

/* While a teaching or hint note is up, the idle reminder steps aside. */
.is-teaching .say.is-idle,
.is-hinting .say.is-idle {
  display: none;
}

/* Once solved, the verdict and its lap time take the goal and tap key's place. */
.is-solved .goal,
.is-solved .key {
  display: none;
}

/* The tap key stays on screen: what one, two and three taps make. */
.key {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 16px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 18px;
  font-weight: 700;
  color: var(--ak-ink);
}

.key li {
  display: inline-flex;
  align-items: center;
}

.key-line,
.key-x,
.key-blank {
  display: inline-block;
  flex: none;
  margin-right: 6px;
}

.key-line {
  width: 30px;
  height: 7px;
  background: url("./art/pen-line.png") center / 100% 100% no-repeat;
}

/* Only the pencil X from x-mark.png (146px to 238px across), not the bars beside it. */
.key-x {
  width: 20px;
  height: 21px;
  background: url("./art/x-mark.png") no-repeat;
  background-size: 83.5px 21px;
  background-position: -31.7px 0;
}

.key-blank {
  width: 26px;
  height: 0;
  border-top: 2px dotted var(--ak-ink-muted);
}

/* A hint is the more specific pointer; the guide's caption and ring return once it is done. */
.is-hinting .teach:not(.hint-note),
.is-hinting .board .notes .pointer {
  display: none;
}

/* Narrow phones: the pictures name the marks and the words give the tap counts. */
@media (max-width: 379px) {
  .key-word {
    display: none;
  }
}

/* Short upright phones: tighter margin spacing so the buttons stay on screen. */
@media (orientation: portrait) and (max-height: 700px) {
  .head {
    min-height: 48px;
  }

  .margin {
    gap: 5px;
    padding-top: 6px;
  }

}

/* On short screens the hint note states the taps itself, so the key steps aside while it is
   up, and the standing goal line with it. */
@media (max-height: 700px) {
  .is-hinting .key,
  .is-hinting .goal {
    display: none;
  }
}

.hint-note[hidden] {
  display: none;
}

/* The step to make leads; the rule behind it follows in one smaller line. */
.hint-note .hint-why {
  display: block;
  font-size: 16px;
  font-weight: 400;
  color: var(--ak-ink-muted);
}

.hint-note {
  display: block;
}

.hint-note .hint-action {
  display: block;
}

.say::before {
  content: "\2014\00a0";
  font-style: normal;
}

/* Teaching sits on a solid backing so it reads over the graph paper. */
.teach {
  margin: 0;
  padding: 8px 12px;
  background: var(--ak-bg);
  border: 2px solid var(--ak-accent-2);
  border-radius: 3px 14px 4px 12px / 12px 4px 14px 3px;
  color: var(--ak-ink);
  font-size: 19px;
  font-weight: 700;
  line-height: 1.3;
}

.teach[hidden] {
  display: none;
}

/* ---------- buttons: words boxed in ink, the check is the rubber stamp ---------- */

.tools,
.verdict-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

/* The patrol log: this lap's clock, the best clean lap and the clean run, in small pen. */
.log {
  margin: auto 0 0;
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ak-ink-muted);
}

.log:empty,
.is-solved .log {
  display: none;
}

.tools {
  flex-wrap: nowrap;
}

.log:empty + .tools {
  margin-top: auto;
}

.tools[hidden] {
  display: none;
}

.pen-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 48px;
  min-width: 48px;
  padding: 0 14px;
  background: transparent;
  border: 2px solid var(--ak-ink);
  border-radius: 14px 4px 12px 5px / 5px 12px 4px 14px;
  color: var(--ak-ink);
  font-size: 18px;
  font-weight: 700;
}

.pen-btn:disabled {
  color: var(--ak-ink-muted);
  border-style: dashed;
  cursor: default;
}

.pen-btn:active:not(:disabled) {
  transform: translate(1px, 1px);
}

.check-btn {
  flex: 1 1 auto;
  min-height: 52px;
  min-width: 0;
  padding: 0 12px;
  background: var(--ak-btn-fill);
  border: 2px solid var(--ak-ink);
  border-radius: 2px 7px 3px 6px;
  color: var(--ak-btn-ink);
  font-family: var(--ak-font-display);
  font-weight: var(--ak-title-weight);
  font-size: 21px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.check-btn:active {
  background: var(--ak-btn-active);
  transform: translate(1px, 1px);
}

@media (hover: hover) {
  .pen-btn:hover:not(:disabled) {
    border-color: var(--ak-accent);
  }

  .check-btn:hover {
    background: var(--ak-btn-hover);
  }
}

/* ---------- mute: top left, the ? lives top right ---------- */

.mute {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  z-index: 5;
  width: 48px;
  height: 48px;
  padding: 8px;
  background: var(--ak-bg);
  border: 2px solid var(--ak-ink);
  border-radius: 50%;
}

.mute svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: var(--ak-ink);
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.mute .mute-strike {
  display: none;
}

.mute.is-muted .mute-waves {
  display: none;
}

.mute.is-muted .mute-strike {
  display: inline;
  stroke: var(--ak-accent);
}

/* ---------- the board: dots, faint dotted gaps, tags, pen and pencil ---------- */

.guides line {
  stroke: var(--ak-ink-muted);
  stroke-width: 0.03;
  stroke-dasharray: 0.04 0.07;
  stroke-linecap: round;
  opacity: 0.8;
}

.dots circle {
  fill: var(--ak-ink);
}

.tag-num {
  font-family: var(--ak-font-display);
  font-size: 58px;
  text-anchor: middle;
  fill: var(--ak-accent);
  stroke: var(--ak-ink);
  stroke-width: 5px;
  stroke-linejoin: round;
  paint-order: stroke;
}

.edge .blot {
  fill: var(--ak-ink);
  opacity: 0;
}

.wrong ellipse {
  fill: none;
  stroke: var(--ak-accent);
  stroke-width: 0.05;
  stroke-linecap: round;
}

/* A hint rings the number or dot it reasons from in blue pen, the "circled" one its words name. */
.because ellipse {
  fill: none;
  stroke: var(--ak-accent-2);
  stroke-width: 0.06;
  stroke-linecap: round;
}

.because .second {
  display: none;
}

.wrong .second {
  stroke-width: 0.025;
  opacity: 0.7;
}

/* The mark a hint asks for, drawn in full so line or X reads at a glance, but paler than a
   mark actually made. */
.ghost {
  opacity: 0.55;
  pointer-events: none;
}

.ghost .xmark {
  opacity: 0.7;
}

.pointer .ring {
  fill: none;
  stroke: var(--ak-accent-2);
  stroke-width: 0.05;
}

.pointer .arrow line,
.pointer .arrow path {
  fill: none;
  stroke: var(--ak-accent-2);
  stroke-width: 0.065;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* The picked gap: a blue highlighter stroke under the finger, kept on the last gap tapped. */
.pick-wash,
.hint-wash {
  fill: none;
  stroke: var(--ak-accent-2);
  stroke-width: 0.3;
  stroke-linecap: round;
}

.pick-wash {
  opacity: 0;
}

.board.is-picking .pick-wash,
.board.is-keys:focus .pick-wash {
  opacity: 0.32;
}

.board.is-pressing .pick-wash {
  opacity: 0.5;
}

/* A hinted gap: the same highlighter, with the asked-for mark drawn faintly on top. */
.hint-wash {
  opacity: 0.28;
}

/* A gap the hint rules out sits under a grey wash, so the blue ones read as the loop to draw. */
.hint-wash.is-out {
  stroke: var(--ak-ink-muted);
  opacity: 0.18;
}

/* The keyboard pen: a box that can also rest between gaps. */
.cursor rect {
  fill: none;
  stroke: none;
}

.board.is-keys:focus .cursor rect {
  stroke: var(--ak-accent-2);
  stroke-width: 0.04;
}

.retrace {
  fill: none;
  stroke: var(--ak-btn-active);
  stroke-width: 0.055;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- the margin verdict and its stamp ---------- */

.verdict {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-areas:
    "stamp text"
    "log log"
    "acts acts";
  column-gap: 12px;
  row-gap: 8px;
  align-items: center;
}

.verdict[hidden] {
  display: none;
}

.verdict:not(.is-solved) {
  grid-template-areas:
    "text text"
    "log log"
    "acts acts";
}

.verdict-text {
  grid-area: text;
  margin: 0;
  font-weight: 700;
  color: var(--ak-ink);
}

.verdict-log {
  grid-area: log;
  margin: 0;
  font-size: 17px;
  color: var(--ak-ink);
}

.verdict-log[hidden] {
  display: none;
}

.verdict-actions {
  grid-area: acts;
}

.stamp {
  grid-area: stamp;
  width: 128px;
  height: auto;
  transform: rotate(-9deg);
  mix-blend-mode: multiply;
}

.stamp[hidden] {
  display: none;
}

.stamp-rim {
  fill: none;
  stroke: var(--ak-accent);
  stroke-width: 5;
  stroke-linejoin: round;
}

.stamp-rim.inner {
  stroke-width: 2;
}

.stamp text {
  font-family: var(--ak-font-display);
  font-size: 34px;
  letter-spacing: 2px;
  fill: var(--ak-accent);
}

/* ---------- sideways: board on the left, the margin on the right ---------- */

@media (orientation: landscape) {
  .play {
    /* The left gutter keeps the board clear of the mute button in the corner. */
    --gutter: 56px;
    --board: min(calc(100dvh - 16px - var(--inset-y)), calc(100dvw - 330px - var(--gutter) - var(--inset-x)), 640px);
    grid-template-columns: var(--board) minmax(240px, 400px);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "board head"
      "board margin";
    justify-content: center;
    align-content: center;
    column-gap: 18px;
    row-gap: 6px;
    padding-left: calc(max(10px, env(safe-area-inset-left)) + var(--gutter));
  }

  .head {
    justify-content: flex-start;
    padding: 0 60px 0 18px;
  }

  .board-wrap {
    align-self: center;
  }

  /* The notebook margin: a double red rule down the left of the notes. */
  .margin {
    position: relative;
    align-self: start;
    padding: 4px 0 0 18px;
    border-top: 0;
    border-left: 2px solid var(--ak-accent);
  }

  .margin::before {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 3px;
    border-left: 1px solid var(--ak-accent);
  }

  .teach {
    padding: 6px 10px;
    font-size: 17px;
  }
}

/* Short sideways screens: while a teaching or hint box is up it carries the instruction,
   so the standing goal line steps aside and the board's next step stays easy to find. */
@media (orientation: landscape) and (max-height: 600px) {
  .is-teaching .goal,
  .is-hinting .goal {
    display: none;
  }

  .margin {
    gap: 6px;
    font-size: 17px;
  }

  .key {
    font-size: 16px;
  }
}

@media (orientation: landscape) and (min-height: 600px) {
  .play {
    --board: min(calc(100dvh - 48px - var(--inset-y)), calc(100dvw - 480px - var(--inset-x)), 640px);
  }
}

/* ---------- motion ---------- */

@media (prefers-reduced-motion: no-preference) {
  .edge.ink-in .pen {
    transform-box: fill-box;
    transform-origin: 10% 50%;
    animation: ink-in 170ms cubic-bezier(0.3, 0.7, 0.4, 1) both;
  }

  .edge.ink-in .blot {
    transform-box: fill-box;
    transform-origin: center;
    animation: blot 420ms 150ms ease-out both;
  }

  .edge.x-in .xmark {
    transform-box: fill-box;
    transform-origin: center;
    animation: x-in 160ms ease-out both;
  }

  .pointer .ring {
    transform-box: fill-box;
    transform-origin: center;
    animation: ring 1.1s ease-in-out infinite;
  }

  .board .pick-wash.flash {
    animation: pick-flash 420ms ease-out;
  }

  @keyframes pick-flash {
    0% { opacity: 0.75; stroke-width: 0.44; }
    100% { opacity: 0.32; stroke-width: 0.3; }
  }

  .wrong {
    animation: scrawl 220ms ease-out both;
    transform-box: fill-box;
    transform-origin: center;
  }

  .edge.settle .pen,
  .tag.settle {
    transform-box: fill-box;
    transform-origin: center;
    animation: settle 300ms var(--delay, 0ms) ease-out both;
  }

  .retrace {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation: retrace 1100ms linear forwards;
  }

  .verdict:not([hidden]) {
    animation: note-in 240ms ease-out both;
  }

  .verdict.is-solved .stamp {
    animation: stamp 420ms cubic-bezier(0.2, 1.4, 0.4, 1) both;
  }

  @keyframes ink-in {
    from { transform: scaleX(0.08); }
    to { transform: scaleX(1); }
  }

  @keyframes blot {
    0% { opacity: 0; transform: scale(0.3); }
    35% { opacity: 0.9; transform: scale(1.25); }
    100% { opacity: 0; transform: scale(0.9); }
  }

  @keyframes x-in {
    from { transform: scale(0.4) rotate(-20deg); }
    to { transform: scale(1) rotate(0deg); }
  }

  @keyframes ring {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.05); opacity: 0.6; }
  }

  @keyframes scrawl {
    from { transform: scale(1.3); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
  }

  @keyframes settle {
    0% { transform: scale(1); }
    45% { transform: scale(1.14); }
    100% { transform: scale(1); }
  }

  @keyframes retrace {
    to { stroke-dashoffset: 0; }
  }

  @keyframes note-in {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: none; }
  }

  @keyframes stamp {
    0% { transform: rotate(-16deg) scale(2.1); opacity: 0; }
    55% { transform: rotate(-7deg) scale(0.92); opacity: 1; }
    75% { transform: rotate(-10.5deg) scale(1.03); }
    100% { transform: rotate(-9deg) scale(1); }
  }
}

/* ---------- start screen: pen doodles in place of the stock badges ---------- */

#activity-start .ak-section .ak-badge {
  width: 32px;
  height: 32px;
  font-size: 0;
  color: transparent;
  background: no-repeat center / contain;
}

/* A pen loop through five dots. */
#activity-start .ak-goal .ak-badge {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' fill='none' stroke='%230A0A0A' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 7 L18 6.5 L18.5 13 L26 13.5 L25.5 25.5 L6.5 26 Z' stroke='%23E8000A'/%3E%3Ccircle cx='6' cy='7' r='1.6' fill='%230A0A0A' stroke='none'/%3E%3Ccircle cx='18' cy='6.5' r='1.6' fill='%230A0A0A' stroke='none'/%3E%3Ccircle cx='26' cy='13.5' r='1.6' fill='%230A0A0A' stroke='none'/%3E%3Ccircle cx='25.5' cy='25.5' r='1.6' fill='%230A0A0A' stroke='none'/%3E%3Ccircle cx='6.5' cy='26' r='1.6' fill='%230A0A0A' stroke='none'/%3E%3C/svg%3E");
}

/* A ballpoint pen with a red scribble. */
#activity-start .ak-controls-block .ak-badge {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' fill='none' stroke='%230A0A0A' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 4.5 L27.5 11 L12 26.5 L5 28 L6.5 21 Z'/%3E%3Cpath d='M18 7.5 L24.5 14'/%3E%3Cpath d='M6.5 21 L12 26.5'/%3E%3Cpath d='M3 30.5 C8 29 10 31 14 29.5' stroke='%23E8000A'/%3E%3C/svg%3E");
}

/* The night guard's torch, as in the page doodle. */
#activity-start .ak-tip .ak-badge {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' fill='none' stroke='%230A0A0A' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 18 L20 18 L18.5 29 L13.5 29 Z'/%3E%3Cpath d='M10 12 C10 9 22 9 22 12 L20 18 L12 18 Z'/%3E%3Cpath d='M16 2.5 L16 6 M7 5 L9.5 7.5 M25 5 L22.5 7.5 M4 11 L7 11.5 M28 11 L25 11.5' stroke='%23E8000A'/%3E%3C/svg%3E");
}
