:root {
  color-scheme: light;
  --ink: #17211e;
  --muted: #64706c;
  --line: #cdd4d1;
  --line-strong: #aeb9b5;
  --page: #f2f4f3;
  --panel: #ffffff;
  --input: #f8faf9;
  --green: #33766d;
  --green-dark: #17231f;
  --green-soft: #e2eeeb;
  --blue: #2b67a0;
  --blue-soft: #e6eff7;
  --gold: #a98234;
  --gold-soft: #f3ecd9;
  --red: #a94a4a;
  --red-soft: #f6e7e7;
  --shadow: 0 10px 30px rgb(24 39 34 / 7%);
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--page); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background: var(--page);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button, input, select { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: 64px;
  padding: 8px clamp(12px, 3vw, 38px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: rgb(255 255 255 / 96%);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.brand {
  min-width: 0;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  text-decoration: none;
}

.brand-mark {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
}

.brand-mark img { width: 100%; height: 100%; display: block; object-fit: contain; }
.brand > span:last-child { min-width: 0; display: grid; gap: 2px; }
.brand strong { font-size: 0.95rem; white-space: nowrap; }
.brand small { color: var(--muted); font-size: 0.72rem; }

.header-actions { display: flex; align-items: center; gap: 8px; }
.privacy-status { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.76rem; font-weight: 700; }
.privacy-status svg { width: 15px; color: var(--green); }

.icon-link, .icon-button {
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  display: inline-grid;
  place-items: center;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 5px;
  text-decoration: none;
}

.icon-link:hover, .icon-button:hover { border-color: var(--green); color: var(--green); }
.icon-link svg, .icon-button svg { width: 17px; height: 17px; }

.workspace {
  width: min(1560px, calc(100% - clamp(20px, 4vw, 64px)));
  margin: 0 auto;
  padding: 22px 0 max(42px, env(safe-area-inset-bottom));
}

.module-tabs {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(5, minmax(150px, 1fr));
  gap: 4px;
  padding: 4px;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: var(--shadow);
}

.module-tabs button {
  min-height: 54px;
  padding: 10px 16px;
  color: #fff;
  background: var(--green);
  border: 1px solid transparent;
  border-radius: 4px;
  font-weight: 800;
  white-space: nowrap;
}

.module-tabs button:hover { background: #28655d; }
.module-tabs button.is-active { background: var(--green-dark); }
.module-tabs button:focus-visible { outline: 3px solid rgb(43 103 160 / 30%); outline-offset: 2px; }

.module-page { display: grid; gap: 16px; margin-top: 20px; }
.module-page[hidden] { display: none; }

.tool-panel {
  min-width: 0;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: var(--shadow);
}

.panel-header {
  min-height: 62px;
  padding: 13px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
}

.panel-header h1, .panel-header h2 { margin: 1px 0 0; font-size: 1rem; line-height: 1.2; }
.panel-header > span { color: var(--muted); font-size: 0.76rem; font-weight: 700; }
.panel-kicker { margin: 0; color: var(--green); font-size: 0.66rem; font-weight: 800; text-transform: uppercase; }
.panel-actions { display: flex; align-items: center; gap: 8px; }

.secondary-button, .run-button {
  min-height: 38px;
  padding: 8px 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 800;
}

.secondary-button { color: var(--ink); background: var(--panel); border: 1px solid var(--line-strong); }
.secondary-button:hover { border-color: var(--green); color: var(--green); }
.run-button { color: #fff; background: var(--green-dark); border: 1px solid var(--green-dark); }
.run-button:hover { background: var(--green); border-color: var(--green); }
.secondary-button svg, .run-button svg { width: 15px; height: 15px; }

.table-scroll { width: 100%; overflow-x: auto; overscroll-behavior-inline: contain; }
.engineering-table { width: 100%; min-width: 760px; border-collapse: collapse; table-layout: fixed; }
.engineering-table th {
  padding: 9px 8px;
  color: var(--muted);
  background: #f8faf9;
  border-bottom: 1px solid var(--line);
  font-size: 0.62rem;
  font-weight: 800;
  text-align: left;
  text-transform: uppercase;
  vertical-align: bottom;
}
.engineering-table td { padding: 5px 4px; border-bottom: 1px solid #e8ecea; vertical-align: middle; }
.engineering-table tr:last-child td { border-bottom: 0; }
.engineering-table tbody tr:nth-child(4n + 1) td { background: var(--gold-soft); }
.engineering-table tbody tr:nth-child(4n + 2) td { background: var(--blue-soft); }
.engineering-table tbody tr:nth-child(4n + 3) td { background: var(--red-soft); }
.engineering-table tbody tr:nth-child(4n + 4) td { background: var(--green-soft); }
.engineering-table input, .engineering-table select {
  width: 100%;
  min-width: 0;
  height: 32px;
  padding: 5px 8px;
  color: var(--ink);
  background: rgb(255 255 255 / 90%);
  border: 1px solid #cad2cf;
  border-radius: 4px;
  font-size: 0.72rem;
}
.engineering-table input:focus, .engineering-table select:focus, .run-panel input:focus, .run-panel select:focus, .hydraulic-controls input:focus, .cellheat-inputs input:focus {
  outline: 2px solid rgb(51 118 109 / 25%);
  border-color: var(--green);
}
.table-value { display: block; padding: 0 8px; font-size: 0.72rem; font-variant-numeric: tabular-nums; }
.row-remove { width: 30px; height: 30px; display: grid; place-items: center; color: var(--muted); background: #fff; border: 1px solid var(--line); border-radius: 4px; }
.row-remove:hover { color: var(--red); border-color: var(--red); }
.row-remove svg { width: 14px; }

.materials-table th:nth-child(1) { width: 25%; }
.materials-table th:nth-child(2) { width: 14%; }
.materials-table th:nth-child(3), .materials-table th:nth-child(4), .materials-table th:nth-child(5) { width: 18%; }
.materials-table th:last-child { width: 42px; }
.blocks-table { min-width: 1080px; }
.blocks-table th:nth-child(1) { width: 17%; }
.blocks-table th:nth-child(2) { width: 16%; }
.blocks-table th:nth-child(3), .blocks-table th:nth-child(4) { width: 10%; }
.blocks-table th:nth-child(5), .blocks-table th:nth-child(6), .blocks-table th:nth-child(7) { width: 9%; }
.blocks-table th:nth-child(8) { width: 13%; }
.blocks-table th:last-child { width: 42px; }
.connections-table th:nth-child(1), .connections-table th:nth-child(2) { width: 27%; }
.connections-table th:nth-child(3) { width: 20%; }
.connections-table th:nth-child(4) { width: 20%; }
.connections-table th:last-child { width: 42px; }

.run-panel form {
  padding: 14px 16px;
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr)) auto minmax(90px, auto);
  gap: 10px;
  align-items: end;
}
.run-panel label, .hydraulic-controls label, .cellheat-inputs label {
  min-width: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 5px;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
}
.run-panel label input, .run-panel label select, .hydraulic-controls input, .cellheat-inputs input {
  grid-column: 1 / -1;
  width: 100%;
  height: 36px;
  padding: 6px 9px;
  color: var(--ink);
  background: var(--input);
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 0.76rem;
}
.run-status { min-height: 38px; display: flex; align-items: center; color: var(--green); font-size: 0.75rem; }

.results-panel { min-height: 360px; }
.metric-grid { padding: 14px 16px; display: grid; grid-template-columns: repeat(5, minmax(120px, 1fr)); gap: 8px; }
.metric-card { min-width: 0; min-height: 84px; padding: 11px; display: grid; align-content: center; gap: 5px; background: #f8faf9; border: 1px solid var(--line); border-radius: 4px; }
.metric-card small { color: var(--muted); font-size: 0.62rem; font-weight: 800; text-transform: uppercase; }
.metric-card strong { font-size: 1.25rem; line-height: 1; font-variant-numeric: tabular-nums; }
.metric-card em { color: var(--gold); font-size: 0.66rem; font-style: normal; font-weight: 800; }
.metric-grid.compact { padding: 0; grid-template-columns: repeat(2, minmax(110px, 1fr)); }

.chart-shell { margin: 0 16px 16px; overflow: hidden; border: 1px solid var(--line); border-radius: 4px; background: #fff; }
.chart-toolbar { min-height: 44px; padding: 9px 12px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--line); font-size: 0.75rem; }
.chart-legend { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 9px; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 0.64rem; font-weight: 700; }
.legend-item::before { width: 14px; height: 3px; content: ""; background: var(--series-color); }
.temperature-chart { width: 100%; aspect-ratio: 2.7 / 1; min-height: 260px; }
.temperature-chart svg { width: 100%; height: 100%; display: block; }
.chart-grid line { stroke: #e2e7e5; stroke-width: 1; }
.chart-label { fill: #64706c; font: 11px Inter, sans-serif; }
.chart-axis { fill: #3c4743; font: 600 12px Inter, sans-serif; }
.chart-series { fill: none; stroke-width: 3; stroke-linejoin: round; stroke-linecap: round; }

.visual-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.visual-panel { min-height: 360px; }
.assembly-view, .flow-chart { min-height: 290px; padding: 18px; display: grid; place-items: center; overflow: auto; }
.assembly-stack { width: min(100%, 630px); display: grid; grid-auto-flow: column; grid-auto-columns: minmax(110px, 1fr); align-items: center; gap: 30px; }
.assembly-block { position: relative; min-height: 140px; padding: 15px 10px; display: grid; place-items: center; text-align: center; background: var(--block-color); border: 2px solid var(--block-border); border-radius: 4px; }
.assembly-block:not(:last-child)::after { position: absolute; left: calc(100% + 3px); top: 50%; width: 24px; height: 2px; content: ""; background: var(--line-strong); }
.assembly-block strong { font-size: 0.8rem; }
.assembly-block span { color: var(--muted); font-size: 0.66rem; }
.network-svg { width: min(100%, 760px); min-width: 620px; height: auto; display: block; }
.network-node rect { fill: #fff; stroke: var(--green); stroke-width: 2; }
.network-node text { fill: var(--ink); font: 700 12px Inter, sans-serif; text-anchor: middle; }
.network-arrow { fill: none; stroke: var(--gold); stroke-width: 3; marker-end: url(#arrowhead); }
.network-flow-label { fill: var(--muted); font: 600 10px Inter, sans-serif; text-anchor: middle; }

.comparison-panel [data-case-comparison] { min-height: 76px; }
.save-case-form { display: flex; gap: 8px; }
.save-case-form input { width: min(220px, 42vw); min-height: 38px; padding: 7px 9px; border: 1px solid var(--line); border-radius: 4px; }
.case-table { min-width: 720px; }
.case-table button, .library-item button { color: var(--red); background: transparent; border: 0; }
.empty-state { padding: 24px 16px; color: var(--muted); font-size: 0.78rem; }
.engineering-note { margin: 0; padding: 4px 3px; color: var(--muted); font-size: 0.7rem; line-height: 1.5; }

.hydraulics-panel, .openfoam-panel, .cellheat-panel, .library-panel { min-height: 620px; }
.hydraulic-controls { padding: 14px 16px; display: grid; grid-template-columns: repeat(3, minmax(140px, 1fr)); gap: 10px; border-bottom: 1px solid var(--line); }
.hydraulic-table { min-width: 900px; }
.hydraulic-results { padding: 18px 16px; display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(260px, 0.6fr); gap: 16px; }
.hydraulic-loop { min-height: 260px; display: grid; grid-template-columns: repeat(4, minmax(110px, 1fr)); align-items: center; gap: 24px; padding: 18px; border: 1px solid var(--line); border-radius: 4px; }
.loop-component { position: relative; min-height: 92px; padding: 12px; display: grid; place-items: center; text-align: center; background: var(--blue-soft); border: 2px solid var(--blue); border-radius: 4px; }
.loop-component:not(:last-child)::after { position: absolute; left: calc(100% + 4px); top: 50%; width: 18px; height: 2px; content: ""; background: var(--blue); }
.loop-component strong { font-size: 0.76rem; }
.loop-component span { color: var(--muted); font-size: 0.64rem; }

.openfoam-layout { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(240px, 0.4fr); }
.openfoam-layout .table-scroll { grid-column: 1 / -1; }
.cfd-viewer { min-height: 470px; position: relative; display: grid; place-items: center; overflow: hidden; background: #18201e; border-right: 1px solid var(--line); perspective: 900px; color: #aeb9b5; }
.cfd-domain { position: relative; width: min(62%, 560px); aspect-ratio: 1.8 / 1; transform: rotateX(58deg) rotateZ(-24deg); transform-style: preserve-3d; border: 2px solid #7faea7; box-shadow: inset 0 0 0 20px rgb(51 118 109 / 7%); }
.cfd-domain::before { position: absolute; inset: 13% 34%; content: ""; border: 4px solid #d3a94e; border-radius: 50%; box-shadow: 0 0 22px rgb(211 169 78 / 35%); }
.cfd-domain::after { position: absolute; inset: 0; content: ""; background-image: linear-gradient(#57736e 1px, transparent 1px), linear-gradient(90deg, #57736e 1px, transparent 1px); background-size: 22px 22px; opacity: 0.45; }
.cfd-details { padding: 18px; color: var(--muted); font-size: 0.76rem; line-height: 1.55; }
.cfd-details strong { color: var(--ink); }
.cfd-details dl { display: grid; grid-template-columns: 1fr auto; gap: 9px; }
.cfd-details dt, .cfd-details dd { margin: 0; }
.cfd-details dd { color: var(--ink); font-weight: 700; }

.cellheat-layout { padding: 16px; display: grid; grid-template-columns: minmax(300px, 0.7fr) minmax(300px, 1.3fr); gap: 18px; }
.cellheat-inputs { display: grid; grid-template-columns: repeat(2, minmax(130px, 1fr)); gap: 10px; }
.library-list { padding: 16px; display: grid; gap: 8px; }
.library-item { padding: 14px; display: grid; grid-template-columns: minmax(0, 1fr) repeat(3, auto); align-items: center; gap: 16px; background: #f8faf9; border: 1px solid var(--line); border-radius: 4px; }
.library-item div { min-width: 0; display: grid; gap: 3px; }
.library-item strong { font-size: 0.82rem; }
.library-item small { color: var(--muted); font-size: 0.66rem; }
.library-item > span { color: var(--ink); font-size: 0.74rem; font-weight: 700; white-space: nowrap; }

@media (max-width: 1050px) {
  .workspace { width: min(100% - 24px, 1560px); }
  .run-panel form { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
  .metric-grid { grid-template-columns: repeat(3, minmax(110px, 1fr)); }
  .visual-grid { grid-template-columns: 1fr; }
  .hydraulic-results, .cellheat-layout { grid-template-columns: 1fr; }
  .hydraulic-loop { grid-template-columns: repeat(2, minmax(120px, 1fr)); }
  .openfoam-layout { grid-template-columns: 1fr; }
  .cfd-viewer { border-right: 0; border-bottom: 1px solid var(--line); }
}

@media (max-width: 700px) {
  .app-header { min-height: 58px; padding-top: max(7px, env(safe-area-inset-top)); }
  .brand-mark { width: 40px; height: 40px; flex-basis: 40px; }
  .privacy-status { display: none; }
  .workspace { width: calc(100% - 16px); padding-top: 12px; }
  .module-tabs { grid-template-columns: repeat(5, minmax(145px, 1fr)); }
  .module-tabs button { min-height: 48px; font-size: 0.76rem; }
  .module-page { margin-top: 10px; gap: 10px; }
  .panel-header { align-items: flex-start; flex-wrap: wrap; }
  .panel-actions { width: 100%; justify-content: flex-end; }
  .engineering-table { min-width: 720px; }
  .run-panel form { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 12px; }
  .run-panel .run-button, .run-status { width: 100%; }
  .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 12px; }
  .metric-card { min-height: 76px; }
  .chart-shell { margin: 0 10px 10px; }
  .temperature-chart { aspect-ratio: 1.45 / 1; min-height: 250px; }
  .assembly-view, .flow-chart { padding: 12px; min-height: 270px; justify-content: start; }
  .assembly-stack { min-width: 620px; }
  .save-case-form { width: 100%; }
  .save-case-form input { flex: 1; width: auto; }
  .hydraulic-controls, .cellheat-inputs { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 12px; }
  .hydraulic-loop { grid-template-columns: 1fr; }
  .loop-component:not(:last-child)::after { display: none; }
  .cfd-viewer { min-height: 330px; }
  .cellheat-layout { padding: 12px; }
  .library-list { padding: 10px; }
  .library-item { grid-template-columns: 1fr auto; gap: 8px; }
  .library-item > span:nth-of-type(2) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
