/* ============================================================================
   Apps (docs/apps-plan.md P4): fullscreen shell, editor, cards-tab tiles.
   Host-side styles only — the inside of the sandboxed frame is styled by
   /css/app-embed.css plus the app author's own CSS.
   ========================================================================== */

/* ---- app shell (docked over the board / fullscreen) ------------------------ */

.app-shell {
  position: fixed;
  inset: 0;
  z-index: var(--z-overlay);
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
}

/* Docked: the shell mirrors the Cards-tab dock slot's rect (inline
   top/left/width/height written by appShell.js createDock) and dresses like a
   card of the board panel it floats over. Above the panel (and its wide
   variant at --z-panel + 1), below true overlays and popovers. */
.app-shell--docked {
  inset: auto;
  z-index: calc(var(--z-panel) + 2);
  border: var(--border-hairline);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-2);
  overflow: hidden;
}

.app-shell--full {
  inset: 0;
  z-index: var(--z-overlay);
}

/* The empty slot renderAppDetail leaves in the Cards tab body: fills the
   visible tab area so the docked shell gets the whole space below the strip. */
.app-dock-slot {
  flex: 1 1 auto;
  min-height: 0;
}

/* Persistent chrome: the bar sits in the flex column above the stage, so the
   frame gets everything below it and the controls are always reachable. */
.app-shell__bar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border-bottom: var(--border-hairline);
}

.app-shell__bar-group {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.app-shell__title {
  margin: 0;
  font-size: var(--text-md);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 40vw;
}

.app-shell__page {
  color: var(--color-ink-secondary);
  font-size: var(--text-sm);
  white-space: nowrap;
}

.app-shell__viewports {
  display: flex;
  gap: 2px;
  padding: 2px;
  border: var(--border-hairline);
  border-radius: var(--radius-pill);
}

.app-shell__vp {
  border: 0;
  background: none;
  font: inherit;
  font-size: var(--text-xs);
  color: var(--color-ink-secondary);
  padding: 2px 10px;
  border-radius: var(--radius-pill);
  cursor: pointer;
}

.app-shell__vp.is-active {
  background: var(--color-active);
  color: var(--color-ink);
}

.app-shell__stage {
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  background: var(--color-bg);
}

.app-shell__window {
  height: 100%;
  width: 100%;
  display: flex;
  background: var(--color-surface-raised);
}

.app-shell__window[data-viewport="mobile"],
.app-shell__window[data-viewport="tablet"] {
  border-left: var(--border-hairline);
  border-right: var(--border-hairline);
}

.app-shell__frame {
  border: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #fff;
}

/* ---- editor ---------------------------------------------------------------- */

.app-shell--editing {
  background: var(--color-bg);
}

.app-editor {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

.app-editor__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border-bottom: var(--border-hairline);
}

.app-editor__bar-group {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-width: 0;
}

.app-editor__title {
  width: min(320px, 32vw);
  font-weight: var(--weight-medium);
}

.app-editor__mode {
  display: flex;
  gap: 2px;
  padding: 2px;
  border: var(--border-hairline);
  border-radius: var(--radius-pill);
}

.app-editor__mode-btn {
  border: 0;
  background: none;
  font: inherit;
  font-size: var(--text-xs);
  color: var(--color-ink-secondary);
  padding: 2px 10px;
  border-radius: var(--radius-pill);
  cursor: pointer;
}

.app-editor__mode-btn.is-active {
  background: var(--color-active);
  color: var(--color-ink);
}

.app-editor__dirty {
  color: var(--color-ink-faint);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.app-editor__body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(360px, 1fr) minmax(320px, 1fr);
}

.app-editor__left {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  border-right: var(--border-hairline);
  background: var(--color-surface-raised);
}

.app-editor__tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: var(--space-1) var(--space-2) 0;
  border-bottom: var(--border-hairline);
  overflow-x: auto;
  flex: none;
}

.app-editor__tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: var(--border-hairline);
  border-bottom: 0;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  background: var(--color-surface);
  color: var(--color-ink-secondary);
  font: inherit;
  font-size: var(--text-sm);
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
}

.app-editor__tab.is-active {
  background: var(--color-surface-raised);
  color: var(--color-ink);
  font-weight: var(--weight-medium);
}

.app-editor__tab--fixed {
  margin-left: auto;
  letter-spacing: var(--tracking-wide);
  font-size: var(--text-xs);
}

.app-editor__tab--fixed + .app-editor__tab--fixed {
  margin-left: 0;
}

.app-editor__tab-x {
  display: inline-flex;
  border-radius: var(--radius-round);
  color: var(--color-ink-faint);
}

.app-editor__tab-x:hover {
  color: var(--color-danger);
}

.app-editor__tab-add {
  flex: none;
}

.app-editor__cm {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.app-editor__cm .cm-editor {
  flex: 1;
  min-height: 0;
  font-size: var(--text-sm);
  background: var(--color-surface-raised);
}

.app-editor__cm .cm-editor.cm-focused {
  outline: none;
}

.app-editor__cm .cm-scroller {
  font-family: var(--font-mono);
  line-height: 1.55;
}

.app-editor__cm .cm-gutters {
  background: var(--color-surface);
  color: var(--color-ink-faint);
  border-right: var(--border-hairline);
}

.app-editor__cm .cm-panels {
  background: var(--color-surface);
  color: var(--color-ink);
  border-bottom: var(--border-hairline);
}

.app-editor__cm .cm-panels.cm-panels-top {
  border-bottom: var(--border-hairline);
}

.app-editor__cm .cm-panel.cm-search {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  font-size: var(--text-sm);
}

.app-editor__cm .cm-panel.cm-search input[type="text"],
.app-editor__cm .cm-panel.cm-search input:not([type]) {
  font: inherit;
  font-size: var(--text-sm);
  color: var(--color-ink);
  background: var(--color-surface-raised);
  border: var(--border-hairline);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  min-width: 10ch;
}

.app-editor__cm .cm-panel.cm-search input[type="checkbox"] {
  accent-color: var(--color-accent, var(--color-ink));
}

.app-editor__cm .cm-panel.cm-search button {
  font: inherit;
  font-size: var(--text-xs);
  color: var(--color-ink-secondary);
  background: var(--color-surface-raised);
  border: var(--border-hairline);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  cursor: pointer;
}

.app-editor__cm .cm-panel.cm-search button:hover {
  color: var(--color-ink);
  background: var(--color-active, var(--color-surface));
}

.app-editor__cm .cm-panel.cm-search label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--color-ink-secondary);
  font-size: var(--text-xs);
  cursor: pointer;
}

.app-editor__cm .cm-searchMatch {
  background: color-mix(in srgb, var(--color-accent, #f0c000) 35%, transparent);
}

.app-editor__cm .cm-searchMatch-selected {
  background: color-mix(in srgb, var(--color-accent, #f0c000) 65%, transparent);
}

.app-editor__right {
  min-width: 0;
  min-height: 0;
  display: flex;
}

.app-editor__preview {
  flex: 1;
  display: flex;
  background: var(--color-bg);
}

.app-editor__frame {
  border: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #fff;
}

/* Entity search popup */
.app-editor__search {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: var(--space-2);
  width: min(360px, 80vw);
}

.app-editor__search-results {
  max-height: 300px;
  overflow-y: auto;
}

.app-editor__cells {
  border-left: 2px solid var(--color-border);
  margin-left: var(--space-3);
}

/* ---- cards-tab / public-collection tile -------------------------------------- */

.app-tile .app-tile__preview {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  min-height: 96px;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--entity-app) 8%, var(--color-surface));
  color: var(--entity-app);
  overflow: hidden;
}

.app-tile .app-tile__preview--empty {
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-3);
}

.app-tile .app-tile__preview--live {
  background: var(--color-surface);
  height: 112px;
}

.app-tile .app-tile__preview--live::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 28px;
  z-index: 1;
  pointer-events: none;
  background: linear-gradient(to bottom, transparent, var(--color-surface));
}

.app-tile .app-tile__placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  min-height: 96px;
  color: var(--color-ink-faint);
}

.app-tile .app-tile__scale {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: top left;
  pointer-events: none;
  overflow: hidden;
  will-change: transform;
}

.app-tile .app-tile__frame {
  display: block;
  border: 0;
  background: #fff;
  pointer-events: none;
}

.app-tile .app-tile__pages {
  font-size: var(--text-xs);
  color: var(--color-ink-faint);
}
