:root {
  --bg: #f3f3f1;
  --surface: #ffffff;
  --surface-2: #ebebe8;
  --text: #171717;
  --muted: #5c5c58;
  --line: #d8d8d3;
  --accent: #e0140f;
  --accent-ink: #ffffff;
  --tile: #1c1c1c;
  --tile-text: #ffffff;
  --focus: #1f6fd1;
  --danger: #b3261e;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #111111;
    --surface: #1c1c1c;
    --surface-2: #2a2a2a;
    --text: #f2f2ef;
    --muted: #a5a5a0;
    --line: #353535;
    --accent: #ff2b20;
    --tile: #1c1c1c;
    --focus: #6aa8f5;
    --danger: #ff8a80;
    color-scheme: dark;
  }
}

:root[data-theme='dark'] {
  --bg: #111111;
  --surface: #1c1c1c;
  --surface-2: #2a2a2a;
  --text: #f2f2ef;
  --muted: #a5a5a0;
  --line: #353535;
  --accent: #ff2b20;
  --tile: #1c1c1c;
  --focus: #6aa8f5;
  --danger: #ff8a80;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: #1b1b1b; color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; text-decoration: none; }
.brand img { display: block; height: 28px; width: auto; }
.brand span {
  padding-left: 10px; border-left: 1px solid #444; color: #c9c9c4;
  font-size: 13px; font-weight: 600; line-height: 1.2; white-space: nowrap;
}
@media (max-width: 419px) { .brand span { display: none; } }
.list-link {
  display: flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 12px;
  border-radius: 999px; text-decoration: none; font-weight: 600; background: #2c2c2c;
}
.badge {
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: var(--accent); color: var(--accent-ink); font-size: 13px; line-height: 22px; text-align: center;
}

main { max-width: 680px; margin: 0 auto; padding: 16px; }

/* ---- home grid ---- */
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (min-width: 520px) { .grid { grid-template-columns: repeat(4, 1fr); } }
.tile {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  aspect-ratio: 1 / 1; padding: 10px 6px; border-radius: var(--radius);
  background: var(--tile); color: var(--tile-text); text-decoration: none;
  border: 1px solid transparent;
}
.tile:active { transform: scale(.97); }
.tile:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.tile .icon { width: 52%; height: auto; fill: var(--accent); }
.tile span { font-size: 13px; font-weight: 700; text-transform: uppercase; text-align: center; line-height: 1.15; }
@media (prefers-color-scheme: dark) { .tile { border-color: #2c2c2c; } }

/* ---- calculator ---- */
.calc-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.calc-head h1 { font-size: 22px; margin: 0; }
.calc-head .std { margin-left: auto; font-size: 13px; color: var(--muted); text-align: right; }
.back {
  display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%;
  background: var(--surface-2); text-decoration: none; font-size: 26px; line-height: 1;
}

.seg {
  display: flex; gap: 4px; padding: 4px; margin: 0 0 12px; border-radius: 10px; background: var(--surface-2);
  grid-column: 1 / -1;
}
.seg button {
  flex: 1; min-height: 38px; border: 0; border-radius: 7px; background: transparent; color: var(--muted);
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
}
.seg button[aria-checked='true'] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .15); }

.drawing {
  margin: 0 0 14px; padding: 8px 8px 10px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line);
}
.drawing figcaption { text-align: center; font-size: 14px; font-weight: 600; color: var(--muted); }
.section { display: block; width: 100%; height: auto; max-height: 46vh; }
.section .solid { fill: var(--accent); fill-opacity: .9; stroke: var(--accent); stroke-width: 1; }
.section .dim { color: var(--muted); }
.section .dim line { stroke: currentColor; stroke-width: .8; }
.section .dim .ext { stroke-width: .5; }
.section .dim .leader { stroke-dasharray: 2 2; }
.section .dim circle { fill: currentColor; }
.section .dim text { fill: currentColor; font-size: 12px; }
.section .dim.active { color: var(--focus); }
.section .dim.active line { stroke-width: 1.4; }
.section .dim.active text { font-weight: 700; }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field > span:first-child { font-size: 13px; font-weight: 600; color: var(--muted); }
.field i { font-style: normal; font-weight: 400; }
.input-unit {
  display: flex; align-items: stretch; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); overflow: hidden;
}
.input-unit:focus-within { border-color: var(--focus); box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 30%, transparent); }
.input-unit input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; }
.input-unit em, .input-unit select {
  display: grid; place-items: center; padding: 0 10px; font-style: normal; color: var(--muted);
  background: var(--surface-2); border: 0; border-left: 1px solid var(--line); font-size: 15px;
}
.input-unit select { width: auto; border-radius: 0; }
input, select {
  width: 100%; min-height: 46px; padding: 0 12px; font: inherit; font-size: 17px; color: var(--text);
}
.field > select {
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}
select:focus-visible, button:focus-visible, a:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.chip {
  grid-column: 1 / -1; justify-self: start; min-height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px dashed var(--line); background: transparent; color: var(--text); font: inherit; font-size: 14px; cursor: pointer;
}

.error { margin: 12px 0 0; padding: 10px 12px; border-radius: 10px; color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); font-size: 14px; }

.details { margin: 16px 0 0; padding: 4px 14px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); }
.details div { display: flex; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.details div:last-child { border-bottom: 0; }
.details dt { color: var(--muted); font-size: 14px; }
.details dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }

/* ---- sticky result bar ---- */
.resultbar {
  position: sticky; bottom: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px; max-width: 680px; margin: 16px auto 0;
  padding: 10px 16px calc(env(safe-area-inset-bottom) + 10px);
  background: #1b1b1b; color: #fff; border-radius: 16px 16px 0 0;
}
.res { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.res output { font-size: 26px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.res small { color: #bdbdb8; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.primary, .btn {
  min-height: 46px; padding: 0 16px; border: 0; border-radius: 10px; font: inherit; font-weight: 700; cursor: pointer;
}
.primary { background: var(--accent); color: var(--accent-ink); }
.primary:disabled { opacity: .45; }
.icon-btn { display: grid; place-items: center; width: 46px; height: 46px; border: 0; border-radius: 10px; background: #2f2f2f; color: #fff; cursor: pointer; }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

/* ---- order list ---- */
.empty { color: var(--muted); text-align: center; padding: 32px 8px; }
.items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.items li {
  display: flex; align-items: center; gap: 10px; padding: 10px 6px 10px 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line);
}
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; text-decoration: none; }
.item-main span { color: var(--muted); font-size: 14px; }
.items b { white-space: nowrap; font-variant-numeric: tabular-nums; }
.remove { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; color: var(--muted); font-size: 24px; cursor: pointer; }
.totals { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.totals div { flex: 1 1 140px; display: flex; flex-direction: column; padding: 12px 14px; border-radius: var(--radius); background: var(--tile); color: #fff; }
.totals span, .totals small { color: #bdbdb8; font-size: 13px; }
.totals strong { font-size: 22px; font-variant-numeric: tabular-nums; }
.list-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.list-actions .btn { display: grid; place-items: center; text-decoration: none; background: var(--surface-2); color: var(--text); }
.list-actions .whatsapp { grid-column: 1 / -1; background: #1f9d55; color: #fff; }
.list-actions .danger { grid-column: 1 / -1; background: transparent; color: var(--danger); border: 1px solid var(--line); }

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 96px); z-index: 20;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: 999px; background: #2b2b2b; color: #fff; font-size: 14px;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { .toast, .tile { transition: none; } .tile:active { transform: none; } }

/* ---- back to ARSDEF Portal (only when opened with ?from=portal) ---- */
.portal-back {
  position: fixed; left: 12px; bottom: calc(env(safe-area-inset-bottom) + 12px); z-index: 15;
  padding: 7px 12px; border-radius: 999px; background: #1b1b1b; color: #fff; border: 1px solid #3a3a3a;
  font-size: 13px; font-weight: 700; text-decoration: none; box-shadow: 0 2px 8px rgba(0, 0, 0, .25);
}
/* Leave an empty strip where the pill sits, so at rest it never covers a control. */
body:has(.portal-back) main { padding-bottom: 60px; }
/* Calculator: sit above the sticky result bar, in a gap kept free just above it. */
body:has(.portal-back):has(.resultbar) main { padding-bottom: 0; }
body:has(.portal-back) .resultbar { margin-top: 60px; }
body:has(.resultbar) .portal-back { bottom: calc(env(safe-area-inset-bottom) + 92px); }
.portal-back:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
