/* =============================================================
   1. Tokens
   ============================================================= */
:root {
  --paper: #f3f4f1;
  --surface: #ffffff;
  --ink: #1c2226;
  --muted: #5e6a70;
  --line: #d8dcd8;
  --line-strong: #b9c0bc;
  --accent: #f5b800;       /* amarillo cinta métrica */
  --accent-deep: #d99f00;
  --cut: #c93a24;          /* lápiz rojo de carpintero */
  --ok: #2f7a4f;
  --warn: #b3471d;
  --board: #efe3cb;
  --sans: "Archivo", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --r-card: 16px;
  --r-ctl: 8px;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --gutter: clamp(16px, 4vw, 32px);
}

/* =============================================================
   2. Reset & base
   ============================================================= */
*, *::before, *::after { box-sizing: border-box; margin: 0; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;
}
img, svg { display: block; max-width: 100%; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
input, select { font: inherit; color: inherit; }
a { color: inherit; }
p { text-wrap: pretty; }
h1, h2, h3, h4 { text-wrap: balance; line-height: 1.1; }
::selection { background: var(--accent); color: var(--ink); }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; border-radius: 4px; }

/* =============================================================
   3. Utilidades
   ============================================================= */
.wrap { width: 100%; max-width: 1240px; margin-inline: auto; padding-inline: var(--gutter); }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: fixed; top: -100px; left: 1rem; padding: .6rem 1rem; background: var(--ink); color: #fff; z-index: 99; border-radius: 8px; }
.skip-link:focus { top: 1rem; }

/* =============================================================
   4. Cabecera y hero
   ============================================================= */
.site-header { border-bottom: 1px solid var(--line); background: var(--surface); }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 1rem; min-height: 56px; }
.logo { display: flex; align-items: center; gap: .55rem; text-decoration: none; font-weight: 800; font-size: 1.15rem; letter-spacing: -.01em; font-stretch: 90%; }
.header-nav { display: none; gap: 1.5rem; font-size: .92rem; }
.header-nav a { text-decoration: none; color: var(--muted); }
.header-nav a:hover { color: var(--ink); }

.hero { padding-top: clamp(18px, 3.5vw, 40px); padding-bottom: 14px; }
.hero h1 {
  font-size: clamp(1.6rem, 1.1rem + 2.6vw, 2.9rem);
  font-weight: 800;
  font-stretch: 78%;
  letter-spacing: -.015em;
  max-width: 22ch;
}
.hero-sub { margin-top: .55rem; color: var(--muted); max-width: 62ch; font-size: clamp(.95rem, .9rem + .25vw, 1.08rem); }
.ruler {
  margin-top: 16px; height: 14px; max-width: 520px;
  background:
    repeating-linear-gradient(90deg, var(--ink) 0 1px, transparent 1px 50px) bottom / 100% 14px no-repeat,
    repeating-linear-gradient(90deg, var(--ink) 0 1px, transparent 1px 10px) bottom / 100% 7px no-repeat;
  border-bottom: 2px solid var(--ink);
  opacity: .75;
}

/* =============================================================
   5. Herramienta
   ============================================================= */
.tool { padding-bottom: 8px; }
.noscript { padding: 1rem; background: #fff4d6; border-radius: var(--r-ctl); margin-bottom: 1rem; }
.tool-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-card);
  box-shadow: 0 1px 0 rgba(28, 34, 38, .04), 0 12px 32px -18px rgba(28, 34, 38, .25);
  overflow: hidden;
}
.tool-card[data-mode="2d"] .only-1d, .tool-card[data-mode="1d"] .only-2d { display: none !important; }

.mode-switch { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--line); background: var(--paper); }
.mode-switch button {
  display: flex; align-items: center; justify-content: center; gap: .45rem; white-space: nowrap;
  padding: .85rem .4rem; font-weight: 700; font-size: clamp(.84rem, .78rem + .3vw, .95rem); color: var(--muted);
  border-bottom: 3px solid transparent;
  transition: color .2s var(--ease-out), background .2s var(--ease-out);
}
.mode-switch button[aria-selected="true"] { color: var(--ink); background: var(--surface); border-bottom-color: var(--accent); }

.tool-grid { display: grid; }
.tool-inputs { padding: clamp(14px, 2.4vw, 24px); display: grid; gap: 14px; align-content: start; }
.step { border: 0; padding: 0; min-width: 0; display: grid; gap: 10px; }
.step legend { font-weight: 800; font-size: 1.02rem; display: flex; align-items: center; gap: .5rem; margin-bottom: 8px; font-stretch: 90%; }
.step-n {
  display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%;
  background: var(--ink); color: #fff; font-size: .8rem; font-stretch: 100%;
}
.row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.row .grow { grid-column: 1 / -1; }
.field { display: grid; gap: 3px; font-size: .78rem; font-weight: 600; color: var(--muted); min-width: 0; }
.field input, .field select, .pc input {
  width: 100%; min-width: 0; height: 40px; padding: 0 10px;
  border: 1px solid var(--line-strong); border-radius: var(--r-ctl); background: #fff;
  font-size: .95rem; font-weight: 500; color: var(--ink);
  font-variant-numeric: tabular-nums;
  transition: border-color .15s, box-shadow .15s;
}
.field select { padding-right: 6px; }
.field input:focus, .field select:focus, .pc input:focus { outline: none; border-color: var(--ink); box-shadow: 0 0 0 3px rgba(245, 184, 0, .35); }
.invalid { border-color: var(--cut) !important; background: #fff6f4 !important; }
.hint { font-size: .8rem; color: var(--muted); }
#grainHint { color: var(--warn); }

/* Piezas */
.pieces-head { display: none; }
.pieces { display: grid; gap: 8px; }
.piece {
  display: grid; gap: 6px; align-items: end;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 58px 40px 30px;
  grid-template-areas: "name name name name name" "w l q rot del";
  padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: #fafbf9;
}
.tool-card[data-mode="1d"] .piece { grid-template-columns: minmax(0, 1fr) 58px 30px; grid-template-areas: "name name name" "l q del"; }
.pc { display: grid; gap: 2px; min-width: 0; }
.pc-name { grid-area: name; grid-template-columns: 10px 1fr; align-items: center; gap: 8px; }
.pc-w { grid-area: w; } .pc-l { grid-area: l; } .pc-q { grid-area: q; }
.pc-rot { grid-area: rot; display: flex; flex-direction: column-reverse; align-items: center; gap: 2px; }
.pc-rot input { width: 20px; height: 38px; accent-color: var(--ink); margin: 0; }
.pc input { height: 38px; }
.m-label { font-size: .7rem; font-weight: 600; color: var(--muted); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; vertical-align: middle; }
.pc-del { grid-area: del; height: 38px; font-size: 1.3rem; color: var(--muted); border-radius: var(--r-ctl); }
.pc-del:hover { color: var(--cut); background: #fbeeea; }
.pieces-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.btn-add {
  padding: .6rem 1rem; border: 1.5px dashed var(--ink); border-radius: 10px; font-weight: 700;
  background: #fff; transition: background .15s;
}
.btn-add:hover { background: #fff7dc; }
.btn-link { font-size: .85rem; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; padding: .3rem 0; }
.btn-link:hover { color: var(--ink); }

.settings { border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; }
.settings summary { cursor: pointer; padding: 10px 0; font-weight: 700; font-size: .92rem; display: flex; justify-content: space-between; gap: 1rem; }
.settings summary span { font-weight: 500; color: var(--muted); font-size: .85rem; }
.settings[open] { padding-bottom: 12px; }
.settings .row { grid-template-columns: 1fr 1fr; margin-bottom: 10px; }
.check { display: flex; align-items: center; gap: 8px; font-size: .9rem; font-weight: 500; margin-bottom: 8px; }
.check input { width: 18px; height: 18px; accent-color: var(--ink); }

.btn-primary {
  width: 100%; min-height: 54px; border-radius: 12px;
  background: var(--accent); color: var(--ink);
  font-weight: 800; font-size: 1.12rem; letter-spacing: .005em;
  box-shadow: inset 0 -3px 0 var(--accent-deep);
  transition: transform .12s var(--ease-out), background .15s;
}
.btn-primary:hover { background: #ffc61a; }
.btn-primary:active { transform: translateY(1px); box-shadow: inset 0 -1px 0 var(--accent-deep); }
.form-error { color: var(--cut); font-size: .88rem; font-weight: 600; }

/* Salida */
.tool-output { border-top: 1px solid var(--line); padding: clamp(14px, 2.4vw, 24px); min-width: 0; background: #fcfcfb; }
.empty-state, .working-state, .result, .error-state { display: none; }
.tool-card[data-state="idle"] .empty-state { display: grid; }
.tool-card[data-state="working"] .working-state { display: flex; }
.tool-card[data-state="done"] .result { display: grid; }
.tool-card[data-state="error"] .error-state { display: block; }
.empty-state { place-items: center; text-align: center; gap: 12px; color: var(--muted); padding: 24px 8px; }
.empty-state svg { width: min(220px, 60%); color: var(--line-strong); }
.working-state { align-items: center; justify-content: center; gap: 12px; min-height: 220px; font-weight: 600; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--ink); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error-state { padding: 16px; border-radius: 10px; background: #fff6f4; border: 1px solid #f0c9c1; color: #7a2413; }
.error-state ul { margin-top: 6px; padding-left: 1.1rem; }

.result { gap: 14px; min-width: 0; }
.buy { padding: 14px 16px; border-radius: 12px; background: var(--ink); color: #fff; }
.buy p + p { margin-top: 4px; }
.buy-main { font-size: 1.08rem; }
.buy-main strong { color: var(--accent); }
.buy-warn strong { color: #ffb4a6; }
.buy-ok, .buy-cost { font-size: .92rem; color: #d7dcda; }
.buy-cost strong { color: #fff; }

.options { display: flex; gap: 8px; flex-wrap: wrap; }
.options button {
  display: grid; text-align: left; gap: 1px; padding: 8px 12px;
  border: 1px solid var(--line-strong); border-radius: 10px; background: #fff; font-size: .82rem; color: var(--muted);
}
.options button strong { color: var(--ink); font-size: .9rem; }
.options button[aria-checked="true"] { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); background: #fff9e6; }

.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.stats div { padding: 9px 12px; box-shadow: 0 0 0 .5px var(--line); }
.stats dt { font-size: .74rem; color: var(--muted); font-weight: 600; }
.stats dd { margin: 0; font-weight: 800; font-size: 1.12rem; font-variant-numeric: tabular-nums; font-stretch: 90%; }
.stats div:first-child dd { color: var(--ok); }
.optimal-note { font-size: .85rem; color: var(--muted); }

.viewer { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.viewer-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; padding: 6px 8px; border-bottom: 1px solid var(--line); background: var(--paper); }
.vb-group { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.icon-btn { width: 34px; height: 34px; display: grid; place-items: center; border-radius: 8px; border: 1px solid var(--line-strong); background: #fff; }
.icon-btn:hover { border-color: var(--ink); }
.toggle { display: flex; align-items: center; gap: 5px; font-size: .82rem; font-weight: 600; padding: 4px 8px; border-radius: 8px; cursor: pointer; }
.toggle input { accent-color: var(--ink); }
.plan { height: clamp(220px, 58vh, 620px); touch-action: none; cursor: grab; user-select: none; background: #fff; }
.plan:active { cursor: grabbing; }
.plan.is-active { box-shadow: inset 0 0 0 2px rgba(245, 184, 0, .6); }
.plan svg { width: 100%; height: 100%; }
.piece-info { font-size: .85rem; padding: 8px 12px; border-top: 1px solid var(--line); color: var(--muted); min-height: 38px; }
.piece-info strong { color: var(--ink); }

.cutlist { border: 1px solid var(--line); border-radius: 12px; padding: 0 14px; background: #fff; }
.cutlist summary { cursor: pointer; padding: 11px 0; font-weight: 800; }
.cutlist[open] { padding-bottom: 12px; }
.cutlist h4 { font-size: .92rem; margin: 12px 0 6px; }
.cutlist h4 small { font-weight: 500; color: var(--muted); }
.cutlist table { width: 100%; border-collapse: collapse; font-size: .85rem; font-variant-numeric: tabular-nums; }
.cutlist th { text-align: left; font-size: .74rem; color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--line); padding: 4px 6px; }
.cutlist td { padding: 5px 6px; border-bottom: 1px solid #eef0ed; }
.cutlist td .swatch { margin-right: 6px; }
.seq { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; font-variant-numeric: tabular-nums; font-weight: 700; }
.seq span em { font-style: normal; font-weight: 600; color: var(--muted); font-size: .85em; }
.seq span { display: inline-flex; align-items: center; gap: 5px; padding: 3px 8px; border: 1px solid var(--line); border-radius: 6px; background: #fafbf9; }
.seq i.swatch { font-style: normal; }
.seq b { color: var(--muted); font-weight: 500; }
.seq-note { font-size: .8rem; color: var(--muted); margin-top: 4px; }

.downloads { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn-dl { min-height: 44px; border-radius: 10px; background: var(--ink); color: #fff; font-weight: 700; font-size: .92rem; }
.btn-dl:hover { background: #2d363b; }
.btn-dl.ghost { background: #fff; color: var(--ink); border: 1.5px solid var(--ink); }
.btn-dl:disabled { opacity: .6; cursor: progress; }
.project-name { max-width: 420px; }

.privacy { display: flex; gap: 8px; align-items: flex-start; font-size: .82rem; color: var(--muted); margin-top: 12px; max-width: 80ch; }
.privacy svg { flex: none; margin-top: 2px; color: var(--ok); }

/* =============================================================
   6. Secciones de contenido
   ============================================================= */
.section { padding-block: clamp(36px, 6vw, 64px) 0; }
.section h2, .seo-text h2 { font-size: clamp(1.4rem, 1.1rem + 1.2vw, 2rem); font-weight: 800; font-stretch: 82%; margin-bottom: 18px; }
.steps { list-style: none; padding: 0; display: grid; gap: 14px; counter-reset: st; }
.steps li { counter-increment: st; padding: 18px 18px 18px 64px; position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; }
.steps li::before {
  content: counter(st); position: absolute; left: 18px; top: 16px;
  width: 32px; height: 32px; display: grid; place-items: center; border-radius: 50%;
  background: var(--accent); font-weight: 800;
}
.steps h3 { font-size: 1.05rem; margin-bottom: 4px; }
.steps p { color: var(--muted); font-size: .95rem; }

.uses { display: grid; gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.uses div { background: var(--surface); padding: 16px 18px; }
.uses h3 { font-size: 1rem; margin-bottom: 4px; }
.uses p { color: var(--muted); font-size: .92rem; }

.tools-grid { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; }
.tools-grid li { border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.tools-grid a, .tools-grid li.soon { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 12px 14px; font-weight: 600; font-size: .92rem; text-decoration: none; }
.tools-grid a:hover { background: #fff9e6; }
.tools-grid li.soon { color: var(--muted); border-style: dashed; background: transparent; }
.tools-grid small { font-size: .72rem; font-weight: 600; white-space: nowrap; }

.content-grid { display: grid; gap: 24px; padding-top: clamp(36px, 6vw, 64px); }
.seo-text { max-width: 72ch; }
.seo-text h3 { font-size: 1.1rem; margin: 22px 0 6px; }
.seo-text p { color: #333b3f; }
.ad-side { display: none; }

.faq details { border-bottom: 1px solid var(--line); max-width: 80ch; }
.faq summary { cursor: pointer; padding: 14px 0; font-weight: 700; list-style: none; display: flex; justify-content: space-between; gap: 1rem; }
.faq summary::-webkit-details-marker { display: none; }
.faq summary::after { content: "+"; font-size: 1.3rem; line-height: 1; color: var(--muted); }
.faq details[open] summary::after { content: "−"; }
.faq details p { padding-bottom: 14px; color: #333b3f; }

.site-footer { margin-top: clamp(48px, 8vw, 88px); border-top: 1px solid var(--line); background: var(--surface); padding-block: 28px; font-size: .9rem; }
.footer-inner { display: grid; gap: 10px; }
.site-footer nav { display: flex; gap: 18px; flex-wrap: wrap; }
.site-footer nav a { color: var(--muted); }
.small { font-size: .8rem; color: var(--muted); }

/* =============================================================
   7. Anuncios (huecos vacíos, sin saltos de diseño)
   ============================================================= */
.ad-slot {
  display: grid; place-items: center;
  border: 1px dashed var(--line-strong); border-radius: 10px;
  color: #9aa3a0; font-size: .72rem; font-weight: 700; letter-spacing: .08em;
  background: repeating-linear-gradient(135deg, transparent 0 10px, rgba(0,0,0,.018) 10px 20px);
}
.ad-leaderboard { min-height: 100px; margin-top: 28px; }
.ad-incontent { min-height: 250px; margin: 22px 0; }
.ad-sidebar { min-height: 600px; }
.ad-small { min-height: 100px; }
.ad-rect { min-height: 250px; width: 100%; margin: 12px 0; }

.ad-toast {
  position: fixed; right: 12px; bottom: 12px; z-index: 50; width: min(320px, calc(100vw - 24px));
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px;
  box-shadow: 0 12px 32px -12px rgba(0,0,0,.35);
}
.ad-toast[hidden] { display: none; }
.ad-close { position: absolute; top: 2px; right: 6px; font-size: 1.3rem; line-height: 1; padding: 4px 8px; color: var(--muted); z-index: 1; }
.ad-dialog { border: 0; border-radius: 16px; padding: 22px; width: min(420px, calc(100vw - 32px)); box-shadow: 0 24px 64px -16px rgba(0,0,0,.45); }
.ad-dialog::backdrop { background: rgba(28, 34, 38, .45); }
.ad-dialog-msg { font-weight: 700; }
.btn-secondary { width: 100%; min-height: 44px; border-radius: 10px; border: 1.5px solid var(--ink); font-weight: 700; }

/* =============================================================
   8. Responsive
   ============================================================= */
@media (min-width: 540px) {
  .downloads { grid-template-columns: repeat(4, 1fr); }
  .stats { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .piece { grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr) 58px 40px 30px; grid-template-areas: "name w l q rot del"; align-items: end; }
  .tool-card[data-mode="1d"] .piece { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr) 58px 30px; grid-template-areas: "name l q del"; }
  .pc-name { align-self: end; height: 38px; }
  .uses { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 720px) {
  .header-nav { display: flex; }
  .hero h1 { max-width: none; }
  .steps { grid-template-columns: repeat(3, 1fr); }
  .uses { grid-template-columns: repeat(3, 1fr); }
  .footer-inner { grid-template-columns: 1fr auto; align-items: center; }
}
@media (min-width: 960px) {
  .tool-grid { grid-template-columns: minmax(380px, 460px) minmax(0, 1fr); }
  .tool-output { border-top: 0; border-left: 1px solid var(--line); }
  .piece, .tool-card[data-mode="1d"] .piece { padding: 0; border: 0; background: none; }
  .m-label { display: none; }
  .pc-rot input { margin-top: 0; height: 38px; }
  .pieces-head {
    display: grid; gap: 6px; font-size: .72rem; font-weight: 600; color: var(--muted);
    grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(0, 1fr) 58px 40px 30px;
  }
  .tool-card[data-mode="1d"] .pieces-head { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr) 58px 30px; }
  .pieces-head span:first-child { padding-left: 18px; }
  .pieces { max-height: 46vh; overflow-y: auto; padding-right: 2px; }
}
@media (min-width: 1280px) {
  .content-grid { grid-template-columns: minmax(0, 1fr) 300px; }
  .ad-side { display: block; }
  .ad-side .ad-slot { position: sticky; top: 16px; }
}

/* =============================================================
   9. Movimiento reducido (solo lo intrusivo)
   ============================================================= */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

/* =============================================================
   10. Impresión: solo el plano y la lista
   ============================================================= */
@media print {
  @page { size: A4 landscape; margin: 10mm; }
  body { background: #fff; }
  .site-header, .hero, .mode-switch, .tool-inputs, .privacy, .ad-slot, .ad-side, .ad-toast, .ad-dialog,
  .section, .content-grid, .site-footer, .viewer-bar, .piece-info, .downloads, .project-name, .options, .skip-link { display: none !important; }
  .tool-card { border: 0; box-shadow: none; }
  .tool-grid { display: block; }
  .tool-output { border: 0; padding: 0; background: #fff; }
  .buy { background: #fff; color: #000; border: 2px solid #000; }
  .buy-main strong, .buy-cost strong, .buy-ok, .buy-cost { color: #000; }
  .plan { height: 150mm; border: 0; }
  .viewer, .cutlist { border: 0; break-inside: auto; }
  .cutlist h4 { break-after: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
