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

@media (min-width: 601px) {
  html { font-size: calc(100vw / 128); }
}

body {
  font-family: 'Courier New', Courier, monospace;
  background: #f5f0e8;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 1.5rem;
  min-height: 0;
}

#pipe-workspace {
  width: 100%;
  flex: 1;
  min-height: 0;
  background: #faf7f0;
  border: 2px solid #1a1a1a;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
  background-image:
    linear-gradient(rgba(26,26,26,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,26,26,0.06) 1px, transparent 1px);
  background-size: 2rem 2rem;
  padding: 1.5rem;
  overflow: hidden;
}

#pipe-form {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 1.5rem;
  width: 100%;
}

.form-row-group {
  display: contents;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.form-row label {
  font-size: 0.75rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #1a1a1a;
}

.form-row input {
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.95rem;
  background: transparent;
  border: none;
  border-bottom: 2px solid #1a1a1a;
  padding: 0.375rem 0.25rem;
  color: #1a1a1a;
  outline: none;
  width: 100%;
  transition: background 0.35s, border-bottom-color 0.35s;
}

.form-row input::placeholder {
  color: #999;
}

.form-row input:focus {
  border-bottom-color: #1a1a1a;
  background: rgba(26, 26, 26, 0.04);
}

.form-row input.field-error {
  border-bottom-color: #c0392b;
  background: rgba(192, 57, 43, 0.07);
}

.form-row input.field-empty {
  border-bottom-color: #c8a800;
  background: rgba(200, 168, 0, 0.09);
}

.form-row input.field-ready {
  border-bottom-color: #226422;
  background: rgba(34, 100, 34, 0.09);
}

.form-row input.field-default {
  color: #aaa;
}

#pipe-display {
  flex: 1;
  width: 100%;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

#pipe-display svg {
  display: block;
  position: absolute;
}

.pipe-tint,
.pipe-hover-tint,
.pipe-bad-tint {
  opacity: 0;
  transition: opacity 0.15s;
}

#pipe-display svg.selected .pipe-tint { opacity: 1; }
#pipe-display svg.previewing .pipe-hover-tint { opacity: 1; }
#pipe-display svg.hovered .pipe-hover-tint { opacity: 1; }

#pipe-display svg.bad-ratio .pipe-bad-tint { opacity: 1; }
#pipe-display svg.bad-ratio.selected .pipe-bad-tint { opacity: 0; }
#pipe-display svg.bad-ratio.hovered .pipe-bad-tint { opacity: 0; }
#pipe-display svg.bad-ratio.previewing .pipe-bad-tint { opacity: 0; }

#ratio-warning {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(160,0,0,0.92);
  color: #faf7f0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1.4rem;
  line-height: 1.7;
  text-align: center;
  padding: 1rem 1.375rem;
  border: 2px solid #1a1a1a;
  box-shadow: 0.1875rem 0.1875rem 0 #1a1a1a;
  max-width: 80%;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s;
  z-index: 10;
}
#ratio-warning.visible { opacity: 1; }

#welcome-text {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  padding: 1rem 10%;
  overflow: hidden;
  box-sizing: border-box;
  font-size: 1.5vw;
  line-height: 1.65;
  color: #333;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

#welcome-text.visible { display: flex; }
.welcome-mobile-only { display: none; }
.mobile-br { display: none; }

#welcome-title {
  font-size: 1.4em;
  font-weight: bold;
  margin: 0 0 0.875rem;
  letter-spacing: 0.02em;
}

#welcome-text p {
  margin: 0 0 0.625rem;
}

#welcome-text p:last-child { margin-bottom: 0; }

#pipe-form-container {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  padding: 1.25rem 1.5rem;
  border: 2px solid #1a1a1a;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
}

.form-row {
  flex: 1;
  min-width: 5rem;
}

#presets-btn {
  background: rgba(0, 130, 130, 0.10);
  color: #005555;
  border: 2px solid #007a7a;
  box-shadow: 0.1875rem 0.1875rem 0 #007a7a;
}
#presets-btn:hover { background: rgba(0, 130, 130, 0.24); }

#help-btn {
  background: rgba(0, 0, 0, 0.07);
  color: #555;
  border: 2px solid #666;
  box-shadow: 0.1875rem 0.1875rem 0 #666;
}
#help-btn:hover { background: rgba(0, 0, 0, 0.18); }

#summarise-btn {
  background: rgba(240, 110, 180, 0.10);
  color: #8b3a6a;
  border: 2px solid #cc6699;
  box-shadow: 0.1875rem 0.1875rem 0 #cc6699;
}
#summarise-btn:hover { background: rgba(240, 110, 180, 0.24); }

#update-pipe-btn {
  background: rgba(180, 130, 0, 0.12);
  color: #7a5800;
  border: 2px solid #b48200;
  box-shadow: 0.1875rem 0.1875rem 0 #b48200;
}
#update-pipe-btn:hover { background: rgba(180, 130, 0, 0.28); }

#pipe-form-actions {
  display: flex;
  justify-content: center;
  gap: 1rem;
  width: 100%;
}

#solve-btn,
#add-pipe-btn,
#delete-pipe-btn,
#update-pipe-btn,
#preview-pipes-btn,
#presets-btn,
#help-btn,
#summarise-btn,
#clear-all-btn {
  flex: 1 1 0;
  max-width: 11.25rem;
  padding: 0.75rem 0.5rem;
  font-size: 0.95rem;
  font-family: 'Courier New', Courier, monospace;
  border-radius: 0;
  cursor: pointer;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}

#solve-btn:active,
#add-pipe-btn:active,
#delete-pipe-btn:active,
#update-pipe-btn:active,
#preview-pipes-btn:active,
#presets-btn:active,
#help-btn:active,
#summarise-btn:active,
#clear-all-btn:active {
  box-shadow: 1px 1px 0;
  transform: translate(2px, 2px);
}

#solve-btn {
  background: rgba(0, 31, 96, 0.12);
  color: #001f60;
  border: 2px solid #001f60;
  box-shadow: 0.1875rem 0.1875rem 0 #001f60;
}
#solve-btn:hover { background: rgba(0, 31, 96, 0.28); }

#delete-pipe-btn {
  background: rgba(200, 0, 0, 0.1);
  color: #c80000;
  border: 2px solid #c80000;
  box-shadow: 0.1875rem 0.1875rem 0 #c80000;
}
#delete-pipe-btn:hover { background: rgba(200, 0, 0, 0.25); }

#add-pipe-btn {
  background: rgba(34, 100, 34, 0.1);
  color: #1a5c1a;
  border: 2px solid #226422;
  box-shadow: 0.1875rem 0.1875rem 0 #226422;
}
#add-pipe-btn:hover { background: rgba(34, 100, 34, 0.25); }

#preview-pipes-btn {
  background: rgba(210, 90, 0, 0.10);
  color: #7a3a00;
  border: 2px solid #c05a00;
  box-shadow: 0.1875rem 0.1875rem 0 #c05a00;
}
#preview-pipes-btn:hover { background: rgba(210, 90, 0, 0.24); }

#clear-all-btn {
  background: rgba(100, 50, 180, 0.10);
  color: #4a1a8b;
  border: 2px solid #6a2fc0;
  box-shadow: 0.1875rem 0.1875rem 0 #6a2fc0;
}
#clear-all-btn:hover { background: rgba(100, 50, 180, 0.24); }

/* ── Summarise Modal ─────────────────────────────────────────────────────────── */
#summarise-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
}

#summarise-modal-overlay.open { display: flex; }

#summarise-modal-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  backdrop-filter: blur(4px);
  background: rgba(245, 240, 232, 0.5);
}

#summarise-modal {
  position: relative;
  width: 70%;
  max-height: 85vh;
  background: #faf7f0;
  border: 2px solid #1a1a1a;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
  padding: 2rem 2.25rem;
  overflow-y: auto;
  background-image:
    linear-gradient(rgba(26,26,26,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,26,26,0.06) 1px, transparent 1px);
  background-size: 2rem 2rem;
  font-family: 'Courier New', Courier, monospace;
}

#summarise-modal h2 {
  font-size: 2.2rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 0;
  color: #1a1a1a;
}

#summarise-modal h3 {
  font-size: 1.6rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin: 1.5rem 0 0.625rem;
  color: #1a1a1a;
}

#summarise-modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 2rem;
  height: 2rem;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;
  background: transparent;
  border: 2px solid #1a1a1a;
  box-shadow: 0.125rem 0.125rem 0 #1a1a1a;
  color: #1a1a1a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}

#summarise-modal-close:hover { background: rgba(26,26,26,0.08); }
#summarise-modal-close:active { box-shadow: 1px 1px 0 #1a1a1a; transform: translate(1px,1px); }

.summarise-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 1.7rem;
}

.summarise-table th {
  text-align: left;
  font-size: 1.44rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  border-bottom: 2px solid #1a1a1a;
  padding: 0.375rem 0.625rem 0.375rem 0;
  color: #1a1a1a;
}

.summarise-table td {
  padding: 0.375rem 0.625rem 0.375rem 0;
  border-bottom: 1px solid rgba(26,26,26,0.15);
  color: #1a1a1a;
}

.summarise-table tbody tr:hover td {
  background: rgba(26,26,26,0.04);
}

.summarise-table tbody tr.pipe-cut td {
  background: rgba(34, 120, 34, 0.08);
}

.summarise-check-col {
  width: 1.75rem;
  text-align: center;
}

.pipe-cut-check {
  cursor: pointer;
  accent-color: #226422;
  width: 0.875rem;
  height: 0.875rem;
}

#summarise-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 3rem;
  margin-bottom: 0.5rem;
}

#summarise-print-btn {
  font-family: 'Courier New', Courier, monospace;
  font-size: 1.5rem;
  letter-spacing: 0.05em;
  padding: 0.4rem 1rem;
  cursor: pointer;
  background: rgba(240, 110, 180, 0.10);
  color: #8b3a6a;
  border: 2px solid #cc6699;
  box-shadow: 0.1875rem 0.1875rem 0 #cc6699;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}
#summarise-print-btn:hover { background: rgba(240, 110, 180, 0.24); }
#summarise-print-btn:active { box-shadow: 1px 1px 0 #cc6699; transform: translate(2px, 2px); }

/* ── Presets Modal ───────────────────────────────────────────────────────────── */
#presets-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
}

#presets-modal-overlay.open {
  display: flex;
}

#presets-modal-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  backdrop-filter: blur(4px);
  background: rgba(245, 240, 232, 0.5);
}

#presets-modal {
  position: relative;
  width: 40%;
  max-height: 70vh;
  background: #faf7f0;
  border: 2px solid #1a1a1a;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
  padding: 2rem 2.25rem;
  overflow-y: auto;
  background-image:
    linear-gradient(rgba(26,26,26,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,26,26,0.06) 1px, transparent 1px);
  background-size: 2rem 2rem;
  font-family: 'Courier New', Courier, monospace;
}

#presets-modal h2 {
  font-size: 2.2rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 1.25rem;
  color: #1a1a1a;
}

#presets-modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 2rem;
  height: 2rem;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;
  background: transparent;
  border: 2px solid #1a1a1a;
  box-shadow: 0.125rem 0.125rem 0 #1a1a1a;
  color: #1a1a1a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}

#presets-modal-close:hover { background: rgba(26,26,26,0.08); }
#presets-modal-close:active { box-shadow: 1px 1px 0 #1a1a1a; transform: translate(1px, 1px); }

#presets-save-row {
  display: flex;
  gap: 0.625rem;
  margin-bottom: 1.25rem;
}

#presets-name-input {
  flex: 1;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1.8rem;
  background: transparent;
  border: none;
  border-bottom: 2px solid #1a1a1a;
  padding: 0.375rem 0.25rem;
  color: #1a1a1a;
  outline: none;
}

#presets-name-input::placeholder { color: #999; }

#presets-save-btn {
  font-family: 'Courier New', Courier, monospace;
  font-size: 1.6rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 0.375rem 0.875rem;
  background: rgba(0, 130, 130, 0.10);
  color: #005555;
  border: 2px solid #007a7a;
  box-shadow: 0.1875rem 0.1875rem 0 #007a7a;
  cursor: pointer;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}

#presets-save-btn:hover { background: rgba(0, 130, 130, 0.24); }
#presets-save-btn:active { box-shadow: 1px 1px 0 #007a7a; transform: translate(2px, 2px); }

.preset-row {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid rgba(26,26,26,0.15);
}

.preset-name {
  flex: 1;
  font-size: 1.8rem;
  color: #1a1a1a;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preset-load-btn,
.preset-delete-btn {
  font-family: 'Courier New', Courier, monospace;
  font-size: 1.5rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 0.25rem 0.625rem;
  cursor: pointer;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}

.preset-load-btn {
  background: rgba(34, 100, 34, 0.1);
  color: #1a5c1a;
  border: 2px solid #226422;
  box-shadow: 0.125rem 0.125rem 0 #226422;
}
.preset-load-btn:hover { background: rgba(34, 100, 34, 0.25); }

.preset-delete-btn {
  background: rgba(200, 0, 0, 0.08);
  color: #c80000;
  border: 2px solid #c80000;
  box-shadow: 0.125rem 0.125rem 0 #c80000;
}
.preset-delete-btn:hover { background: rgba(200, 0, 0, 0.2); }

.presets-empty {
  font-size: 1.7rem;
  color: #888;
  font-style: italic;
}

/* ── Range Modal ────────────────────────────────────────────────────────────── */
#range-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
}
#range-modal-overlay.open { display: flex; }
#range-modal-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  backdrop-filter: blur(4px);
  background: rgba(245, 240, 232, 0.5);
}

#range-modal {
  position: relative;
  width: fit-content;
  min-width: 28rem;
  max-width: 85vw;
  max-height: 75vh;
  background: #faf7f0;
  border: 2px solid #1a1a1a;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
  padding: 2rem 2.25rem;
  overflow: hidden;
  background-image:
    linear-gradient(rgba(26,26,26,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,26,26,0.06) 1px, transparent 1px);
  background-size: 2rem 2rem;
  font-family: 'Courier New', Courier, monospace;
}

#range-modal-content {
  width: 100%;
}

.range-notes-scroll {
  overflow-y: auto;
  max-height: 40vh;
}

#range-modal h2 {
  font-size: 2.2rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 1.25rem;
  color: #1a1a1a;
}

#range-modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 2rem;
  height: 2rem;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;
  background: transparent;
  border: 2px solid #1a1a1a;
  box-shadow: 0.125rem 0.125rem 0 #1a1a1a;
  color: #1a1a1a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}
#range-modal-close:hover  { background: rgba(26,26,26,0.08); }
#range-modal-close:active { box-shadow: 1px 1px 0 #1a1a1a; transform: translate(1px,1px); }

.range-kbd-svg {
  display: block;
  width: 100%;
  height: auto;
  margin-bottom: 1.25rem;
}

.range-desc {
  font-size: 1.7rem;
  line-height: 1.55;
  color: #1a1a1a;
  margin-bottom: 1rem;
}

@media (min-width: 601px) {
  .range-desc { white-space: nowrap; }
}

.range-empty {
  font-size: 1.7rem;
  color: #888;
  font-style: italic;
}

.range-notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0 1.25rem;
}

.range-note-row {
  display: contents;
}

.range-note-row > span {
  font-size: 1.7rem;
  padding: 0.3rem 0;
  border-bottom: 1px solid rgba(26,26,26,0.12);
  color: #1a1a1a;
}

.range-note-row:last-child > span {
  border-bottom: none;
}

.range-note-name {
  font-weight: bold;
  letter-spacing: 0.04em;
}

.range-note-freq {
  color: #555;
}

.range-note-length {
  color: #555;
}

.range-header {
  display: contents;
}

.range-header > span {
  font-size: 1.44rem;
  padding-bottom: 0.3rem;
  border-bottom: 2px solid #1a1a1a;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #1a1a1a;
}

/* ── Help Modal ─────────────────────────────────────────────────────────────── */
#help-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
}

#help-modal-overlay.open {
  display: flex;
}

#help-modal-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  backdrop-filter: blur(4px);
  background: rgba(245, 240, 232, 0.5);
}

#help-modal {
  position: relative;
  width: 65%;
  max-height: 90vh;
  background: #faf7f0;
  border: 2px solid #1a1a1a;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
  padding: 2rem 2.25rem;
  overflow-y: auto;
  background-image:
    linear-gradient(rgba(26,26,26,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,26,26,0.06) 1px, transparent 1px);
  background-size: 2rem 2rem;
  font-family: 'Courier New', Courier, monospace;
}

#help-modal h2 {
  font-size: 17.6px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 1.25rem;
  color: #1a1a1a;
}

#help-modal h3 {
  font-size: 12.8px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin: 1.25rem 0 0.5rem;
  color: #1a1a1a;
}

#help-modal p,
#help-modal li {
  font-size: 14.4px;
  line-height: 1.7;
  color: #1a1a1a;
}

#help-modal li {
  padding-left: 1em;
  text-indent: -1em;
}

#help-modal ul {
  padding-left: 2.4em;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.formula {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin: 1rem 0;
  font-style: italic;
  font-size: 17.6px;
}

.formula-or {
  font-style: normal;
  font-size: 12.5px;
  color: #555;
  padding: 0 0.25rem;
}

.formula-key {
  font-size: 12.5px;
  color: #555;
  text-align: center;
  margin-top: -0.5rem;
  margin-bottom: 0.25rem;
}

@media (min-width: 601px) {
  .formula-key:has(.formula-term) {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: baseline;
    column-gap: 0;
  }
  .formula-key:has(.formula-term) .formula-term {
    white-space: nowrap;
  }
}

.formula-key a {
  color: #4a1a8b;
  text-decoration: underline;
}

.formula-key a:hover {
  color: #6a2fc0;
}

.centered {
  text-align: center;
}

.li-indent {
  display: block;
  padding-left: 1.2em;
}

#help-modal ul.references {
  list-style: none;
  padding-left: 1.6em;
}

#help-modal ul.references li {
  text-indent: -1.6em;
  padding-left: 0;
  margin-bottom: 0.375rem;
  overflow-wrap: break-word;
}

.modal-bottom-row {
  display: flex;
  gap: 2rem;
  align-items: stretch;
  margin-top: 1.5rem;
}

.modal-bottom-col {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.modal-bottom-col:first-child { flex: 51 1 0; }
.modal-bottom-col:last-child  { flex: 49 1 0; }

.modal-bottom-col ul {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.formula-frac {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 0.125rem;
}

.formula-num {
  padding: 0 0.375rem 0.1875rem;
  border-bottom: 2px solid #1a1a1a;
}

.formula-den {
  padding: 0.1875rem 0.375rem 0;
}

#help-modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 16px;
  background: transparent;
  border: 2px solid #1a1a1a;
  box-shadow: 2px 2px 0 #1a1a1a;
  color: #1a1a1a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}

#help-modal-close:hover { background: rgba(26,26,26,0.08); }

#help-modal-close:active {
  box-shadow: 1px 1px 0 #1a1a1a;
  transform: translate(1px, 1px);
}

.label-short { display: none; }

/* ── Mobile ─────────────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  body {
    height: 100dvh;
  }

  #app {
    padding: 10px;
  }

  #pipe-workspace {
    padding: 12px 12px 10px;
  }

  #pipe-form-container {
    padding: 10px 12px;
    gap: 10px;
  }

  .label-short { display: inline; }
  .label-full  { display: none; }

  #pipe-form {
    flex-direction: column;
    gap: 10px;
  }

  .form-row-group {
    display: flex;
    flex-direction: row;
    gap: 8px;
    width: 100%;
  }

  .form-row { min-width: 0; }

  /* Bottom row: Hz, Note (equal), keyboard btn (fixed), End Correction */
  .form-row-group:last-child .form-row { flex: 1; }
  .form-row-group:last-child #keyboard-btn { flex: 0 0 auto; align-self: flex-end; margin-left: 0; }
  .kbd-icon-full  { display: none; }
  .kbd-icon-small { display: block; }

  .form-row label {
    font-size: 0.608rem;
  }

  .form-row input {
    font-size: 0.77rem;
  }

  #pipe-form-actions {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 7px;
  }

  #solve-btn,
  #add-pipe-btn,
  #delete-pipe-btn,
  #update-pipe-btn,
  #preview-pipes-btn,
  #presets-btn,
  #help-btn,
  #summarise-btn,
  #clear-all-btn {
    max-width: none;
    padding: 7px 3px;
    font-size: 0.6rem;
    letter-spacing: 0.03em;
  }

  #summarise-modal,
  #presets-modal,
  #help-modal {
    width: 92%;
    padding: 24px 16px;
  }

  #range-modal {
    min-width: 0;
    padding: 24px 16px;
  }

  #range-modal h2              { font-size: 0.825rem; }
  .range-desc, .range-empty   { font-size: 0.66rem; }
  .range-note-row > span      { font-size: 0.66rem; }
  .range-header > span        { font-size: 0.55rem; }

  #summarise-modal h2          { font-size: 0.825rem; }
  #summarise-modal h3          { font-size: 0.66rem;  }
  #summarise-modal .summarise-table    { font-size: 0.66rem;  }
  #summarise-modal .summarise-table th { font-size: 0.55rem;  }

  .modal-bottom-row {
    flex-direction: column;
    gap: 20px;
  }

  .formula-term { display: block; text-align: center; }
  .formula-sep  { display: none; }
  .formula-key  { margin-bottom: 1em; }
  .formula-or   { width: 100%; text-align: center; }
}

@media (min-width: 601px) { .kbd-icon-small { display: none; } }

/* ── Keyboard button ─────────────────────────────────────────────────────────── */
#keyboard-btn,
#range-btn {
  flex: 0 0 auto;
  align-self: flex-end;
  margin-left: -1.25rem;
  margin-bottom: 0.125rem;
  padding: 0.375rem 0.25rem;
  background: transparent;
  border: 2px solid #1a1a1a;
  box-shadow: 0.125rem 0.125rem 0 #1a1a1a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.95rem;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}
#keyboard-btn:hover,
#range-btn:hover  { background: rgba(26,26,26,0.08); }
#keyboard-btn:active,
#range-btn:active { box-shadow: 1px 1px 0 #1a1a1a; transform: translate(1px,1px); }

#keyboard-btn {
  padding-top: 0.32rem;
  padding-bottom: 0.32rem;
}

#range-btn {
  padding: 0;
  width: calc(0.32rem * 2 + 0.9375rem + 2px);
  height: calc(0.32rem * 2 + 0.9375rem + 2px);
  font-size: 1.1rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: 0.15rem;
}

.kbd-icon-full  { width: 3.25rem; height: 0.9375rem; }
.kbd-icon-small { width: 1.375rem; height: 0.9375rem; }

/* ── Piano Keyboard Modal ────────────────────────────────────────────────────── */
#keyboard-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
}
#keyboard-modal-overlay.open { display: flex; }
#keyboard-modal-overlay::before {
  content: '';
  position: absolute;
  inset: 0;
  backdrop-filter: blur(4px);
  background: rgba(245, 240, 232, 0.5);
}

#keyboard-modal {
  position: relative;
  max-width: 95vw;
  background: #faf7f0;
  border: 2px solid #1a1a1a;
  box-shadow: 0.25rem 0.25rem 0 #1a1a1a;
  padding: 2rem 2.25rem 1.75rem;
  background-image:
    linear-gradient(rgba(26,26,26,0.06) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,26,26,0.06) 1px, transparent 1px);
  background-size: 2rem 2rem;
  font-family: 'Courier New', Courier, monospace;
}

#keyboard-modal h2 {
  font-size: 2.2rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 1.5rem;
  color: #1a1a1a;
}

#keyboard-modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 2rem;
  height: 2rem;
  padding: 0;
  font-family: 'Courier New', Courier, monospace;
  font-size: 1rem;
  background: transparent;
  border: 2px solid #1a1a1a;
  box-shadow: 0.125rem 0.125rem 0 #1a1a1a;
  color: #1a1a1a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}
#keyboard-modal-close:hover  { background: rgba(26,26,26,0.08); }
#keyboard-modal-close:active { box-shadow: 1px 1px 0 #1a1a1a; transform: translate(1px,1px); }

#keyboard-container {
  overflow-x: auto;
}

/* Piano key dimensions stay in px — JS positions black keys using the same pixel values */
.kbd-keys {
  position: relative;
  display: flex;
  align-items: flex-start;
  height: 10.3125rem;
  user-select: none;
}

.kbd-white {
  width: 3rem;
  height: 10.3125rem;
  flex-shrink: 0;
  border: 1px solid #1a1a1a;
  border-right: none;
  background: #faf7f0;
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 0.15rem;
  transition: background 0.08s;
}
.kbd-white-end { border-right: 1px solid #1a1a1a; }
.kbd-white:hover  { background: rgba(26,26,26,0.08); }
.kbd-white:active { background: rgba(26,26,26,0.2); }

.kbd-middle-c {
  background: #c0392b;
}
.kbd-middle-c:hover  { background: #a93226; }
.kbd-middle-c:active { background: #922b21; }

.kbd-octave-label {
  font-size: 3.3rem;
  color: #1a1a1a;
  font-family: 'Courier New', Courier, monospace;
  pointer-events: none;
  letter-spacing: 0;
  line-height: 1;
}

.kbd-black {
  position: absolute;
  width: 1.875rem;
  height: 6.375rem;
  top: 0;
  background: #1a1a1a;
  cursor: pointer;
  z-index: 2;
  transition: background 0.08s;
}
.kbd-black:hover  { background: #3a3a3a; }
.kbd-black:active { background: #555; }

/* ── Accidental picker ───────────────────────────────────────────────────────── */
#keyboard-divider {
  margin-top: 1.125rem;
  border-top: 1px solid rgba(26,26,26,0.2);
}

#keyboard-accidental-picker {
  position: relative;
  height: 6.25rem;
  margin-top: 1.125rem;
}

#keyboard-hint,
#keyboard-accidental-content {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
}

#keyboard-hint {
  font-size: 1.7rem;
  letter-spacing: 0.06em;
  color: #888;
  font-style: italic;
}

#keyboard-accidental-content {
  visibility: hidden;
}

#keyboard-accidental-label {
  font-size: 1.64rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #666;
}

#keyboard-accidental-picker.visible #keyboard-hint            { visibility: hidden; }
#keyboard-accidental-picker.visible #keyboard-accidental-content { visibility: visible; }

#keyboard-sharp-btn,
#keyboard-flat-btn {
  font-family: 'Courier New', Courier, monospace;
  font-size: 1.9rem;
  letter-spacing: 0.04em;
  padding: 0.875rem 2.25rem;
  min-height: 4.25rem;
  min-width: 8rem;
  cursor: pointer;
  background: rgba(0,31,96,0.10);
  color: #001f60;
  border: 2px solid #001f60;
  box-shadow: 0.125rem 0.125rem 0 #001f60;
  transition: box-shadow 0.1s, transform 0.1s, background 0.1s;
}
#keyboard-sharp-btn:hover,
#keyboard-flat-btn:hover  { background: rgba(0,31,96,0.22); }
#keyboard-sharp-btn:active,
#keyboard-flat-btn:active { box-shadow: 1px 1px 0 #001f60; transform: translate(1px,1px); }

@media (max-width: 600px) {
  .form-row-group:first-child .form-row {
    flex: 1 1 0;
  }
  .form-row-group:first-child .form-row label {
    white-space: nowrap;
  }
  #range-btn {
    margin-left: 0;
  }

  #keyboard-modal {
    padding: 24px 12px 20px;
  }
  #keyboard-modal h2 {
    font-size: 0.80rem;
    margin-bottom: 16px;
  }
  #keyboard-accidental-picker {
    height: 3.125rem;
  }
  #keyboard-hint,
  #keyboard-accidental-content {
    gap: 0.75rem;
  }
  #keyboard-hint {
    font-size: 0.85rem;
  }
  #keyboard-accidental-label {
    font-size: 0.82rem;
  }
  #keyboard-sharp-btn,
  #keyboard-flat-btn {
    font-size: 0.95rem;
    padding: 0.4375rem 1.125rem;
    min-height: 2.125rem;
    min-width: 4rem;
  }
  #ratio-warning {
    font-size: 0.82rem;
    padding: 10px 12px;
    width: 90%;
    max-width: 90%;
  }
  #welcome-text {
    width: 100%;
    top: 8px;
    bottom: 0;
    border: none;
    margin: 0;
    font-size: 1rem;
    padding: 10px 12px 0;
  }
  .welcome-mobile-only { display: block; }
  .welcome-desktop-only { display: none; }
  .mobile-br { display: inline; }
}

/* ── Print / Save PDF ────────────────────────────────────────────────────────── */
@media print {
  /* Hide everything, then selectively reveal the modal content */
  body * { visibility: hidden; }

  #summarise-modal,
  #summarise-modal * { visibility: visible; }

  /* Reset the modal to fill the page */
  #summarise-modal {
    position: fixed;
    inset: 0;
    width: 100%;
    max-height: none;
    overflow: visible;
    box-shadow: none;
    border: none;
    padding: 1.5cm 2cm;
    background: #fff;
  }

  /* Hide chrome that doesn't belong on paper */
  #summarise-modal-close,
  #summarise-print-btn { display: none; }

  /* Checkboxes: always empty and larger for manual marking */
  .pipe-cut-check {
    -webkit-appearance: none;
    appearance: none;
    width: 14pt;
    height: 14pt;
    border: 1.5pt solid #1a1a1a;
    background: #fff;
    display: inline-block;
    vertical-align: middle;
  }

  /* Sensible print font sizes */
  #summarise-modal h2           { font-size: 40pt; margin-bottom: 12pt; }
  #summarise-modal h3           { font-size: 26pt; margin-top: 28pt; margin-bottom: 8pt; }
  .summarise-table              { font-size: 22pt; }
  .summarise-table th           { font-size: 18pt; padding-bottom: 6pt; }
  .summarise-table td           { padding: 6pt 16pt 6pt 0; }

  /* Keep rows together and avoid widows */
  .summarise-table tr           { page-break-inside: avoid; }
  .summarise-table thead        { display: table-header-group; }
}
