:root {
  --bg: #e9eff6;
  --panel: #f7fafd;
  --panel-strong: #eef4fb;
  --border: #aebdcf;
  --text: #0f172a;
  --muted: #4f6278;
  --accent: #0f766e;
  --accent-active: #0b625c;
  --tab: #d7e1ed;
  --tab-active: #f7fafd;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  color: var(--text);
  font-family: "Segoe UI", Calibri, Arial, sans-serif;
  background: linear-gradient(180deg, #edf2f8 0%, #e2e9f2 100%);
}

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 8px;
}

.app-header h1 { margin: 0; font-size: 28px; }
.app-header p { margin: 4px 0 0; color: var(--muted); }

.env-pill {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
}

.tabbar {
  display: flex;
  gap: 2px;
  padding: 0;
  border-bottom: none;
  flex: 1 1 auto;
  min-width: 0;
}

.topbar-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  padding-bottom: 8px;
  min-width: 320px;
}

.topbar-download-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

#download-desktop-btn,
#download-manual-btn {
  white-space: nowrap;
}

.download-progress {
  width: min(460px, 100%);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.download-progress > span {
  font-size: 12px;
  color: var(--muted);
  text-align: right;
}

#download-desktop-progress {
  width: 100%;
  height: 10px;
  accent-color: var(--accent);
}

.tab-btn {
  border: 1px solid var(--border);
  border-bottom: none;
  background: var(--tab);
  color: var(--text);
  font-weight: 600;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
}

.tab-btn.active {
  background: var(--tab-active);
}

.tab-btn.hidden {
  display: none;
}

.page {
  padding: 12px;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.card {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  padding: 12px;
}

.card h2 {
  margin: 0 0 8px;
}

.card h3 {
  margin: 8px 0 6px;
}

.muted {
  color: var(--muted);
}

.error-text {
  color: #8a1f1f;
  border-color: #e0a6a6;
  background: #fff4f4;
}

.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 10px;
}

.feature-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}

.feature-card h3 {
  margin-top: 0;
}

.mode-explainer {
  margin-bottom: 8px;
}

.title-help-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title-help-row h3 {
  margin: 0;
}

.title-help-popover {
  position: relative;
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
}

.title-help-btn {
  position: static !important;
  margin: 0;
}

.title-help-card {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  z-index: 30;
  min-width: 420px;
  max-width: 620px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 22px rgba(27, 53, 83, 0.18);
  overflow: hidden;
  display: none;
}

.title-help-popover:hover .title-help-card,
.title-help-popover:focus-within .title-help-card {
  display: block;
}

.mode-table th,
.mode-table td {
  white-space: nowrap;
}

.radio-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
}

.radio-row.two {
  flex-direction: column;
  gap: 8px;
}

.front-checkbox-right {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 10px;
}

.front-checkbox-right > span {
  white-space: nowrap;
}

.cols {
  display: grid;
  gap: 10px;
}

.cols.two {
  grid-template-columns: 1fr 1fr;
}

.cols.two.rain-cols {
  grid-template-columns: 1.1fr 1fr;
  align-items: start;
}

.cols.result-cols {
  grid-template-columns: 1.8fr 1fr;
}

.opland-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  align-items: start;
}

.opland-left-stack {
  display: grid;
  gap: 10px;
  align-items: start;
}

.grid {
  display: grid;
  gap: 10px;
}

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

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

.grid.three {
  grid-template-columns: repeat(3, 1fr);
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 14px;
}

.checkbox-inline-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 24px;
}

.checkbox-inline-label > input[type="checkbox"] {
  margin: 0;
}

label.has-help {
  position: relative;
}

.label-title-row {
  display: inline-flex;
  align-items: flex-start;
  gap: 6px;
  width: max-content;
  max-width: 100%;
}

.label-title-text {
  display: inline;
}

.info-help-btn {
  position: static;
  width: 20px;
  min-width: 20px;
  height: 20px;
  padding: 0;
  margin: 0;
  border-radius: 999px;
  border: 1px solid #5f8fb8;
  background: #eef5fc;
  color: #1f4f7a;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  cursor: help;
}

.info-help-btn:hover,
.info-help-btn:focus-visible {
  background: #dcecfb;
  border-color: #3c78ad;
  color: #143d60;
}

input, select, button {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 8px 10px;
  font-size: 14px;
}

input, select {
  background: #fff;
}

button {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}

button:hover {
  background: var(--accent-active);
  border-color: var(--accent-active);
}

button.ghost {
  background: #fff;
  color: var(--text);
  border-color: var(--border);
}

button.ghost:hover {
  background: #f0f5fb;
  border-color: #95a8be;
}

button.danger {
  background: #b42318;
  border-color: #b42318;
  color: #fff;
}

button.danger:hover {
  background: #991b1b;
  border-color: #991b1b;
}

.actions {
  margin-top: 10px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.actions.compact {
  margin-top: 0;
  justify-content: flex-end;
}

.profile-toolbar {
  display: grid;
  grid-template-columns: 260px 1fr;
  gap: 10px;
  align-items: end;
  margin-bottom: 8px;
}

.profile-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.profile-info {
  margin-top: 6px;
  margin-bottom: 8px;
}

.info-box {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
  display: grid;
  gap: 6px;
}

.info-box.compact {
  margin-top: 8px;
  padding: 8px 10px;
  font-size: 13px;
  white-space: pre-line;
}

.run-readiness {
  margin-top: 8px;
}

.run-readiness.ok {
  border-color: #78b6ab;
  background: #e9f7f4;
  color: #0d4f49;
}

.run-readiness.err {
  border-color: #c8a2a2;
  background: #fff2f2;
  color: #7a1f1f;
}

.run-readiness ul {
  margin: 0;
  padding-left: 18px;
}

.run-estimate {
  margin-top: 4px;
}

.analysis-summaries {
  margin-top: 8px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.summary-lines {
  white-space: pre-line;
}

.calc-preview {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-strong);
  padding: 8px 10px;
  display: grid;
  gap: 4px;
}

.subsection {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.subsection-title-row {
  display: block;
}

.subsection-title-with-toggle {
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}

.subsection-title-with-toggle > input[type="checkbox"] {
  margin: 0;
  width: 18px;
  height: 18px;
}

.subsection.no-top {
  margin-top: 8px;
  padding-top: 0;
  border-top: none;
}

.subsection.hidden {
  display: none;
}

.hidden {
  display: none !important;
}

.catalog-grid {
  align-items: end;
}

.catalog-action-wrap {
  display: flex;
  align-items: flex-end;
  min-height: 58px;
}

.catalog-action-wrap.full-row {
  grid-column: 1 / -1;
  min-height: auto;
}

.catalog-inline-row {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(130px, 0.65fr) auto;
  gap: 10px;
  align-items: end;
}

.catalog-inline-title {
  margin: 0 0 6px;
  font-size: 1.17em;
  font-weight: 700;
}

label.upload-inline-label {
  margin: 0;
  font-size: 1.17em;
  font-weight: 700;
  gap: 8px;
}

.catalog-inline-row > label {
  margin: 0;
}

.catalog-inline-action {
  display: flex;
  align-items: flex-end;
  min-height: 58px;
}

.catalog-pick-btn {
  min-width: 170px;
}

.info-box.catalog-status {
  margin-top: 8px;
}

.info-box.catalog-status.is-success {
  border-color: #78b6ab;
  background: #e9f7f4;
  color: #0d4f49;
  font-weight: 600;
}

.info-box.catalog-status.is-error {
  border-color: #c8a2a2;
  background: #fff2f2;
  color: #7a1f1f;
}

.segmented {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.seg-btn {
  background: #fff;
  color: var(--text);
  border-color: var(--border);
}

.seg-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.seg-btn.disabled {
  opacity: 0.5;
}

.kpi {
  margin-top: 8px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 8px;
}

.kpi.kpi-main {
  grid-template-columns: repeat(3, minmax(150px, 240px));
  justify-content: start;
}

.kpi-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  padding: 8px 10px;
}

.kpi-card .lbl {
  color: var(--muted);
  font-size: 12px;
}

.kpi-card .val {
  margin-top: 2px;
  font-size: 22px;
  font-weight: 700;
  color: var(--accent);
}

.result-controls {
  display: grid;
  grid-template-columns: 1.1fr 1fr 1fr 0.8fr auto;
  align-items: end;
  gap: 8px;
  margin-bottom: 10px;
}

.result-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.plot-wrap {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  padding: 8px;
}

#result-canvas {
  width: 100%;
  height: 380px;
  display: block;
}

.rain-plot-wrap {
  margin-top: 8px;
}

#rain-canvas {
  width: 100%;
  height: 250px;
  display: block;
}

.series-meta {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--muted);
  padding: 6px 10px;
  font-size: 13px;
}

.table-details {
  margin-top: 10px;
}

.table-details summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
}

.table-wrap.table-technical {
  margin-top: 8px;
  max-height: 280px;
}

.history-wrap {
  max-height: 200px;
}

.series-list-wrap {
  max-height: 180px;
}

#loaded-series-table th:first-child,
#loaded-series-table td:first-child {
  width: 70px;
  text-align: center;
}

#loaded-series-table th:nth-child(3),
#loaded-series-table td:nth-child(3) {
  width: 130px;
  text-align: center;
}

#loaded-series-table th:last-child,
#loaded-series-table td:last-child {
  width: 60px;
  text-align: center;
}

#loaded-series-table tbody tr {
  cursor: pointer;
}

.series-delete-btn {
  min-width: 30px;
  width: 30px;
  height: 30px;
  padding: 0;
  line-height: 1;
  border-radius: 999px;
  background: #fff0f0;
  border-color: #f0b4b4;
  color: #b42318;
  font-size: 20px;
  font-weight: 700;
}

.series-delete-btn:hover {
  background: #ffe1e1;
  border-color: #e88989;
  color: #911111;
}

.series-flow-unit {
  min-width: 88px;
  max-width: 110px;
  padding: 4px 6px;
}

.qh-table-wrap {
  max-height: 210px;
}

.qh-table-wrap input {
  width: 100%;
}

.control-events-box {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-strong);
  padding: 8px;
}

.control-events-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.control-events-title-row {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.control-events-wrap {
  max-height: 300px;
}

.control-event-filters {
  margin-top: 8px;
  margin-bottom: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 10px;
}

.control-event-filters label {
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  font-size: 12px;
  line-height: 1.1;
}

.control-event-filters input[type="checkbox"] {
  margin: 0;
  width: 14px;
  height: 14px;
  padding: 0;
}

.control-events-wrap .table tbody tr {
  cursor: pointer;
}

.control-events-wrap .table tbody tr:hover {
  background: #f4f8fc;
}

.table-wrap {
  max-height: 360px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

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

.table th,
.table td {
  border-bottom: 1px solid #e0e8f1;
  padding: 6px 8px;
  text-align: left;
}

.table th {
  background: #edf3fa;
  position: sticky;
  top: 0;
  z-index: 1;
}

.table tbody tr.selected {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  background: #eef8f6;
}

#run-history-table td:first-child,
#run-history-table th:first-child {
  width: 56px;
  text-align: center;
}

.run-history-delta-pos {
  color: #0a5f57;
  font-weight: 600;
}

.run-history-delta-neg {
  color: #7a1f1f;
  font-weight: 600;
}

.run-history-delta-zero {
  color: #4f6278;
}

.json {
  min-height: 300px;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  overflow: auto;
}

.info-text {
  min-height: 520px;
  white-space: pre-wrap;
  line-height: 1.45;
}

.debug-details {
  margin-top: 8px;
}

.debug-details summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 600;
}

.json-small {
  min-height: 160px;
  margin-top: 8px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.48);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 1500;
}

.modal-card {
  width: min(1120px, 98vw);
  max-height: 94vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  padding: 12px;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.28);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.modal-head h3 {
  margin: 0;
}

.modal-close-btn {
  min-width: 34px;
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 24px;
  line-height: 1;
}

.tsv-export-card {
  width: min(720px, 96vw);
}

.tsv-export-grid {
  margin-top: 12px;
}

.tsv-export-columns {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.tsv-export-columns label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  font-weight: 650;
}

.tsv-export-columns input {
  width: 18px;
  height: 18px;
}

.event-editor-layout {
  margin-top: 8px;
  display: grid;
  grid-template-columns: 1.05fr 1fr;
  gap: 10px;
}

.event-editor-data textarea {
  width: 100%;
  min-height: 260px;
  max-height: 64vh;
  resize: vertical;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  font-family: "Consolas", "Courier New", monospace;
  font-size: 13px;
  line-height: 1.4;
}

.event-editor-plot {
  margin-top: 24px;
}

#event-editor-canvas {
  width: 100%;
  height: 320px;
  display: block;
}

@media (max-width: 1100px) {
  .cols.two { grid-template-columns: 1fr; }
  .cols.two.rain-cols { grid-template-columns: 1fr; }
  .cols.result-cols { grid-template-columns: 1fr; }
  .opland-layout { grid-template-columns: 1fr; }
  .result-controls { grid-template-columns: 1fr 1fr; }
  .result-actions { justify-content: flex-start; }
  .kpi.kpi-main { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .grid.three { grid-template-columns: 1fr 1fr; }
  .catalog-inline-row { grid-template-columns: 1fr 1fr auto; }
  .event-editor-layout { grid-template-columns: 1fr; }
  .event-editor-plot { margin-top: 0; }
}

@media (max-width: 760px) {
  .grid.two,
  .grid.three,
  .tsv-export-columns { grid-template-columns: 1fr; }
  .catalog-inline-row { grid-template-columns: 1fr; }
  .catalog-inline-action { min-height: auto; }
  .catalog-pick-btn { width: 100%; min-width: 0; }
  .analysis-summaries { grid-template-columns: 1fr; }
  .result-controls { grid-template-columns: 1fr; }
  .result-actions { flex-wrap: wrap; }
  .topbar {
    flex-wrap: wrap;
    align-items: stretch;
  }
  .tabbar { overflow-x: auto; }
  .topbar-actions {
    width: 100%;
    align-items: stretch;
    padding-bottom: 8px;
  }

  .topbar-download-buttons {
    width: 100%;
  }

  #download-desktop-btn,
  #download-manual-btn {
    width: 100%;
  }

  .download-progress {
    width: 100%;
  }

  .download-progress > span {
    text-align: left;
  }
}
