:root {
  color-scheme: dark;
  --bg: #111311;
  --panel: #1b1d1b;
  --panel-2: #222522;
  --text: #f7f4ef;
  --muted: #9d9a94;
  --line: rgba(255, 255, 255, 0.1);
  --accent: #ff7a3d;
  --good: #95d8a1;
  --bad: #ff9b9b;
  --goal: #d7ae55;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

:root.light {
  color-scheme: light;
  --bg: #f5f3ef;
  --panel: #ffffff;
  --panel-2: #f3f0ea;
  --text: #1d1d1b;
  --muted: #77736c;
  --line: rgba(20, 20, 20, 0.1);
  --shadow: 0 18px 45px rgba(45, 37, 26, 0.11);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select {
  font: inherit;
}

.app-shell {
  width: min(1500px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.brand, .actions, .legend, .icon-key, .section-head, .chart-head, .button-row {
  display: flex;
  align-items: center;
}

.brand { gap: 10px; }
.brand h1 {
  margin: 0;
  font-size: clamp(1.45rem, 4vw, 2rem);
  letter-spacing: 0;
  line-height: 1;
}
.actions { gap: 8px; }
.logo-mark {
  width: 42px;
  height: 29px;
  flex: 0 0 auto;
  overflow: visible;
  color: rgba(255, 255, 255, 0.96);
  filter: drop-shadow(0 7px 16px rgba(255, 122, 61, 0.15));
  transform: translateY(1px);
}

.logo-mark path {
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.logo-sun {
  stroke-width: 2.85;
}

.logo-horizon {
  stroke-width: 2.85;
  opacity: 0.96;
}

:root.light .logo-mark {
  color: rgba(29, 29, 27, 0.92);
  filter: drop-shadow(0 7px 14px rgba(255, 122, 61, 0.13));
}

.icon-button {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.icon-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  border: 1px solid var(--line);
  border-radius: 24px 24px 0 0;
  overflow: hidden;
  background: var(--panel);
}

.summary-grid article {
  min-width: 0;
  padding: clamp(12px, 2vw, 24px);
  border-right: 1px solid var(--line);
}
.summary-grid article:last-child { border-right: 0; }
.summary-grid span {
  display: block;
  color: var(--muted);
  font-weight: 700;
  font-size: clamp(0.72rem, 1.7vw, 0.95rem);
  white-space: nowrap;
}
.summary-grid strong {
  display: block;
  margin-top: 10px;
  font-size: clamp(1.1rem, 3.1vw, 2.2rem);
  letter-spacing: 0;
  white-space: nowrap;
}
.summary-grid strong.bad { color: var(--bad); }
.summary-grid strong.good { color: var(--good); }

.summary-grid .status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: fit-content;
  min-width: 74px;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: clamp(0.95rem, 2.1vw, 1.35rem);
  line-height: 1;
  border: 1px solid transparent;
}

.summary-grid .status-pill.safe {
  color: #d9ffe0;
  background: rgba(95, 190, 111, 0.18);
  border-color: rgba(149, 216, 161, 0.34);
}

.summary-grid .status-pill.risk {
  color: #ffd2d2;
  background: rgba(255, 125, 125, 0.16);
  border-color: rgba(255, 155, 155, 0.32);
}

.chart-panel, .table-panel, details {
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.chart-panel {
  position: relative;
  border-top: 0;
  border-radius: 0 0 24px 24px;
  padding: 16px 18px 8px;
  margin-bottom: 14px;
}

.chart-head, .section-head {
  justify-content: space-between;
  gap: 12px;
}

.chart-head p, .section-head p {
  margin: 0 0 3px;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.8rem;
}

.chart-head h2, .section-head h2 {
  margin: 0;
  font-size: clamp(1rem, 2.4vw, 1.3rem);
}

.legend {
  gap: 12px;
  color: var(--muted);
  font-size: 0.8rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  margin-right: 5px;
}
.good { background: var(--good); }
.bad { background: var(--bad); }
.goal { background: var(--goal); }

.icon-key {
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 750;
}

.icon-key span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  padding: 5px 9px 5px 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel-2) 86%, transparent);
  white-space: nowrap;
}

.key-icon {
  width: 20px;
  height: 20px;
  display: inline-grid;
  place-items: center;
  border-radius: 50%;
  color: #111;
  font-size: 0.78rem;
  line-height: 1;
}

.key-icon svg {
  width: 13.5px;
  height: 13.5px;
  display: block;
  overflow: visible;
}

.key-icon path,
.key-icon circle,
.key-icon rect {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.key-icon.muted path,
.key-icon.muted circle,
.key-icon.muted rect {
  stroke-width: 1.8;
}

.key-icon.accent { background: var(--accent); }
.key-icon.goal { background: var(--goal); }
.key-icon.muted {
  background: var(--muted);
  color: var(--bg);
}

#forecastChart {
  display: block;
  width: 100%;
  height: min(55vh, 560px);
  min-height: 360px;
  margin-top: 8px;
  touch-action: none;
}

.chart-tip {
  position: absolute;
  pointer-events: none;
  transform: translate(-50%, -120%);
  background: rgba(25, 25, 25, 0.92);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.15);
  padding: 7px 10px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 0.82rem;
  white-space: nowrap;
}

.table-panel {
  border-radius: 20px;
  padding: 14px;
  margin-bottom: 14px;
}
.ghost-button, .button-row button, .small-button, .remove-button {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 12px;
  padding: 8px 12px;
  cursor: pointer;
}

.small-button {
  padding: 7px 11px;
  font-size: 0.78rem;
  font-weight: 850;
}

.inline-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.inline-actions select {
  width: auto;
  min-width: 150px;
  padding: 7px 28px 7px 10px;
  font-size: 0.78rem;
}

.remove-button {
  align-self: end;
  color: var(--bad);
  min-height: 38px;
}
.table-wrap {
  margin-top: 12px;
  max-height: 560px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 14px;
}
.table-wrap.collapsed { max-height: 265px; }
table {
  border-collapse: collapse;
  width: 100%;
  min-width: 980px;
  font-size: 0.82rem;
}
th, td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}
th:first-child, td:first-child, th:nth-child(2), td:nth-child(2) { text-align: left; }
thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: color-mix(in srgb, var(--panel) 92%, #000 8%);
  color: var(--muted);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0;
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

details {
  border-radius: 18px;
  padding: 0;
  overflow: hidden;
}
summary {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  list-style: none;
  padding: 16px 54px 16px 16px;
  font-weight: 850;
}
summary::-webkit-details-marker { display: none; }
details[open] summary { border-bottom: 1px solid var(--line); }

summary::after {
  content: "";
  position: absolute;
  top: 50%;
  right: 16px;
  width: 28px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel-2);
  transform: translateY(-50%);
}

summary::before {
  content: "";
  position: absolute;
  top: 50%;
  right: 26px;
  z-index: 1;
  width: 7px;
  height: 7px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: translateY(-62%) rotate(45deg);
  transition: transform 160ms ease, border-color 160ms ease;
}

details[open] summary::before {
  border-color: var(--accent);
  transform: translateY(-38%) rotate(225deg);
}

summary:hover::after {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line));
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  padding: 14px;
}
label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-weight: 750;
  font-size: 0.78rem;
}
.check {
  grid-template-columns: auto 1fr;
  align-items: center;
  color: var(--text);
}
input, select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 11px;
}
input[type="range"] { padding: 0; accent-color: var(--accent); }
.helper {
  color: var(--muted);
  margin: 0;
  padding: 14px 14px 0;
  line-height: 1.5;
}
.button-row {
  gap: 8px;
  padding: 14px;
}
details > label { padding: 0 14px 14px; }

.subsection-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 14px 0;
}

.subsection-title span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.subsection-title strong {
  display: block;
  margin-top: 3px;
  font-size: 0.95rem;
}

.goal-list,
.milestone-list {
  padding: 14px;
  display: grid;
  gap: 10px;
}

.goal-item,
.milestone-item {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--muted);
}

.goal-item.disabled {
  opacity: 0.58;
}

.goal-topline,
.milestone-topline,
.goal-editor,
.milestone-editor {
  display: grid;
  gap: 10px;
}

.goal-topline,
.milestone-topline {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.goal-topline strong,
.milestone-topline strong {
  color: var(--text);
  font-size: 0.82rem;
}

.goal-editor {
  grid-template-columns: minmax(140px, 1.1fr) minmax(90px, 0.55fr) repeat(4, minmax(100px, 0.7fr)) auto;
  align-items: end;
}

.milestone-editor {
  grid-template-columns: minmax(140px, 1.2fr) minmax(120px, 0.8fr) minmax(150px, 1fr) auto;
  align-items: end;
}

@media (max-width: 860px) {
  .app-shell { padding: 10px; }
  .summary-grid { grid-template-columns: repeat(3, 1fr); border-radius: 18px 18px 0 0; }
  .summary-grid article { padding: 11px 10px; border-bottom: 1px solid var(--line); }
  .summary-grid article:nth-child(3n) { border-right: 0; }
  .summary-grid article:nth-last-child(-n + 3) { border-bottom: 0; }
  .chart-panel { border-radius: 0 0 18px 18px; padding: 12px 8px 4px; }
  .chart-head { align-items: flex-start; }
  .legend { font-size: 0.72rem; gap: 8px; }
  .icon-key {
    gap: 6px;
    font-size: 0.7rem;
    margin-top: 10px;
  }
  .icon-key span {
    min-height: 28px;
    padding: 4px 7px 4px 5px;
  }
  .key-icon {
    width: 18px;
    height: 18px;
    font-size: 0.7rem;
  }
  #forecastChart { height: min(56vh, 430px); min-height: 330px; }
  .controls-grid, .form-grid { grid-template-columns: 1fr; }
  .goal-editor, .milestone-editor { grid-template-columns: 1fr; }
  .goal-topline, .milestone-topline { grid-template-columns: 1fr; }
  .inline-actions { flex-wrap: wrap; justify-content: flex-end; }
  .remove-button { width: 100%; }
  .table-panel { padding: 10px; }
}
