:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --border: #dfe7f2;
  --text: #1f2d3d;
  --muted: #60708a;
  --accent: #1c6fe5;
  --accent-soft: #eaf2ff;
  --warning: #fff4d8;
  --warning-text: #7c5c00;
  --success: #dff7ec;
  --danger: #ffedf0;
  --shadow: 0 12px 36px rgba(16, 23, 40, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; font-family: "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
body { padding: 18px; }
button, input, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.modal {
  position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(17, 23, 33, 0.4);
}
.modal.visible { display: flex; }
.modal-card {
  background: var(--panel); border-radius: 18px; box-shadow: var(--shadow); width: min(420px, 90vw); padding: 24px; display: grid; gap: 14px;
}
.modal-card h2 { margin: 0; }
.modal-card label { display: grid; gap: 8px; color: var(--muted); }
.modal-card input { border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px; }
.modal-card form { display: grid; gap: 14px; }
.modal-card p { margin: 0; color: var(--muted); }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.login-error { margin: 0; color: #b42318; font-size: 13px; }
#login-btn, .primary, .ghost, .mini-btn { border-radius: 10px; border: 1px solid transparent; padding: 10px 14px; }
#login-btn, .primary { background: var(--accent); color: white; }
#login-btn:disabled { opacity: .65; cursor: wait; }
.ghost, .mini-btn { background: var(--panel); border-color: var(--border); color: var(--text); }

.app-shell { display: grid; gap: 18px; }
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 16px; background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 20px 22px; box-shadow: var(--shadow);
}
.eyebrow { color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-size: 11px; }
.topbar h1 { margin: 0; font-size: 1.7rem; }
.topbar-actions { display: flex; gap: 10px; flex-wrap: wrap; }

.workspace { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(320px, 0.75fr); gap: 18px; }
.panel { min-width: 0; }
.form-panel { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }
.form-panel { grid-column: 1; grid-row: 1; }
.form-panel > .card { min-width: 0; }
.result-panel { grid-column: 2; grid-row: 1; position: sticky; top: 18px; align-self: start; display: grid; gap: 18px; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 18px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 14px; }
.card.is-collapsible h3 { margin: 0; }
.card-toggle {
  display: flex; width: 100%; align-items: center; justify-content: space-between; gap: 12px;
  padding: 0; border: 0; background: transparent; color: inherit; text-align: left;
  font-size: 1em; font-weight: 700;
}
.card-toggle::after { content: "+"; color: var(--muted); font-size: 1.2em; font-weight: 400; }
.card-toggle[aria-expanded="true"]::after { content: "−"; }
.card-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 3px; }
.card-content { padding-top: 14px; }
.type-switch { display: flex; gap: 10px; flex-wrap: wrap; }
.type-switch button { background: #edf4ff; color: var(--text); border: 1px solid transparent; border-radius: 999px; padding: 10px 14px; }
.type-switch button.active { background: var(--accent); color: white; }
.insulation-switch { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.insulation-switch button {
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px;
  background: #fff; color: var(--text); font-weight: 600;
}
.insulation-switch button.active { background: var(--accent); border-color: var(--accent); color: white; }
.insulation-switch button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.insulation-panel { display: grid; gap: 12px; padding: 16px; border: 1px solid var(--border); border-radius: 14px; background: #fbfcfe; }
.insulation-panel h4 { margin: 0; }
.insulation-panel .table-hint { margin: 0; }
.insulation-results { margin-top: 6px; }
.insulation-result-total input { border-color: var(--accent); }
.fields-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.field {
  display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
.field input, .field select { width: 100%; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: #fffaf2; }
.platform-switch { display: flex; align-items: center; gap: 10px; min-height: 42px; }
.platform-switch input[type="checkbox"] { width: 42px; height: 24px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.field.is-disabled { opacity: .55; }
.field input:disabled { cursor: not-allowed; background: #f1f4f8; }
.field.is-computed input { background: #f7f9fb; }
.computed-fields input { background: #f1f4f8; color: var(--text); font-weight: 600; }

.summary-card { background: linear-gradient(135deg, var(--accent-soft), white); border: 1px solid var(--border); border-radius: 18px; padding: 18px; box-shadow: var(--shadow); }
.summary-label { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.summary-amount { font-size: clamp(2rem, 3vw, 2.6rem); font-weight: 700; margin: 8px 0; }
.calculation-status { margin: 8px 0; color: var(--muted); font-size: 13px; }
.summary-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.summary-grid div { background: rgba(255,255,255,.45); border-radius: 10px; padding: 10px; display: grid; gap: 4px; }
.summary-grid span { color: var(--muted); font-size: 12px; }
.summary-grid .summary-dimensions { grid-column: 1 / -1; }

.chart-wrap {
  display: grid;
  place-items: center;
  min-height: 300px;
  padding: 18px 0 22px;
  background: radial-gradient(ellipse at center, #f3f7ff 0, rgba(243, 247, 255, 0) 68%);
}
.donut {
  width: clamp(220px, 72%, 280px);
  aspect-ratio: 1;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: conic-gradient(#4f7cff 0 40%, #f6c453 40% 70%, #34c38f 70% 92%, #d05f77 92% 100%);
  position: relative;
  filter: drop-shadow(0 12px 18px rgba(38, 63, 105, .16));
  transition: filter .2s ease, transform .2s ease;
}
.donut:hover { transform: translateY(-2px); filter: drop-shadow(0 16px 22px rgba(38, 63, 105, .2)); }
.donut::before {
  content: "";
  position: absolute;
  inset: 34px;
  border-radius: 50%;
  background: linear-gradient(145deg, #fff, #f8faff);
  box-shadow: inset 0 2px 8px rgba(31, 45, 61, .06), 0 0 0 1px rgba(255, 255, 255, .9);
}
#donut-center {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 68%;
  aspect-ratio: 1;
  border-radius: 50%;
  color: #233b5d;
  font-size: clamp(1.7rem, 3vw, 2.25rem);
  font-weight: 750;
  letter-spacing: -.04em;
  font-variant-numeric: tabular-nums;
}
.legend { list-style: none; margin: 16px 0 0; padding: 0; display: grid; gap: 8px; }
.legend li { display: flex; gap: 8px; align-items: center; }
.legend i { width: 12px; height: 12px; display: inline-block; border-radius: 50%; }
.mini-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.mini-table th, .mini-table td { border: 1px solid var(--border); padding: 8px 10px; font-size: 13px; }
.mini-table th { background: #f5f8fd; }
.mini-table input { width: 100%; border: 0; background: transparent; }
.table-scroll { min-width: 0; max-width: 100%; overflow-x: auto; }
.estimate-table { min-width: 470px; margin: 12px 0 0; }
.estimate-table th:not(:first-child), .estimate-table td:not(:first-child) { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.estimate-table td:first-child { min-width: 150px; }
.estimate-table tfoot { font-weight: 700; }
.estimate-total { font-weight: 700; background: #f5f8fd; }
.estimate-swatch { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; }
.pipe-table { min-width: 1000px; }
.pipe-table input { min-width: 76px; }
.pipe-table td[data-pipe-mass], .pipe-table td[data-pipe-part-cost], .pipe-table td[data-pipe-purchase-mass], .pipe-table td[data-pipe-cost] { white-space: nowrap; font-variant-numeric: tabular-nums; }
.delivery-table { min-width: 720px; }
.delivery-table input, .delivery-table select { min-width: 120px; }
.delivery-table td:last-child { white-space: nowrap; font-variant-numeric: tabular-nums; }
.painting-table { min-width: 900px; }
.painting-table td { white-space: nowrap; }
.painting-table input[type="number"] { min-width: 88px; border: 1px solid var(--border); border-radius: 6px; padding: 6px; background: white; }
.painting-choice { display: flex; align-items: center; gap: 7px; }
.painting-choice input { width: auto; accent-color: var(--accent); }
.painting-table tfoot { font-weight: 700; background: #f5f8fd; }
.table-hint { margin: -4px 0 12px; color: var(--muted); font-size: 13px; }
.legend li strong { margin-left: auto; font-variant-numeric: tabular-nums; }
.table-actions { display: flex; gap: 8px; margin: 0 0 16px; }

.saved-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.saved-list li { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; display: flex; justify-content: space-between; align-items: center; }
.saved-actions { display: flex; gap: 12px; }
.saved-list button { background: transparent; border: 0; color: var(--accent); padding: 0; }
.saved-list button[data-action="delete"] { color: #c0394b; }

@media (max-width: 980px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .form-panel, .result-panel { grid-column: 1; }
  .form-panel { grid-row: 2; }
  .result-panel { grid-row: 1; position: static; }
}

@media (max-width: 720px) {
  body { padding: 12px; }
  .app-shell, .workspace, .form-panel, .result-panel { gap: 12px; }
  .topbar { align-items: stretch; flex-direction: column; padding: 16px; }
  .topbar h1 { font-size: 1.45rem; }
  .topbar-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .topbar-actions button { min-width: 0; padding: 9px 8px; }
  .card, .summary-card { padding: 15px; border-radius: 14px; }
  .summary-amount { font-size: clamp(1.8rem, 8vw, 2.4rem); overflow-wrap: anywhere; }
  .summary-grid div { min-width: 0; }
  .summary-grid strong { overflow-wrap: anywhere; }
  .chart-wrap { min-height: 240px; padding: 10px 0 16px; }
  .saved-list li { align-items: flex-start; flex-wrap: wrap; gap: 8px; }
}

@media (max-width: 400px) {
  body { padding: 8px; }
  .topbar, .card, .summary-card { padding: 12px; }
  .topbar-actions { gap: 6px; }
  .topbar-actions button { font-size: .9rem; }
  .summary-grid { gap: 7px; }
  .summary-grid div { padding: 8px; }
  .donut { width: min(72vw, 240px); }
  .donut::before { inset: 28px; }
}
