:root {
  --bg: #f4f1ec;
  --surface: #fffdf9;
  --surface-2: #efe9e0;
  --ink: #2a2119;
  --ink-2: #6b5d50;
  --line: #ddd3c6;
  --accent: #8a4b25;
  --accent-ink: #fff;
  --accent-soft: #f3e4d8;
  --cut: #e01b12;
  --wood: #e9d3b4;
  --wood-edge: #b88b5c;
  --sheet: #f7ead6;
  --ok: #2f7a4a;
  --warn: #9a6a00;
  --warn-bg: #fff4d6;
  --err: #b3261e;
  --err-bg: #fde7e4;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(42, 33, 25, .06), 0 4px 16px rgba(42, 33, 25, .06);
  --tap: 46px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1713;
    --surface: #25201b;
    --surface-2: #2f2922;
    --ink: #f2ebe2;
    --ink-2: #b4a697;
    --line: #3d352d;
    --accent: #e09a62;
    --accent-ink: #1b1713;
    --accent-soft: #3a2b1f;
    --cut: #ff4a3d;
    --wood: #5a4532;
    --wood-edge: #a07850;
    --sheet: #2e261e;
    --warn: #f0c34a;
    --warn-bg: #3a3017;
    --err: #ff8a80;
    --err-bg: #3f1d1a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1rem; }
a { color: inherit; }
.muted { color: var(--ink-2); }
.small { font-size: .85rem; }
.block { display: block; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.ico { width: 22px; height: 22px; flex: none; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn:disabled { opacity: .45; cursor: not-allowed; filter: none; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn.small { min-height: 36px; font-size: .9rem; padding: 0 10px; }
.icon-btn {
  display: inline-grid; place-items: center; width: var(--tap); height: var(--tap);
  border-radius: 12px; border: 1px solid transparent; background: transparent; color: var(--ink); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.danger { color: var(--err); }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 2px; }

/* ---------- Páginas ---------- */
.page { max-width: 960px; margin: 0 auto; padding: 16px 16px 48px; }
.home { display: flex; flex-direction: column; gap: 24px; padding-top: 32px; min-height: 100dvh; }
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark { display: grid; place-items: center; width: 52px; height: 52px; border-radius: 14px; background: var(--accent); color: var(--accent-ink); }
.brand-mark .ico { width: 30px; height: 30px; }
.brand h1 { font-size: 1.75rem; }
.brand p { margin: 2px 0 0; }

.tools { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.tool-card {
  position: relative; display: flex; flex-direction: column; gap: 14px; padding: 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  text-decoration: none; box-shadow: var(--shadow); min-height: 160px;
  transition: transform .12s ease, border-color .12s ease;
}
a.tool-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.tool-card h2 { font-size: 1.15rem; }
.tool-card p { margin: 4px 0 0; color: var(--ink-2); font-size: .92rem; }
.tool-ico { display: grid; place-items: center; width: 46px; height: 46px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); }
.tool-ico .ico { width: 26px; height: 26px; }
.tool-ico.small { width: 38px; height: 38px; }
.tool-card.soon { opacity: .62; cursor: default; box-shadow: none; background: transparent; border-style: dashed; }
.tool-card.soon .tool-ico { background: var(--surface-2); color: var(--ink-2); }
.badge { position: absolute; top: 16px; right: 16px; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-2); background: var(--surface-2); padding: 4px 8px; border-radius: 999px; }

.recent { display: flex; flex-direction: column; gap: 6px; }
.recent h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); margin-bottom: 4px; }
.recent-item { display: flex; justify-content: space-between; gap: 12px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; text-decoration: none; min-height: var(--tap); align-items: center; }
.recent-item:hover { border-color: var(--accent); }
.home-foot { margin-top: auto; display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.link-row { display: inline-flex; align-items: center; gap: 8px; min-height: var(--tap); font-weight: 600; text-decoration: none; color: var(--accent); }

.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.topbar.plain { background: transparent; border: 0; padding: 0 0 12px; position: static; }
.topbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.eyebrow { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-2); font-weight: 600; }
.title-input { width: 100%; font: inherit; font-weight: 700; font-size: 1.05rem; color: var(--ink); background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 2px 6px; margin-left: -6px; }
.title-input:hover, .title-input:focus { border-color: var(--line); background: var(--surface); }
.save-state { font-size: .8rem; color: var(--ok); white-space: nowrap; }
.save-state.dirty { color: var(--warn); }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; margin: 8px 0 12px; }

/* ---------- Editor ---------- */
.editor-page { display: flex; flex-direction: column; height: 100dvh; }
.editor { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 380px; }
.canvas { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.canvas-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 10px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.zoom { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.toggle { display: inline-flex; align-items: center; gap: 6px; padding: 0 10px; min-height: var(--tap); font-size: .9rem; font-weight: 600; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--accent); }
.sheet-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.sheet-tabs button { min-height: 36px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit; font-size: .88rem; font-weight: 600; cursor: pointer; }
.sheet-tabs button.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.canvas-area { flex: 1; min-height: 0; position: relative; background:
  radial-gradient(circle, color-mix(in srgb, var(--ink) 12%, transparent) 1px, transparent 1px) 0 0 / 18px 18px, var(--bg); }
.preview-svg { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: grab; user-select: none; }
.preview-svg:active { cursor: grabbing; }
.sheet { fill: var(--sheet); stroke: var(--wood-edge); stroke-width: 1; vector-effect: non-scaling-stroke; }
.sheet-margin { fill: none; stroke: var(--wood-edge); stroke-dasharray: 4 4; stroke-width: 1; vector-effect: non-scaling-stroke; opacity: .6; }
.piece-fill { fill: var(--wood); stroke: none; }
.piece-cut { fill: none; stroke: var(--cut); stroke-width: 1.3; vector-effect: non-scaling-stroke; }
.piece-label, .piece-dim, .dim-text { text-anchor: middle; fill: var(--ink); font-family: system-ui, sans-serif; pointer-events: none; }
.piece-label { font-weight: 700; }
.piece-dim, .dim-text { fill: var(--ink-2); }
.dim-line { stroke: var(--ink-2); stroke-width: 1; vector-effect: non-scaling-stroke; fill: none; }

.side { overflow-y: auto; border-left: 1px solid var(--line); background: var(--bg); padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.form { display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 12px; }
details.card > summary { cursor: pointer; list-style: none; display: flex; align-items: center; justify-content: space-between; min-height: 28px; }
details.card > summary::after { content: "+"; font-weight: 700; color: var(--ink-2); font-size: 1.2rem; }
details.card[open] > summary::after { content: "–"; }
details.card > summary::-webkit-details-marker { display: none; }
.grid2, .grid3 { display: grid; gap: 10px; }
.grid2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-size: .82rem; font-weight: 600; color: var(--ink-2); }
.field-input { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); overflow: hidden; }
.field-input:focus-within { border-color: var(--accent); }
.field input, .field select, .note-input {
  width: 100%; min-width: 0; min-height: var(--tap); border: 0; background: transparent; color: var(--ink);
  font: inherit; font-size: 1.05rem; font-variant-numeric: tabular-nums; padding: 0 10px;
}
.field select { border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 1rem; }
.note-input { border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: .95rem; }
.field input:focus, .note-input:focus { outline: none; }
.suf { padding: 0 10px 0 0; color: var(--ink-2); font-size: .85rem; }
.field-help { font-size: .78rem; color: var(--ink-2); }
.field.invalid .field-input { border-color: var(--err); background: var(--err-bg); }
.field.invalid .field-label { color: var(--err); }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { min-height: 40px; border: 0; border-radius: 9px; background: transparent; color: var(--ink-2); font: inherit; font-weight: 600; font-size: .93rem; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.soon-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-soon { font-size: .76rem; color: var(--ink-2); border: 1px dashed var(--line); border-radius: 999px; padding: 3px 9px; }

.messages { display: flex; flex-direction: column; gap: 8px; }
.messages:empty { display: none; }
.msg { margin: 0; padding: 10px 12px; border-radius: 10px; font-size: .9rem; border-left: 4px solid; }
.msg-error { background: var(--err-bg); border-color: var(--err); color: var(--ink); }
.msg-warn { background: var(--warn-bg); border-color: var(--warn); color: var(--ink); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: .93rem; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.joints { width: 100%; border-collapse: collapse; font-size: .88rem; }
.joints td { padding: 6px 0; border-top: 1px solid var(--line); }
.joints .num { text-align: right; font-variant-numeric: tabular-nums; padding-left: 8px; white-space: nowrap; }
.summary p { margin: 0; }

/* ---------- Diálogo ---------- */
.dialog { border: 0; border-radius: 18px; padding: 20px; width: min(480px, calc(100vw - 32px)); background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.dialog::backdrop { background: rgba(20, 15, 10, .45); }
.dialog h2 { font-size: 1.2rem; margin-bottom: 6px; }
.dialog p { margin: 0 0 14px; }
.export-list { display: flex; flex-direction: column; gap: 10px; }
.help { margin-top: 14px; font-size: .9rem; }
.help summary { cursor: pointer; font-weight: 600; min-height: 32px; }
.help ol { margin: 6px 0 0; padding-left: 20px; color: var(--ink-2); }
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 12px; }

/* ---------- Proyectos y materiales ---------- */
.project-list, .materials { display: flex; flex-direction: column; gap: 10px; }
.project-item { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 8px 8px 12px; }
.project-main { flex: 1; min-width: 0; display: flex; gap: 12px; align-items: center; text-decoration: none; padding: 6px 0; }
.project-main strong { display: block; }
.project-actions { display: flex; }
.empty { text-align: center; padding: 40px 16px; border: 1px dashed var(--line); border-radius: var(--radius); }
.empty p { margin: 0 0 16px; color: var(--ink-2); }
.material-head { display: flex; align-items: center; gap: 8px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-weight: 600; z-index: 50; max-width: calc(100vw - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.25); transition: opacity .3s; }
.toast-error { background: var(--err); color: #fff; }
.toast.out { opacity: 0; }

/* ---------- Pantallas pequeñas ---------- */
@media (max-width: 860px) {
  .editor-page { height: auto; min-height: 100dvh; }
  .editor { grid-template-columns: 1fr; }
  .canvas { height: 46dvh; min-height: 280px; position: sticky; top: 63px; z-index: 2; background: var(--bg); border-bottom: 1px solid var(--line); }
  .side { border-left: 0; overflow: visible; }
  .hide-sm { display: none; }
}
@media (max-width: 560px) {
  .tools { grid-template-columns: 1fr; }
  .tool-card { min-height: 0; flex-direction: row; align-items: center; padding: 16px; }
  .badge { position: static; margin-left: auto; align-self: center; }
  .home { padding-top: 20px; }
  .topbar { gap: 4px; padding: 6px 8px; }
  .topbar .btn { padding: 0 12px; }
  .save-state { display: none; }
  .grid3 { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .field input { font-size: 1rem; padding: 0 6px; }
  .suf { padding-right: 6px; }
}

/* ---------- Editor de piezas (0.2.0) ---------- */
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.tool-group { display: flex; gap: 4px; }
.tool { display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; }
.tool.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.tool .ico { width: 18px; height: 18px; }
select.mini { min-height: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: .85rem; padding: 0 6px; }
.editor-svg { cursor: crosshair; }
.grid path { stroke: color-mix(in srgb, var(--ink) 10%, transparent); stroke-width: 1; vector-effect: non-scaling-stroke; fill: none; }
.grid path.axis { stroke: color-mix(in srgb, var(--accent) 45%, transparent); }
.pz-fill { fill: color-mix(in srgb, var(--wood) 85%, transparent); stroke: var(--cut); stroke-width: 1.2; vector-effect: non-scaling-stroke; }
.pz.sel .pz-fill { fill: color-mix(in srgb, var(--accent) 22%, var(--wood)); }
.pz.mal .pz-fill { fill: var(--err-bg); stroke: var(--err); stroke-dasharray: 4 3; }
.pz-orig { fill: none; stroke: var(--ink-2); stroke-dasharray: 2 3; stroke-width: 1; vector-effect: non-scaling-stroke; opacity: .7; }
.pz-grabado { fill: none; stroke: #2453d6; stroke-width: 1; vector-effect: non-scaling-stroke; }
.pz-label { text-anchor: middle; dominant-baseline: middle; fill: var(--ink); font-weight: 700; font-family: system-ui, sans-serif; pointer-events: none; }
.union-borde { stroke: var(--ok); fill: none; stroke-linecap: round; opacity: .55; }
.union-borde.mal { stroke: var(--err); }
.borde-sel { stroke: var(--accent); fill: none; stroke-linecap: round; opacity: .9; }
.linea-ranuras { stroke: #2453d6; stroke-dasharray: 6 4; fill: none; }
.vtx { fill: var(--surface); stroke: var(--accent); }
.vtx.on { fill: var(--accent); }
.dibujo { fill: none; stroke: var(--accent); stroke-dasharray: 5 4; }
.vtable { width: 100%; border-collapse: collapse; font-size: .85rem; }
.vtable td { padding: 3px 2px; vertical-align: middle; }
.vtable tr.on { background: var(--accent-soft); }
.vtable .field-label { display: none; }
.vtable .field input { min-height: 36px; font-size: .9rem; }
.static { min-height: var(--tap); display: flex; align-items: center; font-weight: 600; font-variant-numeric: tabular-nums; }
.sub { font-size: .9rem; margin-top: 4px; }
.chip-ok { font-size: .76rem; color: var(--ok); border: 1px solid var(--ok); border-radius: 999px; padding: 3px 9px; }
.msg-ok { background: color-mix(in srgb, var(--ok) 12%, var(--surface)); border-color: var(--ok); }
textarea.coords { min-height: 90px; padding: 8px 10px; font-family: ui-monospace, monospace; resize: vertical; }
@media (max-width: 560px) {
  .tool span { display: none; }
  .canvas-bar .toggle { padding: 0 6px; font-size: .82rem; }
}
.canvas-bar .zoom { flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 560px) {
  .canvas-bar { gap: 4px; padding: 4px 6px; }
  .canvas-bar .zoom .icon-btn { width: 40px; height: 40px; }
}
@media (max-width: 560px) {
  .topbar .eyebrow { display: none; }
  .topbar .icon-btn { width: 40px; height: 40px; }
  .topbar-title { min-width: 60px; }
}
