:root {
  --bg: #f2efe7;
  --panel: rgba(255, 252, 245, 0.86);
  --panel-strong: #fffaf1;
  --border: rgba(67, 63, 54, 0.12);
  --text: #1f2724;
  --muted: #58645f;
  --accent: #0f6c63;
  --accent-2: #d86c30;
  --danger: #a12c2c;
  --shadow: 0 18px 45px rgba(40, 36, 25, 0.08);
  --radius: 22px;
  --font-sans: "Atkinson Hyperlegible", "Segoe UI", sans-serif;
}

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

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  font-family: var(--font-sans);
  background:
    radial-gradient(circle at top left, rgba(216, 108, 48, 0.16), transparent 32%),
    radial-gradient(circle at top right, rgba(15, 108, 99, 0.18), transparent 28%),
    linear-gradient(180deg, #fcfaf4 0%, var(--bg) 100%);
}

button,
input,
textarea,
select,
summary {
  font: inherit;
}

.noscript {
  padding: 1rem;
  background: #fff1f1;
  color: var(--danger);
  text-align: center;
}

.app-shell {
  width: min(1180px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 1.5rem 0 3rem;
}

.site-header {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  align-items: start;
  margin-bottom: 1.5rem;
  padding: 1.6rem 1.6rem 1.2rem;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 6px);
  background: linear-gradient(135deg, rgba(255, 250, 241, 0.95), rgba(244, 251, 249, 0.84));
  box-shadow: var(--shadow);
}

.site-header > * {
  min-width: 0;
}

.eyebrow {
  margin: 0 0 0.35rem;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
}

h1,
h2,
h3,
h4 {
  margin: 0;
  line-height: 1.15;
}

h1 {
  font-size: clamp(2rem, 3vw, 3rem);
}

.lead,
.panel-intro p,
.field-help,
.muted,
.theory-content p,
.theory-content li {
  color: var(--muted);
}

.lead {
  max-width: 62ch;
  margin: 0.75rem 0 0;
  font-size: 1.02rem;
}

.toolbar {
  display: flex;
  gap: 1rem;
  align-items: center;
  flex-wrap: wrap;
  min-width: 0;
}

.toolbar .fit-app-link {
  flex: 1 1 240px;
}

.language-picker {
  display: grid;
  gap: 0.45rem;
  min-width: 140px;
  font-weight: 600;
}

.language-picker select {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.7rem 0.9rem;
  background: #fff;
}

.tab-nav {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.section-nav {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.tab-button {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.9rem 1rem;
  background: rgba(255, 255, 255, 0.75);
  color: var(--text);
  font-weight: 700;
  text-align: center;
  white-space: normal;
  overflow-wrap: anywhere;
  cursor: pointer;
  transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}

.tab-button:hover,
.tab-button:focus-visible {
  transform: translateY(-1px);
  border-color: rgba(15, 108, 99, 0.45);
}

.tab-button.is-active {
  background: linear-gradient(135deg, var(--accent), #16514b);
  color: #fff;
  border-color: transparent;
}

.tab-button--subtle {
  background: rgba(255, 255, 255, 0.62);
}

.tab-panel {
  display: grid;
  gap: 1.25rem;
  scroll-margin-top: 1rem;
}

.panel-intro {
  padding: 0 0.25rem;
}

.panel-intro p {
  margin: 0.55rem 0 0;
  max-width: 70ch;
}

.intro-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 1rem;
}

.fit-app-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  text-decoration: none;
  background: linear-gradient(135deg, var(--accent), #16514b);
  color: #fff;
  border-color: transparent;
}

.fit-app-link:hover,
.fit-app-link:focus-visible {
  border-color: transparent;
}

.panel-grid {
  display: grid;
  grid-template-columns: minmax(270px, 360px) minmax(0, 1fr);
  gap: 1.25rem;
}

.panel-stack {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1.25rem;
}

.panel-grid > * {
  min-width: 0;
}

.panel-stack > * {
  min-width: 0;
}

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  backdrop-filter: blur(8px);
  box-shadow: var(--shadow);
  min-width: 0;
  overflow-wrap: anywhere;
}

.form-card,
.result-card,
.chart-card,
.theory-card {
  padding: 1.2rem;
}

.form-card {
  display: grid;
  gap: 1rem;
  align-content: start;
}

.wide-form {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: start;
}

.wide-form > h2,
.wide-form > .field-help,
.wide-form > .checkbox-row,
.wide-form > .message,
.wide-form > .full-span {
  grid-column: 1 / -1;
}

.measurement-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 1.15fr);
  gap: 1.1rem;
  align-items: stretch;
}

.measurement-left,
.measurement-right {
  display: grid;
  gap: 1rem;
  min-width: 0;
  align-content: start;
}

.measurement-right .fit-paste-group,
.measurement-right .fit-paste-group textarea {
  height: 100%;
}

.measurement-right .fit-paste-group textarea {
  min-height: 100%;
}

.manual-layout {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1.1rem;
  align-items: start;
}

.manual-layout .input-group {
  align-content: start;
}

.manual-layout .field-help {
  min-height: 2.6em;
}

.manual-empty-help {
  visibility: hidden;
}

.chart-control-row {
  display: grid;
  grid-template-columns: minmax(220px, 1.3fr) repeat(2, minmax(180px, 1fr));
  gap: 1rem;
  align-items: end;
  margin: 1rem 0 0.75rem;
}

.chart-control-input {
  align-content: end;
}

.chart-control-checkbox {
  align-self: end;
}

.input-group {
  display: grid;
  gap: 0.45rem;
  min-width: 0;
}

.input-group label,
.result-header h3 {
  font-weight: 700;
}

.field-help {
  margin: 0;
  font-size: 0.93rem;
  overflow-wrap: anywhere;
}

.field-help.is-warning {
  color: var(--danger);
  font-weight: 600;
}

.paired-inputs {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 110px;
  gap: 0.75rem;
  align-items: center;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

input[type="number"],
input[type="file"],
textarea,
select {
  width: 100%;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.72rem 0.85rem;
  background: rgba(255, 255, 255, 0.94);
}

textarea {
  resize: vertical;
}

input:focus-visible,
select:focus-visible,
button:focus-visible,
summary:focus-visible {
  outline: 3px solid rgba(15, 108, 99, 0.22);
  outline-offset: 2px;
}

.checkbox-row {
  display: flex;
  gap: 0.65rem;
  align-items: center;
  font-weight: 600;
}

.message {
  min-height: 1.5rem;
  padding-top: 0.2rem;
  font-size: 0.94rem;
}

.message.is-error {
  color: var(--danger);
}

.message.is-info {
  color: var(--accent);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.85rem;
  margin-top: 1rem;
}

.stat {
  min-width: 0;
  padding: 1rem;
  border-radius: 18px;
  background: var(--panel-strong);
  border: 1px solid var(--border);
}

.stat-label {
  display: block;
  color: var(--muted);
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

.stat strong {
  display: block;
  margin-top: 0.35rem;
  font-size: 1.12rem;
  overflow-wrap: anywhere;
}

.table-wrap {
  overflow-x: auto;
  margin-top: 1rem;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 0.78rem 0.65rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.chart-card {
  overflow: hidden;
}

.chart-header {
  margin-bottom: 0.8rem;
}

.chart {
  width: 100%;
  min-height: 420px;
}

.chart-fallback {
  min-height: 280px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border);
  border-radius: 18px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.55);
  text-align: center;
  padding: 1rem;
}

details summary {
  cursor: pointer;
  font-weight: 700;
  list-style: none;
}

details summary::-webkit-details-marker {
  display: none;
}

details summary::after {
  content: "+";
  float: right;
  color: var(--accent);
}

details[open] summary::after {
  content: "−";
}

.theory-content {
  padding-top: 1rem;
}

.theory-content h4 {
  margin: 1.1rem 0 0.5rem;
}

.theory-content ul {
  margin: 0.5rem 0 0;
  padding-left: 1.2rem;
}

.equation {
  margin: 0.8rem 0;
  padding: 0.9rem 1rem;
  border-radius: 16px;
  background: rgba(15, 108, 99, 0.08);
  color: #123a35;
  font-family: "Courier New", monospace;
  white-space: pre-line;
}

.site-footer {
  margin-top: 1.5rem;
  padding: 0 0.25rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.busy-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(245, 248, 246, 0.72);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
  z-index: 1000;
}

body.is-busy {
  cursor: progress;
}

body.is-busy button,
body.is-busy input,
body.is-busy select,
body.is-busy textarea {
  pointer-events: none;
}

body.is-busy .busy-overlay {
  opacity: 1;
  pointer-events: auto;
}

.busy-overlay__panel {
  display: grid;
  justify-items: center;
  gap: 0.9rem;
  min-width: min(100%, 320px);
  padding: 1.2rem 1.35rem;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
  text-align: center;
}

.busy-spinner {
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 50%;
  border: 3px solid rgba(15, 108, 99, 0.2);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 900px) {
  .site-header {
    flex-direction: column;
    align-items: stretch;
  }

  .panel-grid {
    grid-template-columns: 1fr;
  }

  .toolbar {
    justify-content: flex-start;
  }

  .toolbar .fit-app-link {
    width: 100%;
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 1rem, 100%);
  }

  .site-header,
  .form-card,
  .result-card,
  .chart-card,
  .theory-card {
    padding: 1rem;
  }

  .tab-nav {
    grid-template-columns: 1fr;
  }

  .section-nav {
    grid-template-columns: 1fr;
  }

  .paired-inputs,
  .wide-form,
  .manual-layout,
  .chart-control-row,
  .stat-grid {
    grid-template-columns: 1fr;
  }

  .measurement-layout {
    grid-template-columns: 1fr;
  }

  .chart {
    min-height: 340px;
  }
}
