:root {
  --red: #E30613;
  --anthracite: #575757;
  --turquoise: #0C8282;
  --line: #d9d9d9;
  --bg: #FFFFFF;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, "Liberation Sans", Arimo, "DejaVu Sans", sans-serif;
  color: var(--anthracite);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.45;
}
[x-cloak] { display: none !important; }
body:not(.app) { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }
body:not(.app) > main { flex: 1 0 auto; }
body:not(.app) > .footer { padding-top: 12px; }
/* Other pages (licences, login) scroll as a whole; on wide screens the header stays at the top
   like on the calculator. On narrow screens it scrolls away, also like on the calculator. */
@media (min-width: 901px) {
  body:not(.app) > .topbar { position: sticky; top: 0; z-index: 3; background: var(--bg); }
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px 16px;
  border-bottom: 1px solid var(--line);
}
.brandmark { width: 10px; height: 44px; background: var(--red); }
.topbar h1 {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0.02em;
  color: #000;
  text-transform: uppercase;
}
.topbar .sub { margin: 2px 0 0; font-size: 13px; }

/* The app fills the viewport; only the input list and the result pane scroll.
   The page is exactly one screen tall but is not overflow:hidden, so it stays a normal
   document and the browser's pull-to-refresh keeps working on touch screens. */
body.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
body.app .topbar { flex: none; }

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  grid-template-rows: minmax(0, 1fr);
  gap: 16px;
  padding: 16px 16px 6px;
  width: 100%;
}
.panel { border: 1px solid var(--line); padding: 20px; min-width: 0; }
#result { overflow-y: auto; min-height: 0; position: relative; }
/* The result content fills a 1920 px screen and stops stretching beyond that. */
#result-content { max-width: 1440px; }

/* input panel: scrolling list above a fixed Calculate bar */
.form { display: flex; flex-direction: column; padding: 0; min-height: 0; }
.form-scroll { position: relative; flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior-y: contain; padding: 20px 20px 8px; }
.form-actions { flex: none; padding: 12px 20px 16px; border-top: 1px solid var(--line); background: var(--bg); }
.form-actions .cta { margin-top: 0; }

@media (max-width: 900px) {
  /* narrow screens: normal page scroll, Calculate bar stays pinned to the bottom */
  body.app { display: block; height: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: none; padding: 16px; }
  .topbar { padding: 14px 16px; }
  .form-scroll { overflow: visible; }
  .form-actions { position: sticky; bottom: 0; z-index: 2; }
  #result { overflow: visible; }
}

/* -- form ----------------------------------------------------------- */
.form h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--red);
  margin: 18px 0 10px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}
.form > h2:first-child { margin-top: 0; }

label { display: block; font-size: 12.5px; margin-bottom: 12px; }
.hint { color: #6b6b6b; font-weight: normal; font-size: 11.5px; margin-left: 4px; }

input[type=text], select {
  display: block;
  width: 100%;
  height: 36px;
  margin-top: 4px;
  padding: 0 9px;
  font: inherit;
  font-size: 14px;
  color: #000;
  border: 1px solid var(--anthracite);
  border-radius: 0;
  background: #fff;
}
/* number fields with a typo (see the pattern in index.html), marked once the user has left the field */
input[type=text]:user-invalid { border: 2px solid var(--red); padding: 0 8px; }
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--turquoise);
  outline-offset: 1px;
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-flow { display: grid; grid-template-columns: 1fr 110px; gap: 12px; }

/* "Calculate" choice: a radio group drawn as three toggle buttons */
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tab {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  font: inherit;
  font-size: 12.5px;
  padding: 9px 6px;
  text-align: center;
  color: var(--anthracite);
  background: #fff;
  border: 1px solid var(--anthracite);
  cursor: pointer;
}
.tab:hover { border-color: var(--red); color: var(--red); }
.tab.active { color: #fff; background: var(--anthracite); border-color: var(--anthracite); }
.tab:focus-within { outline: 2px solid var(--turquoise); outline-offset: 2px; }

/* segmented radio group */
.seg { display: flex; gap: 18px; margin: 4px 0 12px; }
.seg label { display: flex; align-items: center; gap: 6px; margin: 0; font-size: 13px; cursor: pointer; }
.seg input { accent-color: var(--turquoise); margin: 0; }

.cta {
  width: 100%;
  margin-top: 8px;
  padding: 11px;
  font: inherit;
  font-size: 15px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #fff;
  background: var(--red);
  border: none;
  cursor: pointer;
}
.cta:hover { background: #c00511; }
.cta { position: relative; }
.cta[disabled] { cursor: progress; }
/* Busy indication: the button is disabled for the whole request (no double submits); a spinner in
   the button and a faded old result appear only if the request takes longer than 250 ms, so a
   normal (fast) calculation shows no flash at all. */
.cta .spinner {
  position: absolute; left: 16px; top: 50%; width: 16px; height: 16px; margin-top: -8px;
  border: 2px solid rgba(255, 255, 255, 0.45); border-top-color: #fff; border-radius: 50%;
  opacity: 0;
}
.form-actions.htmx-request .cta .spinner { animation: busy-show 0s linear 0.25s forwards, busy-spin 0.8s linear infinite; }
#result.htmx-request > * { opacity: 0.45; transition: opacity 0.15s linear 0.25s; }
@keyframes busy-show { to { opacity: 1; } }
@keyframes busy-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .form-actions.htmx-request .cta .spinner { animation: busy-show 0s linear 0.25s forwards; }
}

/* -- output ---------------------------------------------------------- */
.output-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.output-title { margin: 0; font-weight: bold; font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--red); }
.ghost {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 12.5px;
  padding: 6px 12px;
  color: var(--anthracite);
  background: #fff;
  border: 1px solid var(--anthracite);
  cursor: pointer;
}
.ghost:hover { color: var(--red); border-color: var(--red); }

/* icons (Lucide, inline SVG, drawn in the text colour) */
.icon { width: 16px; height: 16px; flex: none; }
.ghost .icon { width: 15px; height: 15px; }
.ghost .icon-done { display: none; }
.ghost.done .icon-default { display: none; }
.ghost.done .icon-done { display: block; color: var(--turquoise); }

.headline { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 8px; }
.tile {
  flex: 1 1 160px;
  max-width: 250px;
  padding: 12px 14px;
  border-left: 3px solid var(--turquoise);
  background: rgba(12, 130, 130, 0.06);
}
.tile-label { display: flex; align-items: center; gap: 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
.tile-sub { display: block; margin-top: 2px; font-size: 13px; color: var(--anthracite); }
.tile-label .icon { width: 15px; height: 15px; color: var(--turquoise); }
.tile-value { display: block; margin-top: 2px; font-size: 26px; font-weight: bold; color: #000; }
.tile-value small { font-size: 13px; font-weight: normal; color: var(--anthracite); }

.result-title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--red);
  margin: 22px 0 10px;
}
.table-scroll { position: relative; overflow-x: auto; }
.curves { width: 100%; border-collapse: collapse; font-size: 13px; }
.curves th, .curves td {
  text-align: right;
  padding: 5px 6px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.curves thead th { border-bottom: 2px solid var(--anthracite); font-weight: bold; vertical-align: bottom; }
.curves .rowhead { text-align: left; }
.curves td { color: #000; }
.curves td.solved, .kpi td.solved { background: rgba(12, 130, 130, 0.12); font-weight: bold; }

.kpi { width: 100%; margin-top: 18px; border-collapse: collapse; font-size: 13.5px; }
.kpi th, .kpi td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); }
.kpi th { font-weight: normal; width: 26%; }
.kpi td { color: #000; font-weight: bold; width: 24%; }

.alert {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 0 0 14px;
  padding: 10px 12px;
  border-left: 4px solid var(--red);
  background: #fbeaea;
  color: #000;
  font-size: 13.5px;
}
.alert .icon { color: var(--red); margin-top: 1px; }
.note { font-size: 12.5px; margin-top: 10px; }
.disclaimer { font-style: italic; }

.output-actions, .output-actions form { display: flex; flex-wrap: wrap; gap: 8px; }
/* applied to the result block while it is rendered to an image */
#result-content.exporting { padding: 20px; background: #fff; }
#result-content.exporting .table-scroll { overflow: visible; }
.empty { font-size: 13.5px; color: #6b6b6b; max-width: 46ch; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* -- login / logout ---------------------------------------------------- */
.topbar-tools { margin-left: auto; display: flex; align-items: center; gap: 16px; }
.topbar-tools .check { margin: 0; }
.login-wrap { display: flex; justify-content: center; align-items: flex-start; padding: 48px 16px; }
.login { width: 100%; max-width: 380px; }
.login .sec {
  font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--red);
  margin: 0 0 14px; padding-bottom: 4px; border-bottom: 1px solid var(--line);
}
input[type=password] {
  display: block; width: 100%; margin-top: 4px; padding: 7px 9px;
  font: inherit; font-size: 14px; color: #000; border: 1px solid var(--anthracite);
  border-radius: 0; background: #fff;
}
input[type=password]:focus-visible { outline: 2px solid var(--turquoise); outline-offset: 1px; }

/* Psychrometric chart. Two layouts:
   - screens wider than 900 px: hidden behind a "Show chart" button and shown in a dialog,
     which also has the "Download image" button
   - narrow screens (phones, stacked layout): chart inline, below the table */
.result-grid { display: grid; grid-template-columns: minmax(0, 1fr); }
/* The dialog is sized so the whole chart (600 × 440, ratio 15:11) fits the window without scrolling:
   its width is the smallest of 92 % of the window width, 860 px, and the width at which the chart
   height still fits 92 % of the window height. Around the chart: 70 px vertically (header 32,
   padding 16 + 20, border 2) and 42 px horizontally (padding 2 × 20, border 2).
   The chart never gets smaller than 330 px high (labels stay readable); below that the dialog scrolls.
   The first width / max-height are the fallback for browsers without dvh support. */
.chart-dialog {
  --chart-min-w: calc(330px * 15 / 11 + 42px);
  padding: 0; border: 1px solid var(--anthracite); color: inherit; overflow: auto;
  width: min(92vw, 860px); max-height: 92vh;
  width: min(92vw, max(var(--chart-min-w), min(860px, calc((92dvh - 70px) * 15 / 11 + 42px))));
  max-height: 92dvh;
}
.chart-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.chart-dialog-body { padding: 16px 20px 20px; }
.chart-dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.chart-dialog-head .btn-label { white-space: nowrap; }
.chart-dialog-head .result-title { margin: 0 0 8px; }
.chart-dialog-actions { display: flex; gap: 8px; }
.psychro-chart { display: block; width: 100%; height: auto; }

/* inline mode: the dialog element is shown as a plain block */
@media (max-width: 900px) {
  .chart-dialog { display: block; position: static; width: auto; max-height: none; overflow: visible; border: 0; margin: 0; }
  .chart-dialog-body { padding: 0; }
  .chart-open-btn, .chart-dialog-actions { display: none; }
  .psychro-chart { max-width: 640px; }
  .result-chart { margin-top: 18px; }
}
/* the image export always contains the chart, below the table */
#result-content.exporting .result-chart { margin-top: 18px; }
#result-content.exporting .chart-dialog { display: block; position: static; width: auto; max-height: none; overflow: visible; border: 0; margin: 0; }
#result-content.exporting .chart-dialog-body { padding: 0; }
#result-content.exporting .psychro-chart { max-width: 640px; }

/* visually hidden, still read by screen readers */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.table-scroll:focus-visible { outline: 2px solid var(--turquoise); outline-offset: 2px; }

/* "Remember my inputs" opt-in */
.check { display: flex; align-items: center; gap: 8px; margin: 18px 0 4px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--turquoise); margin: 0; }

/* footer (licences; imprint and privacy policy will go here too) */
.footer { flex: none; padding: 0 16px 6px; font-size: 11.5px; line-height: 1.4; }
.footer a { color: var(--anthracite); }
.footer a:hover { color: var(--red); }

/* licences page */
.topbar .h1 { margin: 0; font-size: 22px; letter-spacing: 0.02em; color: #000; text-transform: uppercase; font-weight: bold; }
.topbar .sub a { color: var(--anthracite); }
.licenses { width: 100%; max-width: 860px; margin: 0 auto; padding: 24px 16px 8px; }
.licenses h1 { font-size: 20px; color: var(--red); margin: 0 0 12px; }
.licenses h2 { font-size: 15px; color: #000; margin: 28px 0 4px; }
.licenses p { font-size: 13.5px; }
.licenses a { color: var(--turquoise); }
.licenses pre {
  white-space: pre-wrap; font-size: 12px; line-height: 1.45; padding: 12px 14px;
  border: 1px solid var(--line); background: #fafafa; color: #000;
}

/* phones: keep the focused field clear of the pinned Calculate bar */
@media (max-width: 900px) {
  html { scroll-padding-bottom: 96px; }
}

/* input units: a toggle switch  Metric (o-) Imperial  (a checkbox with role="switch") */
.unit-toggle { display: inline-flex; align-items: center; gap: 8px; margin: 2px 0 12px; font-size: 13px; cursor: pointer; }
.unit-toggle .on { color: #000; font-weight: bold; }
.unit-toggle input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; }
.unit-toggle .track {
  position: relative; flex: none; width: 38px; height: 20px; border-radius: 10px;
  background: var(--anthracite); transition: background 0.2s ease;
}
.unit-toggle .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; border-radius: 50%;
  background: #fff; transition: transform 0.2s ease;
}
.unit-toggle input:checked + .track::after { transform: translateX(18px); }
.unit-toggle input:focus-visible + .track { outline: 2px solid var(--turquoise); outline-offset: 2px; }
.unit-toggle:hover .track { background: #3f3f3f; }
@media (prefers-reduced-motion: reduce) { .unit-toggle .track, .unit-toggle .track::after { transition: none; } }
.choice-row h2 { margin-top: 0; }
.choice-row .seg { gap: 12px; flex-wrap: wrap; }
/* inside the grid the bottom margins would add to the next heading's margin instead of collapsing */
.choice-row .seg, .choice-row .unit-toggle { margin-bottom: 0; }
/* output units: same switch, centred in the result header's button row */
.output-actions .unit-toggle { margin: 0 8px 0 0; }

/* result table and the rows below it stay compact on wide screens */
.result-main { max-width: 900px; }

@media (max-width: 600px) {
  /* brand bar and title stay side by side; the tools move to a line of their own */
  .topbar { flex-wrap: wrap; row-gap: 10px; }
  .topbar > .brandmark + div { flex: 1 1 0; min-width: 0; }
  .topbar-tools { flex-basis: 100%; margin-left: 26px; justify-content: space-between; }
}
