/* ============================================================
   BUILDYBOT — APP VIEWER LAYOUT (existing)
   ============================================================ */

/* --- App-specific custom properties (override these) --- */
:root {
  --app-header-height: 48px;
  --app-header-bg: var(--nav-bg);
  --app-header-text: var(--nav-text);
  --app-header-border: var(--border-color);
  --app-header-font-size: 0.88rem;
  --app-header-padding-x: 1rem;
  --app-back-btn-bg: transparent;
  --app-back-btn-color: var(--nav-text);
  --app-back-btn-hover-bg: rgba(255, 255, 255, 0.12);
  --app-back-btn-radius: var(--radius-sm);
  --app-body-bg: var(--bg);
  --app-body-padding: 0;

  /* App additions (works standalone too) */
  --bg: #0b1020;
  --panel: rgba(255,255,255,0.06);
  --panel-2: rgba(255,255,255,0.085);
  --text: rgba(255,255,255,0.92);
  --muted: rgba(255,255,255,0.66);
  --border-color: rgba(255,255,255,0.14);
  --nav-bg: rgba(0,0,0,0.25);
  --nav-text: rgba(255,255,255,0.92);
  --primary: #7c5cff;
  --primary-2: #9b86ff;
  --danger: #ff4d6d;
  --ok: #28c76f;
  --warn: #ffb020;
  --radius-sm: 10px;
  --radius: 14px;
  --shadow: 0 18px 60px rgba(0,0,0,0.45);
  --transition-speed: 0.2s;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, "Noto Sans", "Liberation Sans", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(1200px 600px at 10% 0%, rgba(124,92,255,0.22), transparent 55%),
              radial-gradient(900px 600px at 85% 0%, rgba(40,199,111,0.14), transparent 60%),
              var(--bg);
  color: var(--text);
  font-family: var(--sans);
}

/* --- Header bar --- */
.app-viewer-header {
  position: sticky;
  top: 0;
  z-index: 1030;
  display: flex;
  align-items: center;
  height: var(--app-header-height);
  background: var(--app-header-bg);
  color: var(--app-header-text);
  border-bottom: 1px solid var(--app-header-border);
  padding: 0 var(--app-header-padding-x);
  gap: 0.75rem;
  font-size: var(--app-header-font-size);
  transition: background-color var(--transition-speed, 0.2s) ease,
              border-color var(--transition-speed, 0.2s) ease;
  backdrop-filter: blur(10px);
}

/* --- Back button --- */
.app-viewer-back {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.65rem;
  background: var(--app-back-btn-bg);
  color: var(--app-back-btn-color);
  border: none;
  border-radius: var(--app-back-btn-radius);
  font-size: inherit;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s ease;
}

.app-viewer-back:hover,
.app-viewer-back:focus-visible {
  background: var(--app-back-btn-hover-bg);
  color: var(--app-back-btn-color);
  text-decoration: none;
}

.app-viewer-back i { font-size: 1.1em; }

/* --- Separator between back button and app name --- */
.app-viewer-sep {
  width: 1px;
  height: 1.2rem;
  background: currentColor;
  opacity: 0.25;
  flex-shrink: 0;
}

/* --- App name --- */
.app-viewer-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* --- Right-side slot for optional actions --- */
.app-viewer-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

/* --- Main content area beneath the header --- */
.app-viewer-body {
  flex: 1;
  background: var(--app-body-bg);
  padding: var(--app-body-padding);
  min-height: calc(100vh - var(--app-header-height));
}

/* --- Responsive --- */
@media (max-width: 575.98px) {
  .app-viewer-header { gap: 0.5rem; --app-header-padding-x: 0.65rem; }
  .app-viewer-back span { display: none; }
}

/* ============================================================
   APP UI
   ============================================================ */

.topbar-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.layout {
  display: grid;
  grid-template-columns: 1.08fr 0.92fr;
  gap: 14px;
  padding: 14px;
  max-width: 1500px;
  margin: 0 auto;
}

.pane { min-width: 0; }
.builder { padding-bottom: 18px; }
.preview { position: sticky; top: calc(var(--app-header-height) + 14px); height: calc(100vh - var(--app-header-height) - 28px); overflow: auto; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .preview { position: static; height: auto; }
}

.card {
  background: linear-gradient(180deg, rgba(255,255,255,0.075), rgba(255,255,255,0.05));
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  margin-bottom: 14px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

h2, h3 { margin: 0; font-weight: 650; letter-spacing: 0.2px; }
h2 { font-size: 1rem; }
h3 { font-size: 0.98rem; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.divider {
  height: 1px;
  background: var(--border-color);
  margin: 12px 0;
}

.grid { display: grid; gap: 10px; }
.grid.two { grid-template-columns: 1fr 1fr; }
.grid.one { grid-template-columns: 1fr; }
@media (max-width: 700px) { .grid.two { grid-template-columns: 1fr; } }

.row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.gap-sm { gap: 8px; }

.field { display: grid; gap: 6px; min-width: 0; }
.field.compact { display: flex; align-items: center; gap: 8px; }
.field.compact span { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.field label { color: var(--muted); font-size: 0.86rem; }
.hint { color: var(--muted); font-size: 0.82rem; }

input, select, textarea {
  width: 100%;
  background: rgba(0,0,0,0.18);
  color: var(--text);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 9px 10px;
  outline: none;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: rgba(124,92,255,0.65); box-shadow: 0 0 0 3px rgba(124,92,255,0.18); }

.search { padding-left: 11px; }
.w-110 { width: 110px; }

.btn, .pill, .icon-btn {
  border: 1px solid var(--border-color);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border-radius: 12px;
  padding: 8px 10px;
  cursor: pointer;
  transition: transform 0.05s ease, background-color 0.15s ease, border-color 0.15s ease;
  user-select: none;
  white-space: nowrap;
}
.btn:hover, .pill:hover, .icon-btn:hover { background: rgba(255,255,255,0.09); }
.btn:active, .pill:active, .icon-btn:active { transform: translateY(1px); }
.btn.subtle { background: rgba(0,0,0,0.12); }
.btn.primary {
  background: linear-gradient(180deg, rgba(124,92,255,0.95), rgba(124,92,255,0.7));
  border-color: rgba(124,92,255,0.55);
}
.btn.primary:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.danger { border-color: rgba(255,77,109,0.6); color: rgba(255,220,226,0.95); }
.btn.danger:hover { background: rgba(255,77,109,0.12); }

.icon-btn { width: 36px; height: 36px; padding: 0; display: inline-grid; place-items: center; border-radius: 12px; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
}
.pill .dot {
  width: 8px; height: 8px; border-radius: 99px;
  background: var(--warn);
}
.pill[data-status="Draft"] .dot { background: #8aa0b8; }
.pill[data-status="Sent"] .dot { background: var(--warn); }
.pill[data-status="Accepted"] .dot { background: var(--ok); }
.pill[data-status="Declined"] .dot { background: var(--danger); }
.pill[data-status="Expired"] .dot { background: rgba(255,255,255,0.35); }
.pill .caret { opacity: 0.8; }

.check, .radio {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 0.9rem;
}
.check input, .radio input { width: auto; }

.sections { display: grid; gap: 12px; }

.section-card {
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  overflow: clip;
  background: rgba(0,0,0,0.12);
}

.section-top {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  background: rgba(255,255,255,0.05);
}

.section-title {
  display: flex;
  gap: 10px;
  align-items: center;
  min-width: 0;
}
.section-title input {
  font-weight: 650;
  background: rgba(0,0,0,0.10);
}
.drag-handle {
  font-family: var(--mono);
  opacity: 0.7;
  cursor: grab;
  padding: 6px 8px;
  border: 1px dashed rgba(255,255,255,0.18);
  border-radius: 10px;
  user-select: none;
}
.drag-handle:active { cursor: grabbing; }

.section-actions { display: flex; gap: 8px; align-items: center; }
.section-body { padding: 10px; display: grid; gap: 10px; }
.section-body.collapsed { display: none; }

.table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.1);
}
.table th, .table td {
  padding: 8px 8px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  vertical-align: top;
}
.table th {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  background: rgba(255,255,255,0.04);
}
.table tr:last-child td { border-bottom: none; }
.table input, .table select { padding: 7px 8px; border-radius: 10px; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .tight { width: 1%; white-space: nowrap; }
.table .desc { min-width: 180px; }
.table .faded { opacity: 0.55; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.14);
  color: var(--muted);
  font-size: 0.82rem;
}

.options {
  display: grid;
  gap: 10px;
}

.option-cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(220px, 1fr));
  gap: 10px;
}
@media (max-width: 900px) { .option-cards { grid-template-columns: 1fr; } }

.option-card {
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 14px;
  padding: 10px;
  background: rgba(255,255,255,0.04);
}
.option-card.selected {
  border-color: rgba(124,92,255,0.65);
  box-shadow: 0 0 0 3px rgba(124,92,255,0.14);
  background: rgba(124,92,255,0.08);
}
.option-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.option-name input { font-weight: 650; }
.option-meta { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.85rem; }
.option-total { font-variant-numeric: tabular-nums; font-family: var(--mono); color: rgba(255,255,255,0.85); }

.kv { display: grid; grid-template-columns: auto auto; gap: 6px 12px; font-size: 0.9rem; }
.kv .k { color: var(--muted); }
.kv .v { font-family: var(--mono); }

.list {
  display: grid;
  gap: 8px;
}
.list-item {
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px;
  padding: 10px;
  background: rgba(0,0,0,0.12);
  display: grid;
  gap: 8px;
}
.list-item-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: baseline;
}
.list-item-title {
  font-family: var(--mono);
  font-size: 0.9rem;
}
.list-item-sub { color: var(--muted); font-size: 0.86rem; }
.list-item-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.preview-wrap {
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.05);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.preview-cover {
  display: grid;
  grid-template-columns: 1.2fr 0.8fr;
  gap: 10px;
  padding: 18px 18px 14px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  background: linear-gradient(180deg, rgba(124,92,255,0.14), rgba(0,0,0,0));
}
.preview-cover h1 {
  margin: 4px 0 0 0;
  font-size: 1.5rem;
  letter-spacing: 0.2px;
}
.kicker {
  color: rgba(255,255,255,0.8);
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 0.75rem;
}
.preview-blocks { padding: 16px 18px 18px 18px; display: grid; gap: 14px; }
.preview-block h2 { font-size: 1.02rem; margin-bottom: 8px; }
.customer-block { white-space: pre-wrap; color: rgba(255,255,255,0.9); }
.pre { white-space: pre-wrap; color: rgba(255,255,255,0.88); background: rgba(0,0,0,0.12); border: 1px solid rgba(255,255,255,0.10); border-radius: 12px; padding: 10px; }

.preview-section { margin-top: 10px; }
.preview-section h3 { margin: 0 0 6px 0; }
.preview-section .desc { color: var(--muted); margin-bottom: 8px; }

.preview-option {
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px;
  padding: 10px;
  background: rgba(255,255,255,0.03);
  margin-bottom: 10px;
}
.preview-option.selected {
  border-color: rgba(124,92,255,0.55);
  background: rgba(124,92,255,0.08);
}
.preview-option .head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.preview-option .head .tag {
  font-size: 0.78rem;
  border: 1px solid rgba(255,255,255,0.14);
  color: rgba(255,255,255,0.78);
  padding: 4px 8px;
  border-radius: 999px;
}
.preview-option .head .tag.sel { border-color: rgba(40,199,111,0.5); background: rgba(40,199,111,0.12); }
.preview-option .head .tag.rec { border-color: rgba(255,176,32,0.5); background: rgba(255,176,32,0.10); }
.preview-option table { width: 100%; border-collapse: collapse; }
.preview-option td, .preview-option th { padding: 6px 6px; border-bottom: 1px solid rgba(255,255,255,0.08); }
.preview-option tr:last-child td { border-bottom: none; }
.preview-option th { text-align: left; color: var(--muted); font-weight: 600; font-size: 0.82rem; }
.preview-option .num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); }
.preview-option .mutedline { color: rgba(255,255,255,0.58); font-style: italic; }

.totals {
  border-top: 1px dashed rgba(255,255,255,0.22);
  padding-top: 14px;
}
.totals-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 12px;
  align-items: baseline;
}
.totals-grid .k { color: var(--muted); }
.totals-grid .v { font-family: var(--mono); text-align: right; }
.totals-grid .total { font-size: 1.05rem; font-weight: 700; color: rgba(255,255,255,0.95); }

.acceptance .accept-grid {
  display: grid;
  grid-template-columns: 1fr 220px;
  gap: 14px;
}
@media (max-width: 560px) { .acceptance .accept-grid { grid-template-columns: 1fr; } }
.acceptance .line .label { color: var(--muted); font-size: 0.85rem; margin-bottom: 6px; }
.sigline { height: 28px; border-bottom: 2px solid rgba(255,255,255,0.42); }

.menu {
  position: fixed;
  top: 52px;
  right: 16px;
  background: rgba(20,24,40,0.92);
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 14px;
  padding: 6px;
  box-shadow: var(--shadow);
  display: none;
  min-width: 180px;
  z-index: 2000;
  backdrop-filter: blur(10px);
}
.menu.open { display: block; }
.menu-item {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: rgba(255,255,255,0.9);
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: rgba(255,255,255,0.08); outline: none; }

.modal::backdrop { background: rgba(0,0,0,0.55); backdrop-filter: blur(2px); }
.modal { border: none; padding: 0; background: transparent; }
.modal-card {
  width: min(720px, calc(100vw - 28px));
  border-radius: 16px;
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(18,22,38,0.92);
  box-shadow: var(--shadow);
  padding: 12px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }

.drawer { position: fixed; inset: 0; display: none; z-index: 3000; }
.drawer.open { display: block; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.55); }
.drawer-panel {
  position: absolute;
  top: 0; right: 0;
  width: min(420px, 92vw);
  height: 100%;
  background: rgba(18,22,38,0.96);
  border-left: 1px solid rgba(255,255,255,0.14);
  box-shadow: var(--shadow);
  display: grid;
  grid-template-rows: auto 1fr;
}
.drawer-head { padding: 12px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid rgba(255,255,255,0.12); }
.drawer-body { padding: 12px; display: grid; gap: 12px; overflow: auto; }

.tabs { display: flex; gap: 8px; }
.tab {
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(255,255,255,0.05);
  color: rgba(255,255,255,0.9);
  border-radius: 999px;
  padding: 7px 10px;
  cursor: pointer;
}
.tab.active { background: rgba(124,92,255,0.18); border-color: rgba(124,92,255,0.35); }
.tab-panel.hidden { display: none; }

.preset-list { display: grid; gap: 10px; }
.preset-card {
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 14px;
  padding: 10px;
  background: rgba(0,0,0,0.12);
  display: grid;
  gap: 8px;
}
.preset-card .top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.preset-card .name { font-weight: 650; }
.preset-card .meta { color: var(--muted); font-size: 0.86rem; }
.preset-card .actions { display: flex; gap: 8px; flex-wrap: wrap; }

.hidden { display: none !important; }

/* Print */
@media print {
  body { background: #fff; color: #111; }
  .app-viewer-header, .builder, .drawer, .menu, dialog { display: none !important; }
  .layout { grid-template-columns: 1fr; padding: 0; max-width: none; }
  .preview { position: static; height: auto; overflow: visible; }
  .preview-wrap { border: none; box-shadow: none; background: #fff; }
  .preview-cover { background: #fff; border-bottom: 1px solid #ddd; }
  .preview-blocks { padding: 14px 0; }
  .pre { background: #fff; border: 1px solid #ddd; color: #111; }
  .muted, .kicker { color: #444 !important; }
  .preview-option { background: #fff; border: 1px solid #ddd; }
  .preview-option.selected { border-color: #111; }
  .sigline { border-bottom-color: #111; }
}