:root {
  --paper: #faf7f2; --panel: #fff; --ink: #1b1f23; --muted: #5d646b; --line: #e4ddd2;
  --accent: #b14623; --accent-ink: #fff; --danger: #c62828; --ok: #2e7d32;
  color-scheme: light;
  --font-body: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Barlow Condensed", "IBM Plex Sans", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --paper: #15171a; --panel: #1e2125; --ink: #ece8e1; --muted: #a0a6ad; --line: #33373c; --accent: #e0714b; --accent-ink: #15171a; --danger: #ff6b6b; --ok: #6fcf7a; color-scheme: dark; }
}
* { box-sizing: border-box; }
/* Author display rules (flex, inline-block...) would otherwise override the hidden attribute. */
[hidden] { display: none !important; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 16px/1.5 var(--font-body); }
h1, h2, .brand span { font-family: var(--font-display); letter-spacing: .01em; }
h1 { font-size: 2.25rem; font-weight: 700; line-height: 1.1; margin: 0 0 .35rem; }
h2 { font-size: 1.3rem; margin: 0 0 .5rem; }
.topbar { display: flex; flex-wrap: wrap; gap: .4rem 1rem; align-items: center; justify-content: space-between; padding: .4rem 1rem; border-bottom: 1px solid var(--line); background: var(--panel); }
.brand { display: flex; align-items: center; gap: .5rem; text-decoration: none; color: var(--ink); font-size: 1.6rem; font-weight: 700; }
.beta-tag { font: 600 .65rem/1 system-ui; text-transform: uppercase; letter-spacing: .06em; background: var(--accent); color: var(--accent-ink); padding: .25rem .4rem; border-radius: 4px; }
.account { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.credits-chip { display: inline-flex; flex-wrap: wrap; align-items: baseline; gap: 0 .4rem; max-width: 15rem; padding: .35rem .8rem .4rem; border: 1px solid var(--line); border-radius: 14px; font-size: .9rem; }
.credits-chip strong { font-size: 1.05rem; }
.user-chip { display: flex; align-items: center; gap: .4rem; font-size: .9rem; }
.user-chip img { border-radius: 50%; }
.link-btn { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font: inherit; }
.layout { display: grid; grid-template-columns: minmax(320px, 420px) minmax(0, 1fr); gap: 1rem; padding: 1rem clamp(1rem, 2vw, 2rem); align-items: start; }
@media (max-width: 820px) { .layout { grid-template-columns: 1fr; } }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 1.1rem; min-width: 0; }
.lead { color: var(--muted); margin: 0 0 1rem; }
.field { display: block; margin: 0 0 1rem; }
.field-label { display: block; font-weight: 600; margin-bottom: .3rem; }
.field small { color: var(--muted); display: block; margin-top: .25rem; }
input[type=text], input[type=number] { width: 100%; padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; }
input:focus-visible, button:focus-visible, .dropzone:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.seg-row { display: flex; gap: .75rem; align-items: center; }
.seg-row input[type=range] { flex: 1; accent-color: var(--accent); }
.seg-row input[type=number] { width: 5.5rem; }
.dropzone { display: block; border: 2px dashed var(--line); border-radius: 8px; padding: 1rem; text-align: center; cursor: pointer; color: var(--muted); }
.dropzone.over, .dropzone:hover { border-color: var(--accent); color: var(--ink); }
.dropzone input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.btn { font: inherit; font-weight: 600; padding: .55rem 1rem; border-radius: 6px; border: 1px solid var(--accent); cursor: pointer; }
.btn-primary { background: var(--accent); color: var(--accent-ink); width: 100%; margin-top: .5rem; }
.btn-secondary { background: transparent; color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.quote { border-top: 1px solid var(--line); padding-top: 1rem; }
.quote dl { margin: 0; }
.quote dl > div { display: flex; justify-content: space-between; gap: 1rem; padding: .2rem 0; }
.quote dt { color: var(--muted); }
.quote dd { margin: 0; font-variant-numeric: tabular-nums; text-align: right; }
.price-row dd { display: flex; flex-direction: column; align-items: flex-end; }
.price { font-weight: 700; font-size: 1.15rem; }
/* Free runs (FREE_RUNS=true): the price is struck through with a red cross. */
.price.crossed { position: relative; color: var(--muted); padding: 0 .35rem; }
.price.crossed::before, .price.crossed::after { content: ""; position: absolute; left: 0; right: 0; top: 50%; height: 2px; background: var(--danger); }
.price.crossed::before { transform: rotate(18deg); }
.price.crossed::after { transform: rotate(-18deg); }
.beta-free { color: var(--ok); font-weight: 700; font-size: .9rem; }
.hint { color: var(--muted); font-size: .9rem; margin: .5rem 0 0; min-height: 1.3em; }
.error { color: var(--danger); margin: .5rem 0 0; }
#map { height: clamp(380px, 52vh, 680px); border-radius: 8px; background: var(--paper); border: 1px solid var(--line); }
.summary { display: flex; flex-wrap: wrap; gap: 1.5rem; margin: 1rem 0 .5rem; }
.summary div { display: flex; flex-direction: column; }
.summary span { color: var(--muted); font-size: .85rem; }
.summary strong { font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: .4rem .5rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
td .swatch { display: inline-block; width: .7rem; height: .7rem; border-radius: 50%; margin-right: .4rem; vertical-align: middle; }
dialog { border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--ink); max-width: 28rem; width: calc(100% - 2rem); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.packs { display: grid; gap: .6rem; margin: 1rem 0; }
.pack { display: flex; justify-content: space-between; align-items: center; padding: .7rem .9rem; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink); font: inherit; cursor: pointer; }
.topup-heading { font: 600 .8rem/1 system-ui; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1rem 0 .4rem; }
.promo-row { display: flex; gap: .5rem; }
.promo-row input { flex: 1; min-width: 0; font: inherit; padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px; background: var(--paper); color: var(--ink); text-transform: uppercase; }
.promo-ok { color: var(--ok); font-weight: 600; }
.pack:hover:not(:disabled) { border-color: var(--accent); }
.pack strong { font-size: 1.1rem; }

.spinner { display: inline-block; width: 1.4rem; height: 1.4rem; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
.spinner-sm { width: 1rem; height: 1rem; border-width: 2px; border-color: rgba(255,255,255,.4); border-top-color: var(--accent-ink); vertical-align: -.15em; margin-right: .5rem; }
.map-wrap { position: relative; }
.busy { position: absolute; inset: 0; z-index: 1000; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .75rem; border-radius: 8px; background: color-mix(in srgb, var(--panel) 80%, transparent); color: var(--ink); font-weight: 600; text-align: center; padding: 1rem; }
.busy .spinner { width: 2.4rem; height: 2.4rem; border-width: 4px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.login-missing { color: var(--muted); font-size: .85rem; border: 1px dashed var(--line); border-radius: 6px; padding: .35rem .6rem; }

.progress { width: min(320px, 80%); height: .7rem; border-radius: 999px; background: var(--line); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width .4s ease; }
.progress.indeterminate .progress-fill { width: 35%; animation: slide 1.2s ease-in-out infinite alternate; transition: none; }
.progress-label { font-size: .85rem; font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
@keyframes slide { from { margin-left: 0; } to { margin-left: 65%; } }
@media (prefers-reduced-motion: reduce) { .progress.indeterminate .progress-fill { animation: none; width: 100%; opacity: .5; } .progress-fill { transition: none; } }

tbody tr { cursor: default; }
tbody tr.hl, tbody tr:focus-visible { background: color-mix(in srgb, var(--accent) 14%, transparent); outline: none; }
.seg-tip { background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 4px 14px rgba(0,0,0,.2); padding: .6rem .75rem; white-space: normal; max-width: 20rem; font: .85rem/1.35 var(--font-body); }
.seg-tip::before { display: none; }
.seg-tip strong { display: block; margin-bottom: .35rem; font-family: var(--font-display); font-size: 1rem; }
.seg-tip dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: .15rem .75rem; }
.seg-tip dt { color: var(--muted); }
.seg-tip dd { margin: 0; font-variant-numeric: tabular-nums; }

.credits-bar { flex-basis: 100%; height: .35rem; margin-top: .3rem; border-radius: 999px; background: var(--line); overflow: hidden; }
.credits-bar > span { display: block; height: 100%; width: 0; border-radius: 999px; background: var(--ok); transition: width .4s ease, background-color .3s; }
.credits-bar.low > span { background: var(--danger); }
.credits-chip small { flex-basis: 100%; color: var(--muted); font-size: .75rem; min-height: 1em; }
@media (prefers-reduced-motion: reduce) { .credits-bar > span { transition: none; } }

/* Footer: always visible at the bottom of the window (sticky inside a full-height flex column). */
body { display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }
.layout { width: 100%; flex: 1 0 auto; }
.credits-footer { position: sticky; bottom: 0; z-index: 1100; margin-top: 1rem; padding: .45rem 1rem; background: var(--panel); border-top: 1px solid var(--line); color: var(--muted); font-size: .72rem; line-height: 1.35; text-align: center; }
.credits-footer p { margin: .1rem 0; }
@media (max-width: 640px) { .credits-footer p:last-child { display: none; } }
.credits-footer a { color: inherit; text-decoration-color: var(--line); text-underline-offset: 3px; }
.credits-footer a:hover { color: var(--accent); text-decoration-color: var(--accent); }

/* Compact header: small buttons, and a credits chip that stays one line tall. */
.topbar .btn { padding: .25rem .75rem; font-size: .88rem; }
.topbar .account { gap: .5rem; }
.credits-chip { position: relative; overflow: hidden; display: inline-flex; flex-wrap: nowrap; align-items: baseline; gap: .4rem; max-width: none; padding: .25rem .8rem .35rem; border-radius: 999px; font-size: .88rem; }
.credits-chip .credits-bar { position: absolute; left: 0; right: 0; bottom: 0; flex-basis: auto; height: 3px; margin: 0; border-radius: 0; }
.credits-chip .credits-bar > span { border-radius: 0; }
.credits-chip small { display: none; }

fieldset.field { border: 0; padding: 0; margin: 0 0 1rem; min-width: 0; }
fieldset.field legend { padding: 0; }
.mode-toggle { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: .6rem; }
.mode-toggle label { cursor: pointer; }
.mode-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.mode-toggle span { display: block; padding: .3rem .8rem; font-size: .9rem; color: var(--muted); }
.mode-toggle input:checked + span { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.mode-toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.seg-list { list-style: none; margin: 0 0 .5rem; padding: 0; max-height: 15rem; overflow-y: auto; }
.seg-item { display: flex; align-items: center; gap: .5rem; padding: .15rem 0; }
.seg-item .seg-n { width: 1.6rem; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.seg-item input { width: 5.5rem; }
.seg-item input.invalid { border-color: var(--danger); }
.seg-item .icon-btn { margin-left: auto; background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1rem; padding: .1rem .4rem; border-radius: 4px; }
.seg-item .icon-btn:hover { color: var(--danger); }
.seg-rest { color: var(--muted); font-size: .9rem; }
.seg-rest.error-text { color: var(--danger); }
.seg-actions { display: flex; flex-wrap: wrap; gap: .5rem .75rem; align-items: center; margin-bottom: .25rem; }
.seg-actions .fill { display: inline-flex; align-items: center; gap: .35rem; }
.seg-actions .fill input { width: 3.6rem; padding: .25rem .4rem; }
.btn-sm { padding: .2rem .6rem; font-size: .85rem; font-weight: 500; }

.profile-wrap { margin: .75rem 0 0; }
.profile-wrap figcaption { font-weight: 600; font-size: .9rem; margin-bottom: .25rem; }
#profile { display: block; width: 100%; height: 240px; overflow: visible; }
#profile .p-grid { stroke: var(--line); stroke-width: 1; }
#profile .p-label { fill: var(--muted); font: 10px var(--font-body); }
#profile .p-line { fill: none; stroke: var(--ink); stroke-width: 1.5; stroke-linejoin: round; }
#profile .p-seg { opacity: .45; transition: opacity .15s; }
#profile .p-seg.on { opacity: .95; }
#profile .p-cut { stroke: var(--panel); stroke-width: 1; }
#profile .p-marker { stroke: var(--ink); stroke-width: 1; stroke-dasharray: 3 3; pointer-events: none; }
#profile .p-readout { fill: var(--ink); font: 11px var(--font-body); pointer-events: none; }
#profile .p-hit { fill: transparent; cursor: crosshair; }

.advanced { margin: 0 0 1rem; }
.advanced summary { cursor: pointer; font-weight: 600; margin-bottom: .5rem; }
.advanced[open] summary { margin-bottom: .6rem; }
.advanced output { font-variant-numeric: tabular-nums; color: var(--accent); margin-left: .35rem; }
#slope { flex: 1; accent-color: var(--accent); }
.advanced small { display: block; margin-top: .25rem; color: var(--muted); }

/* Segment data box, shown inside the profile next to the hovered segment. */
.profile-wrap { position: relative; }
#seg-tip.seg-tip { position: absolute; z-index: 5; width: 26rem; max-width: 100%; pointer-events: none; font-size: .85rem; line-height: 1.4; }
#seg-tip[hidden] { display: none; }

/* Wide screens: once there is a plan, the table gets its own column next to map and profile. */
@media (min-width: 1500px) {
  .results:has(#plan:not([hidden])) { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(380px, 1fr); column-gap: 1rem; align-items: start; }
  .results:has(#plan:not([hidden])) > .map-wrap, .results:has(#plan:not([hidden])) > #profile-wrap, .results:has(#plan:not([hidden])) > #summary { grid-column: 1; }
  .results:has(#plan:not([hidden])) > .table-wrap { grid-column: 2; grid-row: 1 / span 3; max-height: calc(100vh - 8rem); overflow: auto; }
  .results:has(#plan:not([hidden])) > #run-error, .results:has(#plan:not([hidden])) > #run-note { grid-column: 1 / -1; }
  .table-wrap thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; }
}

.breakdown { margin: 0 0 .3rem; }
.breakdown summary { cursor: pointer; color: var(--muted); font-size: .9rem; padding: .1rem 0; }
.breakdown summary:hover { color: var(--ink); }
.breakdown[open] summary { margin-bottom: .2rem; }

/* Compact sidebar: keep "Ejecutar" visible without scrolling on laptop screens. */
.controls { padding: .75rem .9rem; }
.controls h1 { font-size: 1.9rem; margin: 0 0 .15rem; }
.controls .lead { font-size: .88rem; line-height: 1.35; margin: 0 0 .55rem; }
.controls .field, .controls fieldset.field, .controls .advanced, .controls .seg-section { margin: 0 0 .5rem; }
.controls .field-label { margin-bottom: .15rem; font-size: .92rem; }
.controls .field small { margin-top: .1rem; font-size: .78rem; line-height: 1.3; }
.controls .dropzone { padding: .5rem; }
.controls input[type=text], .controls input[type=number] { padding: .28rem .55rem; }
.controls .mode-toggle { margin-bottom: .35rem; }
.controls .mode-toggle span { padding: .2rem .75rem; }
.controls .advanced summary { margin-bottom: 0; }
.controls .quote { padding-top: .45rem; }
.controls .quote h2 { font-size: 1.15rem; margin: 0 0 .1rem; }
.controls .quote dl > div { padding: .1rem 0; }
.controls .breakdown { margin: 0 0 .1rem; }
.controls .hint { margin: .3rem 0 0; min-height: 0; font-size: .85rem; }
.controls .btn-primary { margin-top: .35rem; padding: .45rem 1rem; }
@media (max-height: 820px) { .credits-footer p:last-child { display: none; } }

/* "Ejecutar" pinned to the bottom of the sidebar (above the footer), with the price next to it. */
.controls { display: flex; flex-direction: column; min-height: calc(100vh - var(--topbar-h, 52px) - var(--footer-h, 52px) - 3rem); min-height: calc(100dvh - var(--topbar-h, 52px) - var(--footer-h, 52px) - 3rem); }
.controls .run-bar { position: sticky; bottom: var(--footer-h, 52px); margin-top: auto; z-index: 3; background: var(--panel); padding: .4rem 0 .1rem; border-top: 1px solid var(--line); box-shadow: 0 -8px 8px -8px rgba(0,0,0,.18); }
.run-bar dl { margin: 0; }
.run-bar dl > div { display: flex; justify-content: space-between; gap: 1rem; padding: .1rem 0; }
.run-bar dt { color: var(--muted); }
.run-bar dd { margin: 0; font-variant-numeric: tabular-nums; text-align: right; }

.suggest-opts { margin-top: .6rem; display: flex; flex-wrap: wrap; align-items: center; gap: .3rem .9rem; }
.suggest-opts .field-label { flex-basis: 100%; margin: 0; }
.suggest-opts label { font-size: .88rem; display: inline-flex; align-items: center; gap: .3rem; }
.suggest-opts input { width: 3.4rem; padding: .2rem .4rem; }

#plan th small { font-weight: 400; color: var(--muted); }
#seg-tip.sheet { position: fixed; left: .5rem; right: .5rem; top: auto; bottom: .5rem; width: auto; max-width: none; z-index: 1300; }
.long { display: inline; }

/* Tablets and phones: one column, lighter chrome, a table that fits. */
@media (max-width: 820px) {
  .layout { padding: .6rem; gap: .6rem; }
  .controls { min-height: 0; }
  .controls .run-bar { bottom: 0; }
  .credits-footer { position: static; margin-top: .6rem; font-size: .7rem; }
  #map { height: 300px; }
  .summary { gap: .5rem 1.2rem; }
  .summary strong { font-size: 1.1rem; }
  th, td { padding: .3rem .35rem; font-size: .85rem; }
  td .swatch { margin-right: .3rem; }
}
@media (max-width: 640px) {
  .topbar { padding: .35rem .75rem; }
  .brand { font-size: 1.4rem; }
  .long, #user-name { display: none; }
  .credits-chip { padding: .2rem .65rem .3rem; }
}
@media (max-width: 640px) {
  th, td { padding: .3rem .25rem; font-size: .8rem; }
  th { white-space: normal; vertical-align: bottom; }
  #plan th small { display: block; }
}

.seg-section > summary { cursor: pointer; margin-bottom: .3rem; }
.seg-section:not([open]) > summary { margin-bottom: 0; }
.seg-summary { font-weight: 400; color: var(--muted); font-size: .85rem; margin-left: .35rem; }
.seg-section[open] .seg-summary { display: none; }
.seg-section > summary { display: list-item; }

.hint-link { color: var(--accent); font: inherit; font-weight: 600; text-decoration: underline; text-underline-offset: 3px; padding: 0; }
.hint-link:hover { text-decoration-thickness: 2px; }
