/*
 * The phone addiction test (/tools/phone-addiction-test), ALE-221's second
 * free tool. Loaded after style.css.
 *
 * ⚠️ **It adds up the answers with CSS counters, and no script.** Each answer
 * is a radio whose `:checked` increments `total` by its own value and
 * `answered` by one; the line at the end prints both with counter(). So the
 * page keeps the site's policy with no script-src at all - unlike the timers,
 * which need one to ring. The line has to come after the ten questions in the
 * document: a counter is read where it is printed, in document order.
 *
 * Immutable in the Caddyfile, so a change here is test-2.css.
 */

.test {
  counter-reset: total answered;
  margin-top: 2rem;
}

.test fieldset {
  border: 0;
  margin: 0 0 2rem;
  padding: 0;
  min-width: 0;
}

.test legend {
  padding: 0;
  margin-bottom: 0.75rem;
  line-height: 1.5;
}

/* The question's number, in the face the app sets figures in. */
.test legend .n {
  font-family: 'Courier Prime', ui-monospace, 'Courier New', monospace;
  font-weight: 700;
  color: var(--ink-muted);
  margin-right: 0.5rem;
}

/* Six answers in a row: 1 to 6, as the scale numbers them. */
.scale {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 0.375rem;
}

.scale label {
  position: relative;
  display: block;
  cursor: pointer;
}

/* The radio stays in the page for the keyboard and screen readers, and the
   box next to it is what is drawn. */
.scale input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.scale .box {
  display: flex;
  align-items: center;
  justify-content: center;
  /* 44px, the site's tap target. */
  min-height: 2.75rem;
  border: 1px solid var(--divider);
  border-radius: 10px;
  background: var(--surface);
  font-family: 'Courier Prime', ui-monospace, 'Courier New', monospace;
  font-weight: 700;
  font-size: 1.0625rem;
  color: var(--ink);
}

.scale input:checked + .box {
  background: var(--accent);
  border-color: var(--accent-edge);
}

.scale input:focus-visible + .box {
  outline: 2px solid var(--accent-text);
  outline-offset: 2px;
}

/* The two labels the scale gives: what 1 means and what 6 means. */
.ends {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 0.375rem;
  font-size: 0.8125rem;
  color: var(--ink-muted);
}

/* --- the counting -------------------------------------------------------- */

.scale input[value='1']:checked { counter-increment: total 1 answered 1; }
.scale input[value='2']:checked { counter-increment: total 2 answered 1; }
.scale input[value='3']:checked { counter-increment: total 3 answered 1; }
.scale input[value='4']:checked { counter-increment: total 4 answered 1; }
.scale input[value='5']:checked { counter-increment: total 5 answered 1; }
.scale input[value='6']:checked { counter-increment: total 6 answered 1; }

.n-total::after {
  content: counter(total);
}

.n-answered::after {
  content: counter(answered);
}

/*
 * The running total stays at the foot of the screen while the questions
 * scroll past, and stops under the last one. Sticky inside .test, so it
 * belongs to the questions and not to the explanation that follows.
 */
.tally {
  position: sticky;
  bottom: 0;
  margin: 0 -1.5rem;
  padding: 0.875rem 1.5rem calc(0.875rem + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--divider);
  text-align: center;
  font-size: 1rem;
}

.tally b {
  font-family: 'Courier Prime', ui-monospace, 'Courier New', monospace;
  font-weight: 700;
  font-size: 1.25rem;
}

/*
 * "Answered 7 of 10" until every question has an answer, then the total
 * alone. Ten chained :has() rather than one nested one, which CSS does not
 * allow. Where :has() is unsupported, the running line simply stays.
 */
.tally .complete {
  display: none;
}

.test:has([name='q1']:checked):has([name='q2']:checked):has([name='q3']:checked):has([name='q4']:checked):has([name='q5']:checked):has([name='q6']:checked):has([name='q7']:checked):has([name='q8']:checked):has([name='q9']:checked):has([name='q10']:checked) .tally .complete {
  display: inline;
}

.test:has([name='q1']:checked):has([name='q2']:checked):has([name='q3']:checked):has([name='q4']:checked):has([name='q5']:checked):has([name='q6']:checked):has([name='q7']:checked):has([name='q8']:checked):has([name='q9']:checked):has([name='q10']:checked) .tally .running {
  display: none;
}

@media (max-width: 30rem) {
  .tally {
    margin: 0 -1.25rem;
    padding-inline: 1.25rem;
  }
}
