/* 5 Whys tool. Colour and spacing tokens come from home.css's :root. */
:root {
  --why-line: 2px solid var(--primary-blue);
  --why-gap: 20px; /* each half of the connector between a why and its answers; room for "+ Another possible cause" */
  --why-card-width: 250px;
  --why-problem-width: 460px;
}

/* Header height comes from home.css's .header--compact class. */

.why-tool {
  max-width: 1440px;
  text-align: left;
}

.why-tool .section-title {
  margin-bottom: var(--space-md);
}

.why-notice {
  margin: 0 0 var(--space-sm);
  padding: var(--space-xs) var(--space-sm);
  border-left: 4px solid var(--primary-blue);
  border-radius: 4px;
  background: var(--blue-tint);
  font-size: 1rem;
}

.why-notice a,
.why-instructions a {
  color: var(--primary-blue);
  font-weight: 600;
}

.why-instructions {
  margin: var(--space-md) 0;
  line-height: 1.5;
}

.why-instructions ul {
  padding-left: 1.25em;
}

.why-instructions li {
  margin-bottom: var(--space-3xs);
}

/* Sections, tables, boxes and buttons share tools.css with the fishbone. */

.why-tool .tool-section h3 {
  margin-top: var(--space-md);
  font-size: 1.125rem;
}

.why-field-label {
  display: block;
  margin-bottom: var(--space-3xs);
  font-weight: 600;
}

.why-explainer {
  margin-bottom: var(--space-xs);
  color: var(--muted);
  font-size: 0.95rem;
}

.why-problem-input,
.why-fact-input,
.why-answer {
  display: block;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--light-gray);
  border-radius: 4px;
  background: var(--white);
  color: var(--black);
  font: inherit;
  line-height: 1.35;
  resize: none;
  overflow: hidden;
}

.why-problem-input {
  font-size: 1rem;
}

.why-fact-input {
  font-size: 0.95rem;
}

.why-problem-input:focus,
.why-fact-input:focus,
.why-answer:focus {
  border-color: var(--primary-blue);
  outline: 0;
  box-shadow: 0 0 0 2px rgba(4, 75, 166, 0.16);
}

/* Is / Is Not: a row label, then what is and what is not */
.why-facts-grid {
  display: grid;
  grid-template-columns: max-content 1fr 1fr;
  gap: var(--space-2xs) var(--space-xs);
  align-items: start;
}

.why-facts-head {
  font-weight: 600;
}

.why-facts-label {
  padding-top: 6px;
  font-weight: 600;
}

/* =========================================================
   The chain, drawn as a tree under the problem
   ========================================================= */

.why-canvas-wrapper {
  position: relative;
  padding-bottom: 84px; /* room for the logo below the tree */
  border: 1px solid var(--light-gray);
  border-radius: 6px;
  background: var(--white);
}

.why-canvas {
  overflow-x: auto;
  padding: var(--space-md) 0 var(--space-sm);
}

.why-canvas-logo {
  position: absolute;
  left: 18px;
  bottom: 6px;
  width: 92px;
  height: auto;
  opacity: 0.9;
  pointer-events: none;
}

.why-tree {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: max-content;
  min-width: 100%;
  padding: 0 var(--space-xs);
}

.why-problem-card {
  width: var(--why-problem-width);
  padding: var(--space-2xs) var(--space-xs);
  border: 2px solid var(--primary-red);
  border-radius: 8px;
  background: var(--white);
  text-align: center;
}

.why-problem-text {
  font-size: 1rem;
  overflow-wrap: anywhere;
}

.why-problem-text.is-empty {
  color: var(--muted);
  font-style: italic;
}

.why-children {
  position: relative;
  display: flex;
  justify-content: center;
  margin: 0;
  padding: var(--why-gap) 0 0;
  list-style: none;
}

/* From the card above down to the bar across its answers */
.why-children::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  height: var(--why-gap);
  border-left: var(--why-line);
}

.why-branch {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--why-gap) var(--space-2xs) 0;
}

/* Each answer's half of the bar, and the drop from the bar to its card */
.why-branch::before,
.why-branch::after {
  content: '';
  position: absolute;
  top: 0;
  width: 50%;
  height: var(--why-gap);
  border-top: var(--why-line);
}

.why-branch::before {
  right: 50%;
}

.why-branch::after {
  left: 50%;
  border-left: var(--why-line);
}

.why-branch:first-child::before,
.why-branch:last-child::after {
  border-top: 0;
}

.why-branch:only-child::before {
  border: 0;
}

.why-card {
  position: relative;
  width: var(--why-card-width);
  padding: var(--space-2xs) var(--space-2xs) var(--space-xs);
  border: 2px dashed var(--primary-blue);
  border-radius: 8px;
  background: var(--white);
  text-align: left;
}

/* Arrowhead where the line meets the card */
.why-card::before {
  content: '';
  position: absolute;
  top: -9px;
  left: 50%;
  margin-left: -6px;
  border-top: 8px solid var(--primary-blue);
  border-right: 6px solid transparent;
  border-left: 6px solid transparent;
}

.why-card.is-root {
  border-color: var(--primary-red);
  background: var(--red-tint);
}

/* Results: confirmed is drawn solid; ruled out goes grey with everything asked below it */
.why-card--confirmed {
  border-style: solid;
}

.why-card--confirmed .why-card-status {
  color: var(--primary-blue);
  font-weight: 600;
}

.why-card--ruled-out {
  border-color: var(--light-gray);
}

.why-card--ruled-out .why-card-status {
  font-weight: 600;
}

.why-branch.is-ruled-out .why-card {
  opacity: 0.55;
}

.why-branch.is-ruled-out .why-card::before {
  border-top-color: var(--light-gray);
}

.why-card-no {
  color: var(--muted);
}

/* The copy of the canvas the Excel picture is taken from: answers as plain text */
.why-answer--print {
  padding: 2px 0;
  border-color: transparent;
  background: transparent;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.why-card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-3xs) var(--space-2xs);
  margin-bottom: var(--space-3xs);
  font-size: 0.85rem;
}

.why-card-level {
  color: var(--primary-blue);
  font-weight: 700;
}

.why-problem-card .why-card-level {
  color: var(--primary-red);
}

.why-card-tag {
  display: inline-block;
  margin-bottom: var(--space-3xs);
  padding: 0 6px;
  border: 1px solid var(--primary-blue);
  border-radius: 10px;
  color: var(--primary-blue);
  font-size: 0.8rem;
  line-height: 1.5;
}

.why-card-status {
  margin-left: auto;
  color: var(--muted);
}

.why-card.is-root .why-card-status {
  color: var(--primary-red);
  font-weight: 600;
}

.why-card-remove {
  width: 20px;
  height: 20px;
  padding: 0;
  border: 1px solid var(--light-gray);
  border-radius: 50%;
  background: var(--white);
  color: var(--light-gray);
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.why-card-remove:hover {
  border-color: var(--primary-red);
  background: var(--red-tint);
  color: var(--primary-red);
}

.why-answer {
  font-size: 0.95rem;
}

.why-hints:empty {
  display: none;
}

.why-hint {
  margin-top: var(--space-2xs);
  padding: var(--space-3xs) var(--space-2xs);
  border-left: 3px solid var(--primary-red);
  background: var(--bg-light);
  font-size: 0.85rem;
  line-height: 1.4;
}

.why-hint-keep {
  display: block;
  margin-top: 2px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--primary-blue);
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.why-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: var(--space-2xs);
}

.why-action {
  padding: 4px 10px;
  border: 1px solid var(--primary-blue);
  border-radius: 4px;
  background: var(--primary-blue);
  color: var(--white);
  font: inherit;
  font-size: 0.85rem;
  line-height: 1.3;
  cursor: pointer;
}

.why-action--quiet {
  background: var(--white);
  color: var(--primary-blue);
}

.why-action--root {
  border-color: var(--primary-red);
  background: var(--white);
  color: var(--primary-red);
}

.why-action:hover {
  opacity: 0.9;
}

.why-action--quiet:hover {
  background: var(--blue-tint);
  opacity: 1;
}

.why-action--root:hover {
  background: var(--red-tint);
  opacity: 1;
}

.why-first {
  margin-top: var(--space-sm);
  padding: 6px 18px;
  font-size: 1rem;
}

/*
  "+ Another possible cause" (W12): one per why, at its last answer. Wide
  screens show it above that answer, beside the line coming into it, in the
  space the connector leaves (out of the row, so the tree keeps its width);
  phones show the copy straight under the last answer.
*/
.why-add-other {
  padding: 4px 10px;
  border: 1px dashed var(--primary-blue);
  border-radius: 4px;
  background: var(--white);
  color: var(--primary-blue);
  font: inherit;
  font-size: 0.85rem;
  line-height: 1.3;
  white-space: nowrap;
  cursor: pointer;
}

.why-add-other:hover {
  border-style: solid;
  background: var(--blue-tint);
}

/* Right of the line into the card, inside the card's right half, in the two connector gaps above it */
.why-add-other--wide {
  position: absolute;
  top: calc(2px - var(--why-gap));
  left: calc(50% + 10px);
  z-index: 1;
  max-width: calc(var(--why-card-width) / 2 - 14px);
  padding: 2px 8px;
  font-size: 0.78rem;
  line-height: 1.15;
  text-align: left;
  white-space: normal;
}

.why-add-other--narrow {
  display: none;
}

/* =========================================================
   Check table and temporary actions (table look in tools.css)
   ========================================================= */

.why-checks-table td:nth-child(1),
.why-checks-table td:nth-child(2) {
  white-space: nowrap;
}

.why-checks-table th:nth-child(3) { width: 19%; }
.why-checks-table th:nth-child(4) { width: 17%; }
.why-checks-table th:nth-child(5) { width: 11%; }
.why-checks-table th:nth-child(6) { width: 12%; }
.why-checks-table th:nth-child(7) { width: 12%; }
.why-checks-table th:nth-child(8) { width: 20%; }

.why-root-badge {
  display: block;
  color: var(--primary-red);
  font-size: 0.8rem;
  font-weight: 600;
}

.why-actions-table th:nth-child(1) { width: 12%; }
.why-actions-table th:nth-child(3) { width: 13%; }
.why-actions-table th:nth-child(4) { width: 13%; }
.why-actions-table th:nth-child(5) { width: 12%; }
.why-actions-table th:nth-child(6) { width: 18%; }
.why-actions-table th:nth-child(7) { width: 8%; }
.why-actions-table th:nth-child(8) { width: 36px; }

.why-actions-table td:last-child {
  text-align: center;
  vertical-align: middle;
}

/* A permanent fix whose cause isn't confirmed yet (W14) */
.why-action-warning {
  margin-top: 2px;
  color: var(--primary-red);
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.3;
}

.why-action-warning:empty,
.why-weak-fix:empty {
  display: none;
}

.why-weak-fix {
  margin-top: var(--space-xs);
}

/* A row below a ruled-out answer (T4): its own result is kept, but it is set aside */
.why-set-aside {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.8rem;
  font-style: italic;
  font-weight: 600;
  line-height: 1.3;
}

/* =========================================================
   The proof path (T3): each root cause read from the bottom up
   ========================================================= */

.why-proof {
  margin-top: var(--space-md);
}

.why-proof h3 {
  margin: 0 0 var(--space-2xs);
  text-align: center;
}

.why-proof > .why-empty {
  text-align: center;
}

.why-proof-summary {
  margin-bottom: var(--space-xs);
  font-weight: 600;
  text-align: center;
}

.why-proof-summary--warning {
  color: var(--primary-red);
}

.why-proof-path {
  max-width: 820px;
  margin: 0 auto var(--space-sm);
  padding: var(--space-xs) var(--space-sm);
  border: 1px solid var(--light-gray);
  border-left: 4px solid var(--primary-red);
  border-radius: 6px;
}

.why-proof-path.is-proven {
  border-left-color: var(--primary-blue);
}

.why-proof-title {
  margin-bottom: var(--space-2xs);
  color: var(--primary-blue);
  font-weight: 700;
}

.why-proof-how {
  color: var(--muted);
  font-weight: 400;
}

.why-proof-steps {
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Each step: its mark, its number and its text, in three columns */
.why-proof-step,
.why-proof-problem {
  display: grid;
  grid-template-columns: 1.5em 2.5em 1fr;
  gap: var(--space-2xs);
  align-items: baseline;
  line-height: 1.4;
}

.why-proof-mark {
  font-weight: 700;
  text-align: center;
}

.why-proof-step--confirmed .why-proof-mark {
  color: var(--primary-blue);
}

.why-proof-step--ruled-out .why-proof-mark,
.why-proof-step--suspected .why-proof-mark,
.why-proof-step--not-conclusive .why-proof-mark {
  color: var(--primary-red);
}

.why-proof-no {
  color: var(--muted);
  font-weight: 600;
}

.why-proof-text {
  overflow-wrap: anywhere;
}

.why-proof-step:first-child .why-proof-text {
  color: var(--primary-red);
  font-weight: 600;
}

.why-proof-therefore {
  padding-left: calc(4rem + 2 * var(--space-2xs)); /* under the text column: the mark and number columns are 1.5em + 2.5em of a 1rem step */
  color: var(--muted);
  font-size: 0.85rem;
  font-style: italic;
}

.why-proof-problem {
  font-weight: 600;
}

.why-proof-conclusion {
  margin-top: var(--space-2xs);
  font-weight: 600;
}

.why-proof-path:not(.is-proven) .why-proof-conclusion {
  color: var(--primary-red);
}

.why-proof-path.is-proven .why-proof-conclusion {
  color: var(--primary-blue);
}

/* A chain with a ruled-out answer can't explain the problem: one grey line */
.why-proof-collapsed {
  max-width: 820px;
  margin: 0 auto var(--space-2xs);
  padding-left: 1.5em;
  color: var(--muted);
  line-height: 1.4;
  text-indent: -1.5em;
}

.why-proof-collapsed .why-proof-mark {
  display: inline-block;
  width: 1.5em;
  text-indent: 0;
}

.why-proof-legend {
  margin-top: var(--space-2xs);
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  white-space: pre-wrap;
}

/* =========================================================
   Opening a saved 5 Whys (W13)
   ========================================================= */

.why-open {
  margin-bottom: var(--space-sm);
  text-align: center;
}

#why-open {
  padding: 6px 18px;
  border: 1px solid var(--primary-blue);
  border-radius: 4px;
  background: var(--white);
  color: var(--primary-blue);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

#why-open:hover {
  background: var(--blue-tint);
}

#why-open:disabled {
  opacity: 0.6;
  cursor: default;
}

/* What couldn't be matched when a file was opened, under the "Opened …" line */
.why-open-notes {
  display: inline-block;
  margin: var(--space-2xs) 0 0;
  padding-left: 1.2em;
  color: var(--black);
  font-size: 0.9rem;
  text-align: left;
}

.why-empty {
  color: var(--muted);
  font-size: 0.95rem;
  font-style: italic;
}

/* =========================================================
   Messages and the buttons below the tool
   ========================================================= */

.why-message {
  min-height: 1.35em;
  margin: var(--space-sm) 0;
  color: var(--primary-blue);
  line-height: 1.35;
  text-align: center;
}

.why-message.is-error {
  color: var(--primary-red);
  font-weight: 600;
}

.why-confirm-text {
  display: block;
  margin-bottom: var(--space-2xs);
  color: var(--black);
  font-weight: 600;
}

.why-confirm-yes,
.why-confirm-cancel {
  margin: 0 4px;
  padding: 6px 16px;
  border: 0;
  border-radius: 4px;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}

.why-confirm-yes {
  background: var(--primary-red);
  color: var(--white);
}

.why-confirm-cancel {
  border: 1px solid var(--light-gray);
  background: var(--bg-light);
  color: var(--black);
}

/* Soft link to the consultation form, shown with the export-complete message */
.why-cta-link {
  display: inline-block;
  margin-top: var(--space-2xs);
  padding: 6px 16px;
  border-radius: 4px;
  background: var(--primary-red);
  color: var(--white);
  font-weight: 600;
  text-decoration: none;
}

.why-cta-link:hover {
  opacity: 0.9;
}

/* =========================================================
   Tablets and phones: the tables are cards (tools.css)
   ========================================================= */

@media (max-width: 900px) {
  .why-checks-table td:nth-child(1),
  .why-checks-table td:nth-child(2) {
    display: inline-block;
    width: auto;
    margin-right: var(--space-sm);
  }

  .why-tool .why-actions-table td:last-child {
    text-align: right;
  }

}

/* =========================================================
   Phones and narrow windows
   ========================================================= */

@media (max-width: 600px) {
  .why-tool .tool-section {
    padding: var(--space-xs) var(--space-xs) var(--space-sm);
  }

  /* Is and Is not side by side, each row's label above them */
  .why-facts-grid {
    grid-template-columns: 1fr 1fr;
  }

  .why-facts-head:first-child {
    display: none;
  }

  .why-facts-label {
    grid-column: 1 / -1;
    padding-top: var(--space-2xs);
  }

  /*
    The chain as one column: each why straight below the one before it, and
    other possible answers to one why stacked and bracketed by a line on the
    left, with a tick to each of them, instead of side by side.
  */
  .why-tree {
    align-items: stretch;
    width: auto;
    padding: 0 var(--space-xs);
  }

  .why-problem-card,
  .why-card {
    width: 100%;
  }

  /* "+ Another possible cause" straight under the why's last answer instead of above it */
  .why-add-other--wide {
    display: none;
  }

  .why-add-other--narrow {
    display: block;
    align-self: flex-start;
    margin-top: var(--space-2xs);
  }

  .why-children {
    flex-direction: column;
  }

  .why-branch {
    align-items: stretch;
    padding: var(--why-gap) 0 0;
  }

  .why-branch::before,
  .why-branch::after {
    border-top: 0;
  }

  .why-children--split {
    margin-top: var(--why-gap);
    padding: 0 0 0 12px;
    border-left: var(--why-line);
  }

  .why-children--split::before {
    display: none;
  }

  .why-children--split > .why-branch::after {
    display: none;
  }

  .why-children--split > .why-branch::before {
    top: calc(var(--why-gap) + 18px);
    right: auto;
    left: -12px;
    width: 12px;
    height: 0;
    border-top: var(--why-line);
  }

  .why-children--split > .why-branch:first-child {
    padding-top: var(--space-2xs);
  }

  .why-children--split > .why-branch:first-child::before {
    top: calc(var(--space-2xs) + 18px);
  }

  .why-children--split > .why-branch > .why-card::before {
    display: none;
  }
}

/* =========================================================
   Phones only view a saved 5 Whys (T6). app.js sets why-phone from
   the screen's narrow side, and why-has-file once a file is open.
   ========================================================= */

.why-phone .why-instructions,
.why-phone #why-clear,
.why-phone #why-export,
.why-phone #why-add-action {
  display: none;
}

/* Before a file is opened there's only the phone line and the Open button */
.why-phone:not(.why-has-file) .tool-page-nav,
.why-phone:not(.why-has-file) .tool-section,
.why-phone:not(.why-has-file) .tool-buttons--end {
  display: none;
}

/* Boxes shown as their text */
.why-phone .why-problem-input,
.why-phone .why-fact-input,
.why-phone .why-answer {
  padding: 2px 0;
  border-color: transparent;
  background: transparent;
  box-shadow: none;
}

.why-shown {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* No remove button on a phone's action cards, so Status is the last cell: keep it with the others */
.why-phone .why-tool .why-actions-table td:last-child {
  text-align: left;
}

/* Section descriptions (3. Checks, 4. Actions): a short paragraph each, left-aligned
   in a readable column under the centred title, instead of one long centred block */
.why-tool .tool-explainer {
  max-width: 70ch;
  margin: 0 auto var(--space-2xs);
  text-align: left;
  line-height: 1.65;
}

.why-tool .tool-explainer:last-of-type {
  margin-bottom: var(--space-md);
}
