:root {
  --bg: #f4f2ee;
  --panel: #fff;
  --fg: #1c1917;
  --muted: #78716c;
  --accent: #8b7355;
  --line: #e7e5e4;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Source Sans 3", system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
  min-height: 100vh;
}
.top {
  display: flex;
  justify-content: space-between;
  align-items: end;
  padding: 20px 28px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.top h1 {
  margin: 0;
  font-weight: 600;
  font-size: 1.4rem;
  letter-spacing: -0.02em;
}
.sub { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; }
.status { color: var(--muted); font-size: 0.85rem; }
.layout {
  display: grid;
  grid-template-columns: minmax(320px, 360px) 1fr;
  gap: 0;
  min-height: calc(100vh - 88px);
}
.panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-width: 0;
  max-height: calc(100vh - 88px);
  overflow-y: auto;
  overflow-x: hidden;
}
.panel > * {
  min-width: 0;
}
.margins,
.colors,
.fonts,
.templates,
.type-scale {
  min-width: 0;
}
.margin-grid label,
.color-grid label,
.font-grid label {
  min-width: 0;
}
.margins input,
.colors input,
.fonts select {
  width: 100%;
  min-width: 0;
  max-width: 100%;
}
.theme-block {
  flex: 0 0 auto;
  min-width: 0;
}
.theme-block-label {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.theme-gallery {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  /* Rows must size to content: a definite container height would otherwise
     divide the free space and collapse the aspect-ratio previews. */
  grid-auto-rows: max-content;
  align-content: start;
  gap: 10px;
  max-height: min(340px, 42vh);
  overflow-y: auto;
  padding: 2px;
  flex-shrink: 0;
}
.theme-card {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 6px;
  cursor: pointer;
  text-align: left;
  color: var(--fg);
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  min-height: 0;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.theme-card:hover {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
}
.theme-card[aria-selected="true"] {
  border-color: var(--fg);
  box-shadow: 0 0 0 1px var(--fg);
}
.theme-card-preview {
  aspect-ratio: 210 / 297;
  width: 100%;
  flex: none;
  border-radius: 4px;
  overflow: hidden;
  background: #f5f5f4;
  border: 1px solid var(--line);
}
.theme-card-preview svg {
  width: 100%;
  height: 100%;
  display: block;
}
.theme-card-meta {
  font-size: 0.72rem;
  line-height: 1.25;
  text-transform: none;
  letter-spacing: 0;
}
.theme-card-meta strong {
  display: block;
  font-weight: 600;
  font-size: 0.78rem;
}
.theme-card-meta span {
  color: var(--muted);
}
label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
select, button {
  font: inherit;
  text-transform: none;
  letter-spacing: 0;
}
select {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--fg);
}
.check {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.9rem;
  color: var(--fg);
}
.margins,
.templates {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
/* Collapsible panel groups keep the sidebar scannable as options grow. */
details.group {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
details.group > summary {
  cursor: pointer;
  list-style: none;
  padding: 10px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}
details.group > summary::before {
  content: "";
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.15s;
  flex: none;
}
details.group[open] > summary::before {
  transform: rotate(45deg);
}
details.group > summary::-webkit-details-marker {
  display: none;
}
details.group > *:not(summary) {
  margin: 0 12px 12px;
}
details.group > fieldset {
  border: 0;
  padding: 0;
}
details.group label {
  display: block;
  margin-bottom: 10px;
}
.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: sticky;
  bottom: 0;
  background: var(--panel);
  padding: 10px 0 2px;
  z-index: 2;
}
.templates {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.templates label {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.8rem;
  color: var(--fg);
}
.templates select,
.templates input[type="text"] {
  width: 100%;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  color: var(--fg);
  background: #fff;
}
.template-actions {
  display: flex;
  gap: 8px;
}
.ai-theme {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
}
.ai-theme textarea {
  width: 100%;
  min-width: 0;
  resize: vertical;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  color: var(--fg);
  background: #fff;
}
.template-hint {
  margin: 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
}
.margins legend,
.templates legend,
.colors legend {
  padding: 0 6px;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.margin-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.margins label,
.colors label {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.8rem;
  color: var(--fg);
}
.margins input {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  color: var(--fg);
  background: #fff;
}
.colors {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.color-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.colors input[type="color"] {
  width: 100%;
  height: 34px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
.fonts {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.fonts legend {
  padding: 0 6px;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.font-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.fonts label {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.8rem;
  color: var(--fg);
}
.fonts select {
  width: 100%;
}
.type-scale {
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  min-width: 0;
}
.type-scale legend {
  padding: 0 6px;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.type-base-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}
.type-base-grid label {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.8rem;
  color: var(--fg);
  min-width: 0;
}
.type-base-grid input {
  width: 100%;
  min-width: 0;
}
.type-role-grid {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
}
/* Label, field and unit on one row so twelve roles stay scannable. */
.type-role {
  display: grid;
  grid-template-columns: 1fr 68px auto;
  align-items: center;
  gap: 8px;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.8rem;
  color: var(--fg);
  min-width: 0;
}
.type-role input {
  width: 100%;
  min-width: 0;
}
.type-role .unit {
  color: var(--muted);
  font-size: 0.75rem;
}
.docs-link {
  margin: 0;
  font-size: 0.85rem;
}
.docs-link a {
  color: var(--accent);
}
.share {
  padding: 10px 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #fafaf9;
}
.share-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}
.share code {
  display: block;
  font-size: 0.78rem;
  word-break: break-all;
  color: var(--fg);
}
button.tiny {
  padding: 6px 10px;
  font-size: 0.8rem;
  margin-top: 10px;
  width: 100%;
}
button {
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}
button.primary {
  background: var(--fg);
  color: #fff;
}
button.secondary {
  background: transparent;
  border-color: var(--line);
  color: var(--fg);
}
.meta {
  margin-top: auto;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: pre-wrap;
  line-height: 1.45;
}
.preview {
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(45deg, #ebe7e0 25%, transparent 25%),
    linear-gradient(-45deg, #ebe7e0 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #ebe7e0 75%),
    linear-gradient(-45deg, transparent 75%, #ebe7e0 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}
.preview-stage {
  /* Exact sheet ratio, set from the selected profile (app.js). The stage
     fills the pane: as tall as the viewport allows (header 88px + 2×24px
     padding), capped by the pane width — whichever bites first. */
  --stage-ratio: calc(210 / 297);
  --stage-max-h: calc(100vh - 136px);
  aspect-ratio: var(--stage-ratio);
  width: min(96%, calc(var(--stage-max-h) * var(--stage-ratio)));
  border: 1px solid var(--line);
  background: #fff;
  box-shadow: 0 20px 50px rgba(0,0,0,.08);
  position: relative;
  overflow: hidden;
}
.preview-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}
#frame, #pdf, #img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
}
#frame[hidden], #pdf[hidden], #img[hidden] {
  display: none;
}
#img {
  object-fit: contain;
}

/* ---------------------------------------------------------------- modes -- */
.modes {
  display: flex;
  gap: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}
.mode-tab {
  border: 0;
  background: none;
  border-radius: 999px;
  padding: 6px 16px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.mode-tab[aria-selected="true"] {
  background: var(--fg);
  color: #fff;
}
.content-view,
.analytics-view {
  display: none;
  padding: 20px;
  overflow: auto;
}
body[data-mode="content"] #designView,
body[data-mode="analytics"] #designView { display: none; }
body[data-mode="content"] .content-view { display: block; }
body[data-mode="analytics"] .analytics-view { display: block; }

.content-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.content-status { font-size: 0.82rem; color: var(--muted); }
.content-status.is-error { color: #b3261e; font-weight: 600; }
.content-panes {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}
@media (max-width: 900px) {
  .content-panes { grid-template-columns: 1fr; }
}
.content-pane {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  padding: 16px;
  min-width: 0;
}
.content-pane label { display: block; margin-bottom: 10px; font-size: 0.8rem; color: var(--fg); }
.content-pane input[type="text"],
.content-pane input[type="number"],
.content-pane textarea,
.content-pane select {
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  background: #fff;
  color: var(--fg);
}
.content-pane h3 { margin: 0 0 12px; font-size: 1rem; }
.content-pane h4 { margin: 16px 0 6px; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.sections-header { display: flex; align-items: center; justify-content: space-between; margin: 16px 0 8px; }
.sections-header h2 { margin: 0; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.empty { color: var(--muted); font-size: 0.85rem; margin: 8px 0; }
.muted { color: var(--muted); font-weight: 400; }

.section-node { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; }
.section-node > summary {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 12px; cursor: pointer; font-weight: 600; font-size: 0.9rem;
}
.section-node-count {
  background: var(--line); border-radius: 999px; padding: 1px 8px;
  font-size: 0.72rem; font-weight: 600; color: var(--muted);
}
.section-node-body { padding: 0 12px 12px; }
.section-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (max-width: 600px) { .section-fields { grid-template-columns: 1fr; } }
.node-actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
button.danger { border-color: #b3261e; color: #b3261e; }

.item-list { list-style: none; margin: 8px 0; padding: 0; }
.item-row {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto;
  gap: 6px; align-items: center; padding: 3px 0;
  border-bottom: 1px solid var(--line);
}
.item-row.is-active { background: color-mix(in srgb, var(--fg) 6%, transparent); }
.item-row-name {
  border: 0; background: none; text-align: left; font: inherit;
  cursor: pointer; padding: 5px 4px; min-width: 0; color: var(--fg);
}
.item-row-name:hover { text-decoration: underline; }
.item-row-price { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }

.price-fieldset, .nutrition-fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin: 0 0 12px; }
.price-fieldset legend, .nutrition-fieldset legend { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); padding: 0 4px; }
.price-row { display: grid; grid-template-columns: 1fr 100px auto; gap: 6px; margin-bottom: 6px; }
.nutrition-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.chip-picker { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 4px; }
.chip-picker .pick { position: relative; margin: 0; }
.chip-picker .pick input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.chip-picker .pick span {
  display: inline-block; border: 1px solid var(--line); border-radius: 999px;
  padding: 3px 10px; font-size: 0.78rem; cursor: pointer; background: #fff;
}
.chip-picker .pick input:checked + span { background: var(--fg); color: #fff; border-color: var(--fg); }
.chip-picker .pick input:focus-visible + span { outline: 2px solid var(--fg); outline-offset: 1px; }
.item-flags { display: flex; gap: 14px; margin-bottom: 12px; }

.stat-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; margin-bottom: 8px; }
.stat-tile {
  border: 1px solid var(--line); border-radius: 10px; padding: 14px; background: #fff; text-align: center;
}
.stat-tile b { display: block; font-size: 1.7rem; line-height: 1.1; }
.stat-tile span { font-size: 0.78rem; color: var(--muted); }
.stat-list { list-style: none; margin: 0; padding: 0; }
.stat-list li {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 5px 0; border-bottom: 1px solid var(--line); font-size: 0.85rem;
}
.analytics-view h4:first-of-type { margin-top: 20px; }

.auth-banner {
  position: sticky; top: 0; z-index: 100;
  padding: 10px 16px; text-align: center;
  background: #7f1d1d; color: #fff; font-size: 0.9rem;
}
